Skip to content

Commit 9d7bef3

Browse files
committed
Add image generation code sample
1 parent 3094238 commit 9d7bef3

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// SAMPLE: Generate images with Responses API
2+
// PAGE: https://platform.openai.com/docs/guides/images-vision?api-mode=responses#generate-or-edit-images
3+
// GUIDANCE: Instructions to run this code: https://aka.ms/oai/net/start
4+
#pragma warning disable OPENAI001
5+
6+
#:package OpenAI@2.*
7+
#:property PublishAot=false
8+
9+
using System.ClientModel;
10+
using System.Text.Json;
11+
using OpenAI.Responses;
12+
13+
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
14+
OpenAIResponseClient client = new(model: "gpt-image-1", apiKey: key);
15+
16+
OpenAIResponse response = (OpenAIResponse)client.CreateResponse(
17+
BinaryContent.Create(BinaryData.FromObjectAsJson(new
18+
{
19+
model = "gpt-4.1",
20+
input = "Generate an image of gray tabby cat hugging an otter with an orange scarf",
21+
tools = new[]
22+
{
23+
new
24+
{
25+
type = "image_generation"
26+
}
27+
}
28+
}
29+
))
30+
);
31+
32+
var serialised = JsonSerializer.Serialize(response.OutputItems);
33+
34+
Console.WriteLine(serialised);

0 commit comments

Comments
 (0)