Check response status in all GeminiClient methods and add Error::is_retryable() #10

Closed
opened 2026-10-26 18:11:35 +00:00 by andreban · 0 comments
Owner

Summary

#4 added non-2xx status handling to GeminiClient::stream_generate_content: it parses GenerateContentResponseError into Error::VertexError and falls back to Error::HttpStatus { status, body } for non-JSON error bodies.

generate_content, count_tokens, text_embeddings and predict_image in src/client.rs still skip the status check and pass resp.text().await? straight into serde_json::from_str.

Impact

  1. When a load balancer or proxy returns a non-JSON 502/503/504 on :generateContent, generate_content returns Error::Serde(...) instead of Error::HttpStatus { status, body }.
  2. Downstream callers (VertexModel in devrel-pr-reviewer) maintain their own is_retryable_vertex_error(&gemini_rs::error::Error) classifier and tests — and a non-JSON 503 from generate_content is misclassified there as a non-retryable Error::Serde.

Proposed fix

  1. Extract a shared Self::error_for_status(response).await? helper on GeminiClient (matching geologia::GeminiClient::error_for_status) and call it from generate_content, stream_generate_content, count_tokens, text_embeddings and predict_image.

  2. Add pub fn is_retryable(&self) -> bool on gemini_rs::error::Error, following the Vertex AI API errors guidance. Return true for:

    • VertexError with code 429 / 500 / 503, or status RESOURCE_EXHAUSTED / INTERNAL / UNKNOWN / UNAVAILABLE (a 500 can carry either INTERNAL or UNKNOWN)
    • HttpStatus with 429 / 500 / 502 / 503 (502 covers non-JSON gateway errors from proxies)
    • timeout/connect HttpClient errors

    Don't treat 504 DEADLINE_EXCEEDED as retryable: the docs say it means the client deadline is shorter than the server default, and recommend removing or lengthening the deadline rather than retrying.

    This lets downstream callers drop their own Vertex error classification.

Notes for callers

The docs recommend retrying at most twice, with a minimum delay of 1 second and exponential backoff after that. is_retryable() should only classify errors; retry/backoff policy stays with the caller.

## Summary #4 added non-2xx status handling to `GeminiClient::stream_generate_content`: it parses `GenerateContentResponseError` into `Error::VertexError` and falls back to `Error::HttpStatus { status, body }` for non-JSON error bodies. `generate_content`, `count_tokens`, `text_embeddings` and `predict_image` in `src/client.rs` still skip the status check and pass `resp.text().await?` straight into `serde_json::from_str`. ## Impact 1. When a load balancer or proxy returns a non-JSON `502`/`503`/`504` on `:generateContent`, `generate_content` returns `Error::Serde(...)` instead of `Error::HttpStatus { status, body }`. 2. Downstream callers (`VertexModel` in `devrel-pr-reviewer`) maintain their own `is_retryable_vertex_error(&gemini_rs::error::Error)` classifier and tests — and a non-JSON `503` from `generate_content` is misclassified there as a non-retryable `Error::Serde`. ## Proposed fix 1. Extract a shared `Self::error_for_status(response).await?` helper on `GeminiClient` (matching `geologia::GeminiClient::error_for_status`) and call it from `generate_content`, `stream_generate_content`, `count_tokens`, `text_embeddings` and `predict_image`. 2. Add `pub fn is_retryable(&self) -> bool` on `gemini_rs::error::Error`, following the [Vertex AI API errors](https://docs.cloud.google.com/vertex-ai/generative-ai/docs/model-reference/api-errors) guidance. Return `true` for: - `VertexError` with code `429` / `500` / `503`, or status `RESOURCE_EXHAUSTED` / `INTERNAL` / `UNKNOWN` / `UNAVAILABLE` (a 500 can carry either `INTERNAL` or `UNKNOWN`) - `HttpStatus` with `429` / `500` / `502` / `503` (502 covers non-JSON gateway errors from proxies) - timeout/connect `HttpClient` errors Don't treat `504 DEADLINE_EXCEEDED` as retryable: the docs say it means the client deadline is shorter than the server default, and recommend removing or lengthening the deadline rather than retrying. This lets downstream callers drop their own Vertex error classification. ## Notes for callers The docs recommend retrying at most twice, with a minimum delay of 1 second and exponential backoff after that. `is_retryable()` should only classify errors; retry/backoff policy stays with the caller.
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: andreban/gemini-rs#10