Skip to content

feat(py)!: one config class per action; typed options for embedders - #6714

Merged
huangjeff5 merged 18 commits into
mainfrom
feat/py-config-class
Oct 29, 2026
Merged

huangjeff5 merged 18 commits into
mainfrom
feat/py-config-class

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Each model and embedder now has one config class, decided when you define it. Embedders get the same typed path models already had: annotate the fn with EmbedRequest[Cfg] and it receives a validated instance.

Evaluators originally included in this PR were removed because evaluators are excluded from the 1.0 cut and removed in #6947.

Stack

  1. fix(py): embedders always receive an options dict #6668: Embedder fns always get options as a dict, never None.
  2. feat(py)!: one config class per action; typed options for embedders #6714: Annotate a config class to get a typed instance instead. (this PR)
  3. feat(py)!: config pipeline: definition defaults, one merge, None is a value #6712: The values are filled the same way from every entry point.

Decisions

  • One config class per action, from the definition: The class comes from the fn's annotation (ModelRequest[Cfg] or EmbedRequest[Cfg]), from config_schema=, or from both. If both are given and disagree, definition raises INVALID_ARGUMENT. An annotation alone derives config_schema.
  • Embedders get the typed path models already had: EmbedRequest[Cfg] uses the same type-parameter guard and before-validator as ModelRequest[Cfg]. A typed fn gets a validated instance, and missing options becomes the class default instance. Bare EmbedRequest keeps options: dict[str, Any] (from fix(py): embedders always receive an options dict #6668).
  • Unannotated fns still get a request object: An unannotated embedder fn receives an EmbedRequest object when called from Dev UI or CLI runs.

Breaking changes

  • Defining a model, background model, or embedder whose annotation class and config_schema disagree raises at define time.
  • Unannotated embedder fns receive EmbedRequest objects from raw Dev UI / CLI runs, not raw dicts.
  • Added Action.config_schema and define_embedder(config_schema=).

- EmbedRequest.options and genkit.evaluator.EvalRequest.options are
  dict[str, Any] with a None -> {} validator, so plugins read options
  without a None guard and the input schema says type: object.
- A BaseModel passed as options on the wire is dumped to the fields it
  set, the same as ai.embed / ai.evaluate config= and an untyped model
  fn's config.
- Evaluator wrappers no longer reassign req.options.
- Plugins and samples drop their None guards on options.

@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 unifies the configuration pipeline for models, embedders, and evaluators by allowing them to define and validate custom configuration schemas (via config_schema or generic request annotations like EmbedRequest[Cfg] and EvalRequest[Cfg]). It introduces generic request types (EmbedRequest and EvalRequest), validates that configuration arguments are Pydantic BaseModel subclasses, and ensures that the runtime values match the declared schemas. Additionally, helper functions like with_request_annotation are added to wrap unannotated handlers, and tests are introduced to verify these configuration validation behaviors. I have no feedback to provide as there are no review comments.

@huangjeff5
huangjeff5 added this pull request to stack #6715 October 9, 2026 10:00
…ions-dict

# Conflicts:
#	py/packages/genkit/src/genkit/evaluator/__init__.py
#	py/packages/genkit/tests/genkit/ai/embedding_test.py
#	py/packages/genkit/tests/genkit/veneer/veneer_test.py
… config rule

ModelRequest, EmbedRequest and EvalRequest take one type parameter with the
same __class_getitem__ guard and before-validator; only the field name
differs (config vs options). Bare ModelRequest config defaults to Any. Bare
EmbedRequest / EvalRequest options default to dict[str, Any], which is what
an untyped fn receives; EmbedderFn / BatchEvaluatorFn take EmbedRequest[Any]
/ EvalRequest[Any] so a typed fn still fits them. Typed options validate into
the class, and a missing options becomes the class with its defaults.
Action resolves the config class once, from config_schema= and/or the fn's
ModelRequest[Cfg] / EmbedRequest[Cfg] / EvalRequest[Cfg] annotation, for
models, background models, embedders and evaluators.

- Annotation only: config_schema is derived from it, so the Dev UI form
  (customOptions) and generate's call-time check see the class.
- Both given and not the same class: raise INVALID_ARGUMENT at define time.
- embedder()/define_embedder take config_schema=.
- Per-row evaluators: the class comes from the fn's second parameter
  (options: JudgeConfig); the stepper's input is EvalRequest[JudgeConfig].
- Unannotated embedder and batch evaluator fns get a request object from a
  Dev UI / CLI run instead of the raw JSON dict.
- EVALUATOR joins known_annotation_names, and the wrong-class boundary error
  names options for embedders and evaluators.

generate_request_construction_test now expects the call-time error for
annotation-only typed models: a bad value or a foreign config class raises
before the model runs instead of reaching the boundary.
@huangjeff5
huangjeff5 force-pushed the feat/py-config-class branch from fc2f1eb to d948ef8 Compare October 12, 2026 06:58
…main merge

The main merge left this PR's EvalRequest unused: code imported main's
wire EvaluatorRequest, whose options is Any | None, so Dev UI and CLI
evaluator runs got None again. Rename the dict-typed class to
EvaluatorRequest and import it everywhere. Drop the scratch scripts the
merge committed at the repo root.
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.
… BatchEvaluatorRequest[Cfg]

Every action fn takes one request. A per-row evaluator was
(row, options: Cfg); it is now (req: EvaluatorRequest[Cfg]) with
req.row and req.options. A batch evaluator takes
BatchEvaluatorRequest[Cfg] (req.dataset, req.options).

EvaluatorRequest is now a Python-only per-row request that
define_evaluator builds from the action input for each row. The wire
EvalRequest is generated as BatchEvaluatorRequest and the hand-written
generic shadows it, like EmbedRequest. The action input JSON schema
keeps its keys and its EvaluatorRequest title.

The old two-parameter per-row form raises a TypeError that shows the
new signature. Built-in evaluators, the Vertex evaluators, the sample,
docs and tests move to the new forms.
@github-actions github-actions Bot added the docs Improvements or additions to documentation label Oct 12, 2026
Reverts evaluator changes from this PR because evaluators are excluded from
the 1.0 cut and will be removed in #6947. Only embedders receive the options-dict
guarantee here.
Reverts evaluator changes from this PR because evaluators are excluded from
the 1.0 cut and will be removed in #6947. Embedders and models receive typed
config class support.
@huangjeff5

Copy link
Copy Markdown
Contributor Author

Reverted evaluator-related changes from this PR. Evaluators are excluded from the 1.0 cut and will be removed in #6947, so typed options and the config class pipeline here apply strictly to models and embedders.

@huangjeff5 huangjeff5 changed the title feat(py)!: one config class per action; typed options for embedders and evaluators feat(py)!: one config class per action; typed options for embedders Oct 29, 2026
@huangjeff5

Copy link
Copy Markdown
Contributor Author

@apascal07 Reverted the evaluator changes from this PR since evaluators are excluded from the 1.0 cut and being removed in #6947. This PR now strictly scopes typed config classes to models and embedders.

Base automatically changed from fix/py-embed-eval-options-dict to main October 29, 2026 21:07
@huangjeff5
huangjeff5 merged commit 2b45543 into main Oct 29, 2026
25 checks passed
@huangjeff5
huangjeff5 deleted the feat/py-config-class branch October 29, 2026 21:14
huangjeff5 added a commit that referenced this pull request Oct 30, 2026
…types-dump

Conflict resolutions:
- py/packages/genkit/src/genkit/_core/_base.py: GenkitModel.model_config keeps
  main's validate_by_name/validate_by_alias (replacing populate_by_name) plus
  this PR's serialize_by_alias=True and ser_json_bytes='base64'. Main's new
  GenkitConfig class kept as is. Dropped the _keep_none_fields hook: its only
  user, GenerationCommonConfig, is a GenkitConfig on main (#6714), which the
  GenkitModel wrap serializer never touches, so it already keeps explicit None.
- py/scripts/schema_to_typing.py: took main's GenerationUsage.from_messages
  emission; dropped this PR's GenerationCommonConfig _keep_none_fields emission
  for the same reason.
- py/packages/genkit/src/genkit/_core/_typing.py: regenerated with
  ./py/bin/generate_schema_typing; now identical to origin/main.
  ./py/bin/generate_schema_typing --ci passes.

Post-merge fixes:
- nested_dump_test.py: model_ref(...) -> ModelRef(name=...) (#6766 removed
  the model_ref builder).
- flask_nested_types_test.py: type: ignore on @app.post, the same Flask
  T_route async-view error main's flask_test.py already carries.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation python Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants