Skip to content

Commit e062193

Browse files
HavenDVclaude
andcommitted
feat: Add AOT-compatible structured outputs example
Adds a new example using the JsonTypeInfo<T> overload of CreateChatCompletionAsAsync for AOT/trimming compatibility. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent bcfe261 commit e062193

3 files changed

Lines changed: 53 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Structured Outputs (AOT)
2+
3+
Get structured JSON responses using a JsonTypeInfo for AOT/trimming compatibility.
4+
5+
This example assumes `using tryAGI.OpenAI;` is in scope and `apiKey` contains your tryAGI.OpenAI API key.
6+
7+
```csharp
8+
using var client = new OpenAiClient(apiKey);
9+
10+
var response = await client.Chat.CreateChatCompletionAsAsync(
11+
jsonTypeInfo: SourceGeneratedContext.Default.WordsResponse,
12+
messages: ["Generate five random words."],
13+
model: "gpt-4o-mini");
14+
15+
Console.WriteLine("Words:");
16+
foreach (var word in response.Value1!.Words)
17+
{
18+
Console.WriteLine(word);
19+
}
20+
```

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ nav:
1010
- Chat With Vision: examples/chat-with-vision.md
1111
- JSON Response Format: examples/json-response-format.md
1212
- Structured Outputs: examples/structured-outputs.md
13+
- Structured Outputs (AOT): examples/structured-outputs-aot.md
1314
- Embeddings: examples/embeddings.md
1415
- Image Generation: examples/image-generation.md
1516
- Text To Speech: examples/text-to-speech.md
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
order: 46
3+
title: Structured Outputs (AOT)
4+
slug: structured-outputs-aot
5+
6+
Get structured JSON responses using a JsonTypeInfo for AOT/trimming compatibility.
7+
*/
8+
9+
using System.Text.Json.Serialization;
10+
using System.Text.Json.Serialization.Metadata;
11+
12+
namespace tryAGI.OpenAI.IntegrationTests;
13+
14+
public partial class Tests
15+
{
16+
[TestMethod]
17+
public async Task Example_StructuredOutputsAot()
18+
{
19+
using var client = GetAuthenticatedClient();
20+
21+
var response = await client.Chat.CreateChatCompletionAsAsync(
22+
jsonTypeInfo: SourceGeneratedContext.Default.WordsResponse,
23+
messages: ["Generate five random words."],
24+
model: "gpt-4o-mini");
25+
26+
Console.WriteLine("Words:");
27+
foreach (var word in response.Value1!.Words)
28+
{
29+
Console.WriteLine(word);
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)