Skip to content

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

@g-pavlov

Description

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions