From 6b9a70b10dfd9e745cd7204e70e5fc37f5a342dc Mon Sep 17 00:00:00 2001 From: Crawford Collins Date: Tue, 11 Nov 2025 23:26:22 -0600 Subject: [PATCH] adding a logical branch to model_dump pydantic objects in Example.to_dict() --- dspy/primitives/example.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/dspy/primitives/example.py b/dspy/primitives/example.py index 549da6bce7..82177cc0f4 100644 --- a/dspy/primitives/example.py +++ b/dspy/primitives/example.py @@ -1,3 +1,6 @@ +from pydantic import BaseModel + + class Example: """A flexible data container for DSPy examples and training data. @@ -191,7 +194,9 @@ def without(self, *keys): def toDict(self): # noqa: N802 def convert_to_serializable(value): - if hasattr(value, "toDict"): + if isinstance(value, BaseModel): + return value.model_dump() + elif hasattr(value, "toDict"): return value.toDict() elif isinstance(value, list): return [convert_to_serializable(item) for item in value]