Skip to content

feat(py): box background operation before wrap_model - #6130

Open
huangjeff5 wants to merge 4 commits into
feat/py-bg-ops-1-lookupfrom
feat/py-bg-ops-2-box
Open

feat(py): box background operation before wrap_model#6130
huangjeff5 wants to merge 4 commits into
feat/py-bg-ops-1-lookupfrom
feat/py-bg-ops-2-box

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Summary

  • generate() on a background model returns a ModelResponse whose operation is the start handle and whose message is None. It does not poll until done.
  • generate_operation() is that same start and returns the Operation.
  • await next_fn(...) in wrap_model is always a ModelResponse, so a hook that reads .message is safe.
  • The post-model early-return is gated on ActionKind.BACKGROUND_MODEL, not operation is not None. A define_model LRO that also returns a tool request still runs the tool loop.
  • A wrap_model that rebuilds ModelResponse without forwarding operation is FAILED_PRECONDITION (pass through response.operation). It does not blame the model.
  • A wrap_generate rebuild that drops operation is the same status with a wrap_generate message. We do not split “middleware ate it” from “start never had one.”
  • ModelResponse.__eq__ includes operation, so two start handles with different job ids are not equal.

@google-cla

google-cla Bot commented Aug 20, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@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 support for background models (long-running operations) in Genkit. It updates run_one_iteration to handle Operation responses by wrapping them in a ModelResponse before middleware execution, and automatically sets the longRunning capability flag on background models during registration. Additionally, comprehensive tests are added to verify this behavior. The review feedback highlights two robustness improvements: handling cases where the raw response is a dictionary (e.g., over the wire) by deserializing it into an Operation or ModelResponse, and ensuring that if the model's supports field is a Pydantic model rather than a dictionary, it is correctly serialized instead of being overwritten.

Comment thread py/packages/genkit/src/genkit/_ai/_generate.py
Comment thread py/packages/genkit/src/genkit/_core/_background.py
@huangjeff5
huangjeff5 force-pushed the feat/py-bg-ops-2-box branch from 2af5484 to 26276a6 Compare August 21, 2026 21:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant