Repository Public
1# Streaming API responses23> For the complete documentation index, see [llms.txt](/llms.txt). Markdown versions of documentation pages are available by appending `.md` to the page URL.45By default, when you make a request to the OpenAI API, we generate the model's entire output before sending it back in a single HTTP response. When generating long outputs, waiting for a response can take time. Streaming responses lets you start printing or processing the beginning of the model's output while it continues generating the full response.67This guide focuses on HTTP streaming (`stream=true`) over server-sent events (SSE). For persistent WebSocket transport with incremental inputs via `previous_response_id`, see [the Responses API WebSocket mode](https://developers.openai.com/api/docs/guides/websocket-mode).89## Enable streaming101112To start streaming responses, set `stream=True` in your request to the Responses endpoint:1314```javascript15import { OpenAI } from "openai";16const client = new OpenAI();1718const stream = await client.responses.create({19 model: "gpt-6-astra",20 input: [21 {22 role: "user",23 content: "Say 'double bubble bath' ten times fast.",24 },25 ],26 stream: true,27});2829for await (const event of stream) {30 console.log(event);31}32```3334```python35from openai import OpenAI3637client = OpenAI()3839stream = client.responses.create(40 model="gpt-6-astra",41 input=[42 {43 "role": "user",44 "content": "Say 'double bubble bath' ten times fast.",45 },46 ],47 stream=True,48)4950for event in stream:51 print(event)52```5354```go55package main5657import (58 "context"59 "fmt"6061 "github.com/openai/openai-go/v3"62 "github.com/openai/openai-go/v3/responses"63)6465func main() {66 client := openai.NewClient()67 stream := client.Responses.NewStreaming(context.Background(), responses.ResponseNewParams{68 Model: "gpt-6-astra",69 Input: responses.ResponseNewParamsInputUnion{OfString: openai.String("Say 'double bubble bath' ten times fast.")},70 })71 for stream.Next() {72 fmt.Println(stream.Current().Type)73 }74 if err := stream.Err(); err != nil {75 panic(err)76 }77}78```7980```java81import com.openai.client.OpenAIClient;82import com.openai.client.okhttp.OpenAIOkHttpClient;83import com.openai.core.http.StreamResponse;84import com.openai.models.responses.ResponseCreateParams;85import com.openai.models.responses.ResponseStreamEvent;8687ResponseCreateParams params =88 ResponseCreateParams.builder()89 .model("gpt-6-astra")90 .input("Say 'double bubble bath' ten times fast.")91 .build();9293try (StreamResponse<ResponseStreamEvent> stream = client.responses().createStreaming(params)) {94 stream.stream().forEach(System.out::println);95}96```9798```csharp99using OpenAI.Responses;100#pragma warning disable OPENAI001101102string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;103ResponsesClient client = new(key);104105var responses = client.CreateResponseStreamingAsync(106 "gpt-6-astra",107 "Say 'double bubble bath' ten times fast."108);109110await foreach (StreamingResponseUpdate response in responses)111{112 if (response is StreamingResponseOutputTextDeltaUpdate delta)113 {114 Console.Write(delta.Delta);115 }116}117```118119```ruby120require "openai"121122openai = OpenAI::Client.new123124stream = openai.responses.stream(125 model: "gpt-6-astra",126 input: [127 {128 role: "user",129 content: "Say 'double bubble bath' ten times fast."130 }131 ]132)133134stream.each do |event|135 puts(event)136end137```138139140The Responses API uses semantic events for streaming. Each event is typed with a predefined schema, so you can listen for events you care about.141142For a full list of event types, see the [API reference for streaming](https://developers.openai.com/api/reference/resources/responses). Here are a few examples:143144```javascript145for await (const event of stream) {146 if (event.type === "response.output_text.delta") {147 process.stdout.write(event.delta);148 } else if (event.type === "response.completed") {149 console.log("\nResponse completed.");150 } else if (event.type === "error") {151 console.error(event.message);152 }153}154```155156```python157StreamingEvent = (158 ResponseCreatedEvent159 | ResponseInProgressEvent160 | ResponseFailedEvent161 | ResponseCompletedEvent162 | ResponseOutputItemAdded163 | ResponseOutputItemDone164 | ResponseContentPartAdded165 | ResponseContentPartDone166 | ResponseOutputTextDelta167 | ResponseOutputTextAnnotationAdded168 | ResponseTextDone169 | ResponseRefusalDelta170 | ResponseRefusalDone171 | ResponseFunctionCallArgumentsDelta172 | ResponseFunctionCallArgumentsDone173 | ResponseFileSearchCallInProgress174 | ResponseFileSearchCallSearching175 | ResponseFileSearchCallCompleted176 | ResponseCodeInterpreterInProgress177 | ResponseCodeInterpreterCallCodeDelta178 | ResponseCodeInterpreterCallCodeDone179 | ResponseCodeInterpreterCallInterpreting180 | ResponseCodeInterpreterCallCompleted181 | Error182)183```184185```go186type StreamingEvent = responses.ResponseStreamEventUnion187```188189```java190import com.openai.client.OpenAIClient;191import com.openai.client.okhttp.OpenAIOkHttpClient;192import com.openai.core.http.StreamResponse;193import com.openai.models.responses.ResponseCreateParams;194import com.openai.models.responses.ResponseStreamEvent;195196ResponseCreateParams params =197 ResponseCreateParams.builder().model("gpt-5.5").input("Say hello.").build();198199try (StreamResponse<ResponseStreamEvent> stream = client.responses().createStreaming(params)) {200 stream.stream().forEach(System.out::println);201}202```203204```ruby205require "openai"206207client = OpenAI::Client.new208stream = client.responses.stream(model: "gpt-5.5", input: "Say hello.")209stream.each { |event| puts(event) }210```211212213214215216217## Read the responses218219220221If you're using our SDK, every event is a typed instance. You can also identity individual events using the `type` property of the event.222223Some key lifecycle events are emitted only once, while others are emitted multiple times as the response is generated. Common events to listen for when streaming text are:224225```226- `response.created`227- `response.output_text.delta`228- `response.completed`229- `error`230```231232For a full list of events you can listen for, see the [API reference for streaming](https://developers.openai.com/api/reference/resources/responses).233234235236237238## Advanced use cases239240For more advanced use cases, like streaming tool calls, check out the following dedicated guides:241242- [Streaming function calls](https://developers.openai.com/api/docs/guides/function-calling#streaming)243- [Streaming structured output](https://developers.openai.com/api/docs/guides/structured-outputs#streaming)244245## Moderation risk246247Note that streaming the model's output in a production application makes it more difficult to moderate the content of the completions, as partial completions may be more difficult to evaluate. This may have implications for approved usage.248249If you request [moderation scores with a generation request](https://developers.openai.com/api/docs/guides/moderation#moderate-generated-content), the scores arrive after the full generated output is available. They aren't included with partial output deltas.