Pipeline Streaming
Stream data through AI pipelines in real-time for responsive applications.
Stream data through pipelines in real-time for responsive applications. Streaming provides immediate feedback as AI generates responses.
🚀 Basic Streaming
🔄 Streaming Flow
Stream Through Pipeline
pipeline = aiMessage()
.user( "Tell me a story" )
.toDefaultModel()
pipeline.stream( ( chunk ) => {
content = chunk.choices?.first()?.delta?.content ?: ""
print( content )
} )With Bindings
pipeline = aiMessage()
.system( "You are ${style}" )
.user( "Write about ${topic}" )
.toDefaultModel()
// stream( onChunk, input, params, options )
pipeline.stream(
( chunk ) => print( chunk.choices?.first()?.delta?.content ?: "" ),
{ style: "poetic", topic: "nature" } // input bindings
)With Options
Options in Streaming
Streamers accept the same options parameter as run() methods:
Default Options
Runtime Options Override
Note: Return format options don't apply to streaming - chunks are always in provider's streaming format.
Message Streaming
Messages can stream their content:
Collecting Stream Data
Full Response Collection
Structured Collection
Streaming Patterns
Progress Indicator
Real-Time Display
Chunk Processing
Web Streaming
Server-Sent Events (SSE)
WebSocket Streaming
JSON Streaming
Advanced Streaming
Stream with Transforms
Note: Transforms run after streaming completes, not per-chunk.
Conditional Streaming
Stream with Timeout
Practical Examples
Interactive Chat
Markdown Renderer
Progress Tracker
Stream Multiplexer
Error Handling
Stream Error Handling
Graceful Degradation
Best Practices
Flush Output: Call
flush()in web contextsHandle Errors: Streaming can fail mid-response
Track State: Monitor stream progress
Set Timeouts: Prevent infinite streams
Buffer Appropriately: Balance responsiveness and performance
Test Disconnects: Handle client disconnections
Provide Feedback: Show progress indicators
Performance Tips
Minimize Processing: Keep chunk callbacks fast
Buffer When Needed: Don't flush every character
Use Appropriate Models: Some stream better than others
Monitor Memory: Long streams can accumulate
Close Connections: Clean up resources
Next Steps
Pipeline Overview - Complete pipeline guide
Working with Models - Model configuration
Message Templates - Dynamic prompts
Transformers - Data transformation
Last updated