Python: preserve A2A message context_id (#4686)

* Python: forward A2A context_id

* Avoid duplicating A2A context ids

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Eduard van Valkenburg
2026-03-16 22:41:31 +01:00
committed by GitHub
Unverified
parent 414496dda7
commit 0fdcfd0f4c
2 changed files with 19 additions and 1 deletions
@@ -486,13 +486,14 @@ class A2AAgent(AgentTelemetryLayer, BaseAgent):
raise ValueError(f"Unknown content type: {content.type}")
# Exclude framework-internal keys (e.g. attribution) from wire metadata
internal_keys = {"_attribution"}
internal_keys = {"_attribution", "context_id"}
metadata = {k: v for k, v in message.additional_properties.items() if k not in internal_keys} or None
return A2AMessage(
role=A2ARole("user"),
parts=parts,
message_id=message.message_id or uuid.uuid4().hex,
context_id=message.additional_properties.get("context_id"),
metadata=metadata,
)
@@ -507,6 +507,23 @@ def test_prepare_message_for_a2a_with_multiple_contents() -> None:
assert result.parts[3].root.kind == "text" # JSON text remains as text (no parsing)
def test_prepare_message_for_a2a_forwards_context_id() -> None:
"""Test conversion of Message preserves context_id without duplicating it in metadata."""
agent = A2AAgent(client=MagicMock(), _http_client=None)
message = Message(
role="user",
contents=[Content.from_text(text="Continue the task")],
additional_properties={"context_id": "ctx-123", "trace_id": "trace-456"},
)
result = agent._prepare_message_for_a2a(message)
assert result.context_id == "ctx-123"
assert result.metadata == {"trace_id": "trace-456"}
def test_parse_contents_from_a2a_with_data_part() -> None:
"""Test conversion of A2A DataPart."""