-
Notifications
You must be signed in to change notification settings - Fork 2k
.NET: [Breaking] Structured Output improvements #3761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
3df8fe3
.NET: Delete AgentResponse.{Try}Deserialize<T> methods (#3518)
SergeyMenshykh 0709f6d
Merge branch 'main' into feature-so
SergeyMenshykh 1e20c69
.NET:[Breaking] Add support for structured output (#3658)
SergeyMenshykh 9991eb6
.NET: Add decorator for structured output support (#3694)
SergeyMenshykh c26a773
.NET: Support primitives and arrays for SO (#3696)
SergeyMenshykh 30fe0ee
add adr
SergeyMenshykh 7966410
merge with latest main
SergeyMenshykh be90442
add missed change
SergeyMenshykh 6f64fdb
fix compilation issue
SergeyMenshykh 7b48633
address review comments
SergeyMenshykh 8635783
rename adr file name
SergeyMenshykh 04abdbd
reflect decision to have SO decorator as a reference implementation i…
SergeyMenshykh 6dda25c
.NET: Move SO agent to samples (#3820)
SergeyMenshykh 19ff980
.NET: Preserve caller context (#3803)
SergeyMenshykh 2f7c8c5
Merge branch 'main' into feature-so
SergeyMenshykh 2168d59
.NET: Disable irrelevant integration test (#3913)
SergeyMenshykh 87b97da
merge with latest main
SergeyMenshykh b77dbbf
forgotten change
SergeyMenshykh b4e5f8a
address pr review feedback
SergeyMenshykh 24ebbb7
disable intermittently failing integration test.
SergeyMenshykh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
dotnet/samples/GettingStarted/Agents/Agent_Step05_StructuredOutput/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # Structured Output with ChatClientAgent | ||
|
|
||
| This sample demonstrates how to configure ChatClientAgent to produce structured output in JSON format using various approaches. | ||
|
|
||
| ## What this sample demonstrates | ||
|
|
||
| - **ResponseFormat approach**: Configuring agents with JSON schema response format via `ChatResponseFormat.ForJsonSchema<T>()` for inter-agent communication or when the type is not known at compile time | ||
| - **Generic RunAsync<T> method**: Using the generic `RunAsync<T>` method for structured output when the caller needs to work directly with typed objects | ||
| - **Structured output with Streaming**: Using `RunStreamingAsync` to stream responses while still obtaining structured output by assembling and deserializing the streamed content | ||
| - **StructuredOutput middleware**: Adding structured output support to agents that don't natively support it (like A2A agents or models without structured output capability) by transforming text output into structured data using a chat client | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| Before you begin, ensure you have the following prerequisites: | ||
|
|
||
| - .NET 10 SDK or later | ||
| - Azure OpenAI service endpoint and deployment configured | ||
| - Azure CLI installed and authenticated (for Azure credential authentication) | ||
| - User has the `Cognitive Services OpenAI Contributor` role for the Azure OpenAI resource | ||
|
|
||
| **Note**: This sample uses Azure OpenAI models. For more information, see [how to deploy Azure OpenAI models with Azure AI Foundry](https://learn.microsoft.com/en-us/azure/ai-foundry/how-to/deploy-models-openai). | ||
|
|
||
| **Note**: This demo uses Azure CLI credentials for authentication. Make sure you're logged in with `az login` and have access to the Azure OpenAI resource and have the `Cognitive Services OpenAI Contributor` role. For more information, see the [Azure CLI documentation](https://learn.microsoft.com/cli/azure/authenticate-azure-cli-interactively). | ||
|
|
||
| ## Environment Variables | ||
|
|
||
| Set the following environment variables: | ||
|
|
||
| ```powershell | ||
| $env:AZURE_OPENAI_ENDPOINT="https://your-resource.openai.azure.com/" # Replace with your Azure OpenAI resource endpoint | ||
| $env:AZURE_OPENAI_DEPLOYMENT_NAME="gpt-4o-mini" # Optional, defaults to gpt-4o-mini | ||
| ``` | ||
|
|
||
| ## Run the sample | ||
|
|
||
| Navigate to the sample directory and run: | ||
|
|
||
| ```powershell | ||
| cd dotnet/samples/GettingStarted/Agents/Agent_Step05_StructuredOutput | ||
| dotnet run | ||
| ``` | ||
|
|
||
| ## Expected behavior | ||
|
|
||
| The sample will demonstrate four different approaches to structured output: | ||
|
|
||
| 1. **Structured Output with ResponseFormat**: Creates an agent with `ResponseFormat` set to `ForJsonSchema<CityInfo>()`, invokes it with unstructured input, and accesses the structured output via the `Text` property | ||
| 2. **Structured Output with RunAsync<T>**: Creates an agent and uses the generic `RunAsync<CityInfo>()` method to get a typed `AgentResponse<CityInfo>` with the result accessible via the `Result` property | ||
| 3. **Structured Output with RunStreamingAsync**: Creates an agent with JSON schema response format, streams the response using `RunStreamingAsync`, assembles the updates using `ToAgentResponseAsync()`, and deserializes the JSON text into a typed object | ||
| 4. **Structured Output with StructuredOutput Middleware**: Uses the `UseStructuredOutput` method on `AIAgentBuilder` to add structured output support to agents that don't natively support it | ||
|
|
||
| Each approach will output information about the capital of France (Paris) in a structured format. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.