Repository navigation
feat(py)!: one config class per action; typed options for embedders - #6714
Conversation
- 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.
There was a problem hiding this comment.
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.
…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.
fc2f1eb to
d948ef8
Compare
…into feat/py-config-class
…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.
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.
|
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. |
|
@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. |
…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.
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
None.Decisions
ModelRequest[Cfg]orEmbedRequest[Cfg]), fromconfig_schema=, or from both. If both are given and disagree, definition raisesINVALID_ARGUMENT. An annotation alone derivesconfig_schema.EmbedRequest[Cfg]uses the same type-parameter guard and before-validator asModelRequest[Cfg]. A typed fn gets a validated instance, and missingoptionsbecomes the class default instance. BareEmbedRequestkeepsoptions: dict[str, Any](from fix(py): embedders always receive an options dict #6668).EmbedRequestobject when called from Dev UI or CLI runs.Breaking changes
config_schemadisagree raises at define time.EmbedRequestobjects from raw Dev UI / CLI runs, not raw dicts.Action.config_schemaanddefine_embedder(config_schema=).