Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: enhance recursive_serialize_or_str to handle BaseModelV1 and improve exception logging #4132

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading