From 6565c091e4135508a67c386059c013857e49d7e6 Mon Sep 17 00:00:00 2001 From: Bennet Bo Fenner Date: Thu, 8 May 2025 11:06:35 +0200 Subject: [PATCH] agent: Improve Gemini tool schema compatibility (#30216) Closes #30056 Apparently the API supports the "default" field now, so we can remove that transformation. However, optional is not supported See https://ai.google.dev/api/caching#Schema Release Notes: - agent: Improve tool schema compatibility for Gemini models --- crates/assistant_tool/src/tool_schema.rs | 35 +++--------------------- 1 file changed, 4 insertions(+), 31 deletions(-) diff --git a/crates/assistant_tool/src/tool_schema.rs b/crates/assistant_tool/src/tool_schema.rs index 9d2711ecb1..c42e4b565e 100644 --- a/crates/assistant_tool/src/tool_schema.rs +++ b/crates/assistant_tool/src/tool_schema.rs @@ -35,25 +35,17 @@ fn adapt_to_json_schema_subset(json: &mut Value) -> Result<()> { } } - const KEYS_TO_REMOVE: [&str; 4] = [ + const KEYS_TO_REMOVE: [&str; 5] = [ "format", "additionalProperties", "exclusiveMinimum", "exclusiveMaximum", + "optional", ]; for key in KEYS_TO_REMOVE { obj.remove(key); } - if let Some(default) = obj.get("default") { - let is_null = default.is_null(); - // Default is not supported, so we need to remove it - obj.remove("default"); - if is_null { - obj.insert("nullable".to_string(), Value::Bool(true)); - } - } - // If a type is not specified for an input parameter, add a default type if matches!(obj.get("description"), Some(Value::String(_))) && !obj.contains_key("type") @@ -92,26 +84,6 @@ mod tests { use super::*; use serde_json::json; - #[test] - fn test_transform_default_null_to_nullable() { - let mut json = json!({ - "description": "A test field", - "type": "string", - "default": null - }); - - adapt_to_json_schema_subset(&mut json).unwrap(); - - assert_eq!( - json, - json!({ - "description": "A test field", - "type": "string", - "nullable": true - }) - ); - } - #[test] fn test_transform_adds_type_when_missing() { let mut json = json!({ @@ -157,7 +129,8 @@ mod tests { "format": "uint32", "exclusiveMinimum": 0, "exclusiveMaximum": 100, - "additionalProperties": false + "additionalProperties": false, + "optional": true }); adapt_to_json_schema_subset(&mut json).unwrap();