mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-07-01 21:28:10 +00:00
Serialize concrete Pydantic subclasses (#6187)
This commit is contained in:
@@ -99,7 +99,7 @@ def to_serializable(
|
||||
if isinstance(obj, BaseModel):
|
||||
try:
|
||||
return to_serializable(
|
||||
obj=obj.model_dump(mode="json", exclude=exclude),
|
||||
obj=obj.model_dump(mode="json", exclude=exclude, serialize_as_any=True),
|
||||
max_depth=max_depth,
|
||||
_current_depth=_current_depth + 1,
|
||||
_ancestors=new_ancestors,
|
||||
|
||||
@@ -21,6 +21,10 @@ class Person(BaseModel):
|
||||
skills: List[str]
|
||||
|
||||
|
||||
class Container(BaseModel):
|
||||
payload: BaseModel | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class DataclassPerson:
|
||||
name: str
|
||||
@@ -114,6 +118,16 @@ def test_pydantic_model_serialization():
|
||||
)
|
||||
|
||||
|
||||
def test_polymorphic_field_serializes_concrete_subclass():
|
||||
container = Container(
|
||||
payload=Address(street="1 Main", city="Tech City", country="Pythonia")
|
||||
)
|
||||
|
||||
assert to_serializable(container) == {
|
||||
"payload": {"street": "1 Main", "city": "Tech City", "country": "Pythonia"}
|
||||
}
|
||||
|
||||
|
||||
def test_dataclass_serialization_recurses_into_nested_values():
|
||||
person = DataclassPerson(
|
||||
name="Ada",
|
||||
|
||||
Reference in New Issue
Block a user