- Notifications
You must be signed in to change notification settings - Fork1
Experiment using JsonSchemaExporter in .NET 9 to improve developer experience with OpenAI's Structured Outputs
License
jamesmcroft/openai-structured-outputs-with-dotnet
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
The goal of this sample is to demonstrate how to leverage the Azure OpenAI GPT-4o structured outputs feature by generating valid OpenAI JSON schemas for a given Type in .NET.
Important
The original vision was to leverage theJsonSchemaExporter in .NET 9 to generate JSON schemas. However, the generated schemas are not directly compatible with the OpenAI SDK. The current implementation is a workaround to generate valid schemas for structured outputs.
TheOpenAIJsonSchema class provides a mechanism to generate a valid OpenAI JSON schema for any given model Type. The class uses reflection to traverse the properties of the model Type and generate the schema based on the property types.
TheStructuredOutputsExtensions class provides a genericCreateJsonSchemaFormat method that can be passed a model object type and will return theChatResponseFormat object that can be used by the OpenAI SDK. This method uses theOpenAIJsonSchema.For method to generate the schema for the model object type.
In addition to the genericCreateJsonSchemaFormat method, a genericCompleteChat andCompleteChatAsync extension is provided for theChatClient that will return a wrappedChatCompletion object asParsedChatCompletion that includes aParsed property containing the deserialized model object.
classFamily{publicList<Person>Parents{get;set;}publicList<Person>?Children{get;set;}classPerson{publicstringName{get;set;}publicintAge{get;set;}}}ChatCompletionOptionsoptions=new(){ResponseFormat=StructuredOutputsExtensions.CreateJsonSchemaFormat<Family>("family",jsonSchemaIsStrict:true),MaxOutputTokenCount=4096,Temperature=0.1f,TopP=0.1f};List<ChatMessage>messages=[newSystemChatMessage("You are an AI assistant that creates families."),newUserChatMessage("Create a family with 2 parents and 2 children.")];ParsedChatCompletion<Family?>completion=chatClient.CompleteChat(messages,options);Family?family=completion.Parsed;
You can also provide descriptions for schema properties by using theDescriptionAttribute:
classPerson{[Description("The name of the person.")]publicstringName{get;set;}[Description("The age of the person.")]publicintAge{get;set;}}
These are included in the generated schema:
{"type":"object","properties": {"Name": {"type":"string","description":"The name of the person." },"Age": {"type":"integer","description":"The age of the person." } }}- Install the latest.NET 9 SDK.
- InstallPowerShell Core.
- Install theAzure CLI.
- InstallVisual Studio Code.
Additionally, you will require:
- An Azure subscription. If you don't have an Azure subscription, create anaccount.
To setup a local development environment, follow these steps:
Note
For the most optimal sample experience, it is recommended to deploy the necessary infrastructure in a region that supportsGPT-4o version2024-08-06. Find out more about region availability for theGPT-4o model.
az login./Setup-Environment.ps1-DeploymentName<UniqueDeploymentName>-Location<AzureRegion>-SkipInfrastructure$false
The script will deploy the following resources to your Azure subscription:
- Azure AI Services, a managed service for all Azure AI Services, including Azure OpenAI.
- Note: GPT-4o will be deployed as Global Standard with 10K TPM quota allocation. This can be adjusted based on your quota availability in themain.bicep file.
Note
Resources are secured by default with Microsoft Entra ID using Azure RBAC. Your user client ID will be added with the necessary least-privilege roles to access the resources created.
After the script completes, a.env file will be crated in thesrc/StructuredOutputs folder and you can run the sample project by following these steps:
cd src/StructuredOutputsdotnet run- Azure OpenAI GPT-4o Structured Outputs Supported Schemas and Limitations
- How to use chat completions with structured outputs in OpenAI .NET SDK
- .NET 9 JSON Schema Exporter
This project is licensed under the MIT License - see theLICENSE file for details.
About
Experiment using JsonSchemaExporter in .NET 9 to improve developer experience with OpenAI's Structured Outputs
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.