Skip to content

Add ADK model handler#37917

Open
damccorm wants to merge 8 commits intomasterfrom
users/damccorm/adkHandler
Open

Add ADK model handler#37917
damccorm wants to merge 8 commits intomasterfrom
users/damccorm/adkHandler

Conversation

@damccorm
Copy link
Contributor

Adds model handler capable of running ADK agents mid-pipeline.


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a new ModelHandler for Apache Beam's ML Inference library, specifically designed to integrate with the Google Agent Development Kit (ADK). This integration allows users to embed sophisticated AI agents, such as large language model agents, directly into their Beam data processing pipelines. The handler manages agent lifecycle and conversation sessions, providing flexibility for various interaction patterns.

Highlights

  • New ADKAgentModelHandler: Introduced ADKAgentModelHandler to enable running Google Agent Development Kit (ADK) agents within Beam pipelines using the RunInference transform.
  • Flexible Agent Instantiation: Implemented support for providing ADK agents either as direct instances or as zero-argument factory callables, accommodating unpicklable agent states.
  • Session Management: Designed session management within the handler to support both stateless (unique session per element) and stateful (shared session_id for multi-turn conversations) agent interactions.
  • Comprehensive Unit Tests: Included comprehensive unit tests for ADKAgentModelHandler covering initialization, model loading, inference execution, and session handling.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@codecov
Copy link

codecov bot commented Mar 23, 2026

Codecov Report

❌ Patch coverage is 0% with 68 lines in your changes missing coverage. Please review.
✅ Project coverage is 54.75%. Comparing base (0afbdf6) to head (633e08a).
⚠️ Report is 7 commits behind head on master.

Files with missing lines Patch % Lines
.../apache_beam/ml/inference/agent_development_kit.py 0.00% 68 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             master   #37917    +/-   ##
==========================================
  Coverage     54.75%   54.75%            
  Complexity     1690     1690            
==========================================
  Files          1063     1064     +1     
  Lines        166898   167111   +213     
  Branches       1226     1226            
==========================================
+ Hits          91377    91505   +128     
- Misses        73328    73413    +85     
  Partials       2193     2193            
Flag Coverage Δ
python 80.02% <0.00%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@damccorm damccorm marked this pull request as ready for review March 24, 2026 15:15
@damccorm damccorm changed the title [WIP] Add ADK model handler Add ADK model handler Mar 24, 2026
@github-actions
Copy link
Contributor

Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment assign set of reviewers

@damccorm
Copy link
Contributor Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces the ADKAgentModelHandler to enable running Google Agent Development Kit (ADK) agents within Apache Beam's RunInference transform. This includes a new Python module for the handler, comprehensive unit tests, and updates to setup.py to include the google-adk dependency. Feedback suggests improving performance by using asyncio.gather for concurrent agent invocations instead of repeated asyncio.run calls, and addressing an import consistency issue in the test file.

@damccorm
Copy link
Contributor Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces the ADKAgentModelHandler to enable running Google Agent Development Kit (ADK) agents within Apache Beam's RunInference transform. This includes the core implementation, a dedicated test file, and updates to dependencies and the CHANGES.md file. Feedback indicates a critical bug where model.session_service.create_session will raise a SessionExistsError for elements beyond the first when a shared session_id is used, requiring a try...except block. Additionally, the response extraction in _invoke_agent should be improved to handle multi-part agent responses completely by using event.content.text instead of event.content.parts[0].text.

Comment on lines +227 to +231
model.session_service.create_session(
app_name=self._app_name,
user_id=user_id,
session_id=session_id,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

model.session_service.create_session raises a SessionExistsError if a session with the given ID already exists. When a session_id is provided via inference_args, it's shared across all elements in the batch. The current implementation calls create_session for each element, which will cause a SessionExistsError for the second element onwards, crashing the pipeline.

To fix this, you should wrap the call in a try...except SessionExistsError block. This will gracefully handle cases where the session already exists, which is expected for stateful conversations using a shared session ID.

You will also need to import SessionExistsError at the top of the file within the try...except ImportError block:

from google.adk.exceptions import SessionExistsError
Suggested change
model.session_service.create_session(
app_name=self._app_name,
user_id=user_id,
session_id=session_id,
)
try:
model.session_service.create_session(
app_name=self._app_name,
user_id=user_id,
session_id=session_id,
)
except SessionExistsError:
# It's okay if the session already exists for shared session IDs.
pass

Comment on lines +286 to +287
if event.content and event.content.parts:
return event.content.parts[0].text
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The agent's final response may contain multiple text parts. This implementation only considers the text from the first part (parts[0]), which could lead to incomplete results. To handle multi-part responses correctly and idiomatically, you should use the .text property of the content object, which concatenates the text from all parts. This also simplifies the code.

Suggested change
if event.content and event.content.parts:
return event.content.parts[0].text
if event.content:
return event.content.text

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant