Principle:Langchain ai Langchain Streaming Method Invocation
| Knowledge Sources | |
|---|---|
| Domains | Streaming, NLP |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
The public API for streaming chat model responses as an iterator of incremental message chunks.
Description
Streaming method invocation provides the user-facing stream() and astream() methods that return iterators of AIMessageChunk objects. Each chunk contains a portion of the model's response (a few tokens). The methods handle callback setup, delegate to the provider's _stream() or _astream() implementation, and manage error handling and cleanup.
Usage
Use stream() for synchronous streaming and astream() for async streaming. Iterate over the result to process chunks incrementally.
Theoretical Basis
# Abstract streaming pattern (not real code)
for chunk in model.stream(input):
display(chunk.content) # Progressive display
accumulated += chunk # Accumulate if needed