Skip to content

AgentEvaluator support custom metric - #4344

Closed
ftnext wants to merge 9 commits into
google:mainfrom
ftnext:agent-evaluator-support-custom-metric
Closed

ftnext wants to merge 9 commits into
google:mainfrom
ftnext:agent-evaluator-support-custom-metric

Conversation

@ftnext

@ftnext ftnext commented Feb 1, 2026

Copy link
Copy Markdown
Contributor

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

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

Problem:
AgentEvaluator.evaluate did not register custom metrics from EvalConfig, so custom metrics worked in adk eval but not in pytest-based evals.

Solution:
Align AgentEvaluator with the CLI eval flow by registering custom metrics via a local metric registry and a shared default MetricInfo helper.

Testing Plan

Add unit coverage for the registration behavior and a lightweight integration example that uses a custom metric.

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.
% pytest tests/unittests/evaluation tests/unittests/cli

614 passed, 308 warnings in 12.74s

Manual End-to-End (E2E) Tests:

% pytest tests/integration/test_with_test_file.py::test_with_custom_metric

tests/integration/test_with_test_file.py .                               [100%]

1 passed, 12 warnings in 3.57s

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

This change keeps AgentEvaluator behavior consistent with adk eval while avoiding CLI-layer dependencies.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @ftnext, 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 enhances the AgentEvaluator to fully support custom metrics, bringing its evaluation capabilities in line with the adk eval command-line interface. The core change involves dynamically registering custom metric evaluators during the evaluation process, leveraging a new local metric registry and a centralized utility for default metric information. This ensures that users can define and apply their own evaluation metrics consistently across different evaluation contexts, significantly improving the flexibility and extensibility of the evaluation framework.

Highlights

  • Custom Metric Support: The AgentEvaluator now correctly registers and utilizes custom metrics defined in the EvalConfig, resolving an inconsistency where these metrics were only recognized by the adk eval CLI.
  • Centralized MetricInfo Utility: The get_default_metric_info function has been moved to a new, centralized metric_defaults.py module, promoting code reusability and consistency.
  • Local Metric Evaluator Registry: The MetricEvaluatorRegistry has been refactored to be an instance-level object, allowing for dynamic and local registration of custom metric evaluators within the AgentEvaluator's evaluation process.
  • Comprehensive Testing: New unit tests specifically validate the custom metric registration logic within AgentEvaluator, and an integration test demonstrates the end-to-end functionality of custom metrics.

🧠 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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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. ↩

@adk-bot adk-bot added the eval [Component] This issue is related to evaluation label Feb 1, 2026

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

Copy link
Copy Markdown
Contributor

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 effectively adds support for custom metrics within the AgentEvaluator, which is a valuable enhancement for pytest-based evaluations. The changes are well-implemented, including the refactoring of get_default_metric_info into its own module for better separation of concerns and fixing a critical issue in MetricEvaluatorRegistry by changing the registry from a class to an instance attribute. The addition of both unit and integration tests ensures the new functionality is robust. I have a minor suggestion to improve code style by relocating an inline import.

Comment thread src/google/adk/evaluation/agent_evaluator.py Outdated
Comment thread src/google/adk/evaluation/agent_evaluator.py Outdated
ftnext added a commit to ftnext/agent-practice that referenced this pull request Feb 1, 2026
@rohityan rohityan self-assigned this Feb 2, 2026
@rohityan rohityan added the request clarification [Status] The maintainer need clarification or more information from the author label Feb 5, 2026
@rohityan

rohityan commented Feb 5, 2026

Copy link
Copy Markdown
Collaborator

Hi @ftnext , Thank you for your contribution! We appreciate you taking the time to submit this pull request. Can you please address the suggestions.

@ftnext

ftnext commented Feb 8, 2026

Copy link
Copy Markdown
Contributor Author

@ryanaiagent Thank you for the review! I've addressed the suggestions.

@ftnext

ftnext commented Feb 11, 2026

Copy link
Copy Markdown
Contributor Author

To "request clarification" label:
The motivation and detailed context (problem description, proposed solution, and related issues) are documented in the linked issue #4343.

@ogasimli

Copy link
Copy Markdown

Hi @rohityan — any update on the timeline for merging this? We're hitting this exact issue when running custom metrics via AgentEvaluator.evaluate() in pytest. Would be happy to help test or address remaining review feedback if that helps move things forward.

@rohityan rohityan removed the request clarification [Status] The maintainer need clarification or more information from the author label Aug 5, 2026
@rohityan
rohityan requested a review from ankursharmas August 5, 2026 16:57
@rohityan

rohityan commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator

Hi @ankursharmas , can you please review this.

@i-yliu i-yliu assigned i-yliu and unassigned rohityan Aug 5, 2026
Use a per-evaluation metric registry populated from EvalConfig so pytest-based agent evaluations resolve custom metrics consistently with the CLI flow.\n\nMerge the latest main branch and adapt the implementation to the shared registry helper.\n\nCloses google#4343
@ftnext

ftnext commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor Author

Resolved the merge conflicts with the latest main and adapted the change to the current metric registry implementation. Unit tests pass (68 passed).

uv run --extra test pytest \
  tests/unittests/evaluation/test_agent_evaluator.py \
  tests/unittests/evaluation/test_metric_evaluator_registry.py \
  -q

copybara-service Bot pushed a commit that referenced this pull request Aug 18, 2026
Merge #4344

**Please ensure you have read the [contribution guide](https://github.com/google/adk-python/blob/main/CONTRIBUTING.md) before creating a pull request.**

### Link to Issue or Description of Change

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

- Closes: #4343

**Problem:**
`AgentEvaluator.evaluate` did not register custom metrics from `EvalConfig`, so custom metrics worked in `adk eval` but not in pytest-based evals.

**Solution:**
Align `AgentEvaluator` with the CLI eval flow by registering custom metrics via a per-run metric registry and a shared default `MetricInfo` helper. The per-run registry is a fork of `DEFAULT_METRIC_EVALUATOR_REGISTRY` (new `MetricEvaluatorRegistry.fork()`), so the custom metrics declared by one eval config never leak into another run.

### Testing Plan

Add unit coverage for the registration behavior and a lightweight integration example that uses a custom metric.

**Unit Tests:**

- [x] I have added or updated unit tests for my change.
- [x] All unit tests pass locally.

```
% pytest tests/unittests/evaluation tests/unittests/cli

614 passed, 308 warnings in 12.74s
```

**Manual End-to-End (E2E) Tests:**

```
% pytest tests/integration/test_with_test_file.py::test_with_custom_metric

tests/integration/test_with_test_file.py .                               [100%]

1 passed, 12 warnings in 3.57s
```

### Checklist

- [x] I have read the [CONTRIBUTING.md](https://github.com/google/adk-python/blob/main/CONTRIBUTING.md) document.
- [x] I have performed a self-review of my own code.
- [x] I have commented my code, particularly in hard-to-understand areas.
- [x] I have added tests that prove my fix is effective or that my feature works.
- [x] New and existing unit tests pass locally with my changes.
- [x] I have manually tested my changes end-to-end.
- [x] Any dependent changes have been merged and published in downstream modules.

### Additional context

This change keeps `AgentEvaluator` behavior consistent with `adk eval` while avoiding CLI-layer dependencies.

`fork()` returns an isolated copy seeded with the source registry's contents, rather than a bare `MetricEvaluatorRegistry()`. The seeding is what keeps this backwards compatible: registering an `Evaluator` subclass on `DEFAULT_METRIC_EVALUATOR_REGISTRY` is the only way to plug one in, since an eval config can only name a scoring function. Callers who do that today (including ones replacing the evaluator behind a standard metric name) would otherwise silently fall back to the stock evaluator, with no error and a different score. Covered by `test_evaluate_eval_set_keeps_evaluators_from_the_default_registry`.

Co-authored-by: Yi Liu <yiliuly@google.com>
COPYBARA_INTEGRATE_REVIEW=#4344 from ftnext:agent-evaluator-support-custom-metric 3c844a9
PiperOrigin-RevId: 966416454
@adk-bot

adk-bot commented Aug 18, 2026

Copy link
Copy Markdown
Collaborator

Thank you @ftnext for your contribution! 🎉

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

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 Aug 18, 2026
@adk-bot adk-bot closed this Aug 18, 2026
@ftnext
ftnext deleted the agent-evaluator-support-custom-metric branch August 18, 2026 08:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

eval [Component] This issue is related to evaluation merged [Status] This PR is merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Support custom metrics in AgentEvaluator eval flow

5 participants