Repository navigation
refactor(py)!: BackgroundAction is the generic core; BackgroundModel subclasses it - #6757
Conversation
It's a handle over a background model's start, check and cancel actions, not an Action subclass, and start() takes a ModelRequest. The new name matches Go's ai.BackgroundModel and pairs with background_model() / define_background_model(), the way Tool pairs with tool().
There was a problem hiding this comment.
Code Review
This pull request performs a comprehensive rename of BackgroundAction to BackgroundModel across the Python codebase, including documentation, core packages, plugins, and tests. It also updates related helper functions, variables, and error messages to reflect this change consistently. As there are no review comments, I have no feedback to provide.
resolve_operation_model, create_deep_research_background_model and the Veo builders return a BackgroundModel, so they drop the old action name. stamp_operation_action keeps its name: it sets Operation.action.
…d-model-name # Conflicts: # py/packages/genkit/src/genkit/_ai/_model.py
…turn Operation[ModelResponse]
…d-model-name # Conflicts: # py/scripts/schema_to_typing.py
… schema generator
… a stored handle dict A stored handle is wire JSON. ai.check_operation / ai.cancel_operation take the dict back directly and validate it with Operation.model_validate, which stays extra='forbid'. A typo'd key or a missing id is INVALID_ARGUMENT with the field named. A ModelResponse dump gets its own message pointing at response.operation. The action still receives an Operation, so the published check/cancel input schema is unchanged. check_operation / cancel_operation are overloaded like JS checkOperation<T>: Operation[T] -> Operation[T], a stored dict -> Operation[ModelResponse].
|
FYI: In Go, a return &BackgroundModelAction{*core.NewBackgroundActionOf(api.ActionTypeBackgroundModel, name, &core.BackgroundActionOptions[*ModelRequest, *ModelResponse]{
Description: description,
Metadata: metadata,
InputSchema: inputSchema,
Check: checkFn,
Cancel: o.Cancel,
}, wrappedFn)}
|
…ors name the field and set a reason
# Conflicts: # py/packages/genkit-google-genai/src/genkit_google_genai/_models/_gemini.py # py/packages/genkit/src/genkit/_core/_action.py # py/packages/genkit/tests/genkit/ai/genkit_api_test.py # py/packages/genkit/tests/genkit/ai/typed_config_required_test.py
…subclasses it Go and JS split a generic core BackgroundAction[In, Out] (start/check/cancel over Operation[Out]) from the AI-layer BackgroundModel. Python had only the model-specific class, so core imported ModelRequest/ModelResponse and a non-model job had nowhere to live. - genkit/_core/_background.py: BackgroundAction[InT, OutT], a composite of start/check/cancel Actions (not an Action). InT/OutT are read off `class X(BackgroundAction[In, Out])`; instance state is written only in _bind, so a handle rebuilt from the registry matches the defined one. No model imports. Exported from genkit.plugin_api. - genkit/_ai/_background_model.py: BackgroundModel(BackgroundAction[ ModelRequest, ModelResponse]) with ModelInfo/config metadata, the start config check, and the "pass response.operation" guards. label and config_schema are derived from the start action. - ai.check_operation / cancel_operation are generic like JS checkOperation<T>: Operation[T] -> Operation[T], dict -> Operation[Any]. The handle's action key picks the class; model handles keep today's validation order, statuses, reasons and messages. - Plugin.init/resolve/list_actions accept a BackgroundAction. The registry registers all three member actions, and resolves /check-operation and /cancel-operation keys through a plugin that only resolves the start key. - Check/cancel actions carry the start metadata, like JS/Go.
|
agreed, and done in 044a312. |
Brings in #6757's split: BackgroundAction is the generic core, BackgroundModel subclasses it in genkit/_ai/_background_model.py. Conflict resolution ports this branch's class rewrite onto the new layout: - BackgroundAction(name, start, check, *, kind, cancel, metadata, description) and BackgroundModel(name, start, check, *, cancel, label, info, config_schema, metadata, description) build their actions and bind them; background_model() is gone, like the other lowercase builders. - fixed_json_schema for the Operation output type, input type and Operation schemas, now in the shared builder. - Plugin.list_actions is Sequence[Action | BackgroundAction[Any, Any]] (no ActionMetadata, per this branch); registry lists both.
The stack under this PR (#6697, #6698, #6699) landed on main as squash commits, so this merge takes main and reapplies only this PR's changes (e4e07ed..260e048) on top. Adapts to #6757/#6766/#6777: Veo uses _create_veo_background_model and ModelContext, _runtime_client is a @loop_local_client method built from per-loop client kwargs, and GENKIT_CLIENT_HEADER is now GENKIT_USER_AGENT.
Picks up #6757, #6766, #6777, #6775, #6764, #6709. - _handler.py / fastapi_test.py: keep main's genkit.web imports (ContextProvider, RequestData, error_body, read_body, wants_stream) and this PR's to_sse / _stream_to_sse routing. - _action.py: #6709 removed log_served_failure and served_stream_error_event; the core framer now logs with error_status (5xx traceback, 4xx warning) and frames {"error": error_body(e)}, same bytes as before. - _action.py: keep the framer's stdlib logger (noqa TID251), same rationale as the adapter handlers' pyproject exemption.
…onfig-strict Main landed #6697 (GenkitConfig, extra='forbid'), #6698 (flat Deep Research config, McpServerConfig TypedDict), #6757/#6766/#6777 (Model/BackgroundModel classes, ModelContext). Conflicts were only in this PR's files. Resolution: take main's version of _antigravity.py, _deep_research.py and _interactions_lyria.py, then re-apply this PR's net change on top: the `extra` field (EXTRA_DESCRIPTION), the class docstrings, and deep_merge(create_kwargs, extra) at create_interaction. The model() / background_model() builder and persist() removal from the PR are superseded by main's Model/BackgroundModel. Tests: keep main's test_lyria_rejects_unknown_config_fields; drop the PR's nested-key typo test (main's McpServerConfig deliberately passes unknown keys through, and nested file_search is now rejected by main's reject_nested_file_search with its own tests) and the PR's top-level `extra={'temperature': ...}` Lyria test (covered by the generation_config one).
BackgroundAction[In, Out]is the generic core for long-running jobs: a start, check and optional cancel action overOperation[Out].BackgroundModelsubclasses it asBackgroundAction[ModelRequest, ModelResponse]. This is the same split as Go and JS, and it mirrorsActionvsModel.Main Changes
BackgroundActionis generic core,BackgroundModelsubclasses it: Previously Python had one class, namedBackgroundActionbut model-only, withModelRequest/ModelResponsebaked into_core._core/_background.pynow has no model imports.BackgroundModellives in_ai/_background_model.pyand is exported fromgenkit.model.BackgroundActionis exported fromgenkit.plugin_apinext toAction. It's a composite of three registered actions, not anActionsubclass, the same as JS and Go.__init__, so state set only in__init__would be lost.__init_subclass__readsIn/OutoffBackgroundAction[ModelRequest, ModelResponse], and_bind(start, check, cancel)is the only writer of instance state.BackgroundModelreadslabelandconfig_schemaback from the start action. A looked-up model therefore validatesModelResponseand carries the same label as the one that was defined.check_operation/cancel_operationare generic, like JScheckOperation<T>:Operation[T]in givesOperation[T]out, and a stored dict givesOperation[Any]. The handle's action is resolved first and the output is validated against that action's output type. Model handles keep their exact errors, statuses and reasons:UNSUPPORTED_BY_MODELon cancel, the "pass response.operation" guard, andINVALID_ARGUMENTbefore any span.Plugin.init/resolve/list_actionsacceptAction | BackgroundAction. When the registry gets aBackgroundActionfor any of its keys (/background-model/{n},/check-operation/{n}/check,/cancel-operation/{n}/cancel), it registers all three and returns the one asked for, as Go'sRegisterdoes. Returning a bareActionstill works. The plugin-side cleanup is in feat(py)!: Model, BackgroundModel, Embedder, and Tool classes replace lowercase builders #6777.Operationis generic over its output:Operation(GenkitModel, Generic[Out])withOut = Any, matching JSOperation<O = any>and Gocore.Operation[Out]. BareOperationhas the same JSON schema as before;Operation[ModelResponse]validatesoutput. Check and cancel actions now carry the start action's metadata, as in JS.