Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions gateway/platforms/api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,15 @@ def _create_agent(
from hermes_cli.tools_config import _get_platform_tools

runtime_kwargs = _resolve_runtime_agent_kwargs()
# Defer to the server-side `config.yaml` model rather than letting
# the kwarg below collide with a `model` key already populated in
# `runtime_kwargs`. Without this pop, the explicit `model=model`
# on the `AIAgent(...)` constructor a few lines down raises
# `TypeError: AIAgent() got multiple values for keyword argument
# 'model'` and 500s every `POST /v1/chat/completions` regardless
# of payload. See #10773 for the per-request-model design; this is
# the narrow band-aid only.
runtime_kwargs.pop('model', None)
reasoning_config = GatewayRunner._load_reasoning_config()
model = _resolve_gateway_model()
Comment on lines +881 to 891

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considered both of your suggestions (a) 4xx-on-mismatch and (b) drop-only-when-matching, and explicitly picked the unconditional pop for this PR. Two reasons:

  1. Precedence semantics is the design conversation at feat(api-server): honor request-level model field for per-request model selection #10773. The competing PRs (fix(api-server): honor chat completions request model #25552, feat(api-server): honor X-Router-Model for per-request model override #16403, feat(api_server): honor inbound 'model' and 'provider' fields for per-request routing #18549, feat(gateway): add per-request model routing to API server #5862) are wrestling with exactly what the request-level model field should mean — error, override, fallback, mismatch-warn. This PR is the band-aid that unblocks /v1/chat/completions for everyone while that design lands. Implementing either suggestion (a) or (b) here would mean picking a winner in the design debate, which is exactly what I don't want this PR to do.

  2. Failing-loud on caller-provided model would surprise existing API consumers. Before the kwargs collision was ever triggered, callers could already send model in their request — the server-side config.yaml value won by virtue of the explicit model=model kwarg in the constructor, and the caller's value was silently ignored. That's the current implicit behavior of every working deployment that happens to not hit the collision. The narrow pop preserves that behavior exactly.

Added a regression test in tests/gateway/test_api_server.py::TestCreateAgent::test_create_agent_pops_colliding_model_from_runtime_kwargs (commit 86d349a) that locks in the no-TypeError behavior, mirroring the existing test_create_agent_forwards_config_reasoning_effort pattern in the same class.

When #10773's full design lands, the pop gets replaced by whatever precedence semantics it specifies, and the test gets updated alongside. If the maintainer's strong preference is option (b) precedence-aware behavior here rather than at #10773, happy to rework.


Expand Down
61 changes: 61 additions & 0 deletions tests/gateway/test_api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,67 @@ def __init__(self, **kwargs):
assert isinstance(agent, FakeAgent)
assert captured["reasoning_config"] == {"enabled": True, "effort": "xhigh"}

def test_create_agent_pops_colliding_model_from_runtime_kwargs(self, monkeypatch):
"""Regression test for the model-kwarg collision:
`_resolve_runtime_agent_kwargs()` sources `model` from
`config.yaml`, so `runtime_kwargs` can carry a `model` key.
Before the fix, `AIAgent(model=model, **runtime_kwargs)` then raised
`TypeError: AIAgent() got multiple values for keyword argument
'model'` and every `POST /v1/chat/completions` returned HTTP 500.

After the fix, the colliding key is popped out of `runtime_kwargs`
before the constructor call, so the explicit `model=model` (from
`_resolve_gateway_model()`) wins unambiguously and the request
flows. See PR #31139 / issue #10773 for the broader per-request-
model design discussion this band-aid sits beneath.
"""
captured = {}

class FakeAgent:
def __init__(self, **kwargs):
captured.update(kwargs)

monkeypatch.setattr("run_agent.AIAgent", FakeAgent)
# runtime_kwargs DOES carry a colliding `model` key — this is the
# exact shape that triggered the TypeError before the fix.
monkeypatch.setattr(
"gateway.run._resolve_runtime_agent_kwargs",
lambda: {
"provider": "openai-codex",
"model": "model-from-runtime-kwargs-this-collides",
},
)
monkeypatch.setattr(
"gateway.run._resolve_gateway_model",
lambda: "model-from-gateway-resolver",
)
monkeypatch.setattr("gateway.run._load_gateway_config", lambda: {})
monkeypatch.setattr(
"gateway.run.GatewayRunner._load_reasoning_config",
staticmethod(lambda: {}),
)
monkeypatch.setattr(
"gateway.run.GatewayRunner._load_fallback_model",
staticmethod(lambda: None),
)
monkeypatch.setattr("hermes_cli.tools_config._get_platform_tools", lambda *_: set())

adapter = APIServerAdapter(PlatformConfig(enabled=True))
monkeypatch.setattr(adapter, "_ensure_session_db", lambda: None)

# This call would raise TypeError before the fix.
agent = adapter._create_agent(session_id="api-session")

assert isinstance(agent, FakeAgent)
# The explicit model=model kwarg (from _resolve_gateway_model) wins;
# the colliding runtime_kwargs.model was popped before the call.
assert captured["model"] == "model-from-gateway-resolver", (
"Explicit model=model kwarg must win; runtime_kwargs.model was the "
"colliding source that gets popped."
)
# provider should still come through from runtime_kwargs unaffected.
assert captured["provider"] == "openai-codex"


# ---------------------------------------------------------------------------
# Auth checking
Expand Down