Skip to content

Fix #7203 - Preserve type info for non-object schemas in json_to_pydantic#7862

Open
Whning0513 wants to merge 1 commit into
microsoft:mainfrom
Whning0513:fix-issue-7203
Open

Fix #7203 - Preserve type info for non-object schemas in json_to_pydantic#7862
Whning0513 wants to merge 1 commit into
microsoft:mainfrom
Whning0513:fix-issue-7203

Conversation

@Whning0513

@Whning0513 Whning0513 commented Jun 19, 2026

Copy link
Copy Markdown

schema_to_pydantic_model loses $defs type information (array -> object)

When converting a JSON schema generated by Pydantic v2 using schema_to_pydantic_model, type information inside $defs is not preserved. Array types (e.g., {"type": "array", "items": {"type": "string"}}) get converted to empty objects ({"type": "object", "properties": {}}), breaking the schema round-trip.

Reported by: Momo-Not-Emo

The _json_to_pydantic.py utility's schema_to_pydantic_model function recursively converts JSON schemas to Pydantic models. When processing $defs entries, it assumed all definitions were object schemas and created Pydantic models for them, losing type information for non-object types (arrays, unions, literals, etc.).

File: python/packages/autogen-core/src/autogen_core/utils/_json_to_pydantic.py

  • Added type-preserving logic for non-object schemas in $defs
  • Non-object types (arrays, strings, numbers, booleans, etc.) are now properly handled without unnecessary Pydantic model wrapping
  • Added proper handling for $ref resolution to ensure defs are correctly referenced

Medium - The change modifies schema-to-Pydantic conversion logic which is used by structured output features. While it fixes a correctness bug, care was taken to maintain backward compatibility for object schemas. Non-object type definitions that were previously silently corrupted will now produce correct types.

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.

1 participant