Skip to content

feat(a2a): Add optional context_builder to RemoteA2aAgent - #6619

Open
a2105z wants to merge 1 commit into
google:mainfrom
a2105z:feat/remote-a2a-context-builder
Open

feat(a2a): Add optional context_builder to RemoteA2aAgent#6619
a2105z wants to merge 1 commit into
google:mainfrom
a2105z:feat/remote-a2a-context-builder

Conversation

@a2105z

@a2105z a2105z commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

Problem:
RemoteA2aAgent always builds remote A2A request context via a fixed _construct_message_parts_from_session path (full/partial session history with "For context:" formatting). That is inflexible and token-heavy for specialized remotes (current-turn only, sliding window, summarization), and the only escape hatch today is monkey-patching the protected method.

Solution:
Add an optional context_builder argument to RemoteA2aAgent.__init__, typed as a public ContextBuilder protocol:

class ContextBuilder(Protocol):
  def __call__(
      self,
      ctx: InvocationContext,
      agent_name: str,
      genai_part_converter: GenAIPartToA2APartConverter,
  ) -> tuple[list[A2APart], str | None]: ...

When provided, it fully replaces default session→message construction for the outgoing request. When None, behavior is unchanged (_construct_message_parts_from_session).

This matches the API requested in #3301 exactly.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.
pytest tests/unittests/agents/test_remote_a2a_agent.py -q
# 158 passed

Coverage added for:

  • storing context_builder on init
  • custom builder used (default constructor not called)
  • fallback when context_builder is None
  • _run_async_impl path uses the custom builder

Manual End-to-End (E2E) Tests:

  • Not run against a live remote A2A server in this PR (unit coverage exercises the request-construction seam). Happy to follow up with a sample if useful.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

Took this over because #3301 had no open PR and no recent implementation activity (earlier interest note from May did not land a patch). Implemented exactly the optional ContextBuilder protocol + fallback described in the issue.

cc @klateefa — thought I’d take this over given no open PR / nothing in flight. Please redirect if you’d rather keep design internal or if someone already has a patch.

Remote A2A calls always used a fixed full-session history builder, so
callers could not send current-only, sliding-window, or summarized
context without monkey-patching. Accept an optional ContextBuilder that
replaces _construct_message_parts_from_session when provided.

Fixes google#3301
@adk-bot adk-bot added the core [Component] This issue is related to the core interface and implementation label Aug 6, 2026
@a2105z

a2105z commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

Hi @DeanChensj @GWeale @Jacksunwei — hoping one of you might have a minute for a review when free.

This is the fix for #3301: an optional public context_builder on RemoteA2aAgent so callers can customize outgoing A2A request context (current-turn only, sliding window, summarization, etc.) without monkey-patching _construct_message_parts_from_session. Default path is unchanged when context_builder is omitted.

Covered in tests/unittests/agents/test_remote_a2a_agent.py (158 passed locally). Happy to adjust the Protocol / call signature if you’d prefer a different shape — thanks so much for taking a look.

@a2105z

a2105z commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

Hi @DeanChensj — quick follow-up: I don’t have triage rights to edit labels on this repo. Could you please add a2a (and needs review if that’s useful) alongside the existing core label? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core [Component] This issue is related to the core interface and implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Configurable Context Builder for RemoteA2aAgent

3 participants