diff --git a/src/cgis/guardian/providers/openai_compatible.py b/src/cgis/guardian/providers/openai_compatible.py index 70dfe796..2e89be09 100644 --- a/src/cgis/guardian/providers/openai_compatible.py +++ b/src/cgis/guardian/providers/openai_compatible.py @@ -68,6 +68,21 @@ def _shared_payload(self, system_prompt: str, user_prompt: str) -> dict[str, Any payload["temperature"] = self._temperature return payload + def _response_format(self, schema: type[BaseModel]) -> dict[str, Any]: + """The `response_format` body that asks for `schema`. + + Built in one place so the base pass and a provider's prompt-only + fallback (opencode-go, #781) describe the same schema. + """ + return { + "type": "json_schema", + "json_schema": { + "name": schema.__name__, + "strict": True, + "schema": schema.model_json_schema(), + }, + } + async def generate_content(self, system_prompt: str, user_prompt: str) -> str: """Free-text generation.""" return await self._retry(lambda: self._post(self._payload(system_prompt, user_prompt))) @@ -77,12 +92,5 @@ async def generate_structured( ) -> str: """Generation asked to conform to `schema`; returns raw JSON text.""" payload = self._payload(system_prompt, user_prompt) - payload["response_format"] = { - "type": "json_schema", - "json_schema": { - "name": schema.__name__, - "strict": True, - "schema": schema.model_json_schema(), - }, - } + payload["response_format"] = self._response_format(schema) return await self._retry(lambda: self._post(payload)) diff --git a/src/cgis/guardian/providers/opencode.py b/src/cgis/guardian/providers/opencode.py index b1d01f4a..7fe76a8a 100644 --- a/src/cgis/guardian/providers/opencode.py +++ b/src/cgis/guardian/providers/opencode.py @@ -30,11 +30,15 @@ from typing import Any, ClassVar, Final, override import httpx +import structlog +from pydantic import BaseModel from cgis import __version__ from cgis.guardian.providers.base import RETRYABLE_EXCEPTIONS, ProviderUsage from cgis.guardian.providers.openai_compatible import OpenAICompatibleProvider +log = structlog.getLogger(__name__) + #: The model this provider exists for: the finder's default, priced at #: 0.15/0.6 per 1M tokens in/out when the switch was measured (#775). DEFAULT_OPENCODE_MODEL: Final = "deepseek-v4.1-flash" @@ -76,6 +80,16 @@ class OpenCodeTransientError(RuntimeError): """ +class OpenCodeStructuredOutputUnavailableError(RuntimeError): + """The gateway's upstream rejected `response_format` as unavailable (#781). + + Not a bad request to repair and not a transient `inference_failed` to retry + unchanged: the same prompt sent without the parameter is answerable, and the + caller parses the reply defensively. Raised so `generate_structured` can drop + the parameter and continue, never returned and never a verdict. + """ + + class OpenCodeQuotaError(RuntimeError): """Raised on HTTP 429 — the gateway's quota for this key is spent. @@ -117,6 +131,27 @@ def _is_transient_upstream_failure(body_text: str) -> bool: return any(candidate == "inference_failed" for candidate in candidates) +def _is_structured_output_unavailable(body_text: str) -> bool: + """Whether the gateway rejected `response_format` as unavailable (#781). + + Read from the error message, not from the status: a 400 that says something + else is not answerable by dropping the parameter. The wording measured on + 2026-11-05 is "This response_format type is unavailable now". + """ + try: + parsed: object = json.loads(body_text) + except ValueError: + return False + if not isinstance(parsed, dict): + return False + error = parsed.get("error") + message = error.get("message") if isinstance(error, dict) else None + if not isinstance(message, str): + return False + lowered = message.lower() + return "response_format" in lowered and "unavailable" in lowered + + class OpenCodeProvider(OpenAICompatibleProvider): """A DeepSeek model reached through opencode-go's OpenAI-compatible API.""" @@ -150,6 +185,10 @@ def __init__( # value or a bare "/" and the empty-string fallback is not duplicated. self._base_url = (base_url or "").strip().rstrip("/") or DEFAULT_OPENCODE_BASE_URL self._session_id = session_id or uuid.uuid4().hex + # Set once the gateway reports `response_format` unavailable, so the + # remaining calls in this run go prompt-only instead of paying the same + # 400 on every chunk (#781). One review is one provider instance. + self._structured_output_unavailable = False @property @override @@ -254,6 +293,8 @@ def _raise_for_http_error(self, response: httpx.Response) -> None: ) if response.status_code == _BAD_REQUEST and _is_transient_upstream_failure(body_text): raise OpenCodeTransientError(_msg) + if response.status_code == _BAD_REQUEST and _is_structured_output_unavailable(body_text): + raise OpenCodeStructuredOutputUnavailableError(_msg) raise RuntimeError(_msg) @override @@ -268,3 +309,29 @@ def _payload(self, system_prompt: str, user_prompt: str) -> dict[str, Any]: payload = self._shared_payload(system_prompt, user_prompt) payload["reasoning_effort"] = self._effort return payload + + @override + async def generate_structured( + self, system_prompt: str, user_prompt: str, schema: type[BaseModel] + ) -> str: + """Generation with `schema`, dropping `response_format` when the gateway refuses it. + + The gateway's upstreams do not all accept the structured-output + parameter; one answers `400 invalid_request_error` with "This + response_format type is unavailable now" (#781). The prompt still asks + for the same JSON and the caller parses defensively, so re-sending + without the parameter is a real answer rather than a failed review. The + refusal is remembered for the rest of this provider's life: a review + makes one call per chunk, and each would otherwise pay the same 400 + before falling back. + """ + payload = self._payload(system_prompt, user_prompt) + if not self._structured_output_unavailable: + payload["response_format"] = self._response_format(schema) + try: + return await self._retry(lambda: self._post(payload)) + except OpenCodeStructuredOutputUnavailableError: + self._structured_output_unavailable = True + payload.pop("response_format", None) + log.warning("Structured output unavailable upstream; retrying with the prompt only.") + return await self._retry(lambda: self._post(payload)) diff --git a/tests/unit/test_guardian_opencode.py b/tests/unit/test_guardian_opencode.py index d65d3f01..9e3cc49c 100644 --- a/tests/unit/test_guardian_opencode.py +++ b/tests/unit/test_guardian_opencode.py @@ -10,6 +10,7 @@ session header. The failure tests pin what it refuses to return. """ +import copy from typing import Any import httpx @@ -84,7 +85,7 @@ async def __aexit__(self, *_exc: object) -> None: async def post(self, url: str, **kwargs: object) -> httpx.Response: captured["urls"].append(url) captured["headers"].append(kwargs["headers"]) - captured["bodies"].append(kwargs["json"]) + captured["bodies"].append(copy.deepcopy(kwargs["json"])) if single is not None: return single return queue.pop(0) @@ -222,6 +223,74 @@ async def test_a_non_json_body_with_the_code_is_still_retried( assert content.startswith("{") assert provider.slept == [2.0] + @pytest.mark.asyncio + async def test_a_rejected_response_format_falls_back_to_the_prompt( + self, monkeypatch: pytest.MonkeyPatch + ) -> None: + """The gateway may answer `response_format` with "unavailable now" (#781).""" + captured = _serve( + monkeypatch, + [ + _reply( + status=400, + body={ + "error": { + "type": "invalid_request_error", + "message": "This response_format type is unavailable now", + } + }, + ), + _reply(), + ], + ) + provider = _provider() + content = await provider.generate_structured("s", "u", _Judgement) + assert "verdict" in content + assert "response_format" in captured["bodies"][0] + assert "response_format" not in captured["bodies"][1] + assert len(captured["urls"]) == 2 + + @pytest.mark.asyncio + async def test_the_structured_output_refusal_is_remembered_for_the_rest_of_the_run( + self, monkeypatch: pytest.MonkeyPatch + ) -> None: + """One 400 per run, not one per chunk: the next call goes prompt-only.""" + captured = _serve( + monkeypatch, + [ + _reply( + status=400, + body={"error": {"message": "This response_format type is unavailable now"}}, + ), + _reply(), + _reply(), + ], + ) + provider = _provider() + await provider.generate_structured("s", "u", _Judgement) + await provider.generate_structured("s", "u", _Judgement) + assert len(captured["urls"]) == 3 + assert "response_format" not in captured["bodies"][2] + + @pytest.mark.asyncio + async def test_a_failed_fallback_raises_with_its_body( + self, monkeypatch: pytest.MonkeyPatch + ) -> None: + """If the prompt-only retry fails too, the error is carried, not degraded.""" + _serve( + monkeypatch, + [ + _reply( + status=400, + body={"error": {"message": "This response_format type is unavailable now"}}, + ), + _reply(status=400, body={"error": {"message": "still down"}}), + ], + ) + provider = _provider() + with pytest.raises(RuntimeError, match="still down"): + await provider.generate_structured("s", "u", _Judgement) + @pytest.mark.asyncio async def test_hitting_the_token_budget_raises_rather_than_returning_reasoning( self, monkeypatch: pytest.MonkeyPatch