Skip to content

Commit

Permalink
fix: serialize BaseModel subclasses and avoid calling methods on types (
Browse files Browse the repository at this point in the history
#4479)

Fix serialization for BaseModel subclasses by handling type objects separately
  • Loading branch information
ogabrielluiz authored Nov 9, 2024
1 parent 33a42bc commit 69a1c16
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/backend/base/langflow/schema/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

def recursive_serialize_or_str(obj):
try:
if isinstance(obj, type) and issubclass(obj, BaseModel):
# This a type BaseModel and not an instance of it
return repr(obj)
if isinstance(obj, str):
return obj
if isinstance(obj, datetime):
Expand All @@ -30,13 +33,10 @@ def recursive_serialize_or_str(obj):
# return f"{obj}" this generates '<generator object BaseChatModel.stream at 0x33e9ec770>'
# it is not useful
return "Unconsumed Stream"
if hasattr(obj, "dict"):
if hasattr(obj, "dict") and not isinstance(obj, type):
return {k: recursive_serialize_or_str(v) for k, v in obj.dict().items()}
if hasattr(obj, "model_dump"):
if hasattr(obj, "model_dump") and not isinstance(obj, type):
return {k: recursive_serialize_or_str(v) for k, v in obj.model_dump().items()}
if isinstance(obj, type) and issubclass(obj, BaseModel):
# This a type BaseModel and not an instance of it
return repr(obj)
return str(obj)
except Exception: # noqa: BLE001
logger.debug(f"Cannot serialize object {obj}")
Expand Down

0 comments on commit 69a1c16

Please sign in to comment.