Python: fix: A2AAgent.run() ignores session parameter (#4663)#4713
Open
LEDazzio01 wants to merge 1 commit intomicrosoft:mainfrom
Open
Python: fix: A2AAgent.run() ignores session parameter (#4663)#4713LEDazzio01 wants to merge 1 commit intomicrosoft:mainfrom
LEDazzio01 wants to merge 1 commit intomicrosoft:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aims to fix issue #4663 by propagating an Agent Framework session identifier into the A2A protocol context_id, enabling multi-turn correlation for A2AAgent conversations.
Changes:
- Derives an A2A
context_idfrom the providedsessioninA2AAgent.run()and forwards it into message conversion. - Extends
_prepare_message_for_a2a()to accept acontext_idkwarg and ensures a value is always set. - Adds a new unit test module for context_id/session propagation behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
| python/packages/a2a/agent_framework_a2a/_agent.py | Attempts to thread session-derived context through A2A message creation; also adjusts file-part media type wiring. |
| python/packages/a2a/tests/test_a2a_agent_context_id.py | Adds tests around context_id behavior (session-provided vs auto-generated). |
| contribution-logs/2026-03-14.md | Adds a contribution status log entry (unrelated to the PR’s stated fix). |
You can also share your feedback on Copilot code review. Take the survey.
contribution-logs/2026-03-14.md
Outdated
Comment on lines
+1
to
+5
| # 📋 Open Source Contribution Status — March 14, 2026 | ||
|
|
||
| **Date:** March 14, 2026 | ||
| **Contributor:** [@LEDazzio01](https://github.com/LEDazzio01) | ||
|
|
Comment on lines
+265
to
+267
| # Derive context_id from session when available so the remote agent | ||
| # can correlate messages belonging to the same conversation. | ||
| context_id: str | None = session.id if session else None |
| Keyword Args: | ||
| stream: Whether to stream the response. Defaults to False. | ||
| session: The conversation session associated with the message(s). | ||
| When provided, the session's ``id`` is used as the A2A |
| file=FileWithUri( | ||
| uri=content.uri, | ||
| mime_type=content.media_type, | ||
| media_type=content.media_type, |
|
|
||
| from __future__ import annotations | ||
|
|
||
| import uuid |
Comment on lines
+38
to
+43
| session = AgentSession(id="my-session-123") | ||
| message = _make_text_message() | ||
|
|
||
| a2a_msg = agent._prepare_message_for_a2a(message, context_id=session.id) | ||
|
|
||
| assert a2a_msg.context_id == "my-session-123" |
Comment on lines
+35
to
+43
| def test_context_id_set_from_session(self) -> None: | ||
| """When a session is provided, its id should become the A2A context_id.""" | ||
| agent = _make_agent() | ||
| session = AgentSession(id="my-session-123") | ||
| message = _make_text_message() | ||
|
|
||
| a2a_msg = agent._prepare_message_for_a2a(message, context_id=session.id) | ||
|
|
||
| assert a2a_msg.context_id == "my-session-123" |
LEDazzio01
commented
Mar 15, 2026
Contributor
Author
LEDazzio01
left a comment
There was a problem hiding this comment.
Thanks for the thorough review! All issues have been addressed:
- ✅
session.id→session.session_id— Fixed in_agent.py.AgentSessionexposessession_id, notid(commit96a7a13). - ✅ Docstring updated — Now references
session_idcorrectly. - ✅
mime_type→media_typerename reverted — Restoredmime_typeforFileWithUrito match the A2A SDK (commit840c149). - ✅ Unused
uuidimport removed from test file (commit251d3f1). - ✅
AgentSession(id=...)→AgentSession(session_id=...)— Fixed test constructor (commit251d3f1). - ℹ️
contribution-logs/2026-03-14.md— Stale artifact from the fork's main branch; will clean up via fork sync. - ℹ️ End-to-end
run()test — Good suggestion; the current tests cover_prepare_message_for_a2a()directly. An integration test forrun()would require mocking the async streaming client, which may be better suited as a follow-up.
…microsoft#4663) - Extract session.session_id and pass it as context_id to _prepare_message_for_a2a() - Add context_id kwarg to _prepare_message_for_a2a() with fallback to uuid.uuid4().hex - Add 6 unit tests covering context_id propagation scenarios
251d3f1 to
c73b109
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #4663
A2AAgent.run()accepts asessionparameter but never uses it. The A2A protocol supportscontext_idon messages for correlating multi-turn conversations, but it was never set from the session.Root Cause
In
_agent.py, therun()method signature includessession: AgentSession | None = Nonebut the parameter was completely ignored —sessionwas effectively dead code. Meanwhile,_prepare_message_for_a2a()createsA2AMessageobjects without settingcontext_id, so the remote agent has no way to correlate messages from the same conversation.Fix
run(): Extractsession.idand pass it ascontext_idto_prepare_message_for_a2a()_prepare_message_for_a2a(): Accept a newcontext_idkeyword argument and forward it to theA2AMessageconstructorcontext_idis generated viauuid.uuid4().hex(preserving existing single-turn behavior)Tests
Added
test_a2a_agent_context_id.pywith tests covering:context_idis set fromsession.idwhen session is providedcontext_idis auto-generated when no session is providedcontext_id=Noneauto-generates a valuecontext_idvalues