Repository navigation
fix(py/plugins/google-genai): widen API error mapping, detect blocked prompts - #6370
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces robust error translation from the google-genai SDK errors (APIError) to GenkitErrors, ensuring proper status mapping and retry delay propagation. It also adds handling for blocked prompts in Gemini models, returning a BLOCKED finish reason and including prompt feedback in the custom response payload. These changes are supported by extensive unit tests. A review comment suggests a defensive programming improvement in gemini.py to safely handle cases where the block reason might be parsed as a plain string instead of an Enum member.
5675dbd to
e02fc0c
Compare
e02fc0c to
3a1abef
Compare
cabljac
left a comment
There was a problem hiding this comment.
LGTM, thanks @hilariie. Rebase is clean and CI is green.
Checked against #6184: blocked prompts (stream and non-stream), empty candidates -> INTERNAL, empty stream -> UNAVAILABLE, body status disambiguating shared HTTP codes, and API_KEY_INVALID -> UNAUTHENTICATED all land here, and the 5xx / Retry-After / other-surface items are already on main from earlier PRs. Behaviour lines up with the Go plugin. No core API changes needed.
Agree on the BlockedReason thread - the SDK enum handles unknown values, so the getattr fallback isn't needed.
Fixes #6184
Summary
#6098, #6586 and #6710 already cover most of the issue: 5xx mapping, the streaming path, Retry-After and RetryInfo, and wrapping on veo, the embedder and the context cache. This covers the rest.
A prompt the service refuses comes back with no candidates and the reason in the prompt feedback. It now finishes BLOCKED, with the reason as the finish message and the feedback in
custom, instead of an empty response that reads as success. A reply with no candidates that was not blocked fails with INTERNAL, and a stream that ends without a single chunk fails with UNAVAILABLE.When several statuses share an HTTP code, the status named in the error body now picks one, so a 400 carrying FAILED_PRECONDITION is no longer INVALID_ARGUMENT. A body status that disagrees with the code does not override it: the Gemini API answers depleted prepay credit with a 402 that says RESOURCE_EXHAUSTED, and that must not be retried. A bad Gemini API key comes back as a 400 INVALID_ARGUMENT whose ErrorInfo reason is API_KEY_INVALID, and is now UNAUTHENTICATED. The Interactions transport (Deep Research, Lyria, Antigravity) keeps its own mapping.
Model listing in plugin init maps failures the same way as the model calls, so a bad key fails the first generate with a GenkitError instead of a raw ClientError.
test_generate_stream_text_responsemocked__aiter__on the method rather than on the returned stream, so it never streamed and asserted empty content. It now streams real text.Verification
Through the plugin and the real SDK transport: the Gemini API for the bad key, a local server for the rest.
prompt blocked: SAFETYprompt blocked: SAFETY