Skip to content

fix(py/plugins/google-genai): widen API error mapping, detect blocked prompts - #6370

Merged
hilariie merged 3 commits into
mainfrom
fix/py-google-genai-error-mapping
Oct 13, 2026
Merged

hilariie merged 3 commits into
mainfrom
fix/py-google-genai-error-mapping

Conversation

@hilariie

@hilariie hilariie commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

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_response mocked __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.

Case Before After
bad API key, model listing in init raw ClientError UNAUTHENTICATED
400 carrying FAILED_PRECONDITION INVALID_ARGUMENT FAILED_PRECONDITION
blocked prompt finish reason OTHER, empty text BLOCKED, prompt blocked: SAFETY
blocked prompt, streamed finish reason UNKNOWN, empty text BLOCKED, prompt blocked: SAFETY
reply with no candidates finish reason OTHER, empty text INTERNAL
stream that ends with no chunk finish reason UNKNOWN, empty text UNAVAILABLE

@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 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.

@hilariie
hilariie marked this pull request as ready for review September 22, 2026 17:33
@hilariie
hilariie force-pushed the fix/py-google-genai-error-mapping branch from 5675dbd to e02fc0c Compare September 23, 2026 12:26
@cabljac
cabljac self-requested a review September 30, 2026 10:28
@hilariie
hilariie force-pushed the fix/py-google-genai-error-mapping branch from e02fc0c to 3a1abef Compare October 13, 2026 11:46

@cabljac cabljac 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.

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.

@hilariie
hilariie merged commit 1a38cde into main Oct 13, 2026
27 checks passed
@hilariie
hilariie deleted the fix/py-google-genai-error-mapping branch October 13, 2026 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(py/plugins/google-genai): error mapping misses 5xx, Retry-After, and blocked prompts, and covers only gemini.py

2 participants