api_server: honor the model field in /v1/chat/completions - #24008
Closed
evangelinekamin wants to merge 1 commit into
Closed
api_server: honor the model field in /v1/chat/completions#24008evangelinekamin wants to merge 1 commit into
model field in /v1/chat/completions#24008evangelinekamin wants to merge 1 commit into
Conversation
The OpenAI-compatible chat-completions endpoint reads the request's `model` field but only echoes it back — the agent always runs on `model.default`. Most OpenAI-compatible clients expect `model` to actually select the model, and being able to pick a model per conversation (e.g. a cheap default with an escalation lane) without restarting the gateway is handy. Thread the requested model through as a per-request override: - `_handle_chat_completions` treats a `model` other than the advertised name (`_get_advertised_model_name()`) as an override. - `_run_agent` / `_create_agent` gain a `model_override` parameter. - `_create_agent` resolves it against `model_aliases` (so `"opus"` works) or uses it as a literal provider/model id, and passes it to `AIAgent(model=...)` for that turn only. When `model` is omitted or equals the advertised name, behaviour is unchanged — `model.default` is used. Provider credentials (api_key/base_url from `_resolve_runtime_agent_kwargs()`) are left as-is, so this works as long as the override resolves to a model reachable with the configured provider (the common case — e.g. everything via OpenRouter). Tested against a local gateway: `"model": "deepseek/deepseek-v4-pro"` runs that turn on V4 Pro (confirmed via the session row in state.db), `"model": "opus"` resolves through `model_aliases`, and an omitted `model` still uses `model.default`. Left `/v1/runs` alone; it could get the same treatment.
Collaborator
|
Duplicate of #10773, which implements the same per-request |
This was referenced May 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
/v1/chat/completionsreads the request'smodelfield but only echoes it back in the response — the agent always runs onmodel.defaultregardless of what the client asked for. This makes themodelfield actually select the model for that request.Why
OpenAI-compatible clients (Open WebUI, custom front-ends, etc.) generally expect
modelto choose the model. It's also useful to pick a model per conversation — e.g. a cheap default with an explicit escalation to a stronger model for hard turns — without restarting the gateway or running a second instance.How
A per-request override threaded through:
_handle_chat_completionstreats anymodelother than the server's advertised name (_get_advertised_model_name()) as an override; an omittedmodel(or one matching the advertised name) behaves exactly as before._run_agentand_create_agentgain amodel_overrideparameter._create_agentresolves it: if it's a key inmodel_aliasesit uses that alias'smodel, otherwise it's treated as a literal provider/model id, and it's passed toAIAgent(model=...)for that turn only.Provider credentials (
api_key/base_urlfrom_resolve_runtime_agent_kwargs()) are left untouched, so this works cleanly when the override resolves to a model reachable with the configured provider — the common case (everything routed through OpenRouter, or any single-provider setup). A future change could resolve per-provider credentials for cross-provider overrides; this keeps it minimal./v1/runsis left as-is — it could get the same treatment if there's interest.Testing
Against a local gateway:
{"model": "deepseek/deepseek-v4-pro", ...}→ that turn runs on V4 Pro (confirmed via themodelcolumn on the session row instate.db).{"model": "opus", ...}→ resolves throughmodel_aliasesto the configured Opus model id.modelfield → still usesmodel.default.