fix(databricks): send Gemini thinking level and drop stream_options (#13232)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Tim Mellor
2026-11-04 00:18:03 +00:00
committed by GitHub
parent b4531be281
commit 238026cd11
+198 -18
View File
@@ -255,12 +255,25 @@ impl DatabricksV2Provider {
model_name.contains("claude")
}
fn is_gemini_model_service(model_name: &str) -> bool {
Self::is_model_service_fqn(model_name)
&& model_name
.rsplit('.')
.next()
.is_some_and(|service| service.to_lowercase().contains("gemini"))
fn service_or_endpoint_name(model_name: &str) -> String {
let name = if Self::is_model_service_fqn(model_name) {
model_name.rsplit('.').next().unwrap_or(model_name)
} else {
model_name
};
name.to_lowercase()
}
fn is_gemini_model(model_name: &str) -> bool {
Self::service_or_endpoint_name(model_name).contains("gemini")
}
/// Gemini 1.x and 2.x take a thinking budget, not a thinking level.
fn is_budget_thinking_gemini(model_name: &str) -> bool {
let name = Self::service_or_endpoint_name(model_name);
["gemini-1", "gemini-2"]
.iter()
.any(|family| name.contains(family))
}
fn always_on_reasoning_effort(model_config: &ModelConfig) -> Option<&'static str> {
@@ -400,10 +413,37 @@ impl DatabricksV2Provider {
if is_model_service {
payload["model"] = Value::String(model_config.model_name.clone());
}
// The gateway maps `stream_options` into Gemini's `generation_config`, which rejects it.
if Self::is_gemini_model_service(&model_config.model_name) {
// The gateway copies fields it doesn't translate into Gemini's `generation_config`.
// Gemini rejects `stream_options` there, and the gateway doesn't translate
// `reasoning_effort` for every Gemini model, but it does pass `thinking_config` through.
// Gemini 1.x and 2.x keep `reasoning_effort` because they don't accept a thinking level.
if Self::is_gemini_model(&model_config.model_name) {
let use_thinking_level = !Self::is_budget_thinking_gemini(&model_config.model_name);
if let Some(object) = payload.as_object_mut() {
object.remove("stream_options");
if let Some(mut effort) = use_thinking_level
.then(|| object.remove("reasoning_effort"))
.flatten()
{
// Gemini 3 Pro has no medium level; match the native Gemini formatter.
if effort == "medium"
&& Self::service_or_endpoint_name(&model_config.model_name)
.contains("gemini-3-pro")
{
effort = Value::String("low".to_string());
}
// Keep any `thinking_config` the user set in request params.
let thinking = object
.entry("thinking_config")
.or_insert_with(|| serde_json::json!({}));
if let Some(thinking) = thinking.as_object_mut() {
thinking.entry("thinking_level").or_insert(effort);
// Without this the gateway stops streaming reasoning text.
thinking
.entry("include_thoughts")
.or_insert(Value::Bool(true));
}
}
}
}
if let Some(effort) = Self::always_on_reasoning_effort(model_config) {
@@ -812,16 +852,20 @@ mod tests {
}
#[test]
fn detects_gemini_only_from_the_model_service_name() {
assert!(DatabricksV2Provider::is_gemini_model_service(
"catalog.schema.goose-Gemini-3-pro"
));
for model in ["team.gemini.kimi-chat", "databricks-gemini-3-pro"] {
fn detects_gemini_only_from_the_service_or_endpoint_name() {
for model in [
"catalog.schema.goose-Gemini-3-pro",
"databricks-gemini-3-pro",
"gemini-3.6-flash",
] {
assert!(
!DatabricksV2Provider::is_gemini_model_service(model),
"{model} is not a Gemini model service"
DatabricksV2Provider::is_gemini_model(model),
"{model} is a Gemini model"
);
}
assert!(!DatabricksV2Provider::is_gemini_model(
"team.gemini.kimi-chat"
));
}
#[test]
@@ -1031,9 +1075,10 @@ mod tests {
.expect("model service should receive reasoning effort");
}
#[test_case::test_case("catalog.schema.goose-gemini-3-pro" ; "model service")]
#[test_case::test_case("databricks-gemini-3-pro" ; "serving endpoint")]
#[tokio::test]
async fn gemini_model_service_omits_stream_options_and_keeps_usage() {
let model = "catalog.schema.goose-gemini-3-pro";
async fn gemini_omits_stream_options_and_keeps_usage(model: &str) {
let delta = format!(
r#"data: {{"model":"{model}","choices":[{{"delta":{{"content":"Hello"}},"finish_reason":null}}]}}"#
);
@@ -1058,7 +1103,7 @@ mod tests {
let (message, usage) = provider(server.uri())
.complete(&ModelConfig::new(model), "system", &[], &[])
.await
.expect("Gemini model service should stream without stream_options");
.expect("Gemini should stream without stream_options");
let requests = server.received_requests().await.unwrap();
let request: Value = serde_json::from_slice(&requests[0].body).unwrap();
@@ -1069,6 +1114,141 @@ mod tests {
assert_eq!(usage.usage.total_tokens, Some(9));
}
#[tokio::test]
async fn gemini_sends_thinking_level_instead_of_reasoning_effort() {
let model = "system.ai.gemini-4-argon";
let body = "data: {\"choices\":[{\"delta\":{},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n";
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/ai-gateway/mlflow/v1/chat/completions"))
.and(body_partial_json(json!({
"model": model,
"thinking_config": {"thinking_level": "high", "include_thoughts": true}
})))
.respond_with(
ResponseTemplate::new(200)
.set_body_string(body)
.append_header("content-type", "text/event-stream"),
)
.expect(1)
.mount(&server)
.await;
let mut config = ModelConfig::new(model).with_thinking_effort(ThinkingEffort::High);
config.reasoning = Some(true);
provider(server.uri())
.complete(&config, "system", &[], &[])
.await
.expect("Gemini should receive a thinking level");
let requests = server.received_requests().await.unwrap();
let request: Value = serde_json::from_slice(&requests[0].body).unwrap();
assert!(request.get("reasoning_effort").is_none());
}
#[test_case::test_case("system.ai.gemini-3-pro", "low" ; "gemini 3 pro")]
#[test_case::test_case("system.ai.gemini-3-1-pro", "medium" ; "gemini 3.1 pro")]
#[tokio::test]
async fn gemini_medium_effort_thinking_level(model: &str, level: &str) {
let body = "data: {\"choices\":[{\"delta\":{},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n";
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/ai-gateway/mlflow/v1/chat/completions"))
.respond_with(
ResponseTemplate::new(200)
.set_body_string(body)
.append_header("content-type", "text/event-stream"),
)
.expect(1)
.mount(&server)
.await;
let mut config = ModelConfig::new(model).with_thinking_effort(ThinkingEffort::Medium);
config.reasoning = Some(true);
provider(server.uri())
.complete(&config, "system", &[], &[])
.await
.expect("Gemini should receive a thinking level");
let requests = server.received_requests().await.unwrap();
let request: Value = serde_json::from_slice(&requests[0].body).unwrap();
assert_eq!(request["thinking_config"]["thinking_level"], json!(level));
}
#[tokio::test]
async fn gemini_2_5_keeps_reasoning_effort() {
let model = "system.ai.gemini-2-5-flash";
let body = "data: {\"choices\":[{\"delta\":{},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n";
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/ai-gateway/mlflow/v1/chat/completions"))
.and(body_partial_json(json!({
"model": model,
"reasoning_effort": "high"
})))
.respond_with(
ResponseTemplate::new(200)
.set_body_string(body)
.append_header("content-type", "text/event-stream"),
)
.expect(1)
.mount(&server)
.await;
let mut config = ModelConfig::new(model).with_thinking_effort(ThinkingEffort::High);
config.reasoning = Some(true);
provider(server.uri())
.complete(&config, "system", &[], &[])
.await
.expect("Gemini 2.5 should keep reasoning effort");
let requests = server.received_requests().await.unwrap();
let request: Value = serde_json::from_slice(&requests[0].body).unwrap();
assert!(request.get("thinking_config").is_none());
assert!(request.get("stream_options").is_none());
}
#[tokio::test]
async fn gemini_thinking_level_keeps_user_thinking_config() {
let model = "system.ai.gemini-4-argon";
let body = "data: {\"choices\":[{\"delta\":{},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n";
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/ai-gateway/mlflow/v1/chat/completions"))
.respond_with(
ResponseTemplate::new(200)
.set_body_string(body)
.append_header("content-type", "text/event-stream"),
)
.expect(1)
.mount(&server)
.await;
let mut config = ModelConfig::new(model)
.with_thinking_effort(ThinkingEffort::High)
.with_merged_request_params(std::collections::HashMap::from([(
"thinking_config".to_string(),
json!({"include_thoughts": false}),
)]));
config.reasoning = Some(true);
provider(server.uri())
.complete(&config, "system", &[], &[])
.await
.expect("Gemini should keep the user's thinking config");
let requests = server.received_requests().await.unwrap();
let request: Value = serde_json::from_slice(&requests[0].body).unwrap();
assert_eq!(
request["thinking_config"],
json!({"include_thoughts": false, "thinking_level": "high"})
);
assert!(request.get("reasoning_effort").is_none());
}
#[tokio::test]
async fn non_gemini_model_service_keeps_stream_options() {
let model = "catalog.schema.goose-kimi-k3";