File tree Expand file tree Collapse file tree
src/tests/OpenAI.IntegrationTests/Examples Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ ```
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments