Skip to content

The agent that grows with you

fix(aux): translate top-level response_format kwarg on the Anthropic adapter

ethernet8f2d61e3dea0
diff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.py
index 587b9442d3c..8bc5faf871b 100644
--- a/agent/auxiliary_client.py
+++ b/agent/auxiliary_client.py
@@ -2100,6 +2100,19 @@ class _AnthropicCompletionsAdapter:
# - ``_``-prefixed keys: private Hermes plumbing (_reasoning_config
# et al.), never wire fields.
caller_extra_body = kwargs.get("extra_body")
+ # A top-level ``response_format`` kwarg (the OpenAI SDK's documented
+ # call shape) must get the same translation as the extra_body form.
+ # The adapter builds the Messages body from a fixed allow-list of
+ # kwargs, so before this an unrecognized top-level kwarg was dropped
+ # on the floor: the request succeeded but the schema contract
+ # silently became prompt compliance (#85626 review, point 2). When
+ # both shapes are present, the extra_body form wins — it is the shape
+ # every in-tree caller uses.
+ top_level_response_format = kwargs.get("response_format")
+ if top_level_response_format is not None:
+ _translate_anthropic_response_format(
+ anthropic_kwargs, top_level_response_format,
+ )
if caller_extra_body and isinstance(caller_extra_body, dict):
_translate_anthropic_response_format(
anthropic_kwargs, caller_extra_body.get("response_format"),
diff --git a/tests/agent/test_anthropic_structured_output.py b/tests/agent/test_anthropic_structured_output.py
index eb504b6a550..34f92a05935 100644
--- a/tests/agent/test_anthropic_structured_output.py
+++ b/tests/agent/test_anthropic_structured_output.py
@@ -8,7 +8,8 @@ from unittest.mock import MagicMock, patch
def _capture_anthropic_kwargs(
- extra_body: dict, *, model: str = "claude-sonnet-4-6", async_call: bool = False,
+ extra_body: dict | None, *, model: str = "claude-sonnet-4-6",
+ async_call: bool = False, top_level_kwargs: dict | None = None,
) -> dict:
from agent.auxiliary_client import (
_AnthropicCompletionsAdapter,
@@ -35,6 +36,9 @@ def _capture_anthropic_kwargs(
reasoning=None,
finish_reason="stop",
)
+ call_kwargs = dict(top_level_kwargs or {})
+ if extra_body is not None:
+ call_kwargs["extra_body"] = extra_body
with patch(
"agent.anthropic_adapter.create_anthropic_message",
side_effect=_fake_create,
@@ -44,7 +48,7 @@ def _capture_anthropic_kwargs(
model=model,
messages=[{"role": "user", "content": "hi"}],
max_tokens=64,
- extra_body=extra_body,
+ **call_kwargs,
)
if async_call:
asyncio.run(call)
@@ -145,3 +149,66 @@ def test_async_anthropic_adapter_uses_the_same_translation():
"schema": schema,
}
_assert_no_raw_response_format(api_kwargs)
+
+
+def test_top_level_response_format_kwarg_is_translated_not_dropped():
+ """#85626 review, point 2: the top-level kwarg shape must also translate.
+
+ ``client.chat.completions.create(..., response_format=...)`` is the
+ OpenAI SDK's documented call shape. The adapter builds the Messages body
+ from a fixed allow-list of kwargs, so before this an unrecognized
+ top-level kwarg was dropped on the floor: the request succeeded, but the
+ schema contract silently became prompt compliance. Pin-test pattern from
+ PR #85626 (Matt McClean), adapted from strip to translate semantics.
+ """
+ schema = {
+ "type": "object",
+ "properties": {"title": {"type": "string"}},
+ "required": ["title"],
+ }
+ api_kwargs = _capture_anthropic_kwargs(
+ None,
+ top_level_kwargs={
+ "response_format": {
+ "type": "json_schema",
+ "json_schema": {"name": "session_title", "schema": schema},
+ },
+ },
+ )
+
+ assert api_kwargs["output_config"]["format"] == {
+ "type": "json_schema",
+ "schema": schema,
+ }
+ _assert_no_raw_response_format(api_kwargs)
+
+
+def test_extra_body_response_format_wins_over_top_level_kwarg():
+ """When both shapes are present, extra_body wins.
+
+ Every in-tree caller uses the extra_body shape. The top-level kwarg is
+ the compatibility path, so it must not override an explicit extra_body
+ value when a caller somehow sends both.
+ """
+ eb_schema = {"type": "object", "properties": {"a": {"type": "string"}}}
+ top_schema = {"type": "object", "properties": {"b": {"type": "string"}}}
+ api_kwargs = _capture_anthropic_kwargs(
+ {
+ "response_format": {
+ "type": "json_schema",
+ "json_schema": {"schema": eb_schema},
+ },
+ },
+ top_level_kwargs={
+ "response_format": {
+ "type": "json_schema",
+ "json_schema": {"schema": top_schema},
+ },
+ },
+ )
+
+ assert api_kwargs["output_config"]["format"] == {
+ "type": "json_schema",
+ "schema": eb_schema,
+ }
+ _assert_no_raw_response_format(api_kwargs)