-
Notifications
You must be signed in to change notification settings - Fork 1.6k
.NET: Fix stream reconnection for A2AAgent #5275
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 3 commits into
microsoft:a2a-agent-migration
from
SergeyMenshykh:fix-stream-reconnection
Apr 15, 2026
Merged
Changes from all commits
Commits
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
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
23 changes: 23 additions & 0 deletions
23
dotnet/samples/02-agents/A2A/A2AAgent_StreamReconnection/A2AAgent_StreamReconnection.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,23 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFrameworks>net10.0</TargetFrameworks> | ||
|
|
||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="A2A" /> | ||
| <PackageReference Include="Azure.AI.OpenAI" /> | ||
| <PackageReference Include="Azure.Identity" /> | ||
| <PackageReference Include="Microsoft.Extensions.Hosting" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.A2A\Microsoft.Agents.AI.A2A.csproj" /> | ||
| <ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.OpenAI\Microsoft.Agents.AI.OpenAI.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
55 changes: 55 additions & 0 deletions
55
dotnet/samples/02-agents/A2A/A2AAgent_StreamReconnection/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,55 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| // This sample demonstrates how to reconnect to an A2A agent's streaming response using continuation tokens, | ||
| // allowing recovery from stream interruptions without losing progress. | ||
|
|
||
| using A2A; | ||
| using Microsoft.Agents.AI; | ||
| using Microsoft.Extensions.AI; | ||
|
|
||
| var a2aAgentHost = Environment.GetEnvironmentVariable("A2A_AGENT_HOST") ?? throw new InvalidOperationException("A2A_AGENT_HOST is not set."); | ||
|
|
||
| // Initialize an A2ACardResolver to get an A2A agent card. | ||
| A2ACardResolver agentCardResolver = new(new Uri(a2aAgentHost)); | ||
|
|
||
| // Get the agent card | ||
| AgentCard agentCard = await agentCardResolver.GetAgentCardAsync(); | ||
|
|
||
| // Create an instance of the AIAgent for an existing A2A agent specified by the agent card. | ||
| AIAgent agent = agentCard.AsAIAgent(); | ||
|
|
||
| AgentSession session = await agent.CreateSessionAsync(); | ||
|
|
||
| ResponseContinuationToken? continuationToken = null; | ||
|
|
||
| await foreach (var update in agent.RunStreamingAsync("Conduct a comprehensive analysis of quantum computing applications in cryptography, including recent breakthroughs, implementation challenges, and future roadmap. Please include diagrams and visual representations to illustrate complex concepts.", session)) | ||
| { | ||
| // Saving the continuation token to be able to reconnect to the same response stream later. | ||
| // Note: Continuation tokens are only returned for long-running tasks. If the underlying A2A agent | ||
| // returns a message instead of a task, the continuation token will not be initialized. | ||
| // A2A agents do not support stream resumption from a specific point in the stream, | ||
| // but only reconnection to obtain the same response stream from the beginning. | ||
| // So, A2A agents will return an initialized continuation token in the first update | ||
| // representing the beginning of the stream, and it will be null in all subsequent updates. | ||
| if (update.ContinuationToken is { } token) | ||
| { | ||
| continuationToken = token; | ||
| } | ||
|
|
||
| // Imitating stream interruption | ||
| break; | ||
| } | ||
|
|
||
| // Reconnect to the same response stream using the continuation token obtained from the previous run. | ||
| // As a first update, the agent will return an update representing the current state of the response at the moment of calling | ||
| // RunStreamingAsync with the same continuation token, followed by other updates until the end of the stream is reached. | ||
| if (continuationToken is not null) | ||
| { | ||
| await foreach (var update in agent.RunStreamingAsync(session, options: new() { ContinuationToken = continuationToken })) | ||
| { | ||
| if (!string.IsNullOrEmpty(update.Text)) | ||
| { | ||
| Console.WriteLine(update.Text); | ||
| } | ||
| } | ||
| } | ||
29 changes: 29 additions & 0 deletions
29
dotnet/samples/02-agents/A2A/A2AAgent_StreamReconnection/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,29 @@ | ||
| # A2A Agent Stream Reconnection | ||
|
|
||
| This sample demonstrates how to reconnect to an A2A agent's streaming response using continuation tokens, allowing recovery from stream interruptions without losing progress. | ||
|
|
||
| The sample: | ||
|
|
||
| - Connects to an A2A agent server specified in the `A2A_AGENT_HOST` environment variable | ||
| - Sends a request to the agent and begins streaming the response | ||
| - Captures a continuation token from the stream for later reconnection | ||
| - Simulates a stream interruption by breaking out of the streaming loop | ||
| - Reconnects to the same response stream using the captured continuation token | ||
| - Displays the response received after reconnection | ||
|
|
||
| This pattern is useful when network interruptions or other failures may disrupt an ongoing streaming response, and you need to recover and continue processing. | ||
|
|
||
| > **Note:** Continuation tokens are only available when the underlying A2A agent returns a task. If the agent returns a message instead, the continuation token will not be initialized and stream reconnection is not applicable. | ||
|
|
||
| # Prerequisites | ||
|
|
||
| Before you begin, ensure you have the following prerequisites: | ||
|
|
||
| - .NET 10.0 SDK or later | ||
| - An A2A agent server running and accessible via HTTP | ||
|
|
||
| Set the following environment variable: | ||
|
|
||
| ```powershell | ||
| $env:A2A_AGENT_HOST="http://localhost:5000" # Replace with your A2A agent server host | ||
| ``` |
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.
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.