stream_generate_content ends silently on a non-2xx response #3

Closed
opened 2026-10-18 08:58:34 +01:00 by andreban · 0 comments
Owner

Problem

GeminiClient::stream_generate_content doesn't check the HTTP status. When Vertex rejects a request (for example a 400 for an invalid function declaration), the response body is a plain JSON error, not SSE. response.event_stream() finds no data: lines, so the returned stream yields no items and ends cleanly. The caller can't tell a rejected request from an empty answer, and nothing is logged.

generate_content doesn't have this problem: it parses the body as GenerateContentResponse, whose Error variant becomes an Err.

Reproduction

Found while working on andreban/znotes#382 (2026-10-18, gemini-3.8-flash, v1beta1).

  1. Send stream_generate_content a request with a function declaration that Vertex rejects, for example parameters: {"$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "properties": {"q": {"type": "string"}}}.
  2. Vertex answers HTTP 400 with {"error": {"code": 400, "message": "Invalid JSON payload received. Unknown name \"$schema\" at 'tools[0].function_declarations[0].parameters': Cannot find field.", "status": "INVALID_ARGUMENT"}}.
  3. The stream ends without yielding anything. In znotes, /api/agent/chat then returns HTTP 200 with an empty body and logs nothing, so the user sees an empty turn.

Cause

In src/client.rs, stream_generate_content does:

let response = self.client.post(&endpoint_url).bearer_auth(access_token).json(request).send().await?;
Ok(response.event_stream().filter_map(Self::parse_sse_event))

The status is never checked before the body is read as SSE.

Suggested fix

Before building the event stream, check response.status(). On a non-success status, read the body and return an Err, parsing it as GenerateContentResponseError when possible (as generate_content does) and falling back to the status and raw text. Add a test for a 400 JSON body.

## Problem `GeminiClient::stream_generate_content` doesn't check the HTTP status. When Vertex rejects a request (for example a 400 for an invalid function declaration), the response body is a plain JSON error, not SSE. `response.event_stream()` finds no `data:` lines, so the returned stream yields no items and ends cleanly. The caller can't tell a rejected request from an empty answer, and nothing is logged. `generate_content` doesn't have this problem: it parses the body as `GenerateContentResponse`, whose `Error` variant becomes an `Err`. ## Reproduction Found while working on andreban/znotes#382 (2026-10-18, `gemini-3.8-flash`, `v1beta1`). 1. Send `stream_generate_content` a request with a function declaration that Vertex rejects, for example `parameters: {"$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "properties": {"q": {"type": "string"}}}`. 2. Vertex answers HTTP 400 with `{"error": {"code": 400, "message": "Invalid JSON payload received. Unknown name \"$schema\" at 'tools[0].function_declarations[0].parameters': Cannot find field.", "status": "INVALID_ARGUMENT"}}`. 3. The stream ends without yielding anything. In znotes, `/api/agent/chat` then returns HTTP 200 with an empty body and logs nothing, so the user sees an empty turn. ## Cause In `src/client.rs`, `stream_generate_content` does: ```rust let response = self.client.post(&endpoint_url).bearer_auth(access_token).json(request).send().await?; Ok(response.event_stream().filter_map(Self::parse_sse_event)) ``` The status is never checked before the body is read as SSE. ## Suggested fix Before building the event stream, check `response.status()`. On a non-success status, read the body and return an `Err`, parsing it as `GenerateContentResponseError` when possible (as `generate_content` does) and falling back to the status and raw text. Add a test for a 400 JSON body.
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#3