Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions python/packages/anthropic/tests/test_anthropic_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading