Skip to content

fix(core): resolve $ref to $defs defined in nested property schemas#7456

Open
Krishnachaitanyakc wants to merge 1 commit intomicrosoft:mainfrom
Krishnachaitanyakc:fix/schema-to-pydantic-nested-defs
Open

fix(core): resolve $ref to $defs defined in nested property schemas#7456
Krishnachaitanyakc wants to merge 1 commit intomicrosoft:mainfrom
Krishnachaitanyakc:fix/schema-to-pydantic-nested-defs

Conversation

@Krishnachaitanyakc
Copy link
Copy Markdown

Summary

Fixes #7129

When a JSON Schema property defines its own $defs (e.g., an MCP tool schema where a nested parameter object contains enum definitions), schema_to_pydantic_model fails with ReferenceNotFoundError because _json_schema_to_model only processes $defs at the root schema level.

This is triggered when using mcp_server_tools() with MCP servers whose tool input schemas contain nested $defs -- for example, mcp-server-everything-search defines a WindowsSortOption enum inside the windows_params property's $defs.

Error before fix:

autogen_core.utils._json_to_pydantic.ReferenceNotFoundError: Reference `WindowsSortOption` not found in cache. Available: []

Changes

  • In _json_schema_to_model, detect and merge nested $defs into the root schema's $defs so _resolve_ref can locate them
  • Cache object-type nested definitions as models in _model_cache
  • Resolve non-model definitions (enums, primitives) inline when encountered via $ref in properties, avoiding the incorrect creation of empty BaseModels for non-object types
  • Added three new tests covering: nested enum $defs (exact reproduction of When using mcp_server_tools, an error occurs, but it works normally with McpWorkbench. #7129), nested object $defs, and multiple nested $defs across different properties

Test plan

When a JSON Schema property defines its own $defs (e.g., an MCP tool
schema where a nested parameter object contains enum definitions),
schema_to_pydantic_model fails with ReferenceNotFoundError because
_json_schema_to_model only processes $defs at the root schema level.

This fix:
- Merges nested $defs into the root schema's $defs so _resolve_ref
  can locate them
- Caches object-type nested definitions as models in _model_cache
- Resolves non-model definitions (enums, primitives) inline when
  encountered via $ref, avoiding the creation of empty BaseModels

Fixes microsoft#7129
@Krishnachaitanyakc Krishnachaitanyakc marked this pull request as ready for review March 25, 2026 02:11
@Krishnachaitanyakc
Copy link
Copy Markdown
Author

@ekzhu can you please review these changes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When using mcp_server_tools, an error occurs, but it works normally with McpWorkbench.

1 participant