AI Hints
Home›LangGraph Tutorial›Custom and Mixed Streams
LangGraph · Lesson 52 of 120

Custom Stream Data

Progress messages.

⏱ 2 min read▶ Video at 54:58🎓 Free
Watch this lesson in the LangGraph courseWatch on YouTube ↗Full LangGraph course

Sometimes one node does a lot of work, like checking twenty books. You want to tell the customer how it's going, from inside the node. The stream writer sends any data you like, to the stream.

Custom Stream Data
From the video: Custom Stream Data
  • One node, lots of work
  • Report progress
  • get_stream_writer

Progress from a node

Get stream writer gives the node a writer function. Inside the loop, the node writes a small progress dictionary for every book. We stream with mode custom, to receive only those. Run it. A progress line arrives for each book, while the node is still working.

custom_stream.pypython
from typing import TypedDict
from langgraph.config import get_stream_writer
from langgraph.graph import StateGraph, START

STOCK = {"Dune": 3, "Circe": 0, "Emma": 5}

class State(TypedDict):
    report: dict

def check_all(s):
    write = get_stream_writer()
    for i, title in enumerate(STOCK, 1):
        write({"progress": f"checked {title} ({i}/{len(STOCK)})"})
    return {"report": dict(STOCK)}

b = StateGraph(State); b.add_node(check_all); b.add_edge(START, "check_all")
for data in b.compile().stream({}, stream_mode="custom"):
    print(data["progress"])
Output
checked Dune (1/3)
checked Circe (2/3)
checked Emma (3/3)

Ideas for custom data

Custom data is anything you choose. Progress counts. Status messages, like searching policies. Partial results, like each book found. Or timing information for debugging.

keyvalue
Progress3 of 20
Statussearching...
Partial resultsas found
Timingfor debugging

Summary

Get stream writer lets a node send its own data to the stream. Receive it with stream mode custom. Next, several stream modes at once.

Key takeaways
  • get_stream_writer()
  • write({...})
  • stream_mode='custom'
Next up: Several Stream Modes — at once