Overview
Data parts allow tools to stream arbitrary data to the client during execution:- Progress updates: Show percentage complete, current step, estimated time remaining
- Intermediate results: Display results as they’re found (e.g., search results)
- Status changes: Notify users when entering different phases of execution
- Rich media: Stream images, files, or other content incrementally
Basic Example
Let’s extend a flight search tool to emit progress updates:Define Your Data Part Type
First, define a TypeScript type for your custom data part:types/chat.ts
type field must be a string starting with data- followed by your custom identifier. The id field should match the toolCallId so the client can associate the data with the correct tool invocation.Emit Updates from Your Tool
UsegetWritable() inside a step function to write to the stream:workflows/chat/steps/tools.ts
- Call
getWritable<UIMessageChunk>()to get the stream - Use
getWriter()to acquire a writer - Write objects with
type,id, anddatafields - Always call
releaseLock()when done (use try/finally)
Handle Data Parts in the Client
Update your chat component to detect and render the custom data parts:app/page.tsx
Advanced Patterns
Progress Indicators
Show percentage-based progress for long-running operations:workflows/chat/steps/tools.ts
app/page.tsx
Multi-Stage Progress
Track progress through multiple stages:workflows/chat/steps/tools.ts
Streaming Rich Content
Stream images or files as they’re generated:workflows/chat/steps/tools.ts
Error States
Stream error information without throwing:workflows/chat/steps/tools.ts
Reusable Stream Helpers
Create reusable helpers for common streaming patterns:workflows/chat/utils/stream-helpers.ts
workflows/chat/steps/tools.ts
Best Practices
Always Release Locks
Use try/finally to ensure locks are released:lineNumbers
Batch Updates
Don’t stream too frequently - batch updates for better performance:lineNumbers
Type Safety
Define types for your data parts:lineNumbers
Related Documentation
- Building Durable AI Agents - Complete guide to durable agents
getWritable()API Reference - Stream API details- Streaming - Understanding workflow streams
- Defining Tools - Tool implementation patterns