Repository navigation
feat(py): support raise_on_error in generate (default False) - #43
Closed
huangjeff5 wants to merge 8 commits into
Closed
huangjeff5 wants to merge 8 commits into
huangjeff5 wants to merge 8 commits into
Conversation
huangjeff5
force-pushed
the
feat/py-1.0-pr0-raise-on-error
branch
from
November 9, 2026 19:08
f4ae0d5 to
1f40c6f
Compare
huangjeff5
force-pushed
the
feat/py-1.0-pr0-raise-on-error
branch
3 times, most recently
from
November 9, 2026 22:03
a75b702 to
b055d2f
Compare
ai.generate, ai.generate_stream, Prompt.__call__ and Prompt.stream take a
raise_on_error keyword, the Python spelling of JS throwOnError. When on,
they raise the error behind a failed turn instead of returning a
ModelResponse with finish_reason=FAILED and text ''. A follow-up commit
sets the default to False, so existing callers keep the boxed response.
Why: Python now matches JS generate(), which throws by default. The boxed
return predates 0.13 and was never a deliberate Python-only contract. It
made every `return response.text` flow serve HTTP 200 {"result": ""} on
provider 401/403/404/429/503, masked PublicError from tools and RBAC
middleware, and hid max_turns exhaustion, schema failures and bad
resumes.
Mechanism:
- generate_action checks the result inside the `generate` span. If
response.error is set and raise_on_error is on, it raises
failure_exception(result): `_failure.to_exception()` when the turn was
boxed from an exception (PublicError stays PublicError), otherwise a
GenkitError built from response.error.
- Loop-classified failures have no `_failure`, so they raise from
response.error too: max_turns (ABORTED / MAX_TURNS_EXCEEDED), missing
tool (NOT_FOUND / TOOL_NOT_FOUND), and INVALID_OUTPUT (INTERNAL).
- A stream the caller closed (caller_stopped) still resolves .response to
ABORTED and does not raise.
- Streams raise from `async for` after the chunks already sent and from
`await stream.response` (Channel already re-raises the task error).
- Boxed on purpose: the agent runtime passes raise_on_error=False; the
Dev UI /util/generate action calls run_generate directly.
- generate_operation now raises the original error from its inner
generate call; its own error check stays as a fallback.
Unchanged: the Failure type (#6767), dropped-failure logging (#7032),
HTTP PublicError rules (non-public errors still serve 500 "Internal
Error"). Out of scope, separate PRs: output typing narrowing (PR2-15),
web handler cleanup (PR5-05..07), RST_STREAM / to_sse unboxing for
raise_on_error=False paths (PR3-02), error detail preservation (PR3-03).
Tests: tests that assert the boxed shape now pass raise_on_error=False;
new raise_on_error_test.py covers one case per symptom plus the boxed
callers. Docs: migration guide entry, generate/generate_stream and
middleware docstrings, Failure docstring, ollama sample.
Plugin suites (anthropic, openai, google-genai, ollama, bedrock, vertexai, middleware, a2ui, google-cloud) asserted the boxed FAILED response from ai.generate and prompt calls. They now opt into raise_on_error=False so they keep covering the boxed shape. The a2ui Surfaces docstring now says a strict-validation failure raises.
…and support deepcopy
Keep ai.generate, ai.generate_stream, and prompt calls returning boxed ModelResponse by default (finish_reason=FAILED, response.error set) to preserve 100% backward compatibility with 0.12/0.13 and align with the agent loop runtime contract for long-running workflows. Callers can opt into direct exception raising by passing raise_on_error=True.
Docstrings, the replace_on_chunk example, and the ollama sample still described the raise-by-default behavior from an earlier commit. Restore the sample from main, rewrite the docs for the shipped default, and drop the response.error check in generate_operation that raise_on_error=True already covers.
huangjeff5
force-pushed
the
feat/py-1.0-pr0-raise-on-error
branch
from
November 10, 2026 06:12
b070bee to
7dd8746
Compare
The Truncated structured output section and the Failure docstring still passed raise_on_error=False as if it were needed. It is the default; show raise_on_error=True as the opt-in instead.
The guide covers 0.12 -> 0.13. The 1.0 notes go to the release notes.
Collaborator
Author
|
Decision: Don't need to make this change actually. That flag is only there in JS for backwards compatability. Not sure this flag makes sense/makes a difference for background operations. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Callers can now pass
raise_on_error=Truetoai.generate()and prompt calls to raise exceptions on failures instead of inspectingresponse.error.Changes
raise_on_error: bool = False: WhenTrue,generate_actionraisesGenkitError(or preservesPublicError) on provider errors, tool exceptions, schema validation failures, and turn limits. WhenFalse(default), errors remain boxed inModelResponsewith 100% backward compatibility with 0.12/0.13.raise_on_error=True, iteration raises after any initial chunks yield, andawait stream.responseraises. Withraise_on_error=False, stream iteration completes andstream.responseyields the boxedFAILEDresponse.generate_operation()raises the original error: it already raised when a start failed. It now passesraise_on_error=True, so the exception carries the underlying message (e.g.RuntimeError: nope) instead of the scrubbedinternal error, and aPublicErrorstays aPublicError.except GenkitErrorstill catches both.Breaking changes
None.