Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(agent): keep Gemini thought signatures across tool-call steps (#6981)

Wolf Mermelsteind6f2c4e8f973
diff --git a/crates/agent/src/completion.rs b/crates/agent/src/completion.rs
index 7c021d59aa..e5e2015947 100644
--- a/crates/agent/src/completion.rs
+++ b/crates/agent/src/completion.rs
@@ -45,6 +45,15 @@ pub async fn complete<M: ToString>(
)
.await?
}
+ RoutedModel::Gemini(m) => {
+ prompt_once(
+ TracedModel::new(m.completion(), telemetry.clone()),
+ system_prompt,
+ user_message,
+ telemetry,
+ )
+ .await?
+ }
RoutedModel::OpenAiChatCompletions(m) => {
prompt_once(
TracedModel::new(m.completion(), telemetry.clone()),
@@ -93,6 +102,15 @@ pub async fn complete_with_history<M: ToString>(
)
.await?
}
+ RoutedModel::Gemini(m) => {
+ prompt_with_history(
+ TracedModel::new(m.completion(), telemetry.clone()),
+ system_prompt,
+ messages,
+ telemetry,
+ )
+ .await?
+ }
RoutedModel::OpenAiChatCompletions(m) => {
prompt_with_history(
TracedModel::new(m.completion(), telemetry.clone()),
diff --git a/crates/agent/src/model/gemini.rs b/crates/agent/src/model/gemini.rs
new file mode 100644
index 0000000000..e4c426ff5c
--- /dev/null
+++ b/crates/agent/src/model/gemini.rs
@@ -0,0 +1,48 @@
+use crate::model::types::Model;
+use rig_core::{client::CompletionClient, providers::gemini};
+use std::sync::Arc;
+
+/// A Gemini model bound to the native GenerateContent client that serves it.
+///
+/// Gemini 3 requires `thought_signature` on function-call parts of the current
+/// turn. The OpenAI-compatible Chat Completions adapter drops that field, so
+/// Google ids route here rather than through
+/// [`super::openai::OpenAiChatCompletionsModel`].
+pub struct GeminiModel<'a> {
+ model: Model<'a>,
+ client: Arc<gemini::Client>,
+}
+
+impl<'a> GeminiModel<'a> {
+ /// Bind `model` to the client that serves it.
+ pub fn new(model: Model<'a>, client: Arc<gemini::Client>) -> Self {
+ Self { model, client }
+ }
+
+ /// The routed id this model was bound to.
+ pub fn model(&self) -> &Model<'a> {
+ &self.model
+ }
+
+ /// The rig completion model for this id. The id is passed verbatim to the
+ /// Gemini API.
+ pub fn completion(&self) -> gemini::completion::CompletionModel {
+ self.client.completion_model(self.model.name().to_string())
+ }
+
+ /// Ask Gemini to include thought summaries so reasoning deltas reach the
+ /// stream. Thinking itself is on by default for Gemini 3; this only asks
+ /// for the summarized thoughts, not a thinking-level override.
+ pub fn thinking_params(&self) -> Option<serde_json::Value> {
+ Some(serde_json::json!({
+ "generation_config": {
+ "thinking_config": {
+ "include_thoughts": true
+ }
+ }
+ }))
+ }
+}
+
+#[cfg(test)]
+mod test;
diff --git a/crates/agent/src/model/gemini/test.rs b/crates/agent/src/model/gemini/test.rs
new file mode 100644
index 0000000000..285c48b785
--- /dev/null
+++ b/crates/agent/src/model/gemini/test.rs
@@ -0,0 +1,22 @@
+use super::*;
+use crate::model::types::Model;
+
+#[test]
+fn thinking_params_ask_for_thought_summaries() {
+ let model = Model::try_from("google/gemini-3.8-flash").expect("routable id");
+ let client = gemini::Client::builder()
+ .api_key("test-google-key")
+ .build()
+ .expect("gemini client");
+ let gemini = GeminiModel::new(model, Arc::new(client));
+ assert_eq!(
+ gemini.thinking_params(),
+ Some(serde_json::json!({
+ "generation_config": {
+ "thinking_config": {
+ "include_thoughts": true
+ }
+ }
+ }))
+ );
+}
diff --git a/crates/agent/src/model/mod.rs b/crates/agent/src/model/mod.rs
index 519d2b5404..ce76ee7459 100644
--- a/crates/agent/src/model/mod.rs
+++ b/crates/agent/src/model/mod.rs
@@ -5,5 +5,6 @@ mod predefined_model;
pub(crate) mod types;
pub use predefined_model::*;
mod anthropic;
+mod gemini;
mod openai;
pub mod router;
diff --git a/crates/agent/src/model/router.rs b/crates/agent/src/model/router.rs
index 478bdc3d8e..665921ecf2 100644
--- a/crates/agent/src/model/router.rs
+++ b/crates/agent/src/model/router.rs
@@ -4,9 +4,10 @@
//! provider fan-out so the rest of the crate stays provider-agnostic:
//!
//! - [`RoutedModel`] — the routed id bound to its provider client. One arm per
-//! wire protocol: Anthropic-native, OpenAI Responses, and OpenAI-compatible
-//! Chat Completions. Compatible providers live in a data registry keyed by
-//! name, so adding one is [`with_openai_provider`](ModelRouter::with_openai_provider).
+//! wire protocol: Anthropic-native, Gemini GenerateContent, OpenAI Responses,
+//! and OpenAI-compatible Chat Completions. Compatible providers live in a
+//! data registry keyed by name, so adding one is
+//! [`with_openai_provider`](ModelRouter::with_openai_provider).
//! - [`ProviderAgent`] — a built rig agent, with the same arms. Its
//! [`run_stream`](ProviderAgent::run_stream) matches internally, so callers
//! (e.g. `agent_loop`) hold one type and never fan out.
@@ -28,12 +29,13 @@ use rig_agent::streaming::StreamingPrompt;
use rig_agent::tool::server::ToolServerHandle;
use rig_core::completion::{CompletionModel, GetTokenUsage};
use rig_core::message::Message;
-use rig_core::providers::{anthropic, openai};
+use rig_core::providers::{anthropic, gemini, openai};
use rig_core::streaming::StreamedAssistantContent;
use tracing::Instrument as _;
use super::PredefinedModel;
use super::anthropic::AnthropicModel;
+use super::gemini::GeminiModel;
use super::openai::{OpenAiChatCompletionsModel, OpenAiResponsesModel};
use super::types::Model;
use crate::error::AgentError;
@@ -66,17 +68,15 @@ const CEREBRAS_BASE_URL: &str = "https://api.cerebras.ai/v1";
const FIREWORKS_PROVIDER: &str = "fireworks";
/// Fireworks inference endpoint (OpenAI-compatible Chat Completions API).
const FIREWORKS_BASE_URL: &str = "https://api.fireworks.ai/inference/v1";
-/// Provider segment Google Gemini is registered under (OpenAI-compatible Chat
-/// Completions).
+/// Provider segment Google Gemini is registered under (native GenerateContent).
const GOOGLE_PROVIDER: &str = "google";
-/// Gemini's OpenAI-compatible Chat Completions endpoint. The native
-/// `generativelanguage` API is a different wire format and is not used here.
-const GOOGLE_BASE_URL: &str = "https://generativelanguage.googleapis.com/v1beta/openai";
/// A routed model id bound to the provider client that serves it.
pub(crate) enum RoutedModel<'a> {
/// A model on Anthropic's native API.
Anthropic(AnthropicModel<'a>),
+ /// A model on Gemini's native GenerateContent API.
+ Gemini(GeminiModel<'a>),
/// A model on the OpenAI-compatible Chat Completions API.
OpenAiChatCompletions(OpenAiChatCompletionsModel<'a>),
/// A model on OpenAI's Responses API.
@@ -88,6 +88,7 @@ impl<'a> RoutedModel<'a> {
pub(crate) fn provider(&self) -> &str {
match self {
RoutedModel::Anthropic(m) => m.model().provider(),
+ RoutedModel::Gemini(m) => m.model().provider(),
RoutedModel::OpenAiChatCompletions(m) => m.model().provider(),
RoutedModel::OpenAiResponses(m) => m.model().provider(),
}
@@ -97,6 +98,7 @@ impl<'a> RoutedModel<'a> {
pub(crate) fn model_name(&self) -> &str {
match self {
RoutedModel::Anthropic(m) => m.model().name(),
+ RoutedModel::Gemini(m) => m.model().name(),
RoutedModel::OpenAiChatCompletions(m) => m.model().name(),
RoutedModel::OpenAiResponses(m) => m.model().name(),
}
@@ -125,6 +127,18 @@ impl<'a> RoutedModel<'a> {
telemetry,
))
}
+ RoutedModel::Gemini(m) => {
+ let thinking = m.thinking_params();
+ ProviderAgent::Gemini(build_agent(
+ m.completion(),
+ thinking,
+ handle,
+ system_prompt,
+ max_turns,
+ max_tokens,
+ telemetry,
+ ))
+ }
RoutedModel::OpenAiChatCompletions(m) => {
let thinking = m.thinking_params();
ProviderAgent::OpenAiChatCompletions(build_agent(
@@ -155,13 +169,15 @@ impl<'a> RoutedModel<'a> {
/// A built rig agent bound to the provider serving the session's model.
///
-/// The two arms are different concrete `Agent<M>` types; [`run_stream`] hides
+/// The provider arms are different concrete `Agent<M>` types; [`run_stream`] hides
/// that behind one concrete [`ChatCompletionStream`], so callers never match.
///
/// [`run_stream`]: ProviderAgent::run_stream
pub(crate) enum ProviderAgent {
/// An agent over Anthropic's native completion model.
Anthropic(Agent<TracedModel<anthropic::completion::CompletionModel>>),
+ /// An agent over Gemini's native GenerateContent model.
+ Gemini(Agent<TracedModel<gemini::completion::CompletionModel>>),
/// An agent over the OpenAI Chat Completions model.
OpenAiChatCompletions(Agent<TracedModel<openai::completion::CompletionModel>>),
/// An agent over the OpenAI Responses model.
@@ -204,6 +220,21 @@ impl ProviderAgent {
)
.await
}
+ ProviderAgent::Gemini(agent) => {
+ drive_stream(
+ agent,
+ prompt,
+ history,
+ max_turns,
+ inputs,
+ recorder,
+ usage_ctx,
+ model,
+ request_context.clone(),
+ telemetry,
+ )
+ .await
+ }
ProviderAgent::OpenAiChatCompletions(agent) => {
drive_stream(
agent,
@@ -256,14 +287,15 @@ impl ProviderAgent {
/// Routes model api-id strings to the provider client that serves them.
///
-/// Holds native Anthropic and OpenAI Responses clients plus a registry of
-/// OpenAI-compatible Chat Completions clients keyed by provider name. The
-/// built-in [`OPENAI_PROVIDER`] always uses Responses; register compatible
-/// providers with [`with_openai_provider`](Self::with_openai_provider).
+/// Holds native Anthropic, Gemini, and OpenAI Responses clients plus a
+/// registry of OpenAI-compatible Chat Completions clients keyed by provider
+/// name. The built-in [`OPENAI_PROVIDER`] always uses Responses; register
+/// compatible providers with [`with_openai_provider`](Self::with_openai_provider).
#[derive(Clone)]
pub struct ModelRouter {
anthropic: Arc<anthropic::Client>,
openai: Arc<openai::Client>,
+ gemini: Option<Arc<gemini::Client>>,
openai_compatible: HashMap<String, Arc<openai::CompletionsClient>>,
}
@@ -274,6 +306,7 @@ impl ModelRouter {
Self {
anthropic: Arc::new(anthropic),
openai: Arc::new(openai),
+ gemini: None,
openai_compatible: HashMap::new(),
}
}
@@ -293,20 +326,20 @@ impl ModelRouter {
let openai = openai::Client::builder()
.api_key(env.openai_api_key.to_string())
.build()?;
- // Cerebras, Fireworks, and Gemini all speak the OpenAI Chat Completions
- // API, so they ride the compatible-provider registry: a `<provider>/`
- // segment routes to the client registered under that name.
+ // Cerebras and Fireworks speak the OpenAI Chat Completions API, so they
+ // ride the compatible-provider registry: a `<provider>/` segment routes
+ // to the client registered under that name. Gemini uses GenerateContent
+ // so tool-call thought signatures survive the multi-step tool loop.
+ let gemini = gemini::Client::builder()
+ .api_key(env.google_generative_ai_api_key.to_string())
+ .build()?;
Self::new(anthropic, openai)
+ .with_gemini_client(gemini)
.with_openai_provider(CEREBRAS_PROVIDER, CEREBRAS_BASE_URL, &env.cerebras_api_key)?
.with_openai_provider(
FIREWORKS_PROVIDER,
FIREWORKS_BASE_URL,
&env.firework_api_key,
- )?
- .with_openai_provider(
- GOOGLE_PROVIDER,
- GOOGLE_BASE_URL,
- &env.google_generative_ai_api_key,
)
}
@@ -325,6 +358,12 @@ impl ModelRouter {
Ok(ROUTER.get_or_init(|| router))
}
+ /// Bind the native Gemini GenerateContent client. Google ids route here.
+ pub fn with_gemini_client(mut self, client: gemini::Client) -> Self {
+ self.gemini = Some(Arc::new(client));
+ self
+ }
+
/// Register an already-built OpenAI-compatible Chat Completions client under
/// `provider`.
pub fn with_openai_client(
@@ -395,6 +434,15 @@ impl ModelRouter {
self.openai.clone(),
)));
}
+ if parsed.provider() == GOOGLE_PROVIDER {
+ let Some(client) = &self.gemini else {
+ return Err(AgentError::UnknownModel(model.to_string()));
+ };
+ return Ok(RoutedModel::Gemini(GeminiModel::new(
+ parsed,
+ Arc::clone(client),
+ )));
+ }
if let Some(client) = self.openai_compatible.get(parsed.provider()) {
let client = Arc::clone(client);
return Ok(RoutedModel::OpenAiChatCompletions(
diff --git a/crates/agent/src/test/test_rig_invariants.rs b/crates/agent/src/test/test_rig_invariants.rs
index 645a263618..044d2cf726 100644
--- a/crates/agent/src/test/test_rig_invariants.rs
+++ b/crates/agent/src/test/test_rig_invariants.rs
@@ -73,3 +73,80 @@ fn anthropic_wire_parses_stringified_object_tool_use_input() {
};
assert_eq!(input, serde_json::json!({"value": "ok"}));
}
+
+/// Gemini 3 rejects a function-call part that is missing `thought_signature`.
+/// Native GenerateContent copies [`ToolCall::signature`] onto the part; that
+/// is why Google ids do not ride the OpenAI-compatible Chat Completions path.
+#[test]
+fn gemini_wire_copies_tool_call_signature_onto_the_function_call_part() {
+ use rig_core::providers::gemini::completion::gemini_api_types::{Part, PartKind};
+
+ let message = Message::Assistant {
+ id: None,
+ content: OneOrMany::one(AssistantContent::ToolCall(
+ ToolCall::new(
+ "list-companies".to_string(),
+ ToolFunction {
+ name: "ListCompanies".to_string(),
+ arguments: serde_json::json!({}),
+ },
+ )
+ .with_signature(Some("thought-sig".to_owned())),
+ )),
+ };
+
+ let content: rig_core::providers::gemini::completion::gemini_api_types::Content = message
+ .try_into()
+ .expect("assistant tool call must convert");
+ let Part {
+ thought_signature,
+ part: PartKind::FunctionCall(call),
+ ..
+ } = content.parts.first().expect("one part")
+ else {
+ panic!("expected a functionCall part");
+ };
+ assert_eq!(call.name, "ListCompanies");
+ assert_eq!(thought_signature.as_deref(), Some("thought-sig"));
+}
+
+/// The OpenAI Chat Completions serializer has no `extra_content` field, so a
+/// Gemini thought signature stored on [`ToolCall::signature`] is dropped. Pin
+/// that hole so a future rig bump either preserves it (and this assertion
+/// fails for us to delete the native Gemini route) or stays dropped.
+#[test]
+fn openai_chat_completions_wire_drops_tool_call_signatures() {
+ use rig_core::providers::openai::completion::Message as OpenAiMessage;
+
+ let message = Message::Assistant {
+ id: None,
+ content: OneOrMany::one(AssistantContent::ToolCall(
+ ToolCall::new(
+ "list-companies".to_string(),
+ ToolFunction {
+ name: "ListCompanies".to_string(),
+ arguments: serde_json::json!({}),
+ },
+ )
+ .with_signature(Some("thought-sig".to_owned())),
+ )),
+ };
+
+ let wire: Vec<OpenAiMessage> = message
+ .try_into()
+ .expect("assistant tool call must convert");
+ let json = serde_json::to_value(&wire).expect("openai messages serialize");
+ let dumped = json.to_string();
+ assert!(
+ dumped.contains("ListCompanies"),
+ "expected a tool call on the wire: {dumped}"
+ );
+ assert!(
+ !dumped.contains("thought-sig"),
+ "Chat Completions must not silently start echoing signatures: {dumped}"
+ );
+ assert!(
+ !dumped.contains("extra_content"),
+ "Chat Completions gained extra_content; re-evaluate the native Gemini route: {dumped}"
+ );
+}
diff --git a/crates/agent/src/test/test_router.rs b/crates/agent/src/test/test_router.rs
index ef87b73263..998a0982cb 100644
--- a/crates/agent/src/test/test_router.rs
+++ b/crates/agent/src/test/test_router.rs
@@ -1,7 +1,7 @@
use crate::model::PredefinedModel;
use crate::model::router::*;
use crate::model::types::Model;
-use rig_core::providers::{anthropic, openai};
+use rig_core::providers::{anthropic, gemini, openai};
fn test_router() -> ModelRouter {
let anthropic = anthropic::Client::builder()
@@ -92,18 +92,21 @@ fn fireworks_provider_routes_to_chat_completions() {
}
#[test]
-fn google_provider_routes_to_chat_completions() {
- let router = test_router().with_openai_client(
- "google",
- openai::CompletionsClient::builder()
+fn google_provider_routes_to_native_gemini() {
+ let router = test_router().with_gemini_client(
+ gemini::Client::builder()
.api_key("test-google-key")
- .base_url("https://generativelanguage.googleapis.com/v1beta/openai")
.build()
.unwrap(),
);
let routed = router.route("google/gemini-3.8-flash").unwrap();
- assert!(matches!(routed, RoutedModel::OpenAiChatCompletions(_)));
+ assert!(matches!(routed, RoutedModel::Gemini(_)));
assert_eq!(routed.provider(), "google");
assert_eq!(routed.model_name(), "gemini-3.8-flash");
}
+
+#[test]
+fn google_provider_without_a_client_is_unroutable() {
+ assert!(test_router().route("google/gemini-3.8-flash").is_err());
+}