How to Intermediate · 3 min read

How to stream LangGraph output

Quick answer
To stream LangGraph output in LangChain, enable the streaming parameter on the chat model and iterate over the streamed tokens or events as they arrive. Use a chat model like ChatOpenAI with streaming=True and process the output in real time to build or display the graph progressively.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install langchain_openai langchain_community

Setup

Install the required packages and set your OpenAI API key as an environment variable.

  • Install LangChain OpenAI and community packages:
bash
pip install langchain_openai langchain_community

Step by step

This example demonstrates streaming LangGraph output using LangChain with ChatOpenAI. It initializes the chat model with streaming=True and iterates over the streamed tokens to print the graph output progressively.

python
import os
from langchain_openai import ChatOpenAI
from langchain_community.langgraph import LangGraph

# Initialize the chat model with streaming enabled
chat = ChatOpenAI(model_name="gpt-4o", streaming=True, temperature=0, openai_api_key=os.environ["OPENAI_API_KEY"])

# Create LangGraph instance with the streaming chat model
langgraph = LangGraph(chat=chat)

# Define a prompt to generate a graph
prompt = "Generate a LangGraph JSON for a simple workflow with nodes A, B, and C connected sequentially."

# Stream the LangGraph output
for chunk in langgraph.stream(prompt):
    print(chunk, end='', flush=True)

# The output will be streamed JSON chunks representing the graph structure.
output
{
  "nodes": [
    {"id": "A", "label": "Start"},
    {"id": "B", "label": "Process"},
    {"id": "C", "label": "End"}
  ],
  "edges": [
    {"from": "A", "to": "B"},
    {"from": "B", "to": "C"}
  ]
}

Common variations

You can customize streaming behavior and models:

  • Use async streaming with async for loops for asynchronous applications.
  • Switch to other chat models like gpt-4o-mini or claude-3-5-haiku-20241022 by changing the model name.
  • Adjust temperature and max tokens for different output styles and lengths.
python
import asyncio

async def async_stream_langgraph():
    chat = ChatOpenAI(model_name="gpt-4o", streaming=True, temperature=0, openai_api_key=os.environ["OPENAI_API_KEY"])
    langgraph = LangGraph(chat=chat)
    prompt = "Generate a LangGraph JSON for a workflow with nodes X, Y, Z connected in a cycle."

    async for chunk in langgraph.stream(prompt):
        print(chunk, end='', flush=True)

# Run async example
asyncio.run(async_stream_langgraph())
output
{
  "nodes": [
    {"id": "X", "label": "Node X"},
    {"id": "Y", "label": "Node Y"},
    {"id": "Z", "label": "Node Z"}
  ],
  "edges": [
    {"from": "X", "to": "Y"},
    {"from": "Y", "to": "Z"},
    {"from": "Z", "to": "X"}
  ]
}

Troubleshooting

If streaming output does not appear:

  • Ensure streaming=True is set on the ChatOpenAI instance.
  • Verify your OPENAI_API_KEY environment variable is correctly set.
  • Check network connectivity and API usage limits.
  • Use synchronous calls first to confirm the model and prompt work before enabling streaming.

Key Takeaways

  • Enable streaming by setting streaming=True on the ChatOpenAI model.
  • Use LangGraph.stream() to receive graph output incrementally as tokens arrive.
  • Async streaming allows integration into event-driven or UI applications for real-time updates.
Verified 2026-04 · gpt-4o, gpt-4o-mini, claude-3-5-haiku-20241022
Verify ↗