AIStream

AIStream has been removed in AI SDK 4.0. Use streamText.toDataStreamResponse() instead.

Creates a readable stream for AI responses. This is based on the responses returned by fetch and serves as the basis for the OpenAIStream and AnthropicStream. It allows you to handle AI response streams in a controlled and customized manner that will work with useChat and useCompletion.

AIStream will throw an error if response doesn't have a 2xx status code. This is to ensure that the stream is only created for successful responses.

Import

React

import { AIStream } from "ai"

API Signature

response:

Response

customParser:

(AIStreamParser) => void
AIStreamParser

(data: string) => string | void

callbacks:

AIStreamCallbacksAndOptions
AIStreamCallbacksAndOptions

onStart:

() => Promise<void>

onCompletion:

(completion: string) => Promise<void>

onFinal:

(completion: string) => Promise<void>

onToken:

(token: string) => Promise<void>