From 0f051dd36a504b943aecdb046caff879c09db443 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Sat, 26 Apr 2025 13:51:04 +0200 Subject: [PATCH] Fix error when deserializing Gemini streams (#29470) Sometimes Gemini would report `Content` without a `parts` field. Release Notes: - Fixed a bug that would sometimes cause Gemini models to fail streaming their response. --- crates/google_ai/src/google_ai.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/google_ai/src/google_ai.rs b/crates/google_ai/src/google_ai.rs index 9f6045a288aff682e81c041343a98047d5a83671..66a3e6e9c68719b17f5293a9f48ba9d44ada8e8e 100644 --- a/crates/google_ai/src/google_ai.rs +++ b/crates/google_ai/src/google_ai.rs @@ -52,7 +52,10 @@ pub async fn stream_generate_content( if let Some(line) = line.strip_prefix("data: ") { match serde_json::from_str(line) { Ok(response) => Some(Ok(response)), - Err(error) => Some(Err(anyhow!(error))), + Err(error) => Some(Err(anyhow!(format!( + "Error parsing JSON: {:?}\n{:?}", + error, line + )))), } } else { None @@ -156,6 +159,7 @@ pub struct GenerateContentCandidate { #[derive(Debug, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Content { + #[serde(default)] pub parts: Vec, pub role: Role, }