Open gcalabria opened 1 month ago
Is there any way to solve this without changing the code in python packages? For example, changing something from our side insted of changing in packages.
Yes serialization issues should be solvable by suffixing a runnable generator or runnable lambda to dump the object into json.
At the moment, langserve is not considered to be compatible with langgraph -- https://github.com/langchain-ai/langserve?tab=readme-ov-file#%EF%B8%8F-langgraph-compatibility
The APIHandler itself has a serializer property that isn't exposed, but one solution would be to expose it to allow users to pass custom hooks for serde.
A workaround which works is to apply monkey patch to the default function, very much like your PR @gcalabria:
from typing import Any
import langserve.serialization
from langgraph.constants import Send
from pydantic.v1 import BaseModel
def custom_default(obj) -> Any:
"""Custom serialization for well known objects."""
if isinstance(obj, BaseModel):
return obj.dict()
if isinstance(obj, Send):
return {"node": obj.node, "arg": obj.arg}
# Any class which has a `__dict__()` method automatically becomes json serializable.
try:
return obj.__dict__()
except:
pass
raise TypeError(f"Object of type {obj.__class__.__name__} is not JSON serializable")
# Monkey patch the default function
langserve.serialization.default = custom_default
Error:
TypeError: Type is not JSON serializable: Send
I am trying to replicate the langgraph map-reduce example using langserve and langgraph, but then I get the error
TypeError: Type is not JSON serializable: Send
.I only get this error when using langserve. If I run the code in the notebook, then everything is fine. Thus, I think that the problem lies with langserve and not langgraph.
Here is the code I am using:
graph.py
server.py
Here is the error message: