use std::collections::HashMap;
use anyhow::{Context as _, bail};
use futures::{
StreamExt as _,
future::BoxFuture,
stream::{self, BoxStream},
};
use crate::{
datatype::Value,
message::{
Delta as _, FinishReason, Message, MessageDeltaOutput, MessageOutput, Part, PartDelta,
PartDeltaFunction, PartFunction, PartImage, Role, TokenUsage,
},
model::{InferLangModel, LangModelOptions, ThinkingEffort, api_error},
to_value,
tool::ToolDesc,
};
const API_BASE: &str = "https://generativelanguage.googleapis.com/v1beta/models";
#[derive(Clone)]
pub struct GeminiApiModel {
api_key: String,
model: String,
}
impl std::fmt::Debug for GeminiApiModel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("GeminiApiModel")
.field("api_key", &"<redacted>")
.field("model", &self.model)
.finish()
}
}
impl GeminiApiModel {
pub fn new(model: impl Into<String>, api_key: impl Into<String>) -> Self {
Self {
model: model.into(),
api_key: api_key.into(),
}
}
pub fn from_env(model: impl Into<String>) -> anyhow::Result<Self> {
let api_key = std::env::var("GEMINI_API_KEY")
.ok()
.filter(|v| !v.trim().is_empty())
.context("GEMINI_API_KEY is not set")?;
Ok(Self::new(model, api_key))
}
fn body(
&self,
messages: &[Message],
tools: &[ToolDesc],
options: &LangModelOptions,
) -> anyhow::Result<serde_json::Value> {
let mut body = to_value!({"contents": contents(messages)?});
let obj = body.as_object_mut().unwrap();
let system = messages
.iter()
.filter(|m| m.role == Role::System)
.flat_map(|m| m.contents.iter().filter_map(|p| p.as_text()))
.collect::<Vec<_>>()
.join("\n\n");
if !system.is_empty() {
obj.insert(
"systemInstruction".into(),
to_value!({"parts": [{"text": system}]}),
);
}
if !tools.is_empty() {
let declarations: Vec<Value> = tools
.iter()
.map(|tool| {
let mut out = to_value!({
"name": &tool.name,
"parametersJsonSchema": tool.parameters.clone(),
});
if let Some(desc) = &tool.description {
out.as_object_mut()
.unwrap()
.insert("description".into(), desc.into());
}
out
})
.collect();
obj.insert(
"tools".into(),
to_value!([{"functionDeclarations": declarations}]),
);
}
let mut config = Value::object_empty();
let config_obj = config.as_object_mut().unwrap();
if let Some(effort) = options.thinking_effort {
let thinking = if self.model.starts_with("gemini-2") {
let budget: i64 = match effort {
ThinkingEffort::Low => 2048,
ThinkingEffort::Medium => 8192,
ThinkingEffort::High => 24576,
};
to_value!({"thinkingBudget": budget, "includeThoughts": true})
} else {
let level = match effort {
ThinkingEffort::Low => "low",
ThinkingEffort::Medium => "medium",
ThinkingEffort::High => "high",
};
to_value!({"thinkingLevel": level, "includeThoughts": true})
};
config_obj.insert("thinkingConfig".into(), thinking);
}
if let Some(schema) = &options.output_schema {
config_obj.insert("responseMimeType".into(), "application/json".into());
config_obj.insert("responseJsonSchema".into(), schema.clone());
}
if !config_obj.is_empty() {
obj.insert("generationConfig".into(), config);
}
Ok(body.into())
}
}
impl InferLangModel for GeminiApiModel {
fn infer(
&self,
messages: &[Message],
tools: &[ToolDesc],
options: &LangModelOptions,
) -> BoxFuture<'static, anyhow::Result<MessageOutput>> {
let mut deltas = self.infer_stream(messages, tools, options);
Box::pin(async move {
let mut acc = MessageDeltaOutput::new();
while let Some(delta) = deltas.next().await {
acc = acc.accumulate(delta?)?;
}
acc.finish()
})
}
fn infer_stream(
&self,
messages: &[Message],
tools: &[ToolDesc],
options: &LangModelOptions,
) -> BoxStream<'static, anyhow::Result<MessageDeltaOutput>> {
let body = match self.body(messages, tools, options) {
Ok(body) => body,
Err(e) => return Box::pin(stream::once(async move { Err(e) })),
};
let url = format!("{API_BASE}/{}:streamGenerateContent?alt=sse", self.model);
let api_key = self.api_key.clone();
Box::pin(async_stream::try_stream! {
let response = send(&url, &api_key, &body).await?;
let mut saw_finish = false;
let mut bytes = response.bytes_stream();
let mut buf = Vec::new();
loop {
let chunk = bytes.next().await.transpose()?;
let at_eof = chunk.is_none();
match chunk {
Some(chunk) => buf.extend_from_slice(&chunk),
None => buf.extend_from_slice(b"\n\n"),
}
while let Some(data) = next_event(&mut buf) {
if let Some(out) = parse_chunk(&data)? {
saw_finish |= out.finish_reason.is_some();
yield out;
}
}
if at_eof {
break;
}
}
if !saw_finish {
Err(anyhow::anyhow!("the Gemini stream ended before the reply finished"))?;
}
})
}
}
static CLIENT: std::sync::LazyLock<reqwest::Client> = std::sync::LazyLock::new(|| {
reqwest::Client::builder()
.connect_timeout(std::time::Duration::from_secs(30))
.read_timeout(std::time::Duration::from_secs(300))
.build()
.expect("the HTTP client builds")
});
async fn send(
url: &str,
api_key: &str,
body: &serde_json::Value,
) -> anyhow::Result<reqwest::Response> {
const MAX_RETRIES: u32 = 3;
const MAX_WAIT_SECS: u64 = 10;
const RETRY_STATUSES: [u16; 5] = [500, 502, 503, 504, 529];
for attempt in 0..=MAX_RETRIES {
let response = CLIENT
.post(url)
.header("x-goog-api-key", api_key)
.header("accept", "text/event-stream")
.json(body)
.send()
.await?;
let status = response.status();
if status.is_success() {
return Ok(response);
}
if status.as_u16() == 429 && attempt < MAX_RETRIES {
let text = response.text().await.unwrap_or_default();
if !text.contains("google.rpc.RetryInfo") {
return Err(api_error(
status.as_u16(),
format!("Gemini API request failed with status {status}: {text}"),
));
}
let wait_secs = (1u64 << attempt).min(MAX_WAIT_SECS);
log::warn!(
"Rate limited (429). Retrying after {wait_secs}s (attempt {}/{MAX_RETRIES})",
attempt + 1
);
tokio::time::sleep(std::time::Duration::from_secs(wait_secs)).await;
continue;
}
if RETRY_STATUSES.contains(&status.as_u16()) && attempt < MAX_RETRIES {
let wait_secs = (1u64 << attempt).min(MAX_WAIT_SECS);
log::warn!(
"Request failed with status {status}. Retrying after {wait_secs}s (attempt {}/{MAX_RETRIES})",
attempt + 1
);
tokio::time::sleep(std::time::Duration::from_secs(wait_secs)).await;
continue;
}
let text = response.text().await.unwrap_or_default();
return Err(api_error(
status.as_u16(),
format!("Gemini API request failed with status {status}: {text}"),
));
}
unreachable!("retry loop returns or bails on every path")
}
fn next_event(buf: &mut Vec<u8>) -> Option<String> {
let (pos, len) = buf
.windows(2)
.position(|w| w == b"\n\n")
.map(|p| (p, 2))
.or_else(|| {
buf.windows(4)
.position(|w| w == b"\r\n\r\n")
.map(|p| (p, 4))
})?;
let raw: Vec<u8> = buf.drain(..pos + len).collect();
Some(
String::from_utf8_lossy(&raw)
.lines()
.filter_map(|line| line.strip_prefix("data:"))
.map(|data| data.strip_prefix(' ').unwrap_or(data))
.collect::<Vec<_>>()
.join("\n"),
)
}
fn parse_chunk(data: &str) -> anyhow::Result<Option<MessageDeltaOutput>> {
if data.is_empty() {
return Ok(None);
}
let val: Value = serde_json::from_str(data)?;
if let Some(error) = val.pointer("/error") {
bail!(
"Gemini stream error ({}): {}",
error
.pointer("/status")
.and_then(|v| v.as_str())
.unwrap_or("unknown"),
error
.pointer("/message")
.and_then(|v| v.as_str())
.unwrap_or("(no message)")
);
}
if let Some(reason) = val
.pointer("/promptFeedback/blockReason")
.and_then(|v| v.as_str())
{
bail!("Gemini blocked the prompt: {reason}");
}
let Some(candidate) = val.pointer("/candidates/0") else {
return Ok(None);
};
let mut out = MessageDeltaOutput::new();
out.delta.role = Some(Role::Assistant);
let mut call_signed = false;
for part in candidate
.pointer("/content/parts")
.and_then(|v| v.as_array())
.into_iter()
.flatten()
{
if let Some(sig) = part.pointer("/thoughtSignature").and_then(|v| v.as_str()) {
let is_call = part.pointer("/functionCall").is_some();
if !call_signed && (is_call || out.delta.signature.is_none()) {
out.delta.signature = Some(sig.to_owned());
call_signed = is_call;
}
}
let is_thought = part.pointer("/thought").and_then(|v| v.as_bool()) == Some(true);
if let Some(text) = part.pointer("/text").and_then(|v| v.as_str()) {
if is_thought {
out.delta.thinking = Some(out.delta.thinking.unwrap_or_default() + text);
} else if !text.is_empty() {
out.delta.contents.push(PartDelta::Text {
text: text.to_owned(),
});
}
} else if let Some(call) = part.pointer("/functionCall") {
let id = call
.pointer("/id")
.and_then(|v| v.as_str())
.map(str::to_owned)
.unwrap_or_else(|| {
format!("call-{}", &uuid::Uuid::new_v4().simple().to_string()[..12])
});
out.delta.tool_calls.push(PartDelta::Function {
id: Some(id),
function: PartDeltaFunction::WithParsedArgs {
name: call
.pointer("/name")
.and_then(|v| v.as_str())
.unwrap_or_default()
.to_owned(),
arguments: call.pointer("/args").cloned().unwrap_or(Value::Null),
},
});
}
}
out.finish_reason = candidate
.pointer("/finishReason")
.and_then(|v| v.as_str())
.map(|reason| match reason {
"STOP" => FinishReason::Stop {},
"MAX_TOKENS" => FinishReason::Length {},
other => FinishReason::Refusal {
reason: other.to_owned(),
},
});
if out.finish_reason.is_some() {
out.usage = val.pointer("/usageMetadata").and_then(parse_usage);
}
Ok(Some(out))
}
fn parse_usage(usage: &Value) -> Option<TokenUsage> {
let count = |key: &str| {
usage
.pointer(key)
.and_then(|v| v.as_integer())
.map(|v| v as u64)
};
usage.as_object()?;
Some(TokenUsage {
input_tokens: count("/promptTokenCount").unwrap_or(0),
output_tokens: count("/candidatesTokenCount").unwrap_or(0)
+ count("/thoughtsTokenCount").unwrap_or(0),
cache_creation_input_tokens: None,
cache_read_input_tokens: count("/cachedContentTokenCount"),
})
}
fn contents(messages: &[Message]) -> anyhow::Result<Value> {
let call_names: HashMap<&str, &str> = messages
.iter()
.flat_map(|m| m.tool_calls.iter().flatten())
.filter_map(|call| match call {
Part::Function { id, function } => Some((id.as_str(), function.name.as_str())),
_ => None,
})
.collect();
let mut contents: Vec<Value> = Vec::new();
for msg in messages {
match msg.role {
Role::System => {}
Role::Tool => {
let id = msg
.id
.as_deref()
.context("a tool message has no id to match its call")?;
let name = call_names
.get(id)
.with_context(|| format!("no tool call with id {id} precedes its result"))?;
let parts = function_response_parts(id, name, &msg.contents)?;
match contents.last_mut() {
Some(last) if is_function_responses(last) => {
let list = last
.as_object_mut()
.unwrap()
.get_mut("parts")
.unwrap()
.as_array_mut()
.unwrap();
list.extend(parts);
}
_ => contents.push(to_value!({"role": "user", "parts": parts})),
}
}
Role::User | Role::Assistant => {
let mut parts: Vec<Value> = msg
.contents
.iter()
.map(content_part)
.collect::<anyhow::Result<_>>()?;
let mut calls: Vec<Value> = msg
.tool_calls
.iter()
.flatten()
.filter_map(|call| match call {
Part::Function {
id,
function: PartFunction { name, arguments },
} => Some(to_value!({
"functionCall": {"id": id, "name": name, "args": arguments.clone()}
})),
_ => None,
})
.collect();
if let Some(sig) = &msg.signature {
let target = calls.first_mut().or(parts.last_mut());
if let Some(part) = target {
part.as_object_mut()
.unwrap()
.insert("thoughtSignature".into(), sig.into());
}
}
parts.extend(calls);
if parts.is_empty() {
continue;
}
let role = if msg.role == Role::Assistant {
"model"
} else {
"user"
};
contents.push(to_value!({"role": role, "parts": parts}));
}
}
}
Ok(Value::Array(contents))
}
fn is_function_responses(content: &Value) -> bool {
content.pointer("/role").and_then(|v| v.as_str()) == Some("user")
&& content
.pointer("/parts")
.and_then(|v| v.as_array())
.is_some_and(|parts| {
parts.iter().all(|p| {
p.pointer("/functionResponse").is_some() || p.pointer("/inlineData").is_some()
})
})
}
fn function_response_parts(id: &str, name: &str, contents: &[Part]) -> anyhow::Result<Vec<Value>> {
let mut texts = Vec::new();
let mut images = Vec::new();
for part in contents {
match part {
Part::Text { text } => texts.push(text.clone()),
Part::Value { value } => texts.push(match value {
Value::String(s) => s.clone(),
other => serde_json::to_string(other).unwrap_or_default(),
}),
Part::Image { image } => images.push(inline_image(image)?),
Part::Function { .. } => {}
}
}
let mut parts = vec![to_value!({
"functionResponse": {
"id": id,
"name": name,
"response": {"result": texts.join("\n")},
}
})];
parts.extend(images);
Ok(parts)
}
fn content_part(part: &Part) -> anyhow::Result<Value> {
Ok(match part {
Part::Text { text } => to_value!({"text": text}),
Part::Image { image } => inline_image(image)?,
Part::Value { value } => to_value!({"text": match value {
Value::String(s) => s.clone(),
other => serde_json::to_string(other).unwrap_or_default(),
}}),
Part::Function {
function: PartFunction { name, arguments },
..
} => to_value!({"functionCall": {"name": name, "args": arguments.clone()}}),
})
}
pub(super) fn inline_image(image: &PartImage) -> anyhow::Result<Value> {
let (mime_type, data) = match image {
PartImage::Embedded { mime_type, data } => (mime_type.clone(), data.base64()),
PartImage::Url { url } => {
let rest = url
.strip_prefix("data:")
.with_context(|| format!("Gemini takes no image URLs, only data URIs: {url}"))?;
let (meta, data) = rest
.split_once(',')
.with_context(|| format!("malformed data URI: {url}"))?;
let mime_type = meta
.strip_suffix(";base64")
.with_context(|| format!("only base64 data URIs are supported: {url}"))?;
(mime_type.to_owned(), data.to_owned())
}
};
Ok(to_value!({"inlineData": {"mimeType": mime_type, "data": data}}))
}