diff --git a/python/packages/anthropic/agent_framework_anthropic/_chat_client.py b/python/packages/anthropic/agent_framework_anthropic/_chat_client.py index b3b0458457c..b6f471f8397 100644 --- a/python/packages/anthropic/agent_framework_anthropic/_chat_client.py +++ b/python/packages/anthropic/agent_framework_anthropic/_chat_client.py @@ -847,7 +847,8 @@ def _prepare_response_format(self, response_format: type[BaseModel] | dict[str, schema = response_format if isinstance(schema, dict): - schema["additionalProperties"] = False + # Copy, so the caller's response_format is not changed for later requests. + schema = {**schema, "additionalProperties": False} return { "type": "json_schema", diff --git a/python/packages/anthropic/tests/test_anthropic_client.py b/python/packages/anthropic/tests/test_anthropic_client.py index 24597877291..ca3bed0700b 100644 --- a/python/packages/anthropic/tests/test_anthropic_client.py +++ b/python/packages/anthropic/tests/test_anthropic_client.py @@ -3051,6 +3051,19 @@ def test_prepare_response_format_openai_style(mock_anthropic_client: MagicMock) assert result["schema"]["properties"]["name"]["type"] == "string" +def test_prepare_response_format_does_not_modify_caller_schema(mock_anthropic_client: MagicMock) -> None: + """The caller's schema dict keeps its own additionalProperties for later requests.""" + client = create_test_anthropic_client(mock_anthropic_client) + + schema = {"type": "object", "properties": {"name": {"type": "string"}}, "additionalProperties": True} + response_format = {"json_schema": {"schema": schema}} + + result = client._prepare_response_format(response_format) + + assert result["schema"]["additionalProperties"] is False + assert schema["additionalProperties"] is True + + def test_prepare_response_format_direct_schema( mock_anthropic_client: MagicMock, ) -> None: