79656006

Date: 2025-06-06 13:40:13
Score: 1.5
Natty:
Report link

You cannot directly populate a Pydantic model of type A with a model of type B, even if they have overlapping or identical fields, unless model B is a subclass of model A or is explicitly cast or unpacked (e.g. via .dict()) into model A.

A Pydantic model instance is an instance of a specific class — like apples and pears: even if they look alike, they are fundamentally different types. They cannot be treated interchangeably, unlike generic types such as dictionaries.

Solution i ended using

  1. resolve schema definitions
  2. generate template
  3. compare value types when both A and B contain a key path
  4. populate template when compatible types
Reasons:
  • Long answer (-0.5):
  • No code block (0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: lhau