fix: Register custom metrics from eval config in LocalEvalSampler and centralize registration#6424
Open
ItsMacto wants to merge 1 commit into
Open
fix: Register custom metrics from eval config in LocalEvalSampler and centralize registration#6424ItsMacto wants to merge 1 commit into
ItsMacto wants to merge 1 commit into
Conversation
… centralize registration
ItsMacto
force-pushed
the
fix-custom-metrics-registration
branch
from
July 17, 2026 18:52
3f294b8 to
ce20918
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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):
2. Or, if no issue exists, describe the change:
Problem:
Custom metrics declared in
eval_config.custom_metricsare only registered into the metric evaluator registry by an inline loop inside theadk evalCLI command. Theadk optimizeCLI and programmaticLocalEvalSamplerusers never run that loop, so any evaluation involving a custom metric fails withNotFoundError: <metric_name> not found in registry. Notebook users currently have to copy the CLI internals to work around it.Solution:
Centralize the registration in a new helper and call it from
LocalEvalSampler.__init__, as proposed in the issue:register_custom_metrics_from_config(eval_config, metric_evaluator_registry=None)inevaluation/metric_evaluator_registry.py. It reproduces the CLI loop's behavior exactly: the dict key overridesmetric_info.metric_name, entries without ametric_infoget a default one with a [0.0, 1.0] interval, and each entry registers_CustomMetricEvaluator. It registers intoDEFAULT_METRIC_EVALUATOR_REGISTRYunless a registry is passed, and returns the registry it used.LocalEvalSampler.__init__calls the helper, so bothadk optimizeand direct construction get custom metrics registered up front, and a bad custom function path surfaces at construction time rather than mid run. The sampler keeps the returned registry and passes it explicitly toLocalEvalService, mirroring what the eval CLI does.adk evalcommand now calls the helper instead of the inline loop. No behavior change on that path.get_default_metric_infomoved fromcli/cli_eval.pyintoevaluation/metric_evaluator_registry.py, since the evaluation package cannot import from cli. Its only caller was the removed loop; anything importing it from the old cli location would need to switch to the new one.Testing Plan
Unit Tests:
New tests in
tests/unittests/evaluation/test_metric_evaluator_registry.pycover the helper directly: a providedmetric_infois registered under the dict key (with the original name absent from the registry), a missingmetric_infoproduces the default [0.0, 1.0] interval with the description propagated, an eval config without custom metrics is a no-op, and the helper defaults toDEFAULT_METRIC_EVALUATOR_REGISTRYwhen no registry is passed. A new test intests/unittests/optimization/local_eval_sampler_test.pycovers the fix end to end at the unit level: constructing aLocalEvalSamplerwhose eval config declares a custom metric makes that metric resolvable from the registry.A wider sweep over
tests/unittests/evaluation,tests/unittests/optimizationand the CLI eval tests produces the same results as main (no new failures).pyink --checkandisort --check-onlyare clean on all touched files, andmypyreports no new errors compared to main.Manual End-to-End (E2E) Tests:
I ran
adk optimizeend to end against a minimal agent (gemini-2.5-flash, 2 eval cases) whose sampler config declares a custom metric:On current main the run crashes with the error from the issue:
which then surfaces as
TypeError: type NoneType doesn't define __round__ methodin_extract_eval_dataand exit code 1.With this branch, the identical command completes with exit code 0, no registry errors, the custom metric scoring both eval cases:
One note for anyone reproducing this: the custom metric module must be importable at evaluation time (for example via
PYTHONPATH); the agent loader does not put the agents dir onsys.pathfor metric functions. That applies equally before and after this change.Checklist
Additional context
The registry's
_registrydict is a class attribute, so allMetricEvaluatorRegistryinstances share one mapping. This PR does not change that; the new tests clean up the entries they register so the shared state stays untouched across tests.