Skip to content

Commit 08a1193

Browse files
authored
Add code samples for the "streaming API responses" page (#699)
Merging code sample only PR
1 parent 02c8c77 commit 08a1193

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// SAMPLE: Generate streaming responses through Responses API
2+
// PAGE: https://platform.openai.com/docs/guides/streaming-responses#enable-streaming
3+
// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start
4+
#pragma warning disable OPENAI001
5+
6+
#:package System.Linq.Async@6.*
7+
#:package OpenAI@2.*
8+
#:property PublishAot=false
9+
10+
using OpenAI.Responses;
11+
12+
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
13+
OpenAIResponseClient client = new(model: "gpt-5", apiKey: key);
14+
15+
var responses = client.CreateResponseStreamingAsync([
16+
ResponseItem.CreateUserMessageItem([
17+
ResponseContentPart.CreateInputTextPart(
18+
"Say 'double bubble bath' ten times fast."
19+
)
20+
])
21+
]);
22+
23+
await foreach (var response in responses)
24+
{
25+
if (response is StreamingResponseOutputTextDeltaUpdate delta)
26+
{
27+
Console.Write(delta.Delta);
28+
}
29+
}

0 commit comments

Comments
 (0)