Langgraph streaming conflicts with additional callbacks #1039

Closed
opened 2026-02-20 17:42:51 -05:00 by yindo · 2 comments
Owner

Originally created by @yasonk on GitHub (Nov 4, 2025).

Checked other resources

  • This is a bug, not a usage question. For questions, please use the LangChain Forum (https://forum.langchain.com/).
  • I added a clear and detailed title that summarizes the issue.
  • I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example).
  • I included a self-contained, minimal example that demonstrates the issue INCLUDING all the relevant imports. The code run AS IS to reproduce the issue.

Example Code

from dataclasses import dataclass

from langchain.chat_models import init_chat_model
from langchain_core.callbacks import UsageMetadataCallbackHandler
from langgraph.constants import START
from langgraph.graph import StateGraph

model_1 = init_chat_model(model="gpt-4o-mini")  # must have API_KEY in env or pass it in here

@dataclass
class MyState:
    topic: str

callback = UsageMetadataCallbackHandler()

model_with_callback = model_1.with_config(
    callbacks=[callback]
)

def call_model(state: MyState):
    """Call the LLM to generate a joke about a topic"""
    # Note that message events are emitted even when the LLM is run using .invoke rather than .stream
    model_response = model_with_callback.invoke(
        [
            {"role": "user", "content": f"{state.topic}"}
        ]
    )
    return {"topic": model_response.content}


graph = (
    StateGraph(MyState)
    .add_node(call_model)
    .add_edge(START, "call_model")
    .compile()
)

stream_gen = graph.stream(
    {"topic": "What is the capital of France?"},
    stream_mode="messages",
)
for message_chunk, metadata in stream_gen:
    if message_chunk.content:
        print(message_chunk.content, end="|", flush=True)
        print(callback.usage_metadata)

Error Message and Stack Trace (if applicable)

"messages" mode streaming does not work when passing other callbacks in.

Description

I'm trying to stream model output, but also count usage tokens via callback. But all the documented ways that I found for adding callbacks to count token usage end up removing the StreamMessagesHandler callback resulting in no streaming.

This seems to be a pretty important use case -- you want to stream output, but also you want to charge customers for tokens used.

Need to be able to add callbacks without removing existing callbacks.

System Info

System Information

OS: Darwin
OS Version: Darwin Kernel Version 24.6.0: Mon Jul 14 11:30:29 PDT 2025; root:xnu-11417.140.69~1/RELEASE_ARM64_T6000
Python Version: 3.12.4 (main, Jul 29 2024, 13:31:11) [Clang 15.0.0 (clang-1500.3.9.4)]

Package Information

langchain_core: 1.0.2
langchain: 1.0.3
langsmith: 0.4.32
langchain_mcp_adapters: 0.1.11
langchain_openai: 1.0.1
langchain_tavily: 0.2.12
langgraph_sdk: 0.2.9

Optional packages not installed

langserve

Other Dependencies

aiohttp: 3.12.15
httpx: 0.28.1
jsonpatch: 1.33
langchain-anthropic: Installed. No version info available.
langchain-aws: Installed. No version info available.
langchain-community: Installed. No version info available.
langchain-deepseek: Installed. No version info available.
langchain-fireworks: Installed. No version info available.
langchain-google-genai: Installed. No version info available.
langchain-google-vertexai: Installed. No version info available.
langchain-groq: Installed. No version info available.
langchain-huggingface: Installed. No version info available.
langchain-mistralai: Installed. No version info available.
langchain-ollama: Installed. No version info available.
langchain-perplexity: Installed. No version info available.
langchain-together: Installed. No version info available.
langchain-xai: Installed. No version info available.
langgraph: 1.0.2
langsmith-pyo3: Installed. No version info available.
mcp: 1.11.0
openai: 2.6.1
openai-agents: Installed. No version info available.
opentelemetry-api: 1.37.0
opentelemetry-exporter-otlp-proto-http: 1.37.0
opentelemetry-sdk: 1.37.0
orjson: 3.11.3
packaging: 24.2
pydantic: 2.11.3
pytest: 8.4.2
pyyaml: 6.0.3
requests: 2.32.5
requests-toolbelt: 1.0.0
rich: Installed. No version info available.
tenacity: 9.1.2
tiktoken: 0.11.0
typing-extensions: 4.15.0
vcrpy: Installed. No version info available.
zstandard: 0.25.0

Originally created by @yasonk on GitHub (Nov 4, 2025). ### Checked other resources - [x] This is a bug, not a usage question. For questions, please use the LangChain Forum (https://forum.langchain.com/). - [x] I added a clear and detailed title that summarizes the issue. - [x] I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example). - [x] I included a self-contained, minimal example that demonstrates the issue INCLUDING all the relevant imports. The code run AS IS to reproduce the issue. ### Example Code ```python from dataclasses import dataclass from langchain.chat_models import init_chat_model from langchain_core.callbacks import UsageMetadataCallbackHandler from langgraph.constants import START from langgraph.graph import StateGraph model_1 = init_chat_model(model="gpt-4o-mini") # must have API_KEY in env or pass it in here @dataclass class MyState: topic: str callback = UsageMetadataCallbackHandler() model_with_callback = model_1.with_config( callbacks=[callback] ) def call_model(state: MyState): """Call the LLM to generate a joke about a topic""" # Note that message events are emitted even when the LLM is run using .invoke rather than .stream model_response = model_with_callback.invoke( [ {"role": "user", "content": f"{state.topic}"} ] ) return {"topic": model_response.content} graph = ( StateGraph(MyState) .add_node(call_model) .add_edge(START, "call_model") .compile() ) stream_gen = graph.stream( {"topic": "What is the capital of France?"}, stream_mode="messages", ) for message_chunk, metadata in stream_gen: if message_chunk.content: print(message_chunk.content, end="|", flush=True) print(callback.usage_metadata) ``` ### Error Message and Stack Trace (if applicable) ```shell "messages" mode streaming does not work when passing other callbacks in. ``` ### Description I'm trying to stream model output, but also count usage tokens via callback. But all the documented ways that I found for adding callbacks to count token usage end up removing the `StreamMessagesHandler` callback resulting in no streaming. This seems to be a pretty important use case -- you want to stream output, but also you want to charge customers for tokens used. Need to be able to add callbacks without removing existing callbacks. ### System Info System Information ------------------ > OS: Darwin > OS Version: Darwin Kernel Version 24.6.0: Mon Jul 14 11:30:29 PDT 2025; root:xnu-11417.140.69~1/RELEASE_ARM64_T6000 > Python Version: 3.12.4 (main, Jul 29 2024, 13:31:11) [Clang 15.0.0 (clang-1500.3.9.4)] Package Information ------------------- > langchain_core: 1.0.2 > langchain: 1.0.3 > langsmith: 0.4.32 > langchain_mcp_adapters: 0.1.11 > langchain_openai: 1.0.1 > langchain_tavily: 0.2.12 > langgraph_sdk: 0.2.9 Optional packages not installed ------------------------------- > langserve Other Dependencies ------------------ > aiohttp: 3.12.15 > httpx: 0.28.1 > jsonpatch: 1.33 > langchain-anthropic: Installed. No version info available. > langchain-aws: Installed. No version info available. > langchain-community: Installed. No version info available. > langchain-deepseek: Installed. No version info available. > langchain-fireworks: Installed. No version info available. > langchain-google-genai: Installed. No version info available. > langchain-google-vertexai: Installed. No version info available. > langchain-groq: Installed. No version info available. > langchain-huggingface: Installed. No version info available. > langchain-mistralai: Installed. No version info available. > langchain-ollama: Installed. No version info available. > langchain-perplexity: Installed. No version info available. > langchain-together: Installed. No version info available. > langchain-xai: Installed. No version info available. > langgraph: 1.0.2 > langsmith-pyo3: Installed. No version info available. > mcp: 1.11.0 > openai: 2.6.1 > openai-agents: Installed. No version info available. > opentelemetry-api: 1.37.0 > opentelemetry-exporter-otlp-proto-http: 1.37.0 > opentelemetry-sdk: 1.37.0 > orjson: 3.11.3 > packaging: 24.2 > pydantic: 2.11.3 > pytest: 8.4.2 > pyyaml: 6.0.3 > requests: 2.32.5 > requests-toolbelt: 1.0.0 > rich: Installed. No version info available. > tenacity: 9.1.2 > tiktoken: 0.11.0 > typing-extensions: 4.15.0 > vcrpy: Installed. No version info available. > zstandard: 0.25.0
yindo added the bugpending labels 2026-02-20 17:42:51 -05:00
yindo closed this issue 2026-02-20 17:42:51 -05:00
Author
Owner

@yasonk commented on GitHub (Nov 4, 2025):

Looks like this is the right way to do it:

stream_gen = graph.stream(
    {"topic": "What is the capital of France?"},
    config = {"callbacks": [callback]},
    stream_mode="messages",
)
for message_chunk, metadata in stream_gen:
    if message_chunk.content:
        print(message_chunk.content, end="|", flush=True)
print(callback.usage_metadata)

But the documentation is missing this use case and I think that it should have it.
I'll create PR to update this doc:
https://github.com/langchain-ai/docs/blob/d80f89b41e9949ff915b11fca0307c411cc33e24/src/oss/langgraph/streaming.mdx?plain=1#L456

Does anything else need to be updated?

@yasonk commented on GitHub (Nov 4, 2025): Looks like this is the right way to do it: ```python stream_gen = graph.stream( {"topic": "What is the capital of France?"}, config = {"callbacks": [callback]}, stream_mode="messages", ) for message_chunk, metadata in stream_gen: if message_chunk.content: print(message_chunk.content, end="|", flush=True) print(callback.usage_metadata) ``` But the documentation is missing this use case and I think that it should have it. I'll create PR to update this doc: https://github.com/langchain-ai/docs/blob/d80f89b41e9949ff915b11fca0307c411cc33e24/src/oss/langgraph/streaming.mdx?plain=1#L456 Does anything else need to be updated?
Author
Owner

@casparb commented on GitHub (Nov 7, 2025):

Closing as self-resolved.

@casparb commented on GitHub (Nov 7, 2025): Closing as self-resolved.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#1039