Repository navigation
feat(py)!: raise by default on failed generate turns (raise_on_error) - #7134
huangjeff5 wants to merge 3 commits into
Conversation
ai.generate, ai.generate_stream, Prompt.__call__ and Prompt.stream now
raise the error behind a failed turn instead of returning a
ModelResponse with finish_reason=FAILED and text ''. New keyword
raise_on_error: bool = True is the Python spelling of JS throwOnError;
pass raise_on_error=False to 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.
There was a problem hiding this comment.
Code Review
This pull request introduces a significant change for Genkit 1.0 where failed generate calls, stream calls, and prompt executions raise a GenkitError (or PublicError) by default instead of returning a boxed response. A new raise_on_error parameter is added to allow opting back into the boxed response behavior. The PR updates numerous tests, documentation, and samples to align with this new contract, and adds a dedicated test suite. Feedback on the changes suggests adding a defensive check in failure_exception to prevent a potential AttributeError when copying failure details to the exception object.
| if not isinstance(exc, PublicError) and not exc.details and failure is not None and failure.details: | ||
| exc.details = dict(failure.details) |
There was a problem hiding this comment.
To ensure robustness and adhere to defensive programming principles, we should check if exc has the details attribute before accessing or modifying it. If to_exception() ever returns a standard Python exception (e.g., RuntimeError or ValueError) or a custom exception that does not inherit from GenkitError or define details, accessing exc.details directly will raise an AttributeError, crashing the error-handling path and masking the original exception.
if not isinstance(exc, PublicError) and failure is not None and failure.details:\n if hasattr(exc, 'details') and not exc.details:\n exc.details = dict(failure.details)|
Closing: migrated to standalone repository genkit-ai/genkit-python (PR #43): genkit-ai/genkit-python#43 |
Flips
ai.generate()default behavior to raise on failed turns rather than returning a boxed error.Main Changes
generate_actionnow raises aGenkitErrorwhen a turn ends in a model, tool, or turn failure.raise_on_error: bool = Trueopt-out: Internal runtimes (Agent runtime, Dev UI/util/generate) passraise_on_error=Falseto preserve boxed inspection of.errorand partial.messages.