Skip to content

Commit

Permalink
fix: enhance recursive_serialize_or_str to handle BaseModelV1 and imp…
Browse files Browse the repository at this point in the history
…rove exception logging (#4132)

Enhance `recursive_serialize_or_str` to handle `BaseModelV1` and improve exception logging
  • Loading branch information
ogabrielluiz authored Oct 14, 2024
1 parent e2e3369 commit 2b3e826
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/backend/base/langflow/schema/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from loguru import logger
from pydantic import BaseModel
from pydantic.v1 import BaseModel as BaseModelV1
from typing_extensions import TypedDict

from langflow.schema import Data
Expand Down Expand Up @@ -116,11 +117,13 @@ def build_output_logs(vertex, result) -> dict:

def recursive_serialize_or_str(obj):
try:
if isinstance(obj, str):
return obj
if isinstance(obj, dict):
return {k: recursive_serialize_or_str(v) for k, v in obj.items()}
if isinstance(obj, list):
return [recursive_serialize_or_str(v) for v in obj]
if isinstance(obj, BaseModel):
if isinstance(obj, BaseModel | BaseModelV1):
if hasattr(obj, "model_dump"):
obj_dict = obj.model_dump()
elif hasattr(obj, "dict"):
Expand All @@ -138,10 +141,10 @@ def recursive_serialize_or_str(obj):
return {k: recursive_serialize_or_str(v) for k, v in obj.dict().items()}
if hasattr(obj, "model_dump"):
return {k: recursive_serialize_or_str(v) for k, v in obj.model_dump().items()}
if issubclass(obj, BaseModel):
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.opt(exception=True).debug(f"Cannot serialize object {obj}")
logger.debug(f"Cannot serialize object {obj}")
return str(obj)

0 comments on commit 2b3e826

Please # to comment.