Overview
To stream a response from the API, set thestream
parameter to true
in the request body.
The following endpoints support streaming:
- Text generation
- Chat completions
- Generate from no-code agent: Currently, only no-code research agents support streaming.
- Knowledge Graph question
- Web search
Sample request and response
The code below shows a streaming request and response from a text generation request usingcurl
. The response is a stream of server-sent events.
response
If you’re using the chat completions endpoint instead of text generation, the streamed response format is slightly different. The content for each chunk appears in
choices[0].delta.content
. See the Generate chat completions guide for the full streaming response object.Streaming with SDKs
When you stream a response using a Writer SDK, the SDK creates an iterator that yields chunks of the response. You can iterate over the stream to receive the response. See below for examples of streaming with the Python and JavaScript SDKs for each endpoint that supports streaming.Streaming helpers for chat completions
The Python and Node SDKs include streaming helpers for chat completions. These helpers provide more granular details about the streaming events and accumulate the response. To use the streaming helpers, callclient.chat.stream
. Then, include all the same parameters as you would for a non-streaming chat completion request, except omit the stream
parameter.