The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(aux): retry once without response_format when a provider rejects it
ethernet3c675019f124
diff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.pyindex 8bc5faf871b..ca6bea2d26d 100644--- a/agent/auxiliary_client.py+++ b/agent/auxiliary_client.py@@ -4368,6 +4368,71 @@ def _is_unsupported_temperature_error(exc: Exception) -> bool:return _is_unsupported_parameter_error(exc, "temperature")+def _is_structured_output_rejection(exc: Exception) -> bool:+ """Detect provider 400s that reject the structured-output request field.++ One predicate covers the field on both wires, because both come from the+ same caller-supplied ``response_format``:++ - OpenAI wire: the provider rejects ``response_format`` itself. vLLM+ gateways translate the field into ``guided_grammar`` and fail when the+ grammar backend is absent (``compile_grammar_error: No module named+ 'xgrammar'``, #82816). Other endpoints answer ``This response_format+ type is unavailable now``.+ - Anthropic wire: the adapter translates ``response_format`` into+ ``output_config.format``. Gateways that predate structured outputs+ (the documented case is the ``bedrock-mantle`` Messages endpoint)+ reject that field: ``output_config: Extra inputs are not permitted``.++ Callers tolerate an unconstrained reply — the title prompt demands bare+ JSON and ``_extract_title_text`` has a loose-JSON fallback — so the right+ reaction is one retry without the field, not a hard failure.+ """+ status = getattr(exc, "status_code", None)+ if status is not None and status not in {400, 422}:+ return False+ err_lower = str(exc).lower()+ # vLLM grammar-backend failures name the translated parameter, not ours.+ if "guided_grammar" in err_lower or "xgrammar" in err_lower or (+ "compile_grammar_error" in err_lower+ ):+ return True+ if "extra inputs are not permitted" in err_lower and (+ "response_format" in err_lower or "output_config" in err_lower+ ):+ return True+ if "response_format" in err_lower and "unavailable" in err_lower:+ return True+ return (+ _is_unsupported_parameter_error(exc, "response_format")+ or _is_unsupported_parameter_error(exc, "output_config")+ )+++def _without_structured_output_format(kwargs: dict) -> Optional[dict]:+ """Copy *kwargs* without any ``response_format`` request field.++ Removes the top-level kwarg and the ``extra_body`` entry. Returns None+ when the kwargs carry no such field, so call sites do not retry a+ request that the removal did not change.+ """+ changed = False+ retry_kwargs = dict(kwargs)+ if retry_kwargs.pop("response_format", None) is not None:+ changed = True+ extra_body = retry_kwargs.get("extra_body")+ if isinstance(extra_body, dict) and "response_format" in extra_body:+ remaining = {+ k: v for k, v in extra_body.items() if k != "response_format"+ }+ if remaining:+ retry_kwargs["extra_body"] = remaining+ else:+ retry_kwargs.pop("extra_body", None)+ changed = True+ return retry_kwargs if changed else None++def _is_model_not_found_error(exc: Exception) -> bool:"""Detect "the requested model doesn't exist" errors (404 / invalid model).@@ -9541,6 +9606,39 @@ def _call_llm_impl(first_err = retry_errkwargs = retry_kwargs+ if _is_structured_output_rejection(first_err):+ retry_kwargs = _without_structured_output_format(kwargs)+ if retry_kwargs is not None:+ logger.info(+ "Auxiliary %s: provider rejected the structured-output "+ "format field; retrying once without it (schema "+ "enforcement degrades to prompt compliance): %s",+ task or "call", first_err,+ )+ try:+ return _validate_llm_response(+ _relay_sync_completion(+ client,+ retry_kwargs,+ provider=resolved_provider,+ api_mode=resolved_api_mode,+ ), task)+ except Exception as retry_err:+ # Same contract as the temperature rung: fall through to+ # the max_tokens / payment / auth chains below with the+ # stripped kwargs; re-raise anything those chains do not+ # handle.+ if not (+ _is_payment_error(retry_err)+ or _is_connection_error(retry_err)+ or _is_auth_error(retry_err)+ or "max_tokens" in str(retry_err)+ or "unsupported_parameter" in str(retry_err)+ ):+ raise+ first_err = retry_err+ kwargs = retry_kwargs+err_str = str(first_err)# ZAI vision models (glm-4v-flash etc.) return error code 1210# ("API 调用参数有误") when max_tokens is passed on multimodal@@ -10253,6 +10351,40 @@ async def _async_call_llm_impl(first_err = retry_errkwargs = retry_kwargs+ if _is_structured_output_rejection(first_err):+ retry_kwargs = _without_structured_output_format(kwargs)+ if retry_kwargs is not None:+ logger.info(+ "Auxiliary %s (async): provider rejected the "+ "structured-output format field; retrying once without "+ "it (schema enforcement degrades to prompt "+ "compliance): %s",+ task or "call", first_err,+ )+ try:+ return _validate_llm_response(+ await _relay_async_completion(+ client,+ retry_kwargs,+ provider=resolved_provider,+ api_mode=resolved_api_mode,+ ), task)+ except Exception as retry_err:+ # Same contract as the temperature rung: fall through to+ # the max_tokens / payment / auth chains below with the+ # stripped kwargs; re-raise anything those chains do not+ # handle.+ if not (+ _is_payment_error(retry_err)+ or _is_connection_error(retry_err)+ or _is_auth_error(retry_err)+ or "max_tokens" in str(retry_err)+ or "unsupported_parameter" in str(retry_err)+ ):+ raise+ first_err = retry_err+ kwargs = retry_kwargs+err_str = str(first_err)# ZAI vision models (glm-4v-flash etc.) return error code 1210# ("API 调用参数有误") when max_tokens is passed on multimodaldiff --git a/tests/agent/test_structured_output_rejection_retry.py b/tests/agent/test_structured_output_rejection_retry.pynew file mode 100644index 00000000000..6b0132c755d--- /dev/null+++ b/tests/agent/test_structured_output_rejection_retry.py@@ -0,0 +1,307 @@+"""Regression tests for the structured-output rejection retry in+``agent.auxiliary_client``.++Auxiliary callers (title generation, plugin structured completions) send an+OpenAI ``response_format`` request field. Some providers reject the field, or+its Anthropic translation, with a hard 400:++ * vLLM gateways translate ``response_format: json_schema`` into+ ``guided_grammar`` and fail when the grammar backend is absent+ (``compile_grammar_error: No module named 'xgrammar'``, #82816).+ * Some OpenAI-compatible endpoints answer+ ``This response_format type is unavailable now`` (#82816).+ * Anthropic-compatible gateways that predate structured outputs reject the+ translated ``output_config`` field with+ ``output_config: Extra inputs are not permitted`` (the documented case is+ the ``bedrock-mantle`` Messages endpoint).++Callers tolerate an unconstrained reply: the title prompt demands bare JSON+and ``_extract_title_text`` has a loose-JSON fallback. The fix is reactive,+like the temperature retry: when the provider rejects the structured-output+field, retry once without it. These tests lock in that behaviour for both+sync and async paths.+"""++from unittest.mock import patch, MagicMock, AsyncMock++import pytest++from agent.auxiliary_client import (+ call_llm,+ async_call_llm,+ _is_structured_output_rejection,+ _without_structured_output_format,+)+++_TITLE_RESPONSE_FORMAT = {+ "type": "json_schema",+ "json_schema": {+ "name": "session_title",+ "strict": True,+ "schema": {+ "type": "object",+ "properties": {"title": {"type": "string"}},+ "required": ["title"],+ "additionalProperties": False,+ },+ },+}+++class TestIsStructuredOutputRejection:+ """The detector must match the phrasings providers actually return."""++ @pytest.mark.parametrize("message", [+ # vLLM guided_grammar / xgrammar (#82816, verbatim from the report)+ (+ "Error code: 400 - {'error': {'message': 'guided_grammar "+ '\'{"additionalProperties":false}\' has compile_grammar_error: '+ "No module named 'xgrammar'', 'type': 'invalid_request_error'}}"+ ),+ # Second endpoint from the same report+ "HTTP 400: This response_format type is unavailable now",+ # Strict Anthropic-wire gateways rejecting the raw OpenAI field+ "HTTP 400: response_format: Extra inputs are not permitted",+ # Gateways that predate output_config (bedrock-mantle documented case)+ "HTTP 400: output_config: Extra inputs are not permitted",+ # Generic unsupported-parameter phrasings for both field names+ "Unsupported parameter: response_format",+ "output_config is not supported",+ ])+ def test_matches_real_provider_messages(self, message):+ assert _is_structured_output_rejection(RuntimeError(message)) is True++ @pytest.mark.parametrize("message", [+ # Unrelated 400s must NOT trigger a silent schema downgrade+ "HTTP 400: Invalid value: 'tool'. Supported values are: 'assistant'",+ "HTTP 400: Unsupported parameter: temperature",+ "max_tokens is too large for this model",+ "Rate limit exceeded",+ "Connection reset by peer",+ # Alternation errors that happen to mention messages+ "messages: Extra inputs are not permitted",+ ])+ def test_does_not_match_unrelated_errors(self, message):+ assert _is_structured_output_rejection(RuntimeError(message)) is False++ def test_does_not_match_non_400_statuses(self):+ exc = RuntimeError("output_config: Extra inputs are not permitted")+ exc.status_code = 500+ assert _is_structured_output_rejection(exc) is False+++class TestWithoutStructuredOutputFormat:+ """The kwargs scrubber removes the field on both call shapes."""++ def test_removes_extra_body_entry_and_keeps_siblings(self):+ kwargs = {+ "model": "m",+ "extra_body": {+ "response_format": dict(_TITLE_RESPONSE_FORMAT),+ "metadata": {"user_id": "u1"},+ },+ }+ result = _without_structured_output_format(kwargs)+ assert result is not None+ assert result["extra_body"] == {"metadata": {"user_id": "u1"}}+ # The input dict is not mutated.+ assert "response_format" in kwargs["extra_body"]++ def test_drops_extra_body_entirely_when_it_becomes_empty(self):+ kwargs = {+ "model": "m",+ "extra_body": {"response_format": dict(_TITLE_RESPONSE_FORMAT)},+ }+ result = _without_structured_output_format(kwargs)+ assert result is not None+ assert "extra_body" not in result++ def test_removes_top_level_kwarg(self):+ kwargs = {"model": "m", "response_format": dict(_TITLE_RESPONSE_FORMAT)}+ result = _without_structured_output_format(kwargs)+ assert result is not None+ assert "response_format" not in result++ def test_returns_none_when_nothing_to_remove(self):+ assert _without_structured_output_format({"model": "m"}) is None+ assert _without_structured_output_format(+ {"model": "m", "extra_body": {"metadata": {}}}+ ) is None+++def _dummy_response():+ return {"ok": True}+++class TestCallLlmStructuredOutputRetry:+ """``call_llm`` retries once without the field and returns on success."""++ def _setup(self, first_exc):+ client = MagicMock()+ client.base_url = "https://api.openai.com/v1"+ client.chat.completions.create.side_effect = [+ first_exc, _dummy_response(),+ ]+ return client++ @pytest.mark.parametrize("error_message", [+ # vLLM guided_grammar (#82816)+ "Error code: 400 - guided_grammar has compile_grammar_error: "+ "No module named 'xgrammar'",+ # Second endpoint flavor from the same report+ "HTTP 400: This response_format type is unavailable now",+ # Strict gateway that rejects the translated Anthropic field+ "HTTP 400: output_config: Extra inputs are not permitted",+ ])+ def test_retries_once_without_response_format(self, error_message):+ client = self._setup(RuntimeError(error_message))++ with (+ patch("agent.auxiliary_client._resolve_task_provider_model",+ return_value=("openai-codex", "gpt-5.5", None, None, None)),+ patch("agent.auxiliary_client._get_cached_client",+ return_value=(client, "gpt-5.5")),+ patch("agent.auxiliary_client._validate_llm_response",+ side_effect=lambda resp, _task, **_kw: resp),+ ):+ result = call_llm(+ task="title_generation",+ messages=[{"role": "user", "content": "hi"}],+ max_tokens=64,+ extra_body={"response_format": dict(_TITLE_RESPONSE_FORMAT)},+ )++ assert result == {"ok": True}+ assert client.chat.completions.create.call_count == 2+ first_kwargs = client.chat.completions.create.call_args_list[0].kwargs+ retry_kwargs = client.chat.completions.create.call_args_list[1].kwargs+ first_eb = first_kwargs.get("extra_body") or {}+ retry_eb = retry_kwargs.get("extra_body") or {}+ assert "response_format" in first_eb+ assert "response_format" not in retry_eb+ assert "response_format" not in retry_kwargs+ assert retry_kwargs["model"] == first_kwargs["model"]++ def test_unrelated_400_does_not_strip_response_format(self):+ """Unrelated 400s must not silently downgrade the schema contract."""+ client = MagicMock()+ client.base_url = "https://api.openai.com/v1"+ client.chat.completions.create.side_effect = RuntimeError(+ "HTTP 400: Invalid value: 'tool'. Supported values are: 'assistant'"+ )++ with (+ patch("agent.auxiliary_client._resolve_task_provider_model",+ return_value=("openai-codex", "gpt-5.5", None, None, None)),+ patch("agent.auxiliary_client._get_cached_client",+ return_value=(client, "gpt-5.5")),+ patch("agent.auxiliary_client._validate_llm_response",+ side_effect=lambda resp, _task, **_kw: resp),+ patch("agent.auxiliary_client._try_payment_fallback",+ return_value=None),+ ):+ with pytest.raises(RuntimeError, match="Invalid value"):+ call_llm(+ task="title_generation",+ messages=[{"role": "user", "content": "x"}],+ max_tokens=64,+ extra_body={+ "response_format": dict(_TITLE_RESPONSE_FORMAT),+ },+ )+ assert client.chat.completions.create.call_count == 1++ def test_no_retry_when_no_response_format_was_sent(self):+ """A rejection with no field in the request must not loop a retry."""+ client = MagicMock()+ client.base_url = "https://api.openai.com/v1"+ client.chat.completions.create.side_effect = RuntimeError(+ "HTTP 400: output_config: Extra inputs are not permitted"+ )++ with (+ patch("agent.auxiliary_client._resolve_task_provider_model",+ return_value=("openai-codex", "gpt-5.5", None, None, None)),+ patch("agent.auxiliary_client._get_cached_client",+ return_value=(client, "gpt-5.5")),+ patch("agent.auxiliary_client._validate_llm_response",+ side_effect=lambda resp, _task, **_kw: resp),+ patch("agent.auxiliary_client._try_payment_fallback",+ return_value=None),+ ):+ with pytest.raises(RuntimeError):+ call_llm(+ task="title_generation",+ messages=[{"role": "user", "content": "x"}],+ max_tokens=64,+ )+ assert client.chat.completions.create.call_count == 1+++class TestAsyncCallLlmStructuredOutputRetry:+ """``async_call_llm`` mirror of the sync retry semantics."""++ @pytest.mark.asyncio+ async def test_async_retries_once_without_response_format(self):+ client = MagicMock()+ client.base_url = "https://api.openai.com/v1"+ client.chat.completions.create = AsyncMock(side_effect=[+ RuntimeError(+ "Error code: 400 - guided_grammar has compile_grammar_error: "+ "No module named 'xgrammar'"+ ),+ _dummy_response(),+ ])++ with (+ patch("agent.auxiliary_client._resolve_task_provider_model",+ return_value=("openai-codex", "gpt-5.5", None, None, None)),+ patch("agent.auxiliary_client._get_cached_client",+ return_value=(client, "gpt-5.5")),+ patch("agent.auxiliary_client._validate_llm_response",+ side_effect=lambda resp, _task, **_kw: resp),+ ):+ result = await async_call_llm(+ task="title_generation",+ messages=[{"role": "user", "content": "hi"}],+ max_tokens=64,+ extra_body={"response_format": dict(_TITLE_RESPONSE_FORMAT)},+ )++ assert result == {"ok": True}+ assert client.chat.completions.create.await_count == 2+ first_kwargs = client.chat.completions.create.call_args_list[0].kwargs+ retry_kwargs = client.chat.completions.create.call_args_list[1].kwargs+ assert "response_format" in (first_kwargs.get("extra_body") or {})+ assert "response_format" not in (retry_kwargs.get("extra_body") or {})+ assert "response_format" not in retry_kwargs++ @pytest.mark.asyncio+ async def test_async_unrelated_400_does_not_retry(self):+ client = MagicMock()+ client.base_url = "https://api.openai.com/v1"+ client.chat.completions.create = AsyncMock(+ side_effect=RuntimeError("HTTP 400: Invalid value: 'tool'"),+ )++ with (+ patch("agent.auxiliary_client._resolve_task_provider_model",+ return_value=("openai-codex", "gpt-5.5", None, None, None)),+ patch("agent.auxiliary_client._get_cached_client",+ return_value=(client, "gpt-5.5")),+ patch("agent.auxiliary_client._validate_llm_response",+ side_effect=lambda resp, _task, **_kw: resp),+ patch("agent.auxiliary_client._try_payment_fallback",+ return_value=None),+ ):+ with pytest.raises(RuntimeError, match="Invalid value"):+ await async_call_llm(+ task="title_generation",+ messages=[{"role": "user", "content": "x"}],+ max_tokens=64,+ extra_body={+ "response_format": dict(_TITLE_RESPONSE_FORMAT),+ },+ )+ assert client.chat.completions.create.await_count == 1