Skip to content

fix: map A2A Message.role to correct GenAI content role - #5198

Closed
enjoykumawat wants to merge 3 commits into
google:mainfrom
enjoykumawat:fix/a2a-message-role-mapping
Closed

enjoykumawat wants to merge 3 commits into
google:mainfrom
enjoykumawat:fix/a2a-message-role-mapping

Conversation

@enjoykumawat

Copy link
Copy Markdown
Contributor

Summary

Fixes #5186 — convert_a2a_message_to_event() hard-codes role="model" for all resulting ADK events, regardless of the original A2A Message.role. User messages (Role.user) are incorrectly restored as model events.

Root cause: _create_event() always creates genai_types.Content(role="model", ...) without considering the source message's role.

Fix:

  • Add _a2a_role_to_content_role() helper that maps A2ARole.user → "user" and A2ARole.agent → "model"
  • Add content_role parameter to _create_event() (defaults to "model" for backward compatibility)
  • Pass the mapped role from convert_a2a_message_to_event() through to _create_event()

Changes

  • src/google/adk/a2a/converters/to_adk_event.py: Import Role, add role mapping helper, thread content_role through _create_event()
  • tests/unittests/a2a/converters/test_to_adk.py: Add 2 tests verifying user→"user" and agent→"model" role mapping

Test plan

  • All 16 test_to_adk.py tests pass (14 existing + 2 new)
  • New tests verify: Role.user → content.role == "user", Role.agent → content.role == "model"
  • No regressions in existing converter tests

@rohityan rohityan self-assigned this Apr 9, 2026
convert_a2a_message_to_event() hard-coded role="model" for all
resulting ADK events regardless of the original A2A Message.role.
User messages (Role.user) were incorrectly restored as model events.

Add _a2a_role_to_content_role() helper that maps A2A Role.user to
"user" and Role.agent to "model", and pass the mapped role through
_create_event() via a new content_role parameter.

Fixes google#5186
@enjoykumawat
enjoykumawat force-pushed the fix/a2a-message-role-mapping branch from b9190e2 to bec6717 Compare April 9, 2026 07:40
@rohityan rohityan added the a2a [Component] This issue is related a2a support inside ADK. label Apr 9, 2026
@rohityan rohityan added the needs review [Status] The PR/issue is awaiting review from the maintainer label Apr 13, 2026
@rohityan

Copy link
Copy Markdown
Collaborator

Hi @enjoykumawat , Thank you for your contribution! We appreciate you taking the time to submit this pull request. Your PR has been received by the team and is currently under review. We will provide feedback as soon as we have an update to share.

@rohityan

Copy link
Copy Markdown
Collaborator

Hi @DeanChensj , can you please review this.

@haranrk haranrk assigned haranrk and unassigned rohityan Jun 17, 2026
copybara-service Bot pushed a commit that referenced this pull request Jun 17, 2026
Merge #5198

## Summary

Fixes #5186 — `convert_a2a_message_to_event()` hard-codes `role="model"` for all resulting ADK events, regardless of the original A2A `Message.role`. User messages (`Role.user`) are incorrectly restored as model events.

**Root cause:** `_create_event()` always creates `genai_types.Content(role="model", ...)` without considering the source message's role.

**Fix:**
- Add `_a2a_role_to_content_role()` helper that maps `A2ARole.user` → `"user"` and `A2ARole.agent` → `"model"`
- Add `content_role` parameter to `_create_event()` (defaults to `"model"` for backward compatibility)
- Pass the mapped role from `convert_a2a_message_to_event()` through to `_create_event()`

## Changes
- `src/google/adk/a2a/converters/to_adk_event.py`: Import `Role`, add role mapping helper, thread `content_role` through `_create_event()`
- `tests/unittests/a2a/converters/test_to_adk.py`: Add 2 tests verifying user→"user" and agent→"model" role mapping

## Test plan
- [x] All 16 `test_to_adk.py` tests pass (14 existing + 2 new)
- [x] New tests verify: `Role.user` → `content.role == "user"`, `Role.agent` → `content.role == "model"`
- [x] No regressions in existing converter tests

Co-authored-by: Haran Rajkumar <haranrk@google.com>
COPYBARA_INTEGRATE_REVIEW=#5198 from enjoykumawat:fix/a2a-message-role-mapping da6826d
PiperOrigin-RevId: 933926812
@adk-bot

adk-bot commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

Thank you @enjoykumawat for your contribution! 🎉

Your changes have been successfully imported and merged via Copybara in commit fe08a9f.

Closing this PR as the changes are now in the main branch.

@adk-bot adk-bot added the merged [Status] This PR is merged label Jun 17, 2026
@adk-bot adk-bot closed this Jun 17, 2026
FrigaZzz pushed a commit to FrigaZzz/adk-python that referenced this pull request Aug 11, 2026
Merge google#5198

## Summary

Fixes google#5186 — `convert_a2a_message_to_event()` hard-codes `role="model"` for all resulting ADK events, regardless of the original A2A `Message.role`. User messages (`Role.user`) are incorrectly restored as model events.

**Root cause:** `_create_event()` always creates `genai_types.Content(role="model", ...)` without considering the source message's role.

**Fix:**
- Add `_a2a_role_to_content_role()` helper that maps `A2ARole.user` → `"user"` and `A2ARole.agent` → `"model"`
- Add `content_role` parameter to `_create_event()` (defaults to `"model"` for backward compatibility)
- Pass the mapped role from `convert_a2a_message_to_event()` through to `_create_event()`

## Changes
- `src/google/adk/a2a/converters/to_adk_event.py`: Import `Role`, add role mapping helper, thread `content_role` through `_create_event()`
- `tests/unittests/a2a/converters/test_to_adk.py`: Add 2 tests verifying user→"user" and agent→"model" role mapping

## Test plan
- [x] All 16 `test_to_adk.py` tests pass (14 existing + 2 new)
- [x] New tests verify: `Role.user` → `content.role == "user"`, `Role.agent` → `content.role == "model"`
- [x] No regressions in existing converter tests

Co-authored-by: Haran Rajkumar <haranrk@google.com>
COPYBARA_INTEGRATE_REVIEW=google#5198 from enjoykumawat:fix/a2a-message-role-mapping da6826d
PiperOrigin-RevId: 933926812
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a2a [Component] This issue is related a2a support inside ADK. merged [Status] This PR is merged needs review [Status] The PR/issue is awaiting review from the maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A2A inbound message conversion ignores Message.role, always maps to "model"

4 participants