Skip to content

fix(core): preserve $defs array/primitive types in schema_to_pydantic_model#7211

Open
Mr-Neutr0n wants to merge 1 commit intomicrosoft:mainfrom
Mr-Neutr0n:fix/schema-defs-array-type
Open

fix(core): preserve $defs array/primitive types in schema_to_pydantic_model#7211
Mr-Neutr0n wants to merge 1 commit intomicrosoft:mainfrom
Mr-Neutr0n:fix/schema-defs-array-type

Conversation

@Mr-Neutr0n
Copy link

Summary

Fixes #7203

When $defs contains non-object type definitions (arrays, primitives), the schema_to_pydantic_model function was incorrectly creating empty BaseModel classes instead of preserving the original type information.

Before (broken):

# Original schema
{"$defs": {"TaskData": {"type": "array", "items": {"type": "string"}}}}

# After conversion - TaskData becomes empty object ❌
{"$defs": {"TaskData": {"properties": {}, "title": "TaskData", "type": "object"}}}

After (fixed):

# TaskData is correctly preserved as array type ✅
instance = Model(task_data=["item1", "item2"])  # Works!

Changes

  • Add _type_cache dictionary to store non-object type definitions separately from model cache
  • Add _schema_to_python_type() method to convert non-object schemas (arrays, primitives) to Python types
  • Update _process_definitions() to detect non-object types and route them appropriately
  • Update get_ref() to check type cache before model cache

Test Plan

…_model

When `$defs` contains non-object type definitions (arrays, primitives),
the schema_to_pydantic_model function was incorrectly creating empty
BaseModel classes instead of preserving the original type information.

This caused schemas with type aliases like:
  `type TaskData = list[str]`

to be converted from:
  `{"type": "array", "items": {"type": "string"}}`

to:
  `{"type": "object", "properties": {}}`

The fix adds a separate `_type_cache` to store non-object type definitions
and a `_schema_to_python_type` method to properly convert them to Python
types. The `get_ref` method now checks both caches.

Fixes microsoft#7203

Signed-off-by: Harikrishna KP <harikp2002@gmail.com>
@Mr-Neutr0n
Copy link
Author

@microsoft-github-policy-service agree

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.

schema_to_pydantic_model loses $defs type information (array → object)

1 participant