fix(opencode_go): forward chat reasoning effort (#52156)

* test(opencode_go): cover chat reasoning effort forwarding

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(opencode_go): forward chat reasoning effort for reasoning models

Preserve reasoning_effort through chat parameter validation. Keep models without declared reasoning support unchanged. Related contributor change in #51879.

Co-authored-by: PCTEJA <39472754+PCTEJA@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test(opencode_go): tighten capability and translation assertions

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: PCTEJA <39472754+PCTEJA@users.noreply.github.com>
This commit is contained in:
moe-berri 2026-11-17 08:31:56 +08:00 • committed by GitHub
parent 0e61e99120
commit dd7d77fea1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 285 additions and 0 deletions

View file

@ -79,6 +79,14 @@ class OpenCodeGoChatConfig(_GoReportedCostMixin, OpenAILikeChatConfig):
) -> tuple[str, str | None]:
return go_api_base(api_base), go_api_key(api_key)
def get_supported_openai_params(self, model: str) -> list[str]: # mutable-ok: matches OpenAIGPTConfig's list return
supports_reasoning: Final = litellm.supports_reasoning(
model=model, custom_llm_provider=self.custom_llm_provider
)
inherited: Final[list[str]] = super().get_supported_openai_params(model) # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType] # parent returns a bare list # mutable-ok: same
extra: Final = ("reasoning_effort",) if supports_reasoning else ()
return [*inherited, *extra]
def validate_environment(
self,
headers: Mapping[str, object],

View file

@ -11,6 +11,7 @@ from pydantic import JsonValue, TypeAdapter
_LIST_COST: Final = 0.00412
_JSON_OBJECT: Final = TypeAdapter(dict[str, JsonValue])
_CHAT_ROUTE: Final = "/v1/chat/completions"
# OpenCode's documented usage block (opencode.ai/v2/docs/console/api/inference, "Cost", read 2026-10-24):
# usage.cost is what hit the prepaid balance (0 inside the Go plan), usage.opencode.list_cost is the model price
@ -222,6 +223,25 @@ def test_request_is_billed_at_the_reported_list_cost(gateway: Gateway, path: str
assert _logged_spend(response.headers["x-litellm-call-id"]) == _approx(_LIST_COST)
def test_chat_reasoning_preserves_reported_list_cost_guard(gateway: Gateway) -> None:
def respond(request: Request) -> Reply:
assert request.target.endswith("/chat/completions"), request.target
return _respond(request)
with wire_server(respond) as wire, gateway.scenario() as scenario:
deployment: Final = scenario.model(
model="opencode_go/glm-5.3", api_base=wire.url, api_key="synthetic-go-key", drop_params=True
)
response: Final = gateway.request(
"POST", _CHAT_ROUTE,
{**_body(_CHAT_ROUTE, deployment, False), "reasoning_effort": "high"},
)
assert response.status_code == 200, response.text
assert "ok" in response.text, response.text
assert float(response.headers["x-litellm-response-cost"]) == _approx(_LIST_COST)
assert _logged_spend(response.headers["x-litellm-call-id"]) == _approx(_LIST_COST)
@pytest.mark.parametrize(
("model", "stream"),
(
@ -263,3 +283,180 @@ def test_chat_completion_uses_go_model_stream_protocol(gateway: Gateway, model:
assert response.text.rstrip().endswith("data: [DONE]"), response.text
if model == "opencode_go/minimax-m3":
assert _logged_spend(response.headers["x-litellm-call-id"]) == _approx(_LIST_COST)
def _reasoning_input(path: str) -> dict[str, JsonValue]:
if path == "/v1/responses":
return {"input": "hello"}
return {"messages": [{"role": "user", "content": "hello"}], "max_tokens": 32}
def _assert_chat_response(response: str, stream: bool) -> None:
if stream:
assert response.rstrip().endswith("data: [DONE]"), response
events: Final = tuple(
_JSON_OBJECT.validate_json(line.removeprefix("data: "))
for line in response.splitlines()
if line.startswith("data: {")
)
groups: Final = tuple(
TypeAdapter(tuple[dict[str, JsonValue], ...]).validate_python(e["choices"]) for e in events
)
choices: Final = tuple(group[0] for group in groups if group)
deltas: Final = tuple(_JSON_OBJECT.validate_python(choice["delta"]) for choice in choices)
assert "".join(str(delta.get("content") or "") for delta in deltas) == "ok", response
assert tuple(c["finish_reason"] for c in choices if c.get("finish_reason")) == ("stop",), response
return
body: Final = _JSON_OBJECT.validate_json(response)
choices_body: Final = TypeAdapter(tuple[dict[str, JsonValue], ...]).validate_python(body["choices"])
assert _JSON_OBJECT.validate_python(choices_body[0]["message"])["content"] == "ok", response
assert choices_body[0]["finish_reason"] == "stop", response
@pytest.mark.parametrize("effort", ("none", "low", "medium", "high"))
@pytest.mark.parametrize("stream", (False, True), ids=("non-stream", "stream"))
def test_chat_request_reasoning_forwarded_regression(gateway: Gateway, effort: str, stream: bool) -> None:
def respond(request: Request) -> Reply:
assert request.target.endswith("/chat/completions"), request.target
assert request.headers["authorization"] == "Bearer synthetic-go-key"
assert _JSON_OBJECT.validate_json(request.body).get("reasoning_effort") == effort
return _respond(request)
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="opencode_go/glm-5.3", api_base=wire.url, api_key="synthetic-go-key", drop_params=True
)
response: Final = gateway.request(
"POST",
_CHAT_ROUTE,
{"model": model, **_reasoning_input(_CHAT_ROUTE), "reasoning_effort": effort, "stream": stream},
)
assert response.status_code == 200, response.text
_assert_chat_response(response.text, stream)
def test_chat_strict_reasoning_forwarded_regression(gateway: Gateway) -> None:
def respond(request: Request) -> Reply:
assert request.target.endswith("/chat/completions"), request.target
assert _JSON_OBJECT.validate_json(request.body).get("reasoning_effort") == "high"
return _respond(request)
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="opencode_go/glm-5.3", api_base=wire.url, api_key="synthetic-go-key", drop_params=False
)
response: Final = gateway.request(
"POST", _CHAT_ROUTE, {"model": model, **_reasoning_input(_CHAT_ROUTE), "reasoning_effort": "high"}
)
assert response.status_code == 200, response.text
_assert_chat_response(response.text, False)
@pytest.mark.parametrize("session_field", (None, "metadata", "litellm_session_id"))
def test_deployment_reasoning_and_session_forwarded_regression(gateway: Gateway, session_field: str | None) -> None:
def respond(request: Request) -> Reply:
assert request.target.endswith("/chat/completions"), request.target
if session_field is not None:
assert request.headers["x-opencode-session"] == "reasoning-session"
assert _JSON_OBJECT.validate_json(request.body).get("reasoning_effort") == "none"
return _respond(request)
session: Final[dict[str, JsonValue]] = (
{}
if session_field is None
else {"metadata": {"session_id": "reasoning-session"}}
if session_field == "metadata"
else {"litellm_session_id": "reasoning-session"}
)
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="opencode_go/glm-5.3",
api_base=wire.url,
api_key="synthetic-go-key",
drop_params=True,
reasoning_effort="none",
)
response: Final = gateway.request(
"POST", _CHAT_ROUTE, {"model": model, **_reasoning_input(_CHAT_ROUTE), **session}
)
assert response.status_code == 200, response.text
_assert_chat_response(response.text, False)
@pytest.mark.parametrize("path", ("/v1/responses", "/v1/messages"))
def test_translated_reasoning_forwarded_regression(gateway: Gateway, path: str) -> None:
def respond(request: Request) -> Reply:
assert request.target.endswith("/chat/completions"), request.target
effort: Final = _JSON_OBJECT.validate_json(request.body).get("reasoning_effort")
expected_effort: Final = "high" if path == "/v1/responses" else "medium"
assert effort == expected_effort, effort
return _respond(request)
reasoning: Final[dict[str, JsonValue]] = (
{"reasoning": {"effort": "high"}}
if path == "/v1/responses"
else {"thinking": {"type": "enabled", "budget_tokens": 2048}}
)
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="opencode_go/glm-5.3", api_base=wire.url, api_key="synthetic-go-key", drop_params=True
)
response: Final = gateway.request("POST", path, {"model": model, **_reasoning_input(path), **reasoning})
assert response.status_code == 200, response.text
assert "ok" in response.text, response.text
@pytest.mark.parametrize("path", (_CHAT_ROUTE, "/v1/responses", "/v1/messages"))
def test_absent_reasoning_not_injected_guard(gateway: Gateway, path: str) -> None:
def respond(request: Request) -> Reply:
assert request.target.endswith("/chat/completions"), request.target
assert "reasoning_effort" not in _JSON_OBJECT.validate_json(request.body)
return _respond(request)
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(model="opencode_go/glm-5.3", api_base=wire.url, api_key="synthetic-go-key")
response: Final = gateway.request("POST", path, {"model": model, **_reasoning_input(path)})
assert response.status_code == 200, response.text
assert "ok" in response.text, response.text
@pytest.mark.parametrize("drop_params", (True, False))
def test_unknown_model_reasoning_guard(gateway: Gateway, drop_params: bool) -> None:
def respond(request: Request) -> Reply:
assert request.target.endswith("/chat/completions"), request.target
assert "reasoning_effort" not in _JSON_OBJECT.validate_json(request.body)
return _respond(request)
with wire_server(respond) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model="opencode_go/not-in-catalog-x", api_base=wire.url, api_key="synthetic-go-key", drop_params=drop_params
)
response: Final = gateway.request(
"POST", _CHAT_ROUTE, {"model": model, **_reasoning_input(_CHAT_ROUTE), "reasoning_effort": "high"}
)
assert response.status_code == (200 if drop_params else 400), response.text
if drop_params:
_assert_chat_response(response.text, False)
return
assert "UnsupportedParamsError" in response.text, response.text
assert wire.drain() == ()
@pytest.mark.parametrize("model,protocol", (("minimax-m3", "messages"), ("gpt-6-luna", "responses")))
def test_native_protocol_reasoning_guard(gateway: Gateway, model: str, protocol: str) -> None:
def respond(request: Request) -> Reply:
assert request.target.endswith(f"/v1/{protocol}"), request.target
body: Final = _JSON_OBJECT.validate_json(request.body)
if protocol == "messages":
assert _JSON_OBJECT.validate_python(body["thinking"])["type"] == "enabled", body
else:
assert _JSON_OBJECT.validate_python(body["reasoning"])["effort"] == "high", body
return _respond(request)
with wire_server(respond) as wire, gateway.scenario() as scenario:
deployment: Final = scenario.model(model=f"opencode_go/{model}", api_base=wire.url, api_key="synthetic-go-key")
response: Final = gateway.request(
"POST", _CHAT_ROUTE, {"model": deployment, **_reasoning_input(_CHAT_ROUTE), "reasoning_effort": "high"}
)
assert response.status_code == 200, response.text
_assert_chat_response(response.text, False)

View file

@ -227,6 +227,86 @@ def upstream_reply(route: str, model: str) -> Mapping[str, object]:
}
@pytest.fixture
def reasoning_catalog(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setitem(
litellm.model_cost,
"opencode_go/fixture-reasoning",
{
"litellm_provider": "opencode_go",
"mode": "chat",
"supported_endpoints": ["/v1/chat/completions"],
"supports_reasoning": True,
},
)
@pytest.mark.asyncio
@pytest.mark.parametrize("effort", ("none", "low", "medium", "high"))
@pytest.mark.parametrize("drop_params", (True, False))
async def test_chat_reasoning_effort_forwarded_regression(
reasoning_catalog: None, effort: str, drop_params: bool
) -> None:
def respond(request: httpx.Request) -> httpx.Response:
assert request.url.path.endswith("/chat/completions")
assert request.headers["authorization"] == "Bearer synthetic-go-key"
assert json.loads(request.content).get("reasoning_effort") == effort
return httpx.Response(200, json=upstream_reply("chat/completions", "fixture-reasoning"))
with respx.mock(using="httpx") as transport:
route: Final = transport.post().mock(side_effect=respond)
response: Final = await litellm.acompletion(
model="opencode_go/fixture-reasoning",
messages=[{"role": "user", "content": "hello"}],
api_key="synthetic-go-key",
reasoning_effort=effort,
drop_params=drop_params,
)
assert route.called
assert response.choices[0].message.content == "hello"
assert response.choices[0].finish_reason == "stop"
@pytest.mark.asyncio
@pytest.mark.parametrize("drop_params", (True, False))
async def test_unknown_chat_reasoning_effort_guard(drop_params: bool) -> None:
def respond(request: httpx.Request) -> httpx.Response:
assert request.url.path.endswith("/chat/completions")
assert "reasoning_effort" not in json.loads(request.content)
return httpx.Response(200, json=upstream_reply("chat/completions", "not-in-catalog-x"))
with respx.mock(using="httpx", assert_all_called=drop_params) as transport:
route: Final = transport.post().mock(side_effect=respond)
if not drop_params:
with pytest.raises(litellm.UnsupportedParamsError, match="reasoning_effort"):
await litellm.acompletion(
model="opencode_go/not-in-catalog-x", messages=[{"role": "user", "content": "hello"}],
api_key="synthetic-go-key", reasoning_effort="high", drop_params=False,
)
assert not route.called
return
response: Final = await litellm.acompletion(
model="opencode_go/not-in-catalog-x", messages=[{"role": "user", "content": "hello"}],
api_key="synthetic-go-key", reasoning_effort="high", drop_params=True,
)
assert route.called
assert response.choices[0].message.content == "hello"
def test_chat_supported_reasoning_params_regression(reasoning_catalog: None, go_catalog: None) -> None:
base: Final = frozenset(litellm.get_supported_openai_params(model="opencode_go/fixture-chat") or ())
supported: Final = frozenset(litellm.get_supported_openai_params(model="opencode_go/fixture-reasoning") or ())
assert "reasoning_effort" not in base
assert supported == base | {"reasoning_effort"}
def test_unknown_chat_supported_params_guard(go_catalog: None) -> None:
base: Final = frozenset(litellm.get_supported_openai_params(model="opencode_go/fixture-chat") or ())
supported: Final = frozenset(litellm.get_supported_openai_params(model="opencode_go/not-in-catalog-x") or ())
assert "reasoning_effort" not in supported
assert supported == base
@pytest.mark.asyncio
@pytest.mark.parametrize("upstream", ["chat/completions", "messages", "responses", "gemini"])
@pytest.mark.parametrize("ingress", ["chat", "responses", "messages"])