Python: fix(claude): preserve $defs in JSON schema for nested Pydantic models#3655
Merged
moonbox3 merged 3 commits intomicrosoft:mainfrom Feb 4, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes the Claude agent’s FunctionTool → SDK MCP conversion so that Pydantic nested models produce valid JSON schemas by preserving the $defs section, and adds a regression test to prevent regressions for nested types.
Changes:
- Updated
_function_tool_to_sdk_mcp_toolto carry over the$defssection from the Pydantic model’smodel_json_schema()into the MCP tool input schema. - Added a dedicated test ensuring that tools with nested Pydantic models have
$defspreserved and that the top-level property referencing the nested model is present.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
python/packages/claude/agent_framework_claude/_agent.py |
Extends the generated MCP input schema to include $defs from the underlying Pydantic schema so nested $ref types resolve correctly. |
python/packages/claude/tests/test_claude_agent.py |
Adds a regression test using nested Pydantic models to verify that $defs is preserved and that the nested model reference appears in properties. |
dmytrostruk
approved these changes
Feb 4, 2026
Member
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
moonbox3
approved these changes
Feb 4, 2026
Contributor
Author
|
@moonbox3 - Can we have please have a look at this ? |
- Preserve $defs section from Pydantic JSON schema when converting FunctionTool to SDK MCP tool - This fixes tools with nested Pydantic models that use $ref references - Add test for nested type schema preservation Fixes microsoft#3654
Contributor
|
I need to get another PR merged that fixes a bug, and thus allows the failing unit test to pass. |
b33d823 to
e8d3930
Compare
Contributor
Author
sure, I just cherry-picked your test failure fixes here |
moonbox3
approved these changes
Feb 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes a bug where
_function_tool_to_sdk_mcp_tool()dropped the$defssection from Pydantic JSON schemas, breaking any tool that uses nested Pydantic models.Problem
When converting a FunctionTool to an SDK MCP tool, the code was only extracting
propertiesandrequiredfrom the Pydantic model's JSON schema, but dropping the$defssection which contains definitions for nested types:This caused tools with nested Pydantic models to produce invalid/unresolvable JSON schemas. The schema would contain
$refreferences like"$ref": "#/$defs/SomeNestedType"but the$defssection was missing, so Claude could not resolve the type and refused to call the tool.Changes
_agent.py$defssection from Pydantic JSON schema when present:test_claude_agent.pytest_function_tool_to_sdk_mcp_tool_preserves_defs_for_nested_types- verifies that nested Pydantic models generate schemas with$defspreservedTesting
All 45 tests pass:
Fixes #3654