Add serialize_field function to handle serialization of BaseModel and Document types
This commit is contained in:
parent
073e4b7ccf
commit
acfb9b1061
1 changed files with 17 additions and 0 deletions
|
|
@ -1,5 +1,8 @@
|
||||||
from typing import Any, Union
|
from typing import Any, Union
|
||||||
|
|
||||||
|
from langchain_core.documents import Document
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
from langflow.interface.utils import extract_input_variables_from_prompt
|
from langflow.interface.utils import extract_input_variables_from_prompt
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -33,3 +36,17 @@ def flatten_list(list_of_lists: list[Union[list, Any]]) -> list:
|
||||||
else:
|
else:
|
||||||
new_list.append(item)
|
new_list.append(item)
|
||||||
return new_list
|
return new_list
|
||||||
|
|
||||||
|
|
||||||
|
def serialize_field(value):
|
||||||
|
"""Unified serialization function for handling both BaseModel and Document types,
|
||||||
|
including handling lists of these types."""
|
||||||
|
if isinstance(value, (list, tuple)):
|
||||||
|
return [serialize_field(v) for v in value]
|
||||||
|
elif isinstance(value, Document):
|
||||||
|
return value.to_json()
|
||||||
|
elif isinstance(value, BaseModel):
|
||||||
|
return value.model_dump()
|
||||||
|
elif isinstance(value, str):
|
||||||
|
return {"result": value}
|
||||||
|
return value
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue