From 3d1e436ac638f42cb3303b5db1dc41f1a291126e Mon Sep 17 00:00:00 2001 From: Bob Lee Date: Thu, 19 Nov 2026 15:17:38 +0800 Subject: [PATCH 1/2] fix(gemini): Infer object/array types for untyped tool schemas Gemini only accepts properties on OBJECT schemas and items on ARRAY schemas. Tool schemas that intentionally omit type (Office paragraph items accept strings or objects; XLSX rows also accept an integer for freeze_panes) were forwarded untyped and rejected with 400 INVALID_ARGUMENT. Infer the structural type in the Gemini sanitizer while keeping fully untyped leaves unconstrained. --- .../src/providers/gemini/message_converter.rs | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/crates/adapters/ai-adapters/src/providers/gemini/message_converter.rs b/src/crates/adapters/ai-adapters/src/providers/gemini/message_converter.rs index 259f3fb153..306f8ca1ee 100644 --- a/src/crates/adapters/ai-adapters/src/providers/gemini/message_converter.rs +++ b/src/crates/adapters/ai-adapters/src/providers/gemini/message_converter.rs @@ -523,6 +523,16 @@ impl GeminiMessageConverter { } sanitized.insert("type".to_string(), Value::String(schema_type)); } + if !sanitized.contains_key("type") { + // JSON Schema allows untyped schemas whose `properties` / `items` + // only constrain matching instances, but Gemini only accepts those + // keywords on OBJECT / ARRAY schemas. Infer the structural type. + if sanitized.contains_key("properties") { + sanitized.insert("type".to_string(), json!("object")); + } else if sanitized.contains_key("items") { + sanitized.insert("type".to_string(), json!("array")); + } + } if nullable { sanitized.insert("nullable".to_string(), Value::Bool(true)); } @@ -904,6 +914,46 @@ mod tests { ); } + #[test] + fn infers_structural_type_for_untyped_properties_and_items() { + let schema = GeminiMessageConverter::sanitize_schema(json!({ + "type": "object", + "properties": { + "paragraphs": { + "type": "array", + "items": { + "description": "String or paragraph object.", + "properties": { "text": { "type": "string" } }, + "additionalProperties": true + } + }, + "rows": { + "description": "2-D array or integer.", + "items": { + "type": "array", + "items": { + "properties": { + "value": {}, + "formula": { "type": "string" } + } + } + } + }, + "untyped_leaf": { "description": "Anything." } + } + })); + + let props = &schema["properties"]; + assert_eq!(props["paragraphs"]["items"]["type"], json!("object")); + assert_eq!(props["rows"]["type"], json!("array")); + assert_eq!(props["rows"]["items"]["type"], json!("array")); + assert_eq!(props["rows"]["items"]["items"]["type"], json!("object")); + assert!(props["untyped_leaf"].get("type").is_none()); + assert!(props["rows"]["items"]["items"]["properties"]["value"] + .get("type") + .is_none()); + } + #[test] fn maps_web_search_to_native_google_search_tool() { let tools = Some(vec![ToolDefinition { From be84b2a36ff88b829b003cdecd5a48703873fc20 Mon Sep 17 00:00:00 2001 From: Bob Lee Date: Thu, 19 Nov 2026 15:29:00 +0800 Subject: [PATCH 2/2] fix(gemini): Drop schema keywords that conflict with the resolved type Mixed-type anyOf/oneOf unions and type arrays collapse to one Gemini type, which could leave items, properties, required, or string/number constraints on a type that cannot carry them. Gemini also rejects non-string enum values, untyped enums, required names missing from properties, tuple items, and boolean property schemas. Normalize these in the Gemini sanitizer. Across the 62 product tools, this removes the remaining Gemini-incompatible nodes in DesktopCard (nullable string enums), GenerativeUI (untyped enums), and ComputerUse (minLength left on an object). --- .../src/providers/gemini/message_converter.rs | 161 +++++++++++++++++- 1 file changed, 159 insertions(+), 2 deletions(-) diff --git a/src/crates/adapters/ai-adapters/src/providers/gemini/message_converter.rs b/src/crates/adapters/ai-adapters/src/providers/gemini/message_converter.rs index 306f8ca1ee..4803645e84 100644 --- a/src/crates/adapters/ai-adapters/src/providers/gemini/message_converter.rs +++ b/src/crates/adapters/ai-adapters/src/providers/gemini/message_converter.rs @@ -488,8 +488,15 @@ impl GeminiMessageConverter { Value::Object( properties .into_iter() - .map(|(name, schema)| { - (name, Self::strip_unsupported_schema_fields(schema)) + .filter_map(|(name, schema)| match schema { + // JSON Schema boolean forms: `true` accepts any + // value, `false` forbids the property. + Value::Bool(true) => Some((name, json!({}))), + Value::Bool(false) => None, + schema => Some(( + name, + Self::strip_unsupported_schema_fields(schema), + )), }) .collect(), ), @@ -498,6 +505,17 @@ impl GeminiMessageConverter { continue; } + if key == "items" { + // Gemini only accepts a single item schema. Tuple (`items: [...]`) + // and boolean forms become an unconstrained element schema. + let items = match value { + Value::Object(_) => Self::strip_unsupported_schema_fields(value), + _ => json!({}), + }; + sanitized.insert(key, items); + continue; + } + if Self::is_supported_schema_key(&key) { sanitized.insert(key, Self::strip_unsupported_schema_fields(value)); } @@ -531,8 +549,15 @@ impl GeminiMessageConverter { sanitized.insert("type".to_string(), json!("object")); } else if sanitized.contains_key("items") { sanitized.insert("type".to_string(), json!("array")); + } else if sanitized + .get("enum") + .and_then(Value::as_array) + .is_some_and(|values| values.iter().all(Value::is_string)) + { + sanitized.insert("type".to_string(), json!("string")); } } + Self::drop_keywords_incompatible_with_type(&mut sanitized); if nullable { sanitized.insert("nullable".to_string(), Value::Bool(true)); } @@ -549,6 +574,83 @@ impl GeminiMessageConverter { } } + /// Gemini validates schema keywords against the schema `type` and rejects a request + /// that carries, for example, `items` on a STRING or `properties` on an ARRAY. + /// Mixed-type unions and `type` arrays collapse to a single type here, so drop the + /// keywords that the selected type cannot carry. This only loosens constraints. + fn drop_keywords_incompatible_with_type(schema: &mut Map) { + let Some(schema_type) = schema + .get("type") + .and_then(Value::as_str) + .map(str::to_ascii_lowercase) + else { + if schema + .get("enum") + .and_then(Value::as_array) + .is_some_and(|values| !values.iter().all(Value::is_string)) + { + schema.remove("enum"); + } + return; + }; + let is_number = matches!(schema_type.as_str(), "number" | "integer"); + + if schema_type == "object" { + let property_names: Option> = schema + .get("properties") + .and_then(Value::as_object) + .map(|properties| properties.keys().cloned().collect()); + // Gemini rejects `required` entries that are not declared in `properties`. + match (schema.get_mut("required"), property_names) { + (Some(Value::Array(required)), Some(names)) => { + required.retain(|name| { + name.as_str() + .is_some_and(|name| names.iter().any(|known| known == name)) + }); + if required.is_empty() { + schema.remove("required"); + } + } + (Some(_), _) => { + schema.remove("required"); + } + (None, _) => {} + } + } else { + schema.remove("properties"); + schema.remove("required"); + } + if schema_type != "array" { + schema.remove("items"); + schema.remove("minItems"); + schema.remove("maxItems"); + } + if schema_type != "string" { + schema.remove("minLength"); + schema.remove("maxLength"); + schema.remove("pattern"); + } + if !is_number { + schema.remove("minimum"); + schema.remove("maximum"); + } + + if let Some(values) = schema.get_mut("enum") { + // Gemini only accepts string enums on STRING schemas. + let keep = schema_type == "string" + && match values { + Value::Array(values) => { + values.retain(Value::is_string); + !values.is_empty() + } + _ => false, + }; + if !keep { + schema.remove("enum"); + } + } + } + fn is_supported_schema_key(key: &str) -> bool { matches!( key, @@ -954,6 +1056,61 @@ mod tests { .is_none()); } + #[test] + fn drops_keywords_that_conflict_with_the_collapsed_union_type() { + let schema = GeminiMessageConverter::sanitize_schema(json!({ + "type": "object", + "properties": { + "string_or_list": { + "anyOf": [ + { "type": "string", "minLength": 1 }, + { "type": "array", "items": { "type": "string" }, "minItems": 1 } + ] + }, + "list_or_string": { + "type": ["array", "string"], + "items": { "type": "string" }, + "maxLength": 10 + }, + "level": { "type": "integer", "enum": [1, 2, 3], "minimum": 1 }, + "mode": { "type": ["string", "null"], "enum": ["fast", "slow", null] }, + "untyped_choice": { "enum": ["a", "b"] }, + "untyped_numeric_choice": { "enum": [1, 2] }, + "tuple": { "type": "array", "items": [{ "type": "string" }, { "type": "integer" }] }, + "any_value": true, + "never": false + }, + "required": ["string_or_list", "never", "missing"] + })); + + let props = &schema["properties"]; + assert_eq!(props["string_or_list"]["type"], json!("string")); + assert_eq!(props["string_or_list"]["minLength"], json!(1)); + assert!(props["string_or_list"].get("items").is_none()); + assert!(props["string_or_list"].get("minItems").is_none()); + + assert_eq!(props["list_or_string"]["type"], json!("array")); + assert_eq!(props["list_or_string"]["items"]["type"], json!("string")); + assert!(props["list_or_string"].get("maxLength").is_none()); + + assert_eq!(props["level"]["type"], json!("integer")); + assert_eq!(props["level"]["minimum"], json!(1)); + assert!(props["level"].get("enum").is_none()); + + assert_eq!(props["mode"]["type"], json!("string")); + assert_eq!(props["mode"]["nullable"], json!(true)); + assert_eq!(props["mode"]["enum"], json!(["fast", "slow"])); + + assert_eq!(props["untyped_choice"]["type"], json!("string")); + assert!(props["untyped_numeric_choice"].get("enum").is_none()); + assert!(props["untyped_numeric_choice"].get("type").is_none()); + + assert_eq!(props["tuple"]["items"], json!({})); + assert_eq!(props["any_value"], json!({})); + assert!(props.get("never").is_none()); + assert_eq!(schema["required"], json!(["string_or_list"])); + } + #[test] fn maps_web_search_to_native_google_search_tool() { let tools = Some(vec![ToolDefinition {