langserve icon indicating copy to clipboard operation
langserve copied to clipboard

Streaming events from a tool-calling agent with /serve_events fails on_tool_end

Open g-pavlov opened this issue 8 months ago • 0 comments

Summary: Streaming events from a tool-calling agent with /serve_events fails when yielding the on_tool_end event.

Symptoms: (essentials only)

File "/Users/<user>/Library/Caches/pypoetry/virtualenvs/ai-gateway-B2nAK5wE-py3.12/lib/python3.12/site-packages/langserve/serialization.py", line 108, in default
    return super().default(obj)
           ^^^^^^^
RuntimeError: super(): __class__ cell not found

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: Type is not JSON serializable: AsyncCallbackManager

Environment:

  • Mac/Darwin
  • Poetry shell
  • python 3.12
  • langserve: 0.3.1
  • langchain: 0.3.20
  • Model: claude sonnet 3.5

Details: During /serve_events data chunk building, specifically on_tool_end event, the last step is to serialize the object to string using json serialzier. This fails because the object contains a non-serializable field value callback=AsyncCallbackManager. The error is yielded at: https://github.com/langchain-ai/langserve/blob/main/langserve/serialization.py#L194.

Workarounds: The temporary fix is to monkeypatch langserve's serializer early on and skip CallbackManager or AsyncCallbackManager object from the serialization.

Example: In app/server.py:

# monkey patching langserve json serialization to fix bug
# with not-serializable callback field values (AsyncCallbackManager)
# Apply BEFORE importing any langserve package
def safe_default(obj):
    # Strip known non-serializable types
    typename = type(obj).__name__
    if "CallbackManager" in typename or "AsyncCallbackManager" in typename:
        return str(obj)  # or return None
    try:
        return obj.__dict__
    except Exception:
        return str(obj)
import langserve.serialization
langserve.serialization.default = safe_default

from langserve import add_routes

g-pavlov avatar Mar 24 '25 09:03 g-pavlov