-
Couldn't load subscription status.
- Fork 613
.NET: Add support for background responses #1501
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
SergeyMenshykh
merged 26 commits into
microsoft:main
from
SergeyMenshykh:add-background-responses
Oct 22, 2025
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
7226300
add support for background responses
SergeyMenshykh 1e2a0a4
Merge branch 'main' into add-background-responses
SergeyMenshykh 6f9d678
Update dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunResponseUp…
SergeyMenshykh 2bee1c3
fix broken link
SergeyMenshykh 14f5b87
Merge branch 'add-background-responses' of https://github.com/SergeyM…
SergeyMenshykh 0f1a799
fix xml comments and background responses properties override funcito…
SergeyMenshykh a8cd337
change ai model provider
SergeyMenshykh 99c6bb5
Merge branch 'main' into add-background-responses
SergeyMenshykh f1e2105
use Run{Streaming}Async overloads that don't require messages
SergeyMenshykh 190e132
Merge branch 'add-background-responses' of https://github.com/SergeyM…
SergeyMenshykh ade1519
stop using m: prefix in cref attribute of <see/> element.
SergeyMenshykh 955d926
reject input messages provided with continuation token + don't extrac…
SergeyMenshykh a9665ad
use agent thread for background-responses sample
SergeyMenshykh a61ca04
require agent thread for background responses
SergeyMenshykh 0507e3f
Merge branch 'main' into add-background-responses
SergeyMenshykh f338305
Update dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs
SergeyMenshykh 8a053a0
Update dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs
SergeyMenshykh dacada8
remove CA1200
SergeyMenshykh 7b1cba7
Merge branch 'add-background-responses' of https://github.com/SergeyM…
SergeyMenshykh 53d0288
Update dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunOptions.cs
SergeyMenshykh ce4c9d8
Update dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunResponse.cs
SergeyMenshykh dc63bdd
Update dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunResponse.cs
SergeyMenshykh a1166cb
address pr review comments
SergeyMenshykh 89637ef
Merge branch 'add-background-responses' of https://github.com/SergeyM…
SergeyMenshykh b254cbb
Merge branch 'main' into add-background-responses
SergeyMenshykh a502ce7
Update dotnet/samples/GettingStarted/Agents/Agent_Step17_BackgroundRe…
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
File renamed without changes.
1,689 changes: 1,689 additions & 0 deletions
1,689
docs/decisions/0009-support-long-running-operations.md
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
20 changes: 20 additions & 0 deletions
20
...ngStarted/Agents/Agent_Step17_BackgroundResponses/Agent_Step17_BackgroundResponses.csproj
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,20 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
|
|
||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Azure.AI.OpenAI" /> | ||
| <PackageReference Include="Azure.Identity" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.OpenAI\Microsoft.Agents.AI.OpenAI.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
70 changes: 70 additions & 0 deletions
70
dotnet/samples/GettingStarted/Agents/Agent_Step17_BackgroundResponses/Program.cs
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,70 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| // This sample shows how to use background responses with ChatClientAgent and OpenAI Responses. | ||
|
|
||
| using Azure.AI.OpenAI; | ||
| using Azure.Identity; | ||
| using Microsoft.Agents.AI; | ||
| using OpenAI; | ||
|
|
||
| var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); | ||
| var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; | ||
|
|
||
| AIAgent agent = new AzureOpenAIClient( | ||
| new Uri(endpoint), | ||
| new AzureCliCredential()) | ||
| .GetOpenAIResponseClient(deploymentName) | ||
| .CreateAIAgent(instructions: "You are good at telling jokes.", name: "Joker"); | ||
|
|
||
| // Enable background responses (only supported by OpenAI Responses at this time). | ||
| AgentRunOptions options = new() { AllowBackgroundResponses = true }; | ||
|
|
||
| AgentThread thread = agent.GetNewThread(); | ||
|
|
||
| // Start the initial run. | ||
| AgentRunResponse response = await agent.RunAsync("Tell me a joke about a pirate.", thread, options); | ||
|
|
||
| // Poll until the response is complete. | ||
| while (response.ContinuationToken is { } token) | ||
| { | ||
| // Wait before polling again. | ||
| await Task.Delay(TimeSpan.FromSeconds(2)); | ||
|
|
||
| // Continue with the token. | ||
| options.ContinuationToken = token; | ||
|
|
||
| response = await agent.RunAsync(thread, options); | ||
| } | ||
|
|
||
| // Display the result. | ||
| Console.WriteLine(response.Text); | ||
|
|
||
| // Reset options and thread for streaming. | ||
| options = new() { AllowBackgroundResponses = true }; | ||
| thread = agent.GetNewThread(); | ||
|
|
||
| AgentRunResponseUpdate? lastReceivedUpdate = null; | ||
| // Start streaming. | ||
| await foreach (AgentRunResponseUpdate update in agent.RunStreamingAsync("Tell me a joke about a pirate.", thread, options)) | ||
| { | ||
| // Output each update. | ||
| Console.Write(update.Text); | ||
|
|
||
| // Track last update. | ||
| lastReceivedUpdate = update; | ||
|
|
||
| // Simulate connection loss after first piece of content received. | ||
| if (update.Text.Length > 0) | ||
| { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| // Resume from interruption point. | ||
| options.ContinuationToken = lastReceivedUpdate?.ContinuationToken; | ||
|
|
||
| await foreach (AgentRunResponseUpdate update in agent.RunStreamingAsync(thread, options)) | ||
| { | ||
| // Output each update. | ||
| Console.Write(update.Text); | ||
| } | ||
22 changes: 22 additions & 0 deletions
22
dotnet/samples/GettingStarted/Agents/Agent_Step17_BackgroundResponses/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,22 @@ | ||
| # What This Sample Shows | ||
|
|
||
| This sample demonstrates how to use background responses with ChatCompletionAgent and OpenAI Responses for long-running operations. Background responses support: | ||
|
|
||
| - **Polling for completion** - Non-streaming APIs can start a background operation and return a continuation token. Poll with the token until the response completes. | ||
| - **Resuming after interruption** - Streaming APIs can be interrupted and resumed from the last update using the continuation token. | ||
|
|
||
| > **Note:** Background responses are currently only supported by OpenAI Responses. | ||
|
|
||
| # Prerequisites | ||
|
|
||
| Before you begin, ensure you have the following prerequisites: | ||
|
|
||
| - .NET 8.0 SDK or later | ||
| - OpenAI api key | ||
|
|
||
| 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 | ||
| ``` |
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
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.