ai-stream-utils has 3,204 recorded weekly downloads on npm, an increase of 88.2% over the past 30 days, ranking #1709 of 3,247 tracked listings.
About ai-stream-utils
This library provides composable filter and transformation utilities for UI message streams created by streamText() in the AI SDK.
Why?
The AI SDK UI message stream created by toUIMessageStream() streams all parts (text, tools, reasoning, etc.) to the client by default. However, you may want to:
- Filter: Tool calls like database searches often contain large amounts of data or sensitive information that should not be streamed to the client
- Transform: Modify text or tool outputs while they are streamed to the client
- Observe: Log stream lifecycle events, update states, or run side-effects without modifying the stream
This library provides type-safe, composable utilities for all these use cases.
Installation
[!NOTE] Version compatibility:
- Use
ai-stream-utils@2.xfor AI SDK v6- Use
ai-stream-utils@3.xfor AI SDK v7
npm install ai-stream-utils@2 # AI SDK v6
npm install ai-stream-utils@3 # AI SDK v7
Usage
The pipe function provides a composable pipeline API for filtering, transforming, and observing UI message streams. Multiple operators can be chained together, and type guards automatically narrow chunk and part types, thus enabling type-safe stream transformations with autocomplete.
.filter()
Filter chunks by returning true to keep or false to exclude. The predicate may be async.
const stream = pipe(result.toUIMessageStream())
.filter(({ chunk, part }) => {
// chunk.type: "text-delta" | "text-start" | "tool-input-available" | ...
// part.type: "text" | "reasoning" | "tool-weather" | ...
if (chunk.type === "data-weather") {
return false; // exclude chunk
}
return true; // keep chunk
})
.toStream();
Type Guards
Generic type guards provide a simpler API for common filtering patterns:
includeChunks("text-delta")orincludeChunks(["text-delta", "text-end"]): Include only specific chunk typesexcludeChunks("text-delta")orexcludeChunks(["text-delta", "text-end"]): Exclude only specific chunk typesincludeParts("text")orincludeParts(["text", "reasoning"]): Include only specific part typesexcludeParts("reasoning")orexcludeParts(["reasoning", "tool-database"]): Exclude only specific part types
Filtering tools is the most common use case and the tool-filter type guards provide a convenient API for filtering tool chunks by tool name:
excludeTools()orexcludeTools("weather")orexcludeTools(["weather", "database"]): Exclude all tools or specific tools by nameincludeTools()orincludeTools("weather")orincludeTools(["weather", "database"]): Include all tools or specific tools by name
[!NOTE] The tool-filter type guards only affect tool chunks. Non-tool chunks will pass through.
Examples
Exclude tool calls from the client.
// Exclude by part type (requires "tool-" prefix)
const stream = pipe(result.toUIMessageStream())
.filter(excludeParts(["tool-weather", "tool-database"]))
.toStream();
// Exclude by tool name (without "tool-" prefix)
const stream = pipe(result.toUIMessageStream())
.filter(excludeTools(["weather", "database"]))
.toStream();
// Exclude all tools
const stream = pipe(result.toUIMessageStream()).filter(excludeTools()).toStream();
// Include only specific tools (without "tool-" prefix)
const stream = pipe(result.toUIMessageStream())
.filter(includeTools(["weather"]))
.toStream();
[!NOTE]
excludeTools()andincludeTools()filters tool chunks on the server before streaming to the client. This affects all tool types including:
- Server-side tools with
executefunctions- Client-side tools without
executefunctions- Tools that require human approval via
needsApprovalExcluded tools will not appear in the client's message parts, so users won't see tool call UI or be able to approve/reject filtered tools.
.map()
Transform chunks by returning a chunk, an array of chunks, or null to exclude. The callback may be async, which lets a chunk be rewritten from an awaited result.
const stream = pipe(result.toUIMessageStream())
.map(({ chunk, part }) => {
// chunk.type: "text-delta" | "text-start" | "tool-input-available" | ...
// part.type: "text" | "reasoning" | "tool-weather" | ...
if (chunk.type === "text-start") {
return chunk; // pass through unchanged
}
if (chunk.type === "text-delta") {
return { ...chunk, delta: "modified" }; // transform chunk
}
if (chunk.type === "data-weather") {
return [chunk1, chunk2]; // emit multiple chunks
}
return null; // exclude chunk (same as filter)
})
.toStream();
Examples
Convert text to uppercase.
const stream = pipe(result.toUIMessageStream())
.map(({ chunk }) => {
if (chunk.type === "text-delta") {
return { ...chunk, delta: chunk.delta.toUpperCase() };
}
return chunk;
})
.toStream();
Helpers…
Description from the npm listing.
- ai
- ai-sdk
- chunk
- filter
- map
- message
- sdk
- stream
- transform
- ui
- ui-message
- ui-message-chunk
- ui-message-stream
- utils
README badge
Show your rank in a README or on your website. The badge updates automatically.
Charts
Images: 30d · 1y · all time · 1600×900 dark
Adoption over time
7/30/90-day growth
Ranking history
Listing placement
Listing placement
Filled points are observed values; hollow points come from Internet Archive snapshots of the listing page. Dashed segments span periods with no observations — values between points are not interpolated.
Each band is one page of 20. Weekly downloads on a linear scale. Placement is projected from weekly downloads, so it can differ from the marketplace's own sort order. Hover a dot to see the listing.
Recent observations
Full history (170)Similar npm AI SDKs plugins
All 20 ai-stream-utils alternativesData source
Weekly downloads (trailing 7 complete days) come from the official npm downloads API. Downloads include CI and mirrors and are not equivalent to installs or users. npm publishes no ratings.
Listing: https://www.npmjs.com/package/ai-stream-utils. First seen Oct 16, 2026; last successful collection Dec 6, 2026. See the methodology.