Skip to content

Python: fix: A2AAgent.run() ignores session parameter (#4663)#4713

Open
LEDazzio01 wants to merge 1 commit intomicrosoft:mainfrom
LEDazzio01:fix/a2a-agent-session-context-id
Open

Python: fix: A2AAgent.run() ignores session parameter (#4663)#4713
LEDazzio01 wants to merge 1 commit intomicrosoft:mainfrom
LEDazzio01:fix/a2a-agent-session-context-id

Conversation

@LEDazzio01
Copy link
Contributor

Summary

Fixes #4663

A2AAgent.run() accepts a session parameter but never uses it. The A2A protocol supports context_id on messages for correlating multi-turn conversations, but it was never set from the session.

Root Cause

In _agent.py, the run() method signature includes session: AgentSession | None = None but the parameter was completely ignored — session was effectively dead code. Meanwhile, _prepare_message_for_a2a() creates A2AMessage objects without setting context_id, so the remote agent has no way to correlate messages from the same conversation.

Fix

  1. run(): Extract session.id and pass it as context_id to _prepare_message_for_a2a()
  2. _prepare_message_for_a2a(): Accept a new context_id keyword argument and forward it to the A2AMessage constructor
  3. When no session is provided, a random context_id is generated via uuid.uuid4().hex (preserving existing single-turn behavior)
+        # Derive context_id from session when available
+        context_id: str | None = session.id if session else None
+
         if continuation_token is not None:
             a2a_stream = self.client.resubscribe(...)
         else:
             normalized_messages = normalize_messages(messages)
-            a2a_message = self._prepare_message_for_a2a(normalized_messages[-1])
+            a2a_message = self._prepare_message_for_a2a(normalized_messages[-1], context_id=context_id)

Tests

Added test_a2a_agent_context_id.py with tests covering:

  • context_id is set from session.id when session is provided
  • context_id is auto-generated when no session is provided
  • ✅ Explicitly passing context_id=None auto-generates a value
  • ✅ Different sessions produce different context_id values
  • ✅ Message role and content are preserved through conversion

Copilot AI review requested due to automatic review settings March 15, 2026 20:25
@markwallace-microsoft markwallace-microsoft added documentation Improvements or additions to documentation python labels Mar 15, 2026
@github-actions github-actions bot changed the title fix: A2AAgent.run() ignores session parameter (#4663) Python: fix: A2AAgent.run() ignores session parameter (#4663) Mar 15, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_id from the provided session in A2AAgent.run() and forwards it into message conversion.
  • Extends _prepare_message_for_a2a() to accept a context_id kwarg 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.

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"
Copy link
Contributor Author

@LEDazzio01 LEDazzio01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the thorough review! All issues have been addressed:

  1. session.idsession.session_id — Fixed in _agent.py. AgentSession exposes session_id, not id (commit 96a7a13).
  2. Docstring updated — Now references session_id correctly.
  3. mime_typemedia_type rename reverted — Restored mime_type for FileWithUri to match the A2A SDK (commit 840c149).
  4. Unused uuid import removed from test file (commit 251d3f1).
  5. AgentSession(id=...)AgentSession(session_id=...) — Fixed test constructor (commit 251d3f1).
  6. ℹ️ contribution-logs/2026-03-14.md — Stale artifact from the fork's main branch; will clean up via fork sync.
  7. ℹ️ End-to-end run() test — Good suggestion; the current tests cover _prepare_message_for_a2a() directly. An integration test for run() 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
@LEDazzio01 LEDazzio01 force-pushed the fix/a2a-agent-session-context-id branch from 251d3f1 to c73b109 Compare March 15, 2026 20:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [agent-framework-a2a] A2AAgent.run() ignores session parameter - context_id not propagated to A2A protocol

3 participants