Skip to content

feat(py): genkit.evaluator exports Evaluator and BatchEvaluator for plugin authors - #6689

Merged
huangjeff5 merged 19 commits into
mainfrom
feat/py-evaluator-builder
Oct 12, 2026
Merged

huangjeff5 merged 19 commits into
mainfrom
feat/py-evaluator-builder

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

genkit.evaluator now exports Evaluator and BatchEvaluator (Action subclasses) so plugins can construct evaluator actions once and return them directly across init(), resolve(), and list_actions() without spinning up a hidden Genkit() instance.

# --- BEFORE ---
# Plugins had no way to build an unregistered evaluator Action, so VertexAI
# instantiated a second Genkit() app inside the plugin just to call define_evaluator
# (which in dev mode spun up a duplicate Dev UI server).

# --- AFTER ---
from genkit.evaluator import BaseDataPoint, Evaluator, EvaluatorResponse, Score
from genkit.plugin_api import ActionKind, Plugin


async def allergen_check(datapoint: BaseDataPoint, options: object | None = None) -> EvaluatorResponse:
    ok = "peanut" not in str(datapoint.output).lower()
    return EvaluatorResponse(test_case_id=datapoint.test_case_id or "", evaluation=[Score(score=ok)])


class BistroPlugin(Plugin):
    name = "bistro"

    def __init__(self) -> None:
        self._allergen = Evaluator(
            "bistro/allergen_check",
            allergen_check,
            display_name="Allergen check",
            definition="Flags dishes that contain peanuts.",
        )

    # 1. Return the Evaluator instance directly in list_actions()
    async def list_actions(self):
        return [self._allergen]

    # 2. Return the Evaluator action on demand
    async def resolve(self, action_type, name):
        if action_type == ActionKind.EVALUATOR and name == "bistro/allergen_check":
            return self._allergen
        return None

Main Changes

  • Evaluator and BatchEvaluator subclass Action: Previously, plugins could only build evaluators through ai.define_evaluator(), forcing the Vertex AI plugin to instantiate a private Genkit() app on import (which launched a second reflection server in dev mode). Furthermore, exporting a lowercase from genkit.evaluator import evaluator function in Python shadows both the genkit.evaluator submodule and the local evaluator variable in caller code. We made Evaluator and BatchEvaluator PascalCase subclasses of Action[EvaluatorRequest, EvaluatorResponse, Never] whose __init__ wires up dataset iteration, per-datapoint spans, and Dev UI metadata, so plugins can return them directly from Plugin.init(), Plugin.resolve(), and Plugin.list_actions().
  • EvaluatorRequest and EvaluatorResponse naming: Renamed EvalRequest → EvaluatorRequest and EvalFnResponse → EvaluatorResponse across all packages and tests to establish consistent type naming with Go and Dart.
  • Plugin.list_actions() accepts Action (Evaluator / BatchEvaluator) directly: Previously, advertising an evaluator in Plugin.list_actions() required a separate metadata builder that duplicated the display name, definition, billing flag, and config schema already passed to Evaluator(...). Registry.list_actions() now converts Action instances via Action._action_metadata(), so Plugin.list_actions() can return Evaluator(...) and BatchEvaluator(...) instances directly without a public .action_metadata() step.
  • One input_schema and output_schema field on ActionMetadata: Previously, ActionMetadata exposed both input_schema / output_schema and input_json_schema / output_json_schema, and plugins setting input_json_schema had their schemas silently dropped when serialized to the Dev UI as inputSchema / outputSchema. ActionMetadata now defines only input_schema: dict[str, Any] | None and output_schema: dict[str, Any] | None, rejecting the retired *_json_schema fields and unserialized class objects at construction time.
  • App-level ai.define_evaluator is unchanged: Previously, define_evaluator and define_batch_evaluator built raw Action instances inline. They now instantiate Evaluator(...) and BatchEvaluator(...) and register them on ai.registry, returning the typed Evaluator / BatchEvaluator instance while keeping the exact same call signature for application code.
  • Reject synchronous callbacks at construction time: Previously, passing a synchronous def function to an evaluator succeeded at registration time and only failed when await fn(...) raised a TypeError on every dataset row during an eval run. Both Evaluator and BatchEvaluator now check inspect.iscoroutinefunction(fn) in __init__ and raise TypeError immediately when defined.

@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 introduces a new evaluator function to build evaluator actions without registering them immediately, allowing plugin authors to defer registration or resolve them dynamically. It also updates evaluator_action_metadata to require display names, definitions, and billing flags for the Dev UI. The Google GenAI plugin is refactored to adopt these new APIs, and corresponding tests are added. Feedback suggests validating that the provided evaluator function (fn) is an asynchronous coroutine function when building the evaluator action to prevent runtime errors.

@github-actions github-actions Bot added docs Improvements or additions to documentation python Python build labels Oct 8, 2026
@huangjeff5
huangjeff5 requested a review from apascal07 October 8, 2026 20:34
@huangjeff5
huangjeff5 marked this pull request as draft October 9, 2026 03:35
@huangjeff5
huangjeff5 marked this pull request as ready for review October 9, 2026 03:42
…stering it

Plugins can now return evaluators from init()/resolve() the same way they
return models and embedders. evaluator_action_metadata takes the display
name, definition and billed flag so a listed evaluator shows the same card
in the Dev UI as the built one. The Vertex plugin uses both, so starting it
no longer constructs a throwaway Genkit app (and an extra Dev UI reflection
server), and its evaluator listing carries real names and billed=true.
@huangjeff5
huangjeff5 force-pushed the feat/py-evaluator-builder branch from 7d7b35d to 7837b84 Compare October 9, 2026 03:59
…lder

# Conflicts:
#	py/packages/genkit-google-genai/src/genkit_google_genai/_evaluators/_evaluation.py
#	py/packages/genkit-google-genai/src/genkit_google_genai/_google.py
#	py/packages/genkit/src/genkit/_ai/_evaluator.py
… METRIC_CONFIGS directly

define_batch_evaluator uses _evaluator_metadata again instead of the
pre-#6669 block that wrote into the caller's nested evaluator dict, and
the builder keeps main's private name and config_schema type. Vertex
builds one evaluator per metric with no None path, and the duplicate
listing test folds its enum check into the existing one.
…ithout registering it

define_batch_evaluator is now batch_evaluator(...) plus register, the same
split as evaluator / define_evaluator, so a plugin can return a batch
evaluator from init() or resolve() without a registry.
@huangjeff5 huangjeff5 changed the title feat(py): genkit.evaluator.evaluator builds an evaluator without registering it feat(py): genkit.evaluator.Evaluator builds an evaluator without registering it Oct 12, 2026
@huangjeff5 huangjeff5 changed the title feat(py): genkit.evaluator.Evaluator builds an evaluator without registering it feat(py): genkit.evaluator exports Evaluator and BatchEvaluator for plugin authors Oct 12, 2026
…a-key' into feat/py-evaluator-builder

# Conflicts:
#	py/packages/genkit-ollama/src/genkit_ollama/plugin_api.py
#	py/packages/genkit/src/genkit/_ai/_evaluator.py
#	py/scripts/schema_to_typing.py
@huangjeff5
huangjeff5 merged commit 3bfb651 into main Oct 12, 2026
27 checks passed
@huangjeff5
huangjeff5 deleted the feat/py-evaluator-builder branch October 12, 2026 08:59
huangjeff5 added a commit that referenced this pull request Oct 12, 2026
Main's #6689 turned define_evaluator / define_batch_evaluator into the
Evaluator and BatchEvaluator classes. Port this PR's typed options onto
them: the options class from the per-row fn's annotation, config_schema
on the action, Dev UI customOptions, and an EvaluatorRequest for an
unannotated batch fn. EvalRequest[Cfg] is now EvaluatorRequest[Cfg] and
EvalFnResponse is EvaluatorResponse, following main.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build docs Improvements or additions to documentation python Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants