Skip to content

Conversation

@pingsutw
Copy link
Member

@pingsutw pingsutw commented Jan 28, 2026

Summary

  • Use model_fields instead of typing.get_type_hints() in get_literal_type to handle Pydantic models with forward references to locally-defined classes
  • typing.get_type_hints() fails when it cannot resolve forward references that are not in the module's global namespace (e.g., classes defined inside functions)
  • Pydantic's model_fields already has the resolved type information, making it more robust for these cases

Test plan

  • Added unit test test_pydantic_model_with_locally_defined_nested_model that verifies:
    • get_literal_type works with locally-defined nested models
    • to_literal serializes the model correctly
    • to_python_value deserializes the model correctly with all values preserved

Use model_fields instead of typing.get_type_hints() in get_literal_type
to handle Pydantic models with forward references to locally-defined classes.

typing.get_type_hints() fails when it cannot resolve forward references
that are not in the module's global namespace (e.g., classes defined
inside functions). Pydantic's model_fields already has the resolved
type information, making it more robust for these cases.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Kevin Su <pingsutw@apache.org>
Signed-off-by: Kevin Su <pingsutw@apache.org>
Signed-off-by: Kevin Su <pingsutw@apache.org>
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.

2 participants