LangChain provides a few different ways of getting updates from chains/graphs that are streaming:
stream
with various modes Stream outputs
stream_events
How to stream runnables | 🦜️🔗 LangChain
callbacks
can’t link due to link limit
Is there canonical guidance on when to use which type of processing?
It would be great to just have a 3-column table that described when each should be used. They all seem to have mostly the same exposed hooks, but I wonder if there are any features/tradeoffs unique to one or more of these approaches (e.x exposed metadata, better filtering, latency, processing order, language-specific considerations, etc)
Here’s the canonical breakdown:
stream()
- Use for final outputs only. Best for simple chains where you only need the end result. Lowest latency, minimal overhead. Limited to final chain outputs.
stream_events()
- Use when you need granular control over intermediate steps, tokens, or specific component outputs. Provides rich metadata, filtering capabilities, and access to internal chain state. Higher overhead but most flexible.
callbacks
- Use for logging, monitoring, or side effects (analytics, debugging). Runs alongside execution without affecting the main flow. Best for observability and integration with external systems.
Choose stream()
for simple output streaming, stream_events()
for detailed intermediate access, and callbacks
for monitoring/logging purposes.
1 Like
Thanks Abdul, this is very helpful.
Follow up- so if we’re currently using stream()
with a few different stream modes [updates, messages, custom]
, should we instead switch over to using stream_events
?
Our reason for considering this is that processing all of those modes’ output manually from stream has become a messy task and stream_events might give more structure to it. Seems like that intuition might be right, and stream() should be reserved for simple final output cases (mode=None)
Hey Edward,
Like Abdul mentioned, stream_events()
has the most rich, granular output. If the concern here is processing, any processing that you are currently doing with the stream()
modes, you will also likely have to do the same processing with stream_events()
I wouldn’t say that using stream_events() is a better pattern, I think both streaming multiple modes or using stream_events() are both viable approaches.
2 Likes