Stream Helpers: OpenAIStream

ID: 1652https://ai-sdk.dev/docs/reference/stream-helpers/openai-stream
Source

OpenAIStream

Transforms the response from OpenAI's language models into a ReadableStream.

Note: Prior to v4, the official OpenAI API SDK does not support the Edge Runtime and only works in serverless environments. The openai-edge package is based on fetch instead of axios (and thus works in the Edge Runtime) so we recommend using openai v4+ or openai-edge.

Import

React

<Snippet text={import { OpenAIStream } from "ai"} prompt={false} />

API Signature

Parameters

<PropertiesTable content={[ { name: 'response', type: 'Response', description: 'The response object returned by a call made by the Provider SDK.', }, { name: 'callbacks', type: 'AIStreamCallbacksAndOptions', isOptional: true, description: 'An object containing callback functions to handle the start, each token, and completion of the AI response. In the absence of this parameter, default behavior is implemented.', properties: [ { type: 'AIStreamCallbacksAndOptions', parameters: [ { name: 'onStart', type: '() => Promise', description: 'An optional function that is called at the start of the stream processing.', }, { name: 'onCompletion', type: '(completion: string) => Promise', description: "An optional function that is called for every completion. It's passed the completion as a string.", }, { name: 'onFinal', type: '(completion: string) => Promise', description: 'An optional function that is called once when the stream is closed with the final completion message.', }, { name: 'onToken', type: '(token: string) => Promise', description: "An optional function that is called for each token in the stream. It's passed the token as a string.", }, ], }, ], }, ]} />