Skip to content

Python: Set additionalProperties false on nested objects in Anthropic structured outputs - #10281

Open
ni (nniiovoo) wants to merge 2 commits into
microsoft:mainfrom
nniiovoo:python-anthropic-nested-additional-properties
Open

ni (nniiovoo) wants to merge 2 commits into
microsoft:mainfrom
nniiovoo:python-anthropic-nested-additional-properties

Conversation

@nniiovoo

@nniiovoo ni (nniiovoo) commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Motivation & Context

AnthropicClient set additionalProperties: false only 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 supports additionalProperties: false on objects. Repro is in #10280.

Description & Review Guide

  • What are the major changes? A small _forbid_additional_properties helper that sets additionalProperties: false on every nested object schema, replacing a missing, true or schema-valued one (including nullable ["object", "null"] types), used for both Pydantic models and dict schemas. It walks properties and $defs as maps of schemas, so a field called properties or type isn't treated as a schema. Dict schemas are deep-copied first, so the caller's dict is still left alone.
  • What is the impact of these changes? Nested models and nested objects are now sent with additionalProperties: false, as Anthropic requires. Flat schemas are sent the same as before.
  • What do you want reviewers to focus on? Map fields (dict[str, X]) have a schema-valued additionalProperties, which Anthropic doesn't support. They are set to false too, like the Anthropic SDK's transform_schema does, 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

  • No material AI assistance was used.
  • This is an AI-assisted contribution. I reviewed, understood, and verified all submitted content and accept responsibility for it.

AI assistance details: issue analysis, implementation, tests, and this description.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR links to an agreed issue with no competing open PR, or the Related Issue section documents a trivial-change or repository-automation exception.
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

… structured outputs

Signed-off-by: NNIIOVO <haimingni44@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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: false on 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:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@nniiovoo ni (nniiovoo) Nov 12, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@nniiovoo
ni (nniiovoo) deployed to github-app-auth November 12, 2026 20:45 — with GitHub Actions Active
@nniiovoo
ni (nniiovoo) deployed to github-app-auth November 12, 2026 20:45 — with GitHub Actions Active

This branch was successfully deployed

1 active deployment
github-app-auth — bc84a3d7 Deployed Nov 12, 2026 by nniiovoo via add_label #27756
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: Anthropic structured output: nested objects are sent without additionalProperties: false

3 participants