Skip to content

Commit 3a3f788

Browse files
authored
fix(serializer): pydantic compat v1 v2 (#988)
1 parent a3a350e commit 3a3f788

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

langfuse/serializer.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from typing import Any
99
from uuid import UUID
1010
from collections.abc import Sequence
11-
from langfuse.api.core import serialize_datetime
11+
from langfuse.api.core import serialize_datetime, pydantic_utilities
1212
from pathlib import Path
1313
from logging import getLogger
1414
from pydantic import BaseModel
@@ -72,13 +72,17 @@ def default(self, obj: Any):
7272
return obj.isoformat()
7373

7474
if isinstance(obj, BaseModel):
75-
obj.model_rebuild() # This method forces the OpenAI model to instantiate its serializer to avoid errors when serializing
75+
obj.model_rebuild() if pydantic_utilities.IS_PYDANTIC_V2 else obj.update_forward_refs() # This method forces the OpenAI model to instantiate its serializer to avoid errors when serializing
7676

7777
# For LlamaIndex models, we need to rebuild the raw model as well if they include OpenAI models
7878
if isinstance(raw := getattr(obj, "raw", None), BaseModel):
79-
raw.model_rebuild()
79+
raw.model_rebuild() if pydantic_utilities.IS_PYDANTIC_V2 else raw.update_forward_refs()
8080

81-
return obj.model_dump()
81+
return (
82+
obj.model_dump()
83+
if pydantic_utilities.IS_PYDANTIC_V2
84+
else obj.dict()
85+
)
8286

8387
if isinstance(obj, Path):
8488
return str(obj)

0 commit comments

Comments
 (0)