Repository navigation
Python: Set additionalProperties false on nested objects in Anthropic structured outputs - #10281
ni (nniiovoo) wants to merge 2 commits into
Conversation
… structured outputs Signed-off-by: NNIIOVO <haimingni44@gmail.com>
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues were identified for the nested structured-output schema normalization change.
No open issues 🎉
What changed in this PR
This PR addresses #10280 by extending Anthropic structured-output schema normalization from the root to nested objects.
Changes:
- Recursively sets
additionalProperties: falseon nested object schemas, including nullable objects, while preserving explicit nested constraints. - Deep-copies dictionary schemas to avoid modifying caller-owned data.
- Adds regression tests for nested Pydantic models, dictionary schemas, nullable objects, and caller immutability.
| File | Description |
|---|---|
python/packages/anthropic/tests/test_anthropic_client.py |
Adds coverage for nested schema normalization and preservation of caller data. |
python/packages/anthropic/agent_framework_anthropic/_chat_client.py |
Adds recursive normalization for Pydantic and dictionary schemas, with deep copying for dictionaries. |
Review effort: Balanced
| is_object = schema_type == "object" or (isinstance(schema_type, list) and "object" in schema_type) | ||
| if is_object or "properties" in schema: | ||
| additional = schema.get("additionalProperties") | ||
| if additional is None or additional is True: |
There was a problem hiding this comment.
ni (@nniiovoo) Nested map schemas are not normalized to additionalProperties false — A nested map schema, such as one generated for dict[str, str], uses an object with additionalProperties set to a schema dictionary (for example, {"type": "string"}). This condition leaves that value unchanged, so the resulting nested object does not meet the structured-output requirement for additionalProperties: false and the Anthropic request can be rejected. Normalize schema-valued additionalProperties too, or reject this unsupported schema before sending the request.
There was a problem hiding this comment.
Thanks. I went with normalizing: object schemas now always get additionalProperties: false, so a dict[str, str] field is closed too, the same as the Anthropic SDK's transform_schema does. Added a test for it.
Signed-off-by: NNIIOVO <haimingni44@gmail.com>
Motivation & Context
AnthropicClientsetadditionalProperties: falseonly on the root of a structured output schema, so nested models in$defs(and nested objects in dict schemas) went out without it. Anthropic only supportsadditionalProperties: falseon objects. Repro is in #10280.Description & Review Guide
_forbid_additional_propertieshelper that setsadditionalProperties: falseon every nested object schema, replacing a missing,trueor schema-valued one (including nullable["object", "null"]types), used for both Pydantic models and dict schemas. It walkspropertiesand$defsas maps of schemas, so a field calledpropertiesortypeisn't treated as a schema. Dict schemas are deep-copied first, so the caller's dict is still left alone.additionalProperties: false, as Anthropic requires. Flat schemas are sent the same as before.dict[str, X]) have a schema-valuedadditionalProperties, which Anthropic doesn't support. They are set tofalsetoo, like the Anthropic SDK'stransform_schemadoes, so such a field can only be an empty object.Tests: I added tests for a nested Pydantic model, a nested dict schema with a field named
properties, a nullable nested object, and a map field (they fail on main).Related Issue
Fixes #10280
AI Assistance
AI assistance details: issue analysis, implementation, tests, and this description.
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.