---
title: Quickstart - Request a response with structured output
description: Learn how to create a chat app that responds with structured output, that is, output that conforms to a type that you specify.
ms.date: 03/04/2026
ms.topic: quickstart
ai-usage: ai-assisted
---

# Request a response with structured output

In this quickstart, you create a chat app that requests a response with *structured output*. A structured output response is a chat response that's of a type you specify instead of just plain text. The chat app you create in this quickstart analyzes sentiment of various product reviews, categorizing each review according to the values of a custom enumeration.

## Prerequisites

- [.NET 8 or a later version](https://dotnet.microsoft.com/download)
- [Visual Studio Code](https://code.visualstudio.com/) (optional)

## Configure the AI service

To provision an Azure OpenAI service and model using the Azure portal, complete the steps in the [Create and deploy an Azure OpenAI Service resource](/azure/ai-services/openai/how-to/create-resource?pivots=web-portal) article. In the "Deploy a model" step, select the `gpt-5` model.

## Create the chat app

Complete the following steps to create a console app that connects to the `gpt-5` AI model.

1. In a terminal window, navigate to the directory where you want to create your app, and create a new console app with the `dotnet new` command:

    ```dotnetcli
    dotnet new console -o SOChat
    ```

1. Navigate to the `SOChat` directory, and add the necessary packages to your app:

    ```dotnetcli
    dotnet add package Azure.AI.OpenAI
    dotnet add package Azure.Identity
    dotnet add package Microsoft.Extensions.AI
    dotnet add package Microsoft.Extensions.AI.OpenAI
    dotnet add package Microsoft.Extensions.Configuration
    dotnet add package Microsoft.Extensions.Configuration.UserSecrets
    ```

1. Run the following commands to add [app secrets](/aspnet/core/security/app-secrets) for your Azure OpenAI endpoint and tenant ID:

    ```bash
    dotnet user-secrets init
    dotnet user-secrets set AZURE_OPENAI_ENDPOINT <your-Azure-OpenAI-endpoint>
    dotnet user-secrets set AZURE_TENANT_ID <your-tenant-ID>
    ```

   > [!NOTE]
   > Depending on your environment, the tenant ID might not be needed. In that case, remove it from the code that instantiates the <xref:Azure.Identity.DefaultAzureCredential>.

1. Open the new app in your editor of choice.

## Add the code

1. Define the enumeration that describes the different sentiments.


1. Create the <xref:Microsoft.Extensions.AI.IChatClient> that will communicate with the model.


   > [!NOTE]
   > <xref:Azure.Identity.DefaultAzureCredential> searches for authentication credentials from your environment or local tooling. You'll need to assign the `Azure AI Developer` role to the account you used to sign in to Visual Studio or the Azure CLI. For more information, see [Authenticate to Foundry tools with .NET](../azure-ai-services-authentication.md).

1. Send a request to the model with a single product review, and then print the analyzed sentiment to the console. You declare the requested structured output type by passing it as the type argument to the <xref:Microsoft.Extensions.AI.ChatClientStructuredOutputExtensions.GetResponseAsync``1(Microsoft.Extensions.AI.IChatClient,System.String,Microsoft.Extensions.AI.ChatOptions,System.Nullable{System.Boolean},System.Threading.CancellationToken)?displayProperty=nameWithType> extension method.


   This code produces output similar to:

   ```output
   Sentiment: Positive
   ```

1. Instead of just analyzing a single review, you can analyze a collection of reviews.


   This code produces output similar to:

   ```output
   Review: Best purchase ever! | Sentiment: Positive
   Review: Returned it immediately. | Sentiment: Negative
   Review: Hello | Sentiment: Neutral
   Review: It works as advertised. | Sentiment: Neutral
   Review: The packaging was damaged but otherwise okay. | Sentiment: Neutral
   ```

1. And instead of requesting just the analyzed enumeration value, you can request the text response along with the analyzed value.

   Define a [record type](../../csharp/language-reference/builtin-types/record.md) to contain the text response and analyzed sentiment:


   (This record type is defined using [primary constructor](../../csharp/programming-guide/classes-and-structs/instance-constructors.md#primary-constructors) syntax. Primary constructors combine the type definition with the parameters necessary to instantiate any instance of the class. The C# compiler generates public properties for the primary constructor parameters.)

   Send the request using the record type as the type argument to `GetResponseAsync<T>`:


   This code produces output similar to:

   ```output
   Response text: Certainly, I have analyzed the sentiment of the review you provided.
   Sentiment: Neutral
   ```

## Clean up resources

If you no longer need them, delete the Azure OpenAI resource and model deployment.

1. In the [Azure portal](https://aka.ms/azureportal), navigate to the Azure OpenAI resource.
1. Select the Azure OpenAI resource, and then select **Delete**.

## See also

- [Structured outputs (Azure OpenAI Service)](/azure/ai-services/openai/how-to/structured-outputs)
- [Using JSON schema for structured output in .NET for OpenAI models](https://devblogs.microsoft.com/semantic-kernel/using-json-schema-for-structured-output-in-net-for-openai-models)
- [Introducing Structured Outputs in the API (OpenAI)](https://openai.com/index/introducing-structured-outputs-in-the-api/)
