Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Send complete tool JSON schemas to every provider (#63342)

Conrad Irwin8cfa848cd240
diff --git a/crates/agent/examples/dump_tool_schemas.rs b/crates/agent/examples/dump_tool_schemas.rs
new file mode 100644
index 00000000000..edb262f2798
--- /dev/null
+++ b/crates/agent/examples/dump_tool_schemas.rs
@@ -0,0 +1,14 @@
+//! Zed built-in tool schema dump.
+//!
+//! This example serializes the same normalized built-in tool definitions used
+//! to construct language model requests.
+
+use anyhow::Result;
+
+fn main() -> Result<()> {
+ let mut tools = agent::built_in_tools().collect::<Vec<_>>();
+ tools.sort_by(|left, right| left.name.cmp(&right.name));
+ serde_json::to_writer_pretty(std::io::stdout().lock(), &tools)?;
+ println!();
+ Ok(())
+}
diff --git a/crates/agent/src/tests/mod.rs b/crates/agent/src/tests/mod.rs
index 2ed44e285c7..44ac412dcbb 100644
--- a/crates/agent/src/tests/mod.rs
+++ b/crates/agent/src/tests/mod.rs
@@ -28,8 +28,8 @@ use language_model::{
CompletionIntent, LanguageModel, LanguageModelCompletionError, LanguageModelCompletionEvent,
LanguageModelId, LanguageModelImageExt, LanguageModelProviderId, LanguageModelProviderName,
LanguageModelRegistry, LanguageModelRequest, LanguageModelRequestMessage,
- LanguageModelToolResult, LanguageModelToolSchemaFormat, LanguageModelToolUse, MessageContent,
- ProviderErrorCategory, Role, StopReason, TokenUsage,
+ LanguageModelToolResult, LanguageModelToolUse, MessageContent, ProviderErrorCategory, Role,
+ StopReason, TokenUsage,
fake_provider::{FakeLanguageModel, FakeLanguageModelProvider},
};
use pretty_assertions::assert_eq;
@@ -1729,10 +1729,7 @@ async fn test_mcp_tools(cx: &mut TestAppContext) {
name: "echo".into(),
title: None,
description: None,
- input_schema: serde_json::to_value(EchoTool::input_schema(
- LanguageModelToolSchemaFormat::JsonSchema,
- ))
- .unwrap(),
+ input_schema: EchoTool::input_schema().to_value(),
output_schema: None,
annotations: None,
}],
@@ -2323,10 +2320,7 @@ async fn test_mcp_tool_truncation(cx: &mut TestAppContext) {
name: "echo".into(), // Conflicts with native EchoTool
title: None,
description: None,
- input_schema: serde_json::to_value(EchoTool::input_schema(
- LanguageModelToolSchemaFormat::JsonSchema,
- ))
- .unwrap(),
+ input_schema: EchoTool::input_schema().to_value(),
output_schema: None,
annotations: None,
},
@@ -2350,10 +2344,7 @@ async fn test_mcp_tool_truncation(cx: &mut TestAppContext) {
name: "echo".into(), // Also conflicts with native EchoTool
title: None,
description: None,
- input_schema: serde_json::to_value(EchoTool::input_schema(
- LanguageModelToolSchemaFormat::JsonSchema,
- ))
- .unwrap(),
+ input_schema: EchoTool::input_schema().to_value(),
output_schema: None,
annotations: None,
},
@@ -2424,10 +2415,7 @@ async fn test_mcp_tool_truncation(cx: &mut TestAppContext) {
name: "echo".into(), // Also conflicts - will be disambiguated as azure_dev_ops_echo
title: None,
description: None,
- input_schema: serde_json::to_value(EchoTool::input_schema(
- LanguageModelToolSchemaFormat::JsonSchema,
- ))
- .unwrap(),
+ input_schema: EchoTool::input_schema().to_value(),
output_schema: None,
annotations: None,
}],
diff --git a/crates/agent/src/thread.rs b/crates/agent/src/thread.rs
index f850907d7a9..6185cb54cc7 100644
--- a/crates/agent/src/thread.rs
+++ b/crates/agent/src/thread.rs
@@ -43,9 +43,9 @@ use language_model::{
CompletionIntent, LanguageModel, LanguageModelCompletionError, LanguageModelCompletionEvent,
LanguageModelId, LanguageModelImage, LanguageModelProviderId, LanguageModelRegistry,
LanguageModelRequest, LanguageModelRequestMessage, LanguageModelRequestTool,
- LanguageModelToolResult, LanguageModelToolResultContent, LanguageModelToolSchemaFormat,
- LanguageModelToolUse, LanguageModelToolUseId, MessageContent, ProviderErrorCategory, Role,
- SelectedModel, Speed, StopReason, TokenUsage, ZED_CLOUD_PROVIDER_ID,
+ LanguageModelToolResult, LanguageModelToolResultContent, LanguageModelToolUse,
+ LanguageModelToolUseId, MessageContent, ProviderErrorCategory, Role, SelectedModel, Speed,
+ StopReason, TokenUsage, ZED_CLOUD_PROVIDER_ID,
};
use project::{Project, trusted_worktrees::TrustedWorktrees};
use prompt_store::ProjectContext;
@@ -4053,10 +4053,10 @@ impl Thread {
let tools = if let Some(turn) = self.running_turn.as_ref() {
turn.tools
.iter()
- .filter_map(|(tool_name, tool)| {
+ .map(|(tool_name, tool)| {
log::trace!("Including tool: {}", tool_name);
let mut description = tool.description().to_string();
- let mut schema = tool.input_schema(model.tool_input_format()).log_err()?;
+ let mut schema = tool.input_schema();
// TEMPORARY (sandboxing feature flag): with the flag off,
// the fetch and create_directory descriptions/schemas must
// not advertise sandbox-dependent behavior (host grants,
@@ -4086,12 +4086,12 @@ impl Thread {
}
}
}
- Some(LanguageModelRequestTool::function(
+ LanguageModelRequestTool::function(
tool_name.to_string(),
description,
schema,
tool.supports_input_streaming(),
- ))
+ )
})
.collect::<Vec<_>>()
} else {
@@ -5065,8 +5065,8 @@ where
) -> SharedString;
/// Returns the JSON schema that describes the tool's input.
- fn input_schema(format: LanguageModelToolSchemaFormat) -> Schema {
- language_model::tool_schema::root_schema_for::<Self::Input>(format)
+ fn input_schema() -> Schema {
+ language_model::tool_schema::root_schema_for::<Self::Input>()
}
/// Returns whether the tool supports streaming of tool use parameters.
@@ -5144,7 +5144,7 @@ pub trait AnyAgentTool {
fn description(&self) -> SharedString;
fn kind(&self) -> acp::ToolKind;
fn initial_title(&self, input: serde_json::Value, _cx: &mut App) -> SharedString;
- fn input_schema(&self, format: LanguageModelToolSchemaFormat) -> Result<serde_json::Value>;
+ fn input_schema(&self) -> serde_json::Value;
fn supports_input_streaming(&self) -> bool {
false
}
@@ -5195,10 +5195,10 @@ where
self.0.initial_title(parsed_input, _cx)
}
- fn input_schema(&self, format: LanguageModelToolSchemaFormat) -> Result<serde_json::Value> {
- let mut json = serde_json::to_value(T::input_schema(format))?;
- language_model::tool_schema::adapt_schema_to_format(&mut json, format)?;
- Ok(json)
+ fn input_schema(&self) -> serde_json::Value {
+ let mut schema = T::input_schema().to_value();
+ language_model::tool_schema::normalize_tool_schema(&mut schema);
+ schema
}
fn supports_provider(&self, provider: &LanguageModelProviderId) -> bool {
diff --git a/crates/agent/src/tools.rs b/crates/agent/src/tools.rs
index 7630d2e3f43..0059a1ba128 100644
--- a/crates/agent/src/tools.rs
+++ b/crates/agent/src/tools.rs
@@ -34,7 +34,7 @@ use feature_flags::{
CreateThreadToolFeatureFlag, FeatureFlagAppExt as _, LspToolFeatureFlag, RenameToolFeatureFlag,
};
use gpui::App;
-use language_model::{LanguageModelRequestTool, LanguageModelToolSchemaFormat};
+use language_model::LanguageModelRequestTool;
use serde::{
Deserialize, Deserializer,
de::{DeserializeOwned, Error as _},
@@ -162,10 +162,12 @@ macro_rules! tools {
/// A list of all built-in tools
pub fn built_in_tools() -> impl Iterator<Item = LanguageModelRequestTool> {
fn language_model_tool<T: AgentTool>() -> LanguageModelRequestTool {
+ let mut input_schema = T::input_schema().to_value();
+ language_model::tool_schema::normalize_tool_schema(&mut input_schema);
LanguageModelRequestTool::function(
T::NAME.to_string(),
T::description().to_string(),
- T::input_schema(LanguageModelToolSchemaFormat::JsonSchema).to_value(),
+ input_schema,
T::supports_input_streaming(),
)
}
@@ -246,6 +248,33 @@ pub fn tool_feature_flag_enabled(tool_name: &str, cx: &App) -> bool {
mod tests {
use super::*;
+ #[test]
+ fn built_in_tool_schemas_are_normalized() {
+ let tools = built_in_tools().collect::<Vec<_>>();
+
+ assert_eq!(tools.len(), ALL_TOOL_NAMES.len());
+ for tool in tools {
+ let language_model::LanguageModelRequestToolInput::Function { input_schema, .. } =
+ tool.input
+ else {
+ panic!("built-in tool `{}` should use a JSON schema", tool.name);
+ };
+ assert_eq!(input_schema.get("$schema"), None, "tool `{}`", tool.name);
+ assert_eq!(input_schema.get("title"), None, "tool `{}`", tool.name);
+ assert_eq!(
+ input_schema.get("description"),
+ None,
+ "tool `{}`",
+ tool.name
+ );
+ assert!(
+ input_schema["properties"].is_object(),
+ "tool `{}` should have object properties",
+ tool.name
+ );
+ }
+ }
+
#[test]
fn fetch_and_terminal_are_forbidden_in_restricted_mode() {
assert!(!tool_allowed_in_restricted_mode(FetchTool::NAME));
diff --git a/crates/agent/src/tools/context_server_registry.rs b/crates/agent/src/tools/context_server_registry.rs
index 808535a7135..9fd027a10bd 100644
--- a/crates/agent/src/tools/context_server_registry.rs
+++ b/crates/agent/src/tools/context_server_registry.rs
@@ -318,21 +318,19 @@ impl AnyAgentTool for ContextServerTool {
format_mcp_initial_title(&self.tool.name, &input).into()
}
- fn input_schema(
- &self,
- format: language_model::LanguageModelToolSchemaFormat,
- ) -> Result<serde_json::Value> {
+ fn input_schema(&self) -> serde_json::Value {
let mut schema = self.tool.input_schema.clone();
- language_model::tool_schema::adapt_schema_to_format(&mut schema, format)?;
- Ok(match schema {
+ schema = match schema {
serde_json::Value::Null => {
- serde_json::json!({ "type": "object", "properties": [] })
+ serde_json::json!({ "type": "object", "properties": {} })
}
serde_json::Value::Object(map) if map.is_empty() => {
- serde_json::json!({ "type": "object", "properties": [] })
+ serde_json::json!({ "type": "object", "properties": {} })
}
_ => schema,
- })
+ };
+ language_model::tool_schema::normalize_tool_schema(&mut schema);
+ schema
}
fn run(
diff --git a/crates/agent/src/tools/terminal_tool.rs b/crates/agent/src/tools/terminal_tool.rs
index ea0ea51eec9..a674dd50312 100644
--- a/crates/agent/src/tools/terminal_tool.rs
+++ b/crates/agent/src/tools/terminal_tool.rs
@@ -2583,10 +2583,7 @@ mod tests {
#[test]
fn test_terminal_tool_input_schema_mentions_forbidden_substitutions() {
- let schema = <TerminalTool as crate::AgentTool>::input_schema(
- language_model::LanguageModelToolSchemaFormat::JsonSchema,
- );
- let schema_json = serde_json::to_value(schema).expect("schema should serialize");
+ let schema_json = <TerminalTool as crate::AgentTool>::input_schema().to_value();
let schema_text = schema_json.to_string();
assert!(
@@ -2628,10 +2625,7 @@ mod tests {
#[test]
fn test_terminal_tool_input_schema_mentions_head_and_tail_parameters() {
- let schema = <TerminalTool as crate::AgentTool>::input_schema(
- language_model::LanguageModelToolSchemaFormat::JsonSchema,
- );
- let schema_json = serde_json::to_value(schema).expect("schema should serialize");
+ let schema_json = <TerminalTool as crate::AgentTool>::input_schema().to_value();
let schema_text = schema_json.to_string();
assert!(schema_text.contains("head_lines"));
diff --git a/crates/agent_ui/src/buffer_codegen.rs b/crates/agent_ui/src/buffer_codegen.rs
index 5782f7a086b..020e0bc432e 100644
--- a/crates/agent_ui/src/buffer_codegen.rs
+++ b/crates/agent_ui/src/buffer_codegen.rs
@@ -498,7 +498,6 @@ impl CodegenAlternative {
let temperature = AgentSettings::temperature_for_model(model, cx);
- let tool_input_format = model.tool_input_format();
let tool_choice = model
.supports_tool_choice(LanguageModelToolChoice::Any)
.then_some(LanguageModelToolChoice::Any);
@@ -525,17 +524,21 @@ impl CodegenAlternative {
user_message.content.push(user_prompt.into());
messages.push(user_message);
+ let tool_schema = |mut schema: serde_json::Value| {
+ language_model::tool_schema::normalize_tool_schema(&mut schema);
+ schema
+ };
let tools = vec![
LanguageModelRequestTool::function(
REWRITE_SECTION_TOOL_NAME.to_string(),
"Replaces text in <rewrite_this></rewrite_this> tags with your replacement_text.".to_string(),
- language_model::tool_schema::root_schema_for::<RewriteSectionInput>(tool_input_format).to_value(),
+ tool_schema(language_model::tool_schema::root_schema_for::<RewriteSectionInput>().to_value()),
false,
),
LanguageModelRequestTool::function(
FAILURE_MESSAGE_TOOL_NAME.to_string(),
"Use this tool to provide a message to the user when you're unable to complete a task.".to_string(),
- language_model::tool_schema::root_schema_for::<FailureMessageInput>(tool_input_format).to_value(),
+ tool_schema(language_model::tool_schema::root_schema_for::<FailureMessageInput>().to_value()),
false,
),
];
diff --git a/crates/copilot_chat/src/model.rs b/crates/copilot_chat/src/model.rs
index b447f0635cd..385dae17d7f 100644
--- a/crates/copilot_chat/src/model.rs
+++ b/crates/copilot_chat/src/model.rs
@@ -3,8 +3,8 @@ use std::{pin::Pin, str::FromStr as _, sync::Arc};
use crate::responses as copilot_responses;
use crate::{
ChatLocation, ChatMessage, ChatMessageContent, ChatMessagePart, CopilotChat, Function,
- FunctionContent, ImageUrl, Model as CopilotChatModel, ModelVendor,
- Request as CopilotChatRequest, ResponseEvent, Tool, ToolCall, ToolCallContent, ToolChoice,
+ FunctionContent, ImageUrl, Model as CopilotChatModel, Request as CopilotChatRequest,
+ ResponseEvent, Tool, ToolCall, ToolCallContent, ToolChoice,
};
use anthropic::{
AnthropicModelMode,
@@ -22,8 +22,8 @@ use language_model::{
LanguageModelCostInfo, LanguageModelEffortLevel, LanguageModelId, LanguageModelName,
LanguageModelProviderId, LanguageModelProviderName, LanguageModelRequest,
LanguageModelRequestMessage, LanguageModelToolChoice, LanguageModelToolResultContent,
- LanguageModelToolSchemaFormat, LanguageModelToolUse, MessageContent, ProviderErrorCategory,
- RateLimiter, Role, StopReason, TokenUsage,
+ LanguageModelToolUse, MessageContent, ProviderErrorCategory, RateLimiter, Role, StopReason,
+ TokenUsage,
};
use util::debug_panic;
@@ -107,17 +107,6 @@ impl LanguageModel for CopilotChatLanguageModel {
.collect()
}
- fn tool_input_format(&self) -> LanguageModelToolSchemaFormat {
- match self.model.vendor() {
- ModelVendor::OpenAI | ModelVendor::Anthropic => {
- LanguageModelToolSchemaFormat::JsonSchema
- }
- ModelVendor::Google | ModelVendor::XAI | ModelVendor::Unknown => {
- LanguageModelToolSchemaFormat::JsonSchemaSubset
- }
- }
- }
-
fn supports_tool_choice(&self, choice: LanguageModelToolChoice) -> bool {
match choice {
LanguageModelToolChoice::Auto
diff --git a/crates/google_ai/src/completion.rs b/crates/google_ai/src/completion.rs
index 6652722787c..bc28b5b9ed4 100644
--- a/crates/google_ai/src/completion.rs
+++ b/crates/google_ai/src/completion.rs
@@ -139,7 +139,8 @@ pub fn into_google(
Ok(FunctionDeclaration {
name: tool.name,
description: tool.description,
- parameters: input_schema,
+ parameters: None,
+ parameters_json_schema: Some(input_schema),
})
}
LanguageModelRequestToolInput::Custom { .. } => {
@@ -484,7 +485,7 @@ mod tests {
Content, FunctionCall, FunctionCallPart, GenerateContentCandidate, GenerateContentResponse,
Part, Role as GoogleRole,
};
- use language_model_core::LanguageModelRequestMessage;
+ use language_model_core::{LanguageModelRequestMessage, LanguageModelRequestTool};
use serde_json::json;
fn text_request() -> LanguageModelRequest {
@@ -523,6 +524,43 @@ mod tests {
assert_eq!(serialized["includeThoughts"], true);
}
+ #[test]
+ fn into_google_uses_json_schema_function_parameters() {
+ let input_schema = json!({
+ "type": "object",
+ "properties": {
+ "globs": {
+ "anyOf": [
+ { "type": "string" },
+ {
+ "type": "array",
+ "items": { "type": "string" }
+ }
+ ]
+ }
+ }
+ });
+ let mut request = text_request();
+ request.tools = vec![LanguageModelRequestTool::function(
+ "grep".to_string(),
+ "Search files".to_string(),
+ input_schema.clone(),
+ false,
+ )];
+
+ let request = into_google(
+ request,
+ "gemini-3.5-flash".to_string(),
+ GoogleModelMode::Default,
+ )
+ .unwrap();
+ let serialized = serde_json::to_value(request).unwrap();
+ let declaration = &serialized["tools"][0]["functionDeclarations"][0];
+
+ assert_eq!(declaration["parametersJsonSchema"], input_schema);
+ assert_eq!(declaration.get("parameters"), None);
+ }
+
#[test]
fn into_google_turns_off_budget_thinking_when_supported() {
let mut request = text_request();
diff --git a/crates/google_ai/src/google_ai.rs b/crates/google_ai/src/google_ai.rs
index fe37dafe781..5734bb2c157 100644
--- a/crates/google_ai/src/google_ai.rs
+++ b/crates/google_ai/src/google_ai.rs
@@ -452,7 +452,14 @@ pub enum FunctionCallingMode {
pub struct FunctionDeclaration {
pub name: String,
pub description: String,
- pub parameters: serde_json::Value,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub parameters: Option<serde_json::Value>,
+ #[serde(
+ rename = "parametersJsonSchema",
+ default,
+ skip_serializing_if = "Option::is_none"
+ )]
+ pub parameters_json_schema: Option<serde_json::Value>,
}
#[derive(Debug, Default)]
diff --git a/crates/language_model/src/language_model.rs b/crates/language_model/src/language_model.rs
index 995b4818e95..d4bb4d5f13d 100644
--- a/crates/language_model/src/language_model.rs
+++ b/crates/language_model/src/language_model.rs
@@ -210,10 +210,6 @@ pub trait LanguageModel: Send + Sync {
false
}
- fn tool_input_format(&self) -> LanguageModelToolSchemaFormat {
- LanguageModelToolSchemaFormat::JsonSchema
- }
-
fn max_token_count(&self) -> u64;
fn max_output_tokens(&self) -> Option<u64> {
None
diff --git a/crates/language_model_core/src/language_model_core.rs b/crates/language_model_core/src/language_model_core.rs
index d2975e18ebd..470844f8f0f 100644
--- a/crates/language_model_core/src/language_model_core.rs
+++ b/crates/language_model_core/src/language_model_core.rs
@@ -24,7 +24,6 @@ pub use crate::provider::*;
pub use crate::rate_limiter::*;
pub use crate::request::*;
pub use crate::role::*;
-pub use crate::tool_schema::LanguageModelToolSchemaFormat;
pub use crate::util::{
fix_streamed_json, is_context_window_exceeded_message, parse_prompt_too_long,
parse_tool_arguments,
diff --git a/crates/language_model_core/src/tool_schema.rs b/crates/language_model_core/src/tool_schema.rs
index 49c5d4bbe75..b03c834459d 100644
--- a/crates/language_model_core/src/tool_schema.rs
+++ b/crates/language_model_core/src/tool_schema.rs
@@ -1,183 +1,123 @@
-use anyhow::Result;
-use schemars::{
- JsonSchema, Schema,
- generate::SchemaSettings,
- transform::{Transform, transform_subschemas},
-};
-use serde_json::{Map, Value, json};
-
-/// Indicates the format used to define the input schema for a language model tool.
-#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
-pub enum LanguageModelToolSchemaFormat {
- /// A JSON schema, see https://json-schema.org
- JsonSchema,
- /// A subset of an OpenAPI 3.0 schema object supported by Google AI, see https://ai.google.dev/api/caching#Schema
- JsonSchemaSubset,
+use anyhow::{Context as _, Result, bail};
+use schemars::{JsonSchema, Schema, generate::SchemaSettings};
+use serde_json::{Map, Value};
+
+pub fn root_schema_for<T: JsonSchema>() -> Schema {
+ SchemaSettings::draft07()
+ .with(|settings| {
+ settings.meta_schema = None;
+ settings.inline_subschemas = true;
+ })
+ .into_generator()
+ .root_schema_for::<T>()
}
-pub fn root_schema_for<T: JsonSchema>(format: LanguageModelToolSchemaFormat) -> Schema {
- let mut generator = match format {
- LanguageModelToolSchemaFormat::JsonSchema => SchemaSettings::draft07()
- .with(|settings| {
- settings.meta_schema = None;
- settings.inline_subschemas = true;
- })
- .into_generator(),
- LanguageModelToolSchemaFormat::JsonSchemaSubset => SchemaSettings::openapi3()
- .with(|settings| {
- settings.meta_schema = None;
- settings.inline_subschemas = true;
- })
- .with_transform(ToJsonSchemaSubsetTransform)
- .into_generator(),
- };
- generator.root_schema_for::<T>()
-}
-
-#[derive(Debug, Clone)]
-struct ToJsonSchemaSubsetTransform;
-
-impl Transform for ToJsonSchemaSubsetTransform {
- fn transform(&mut self, schema: &mut Schema) {
- if let Some(obj) = schema.as_object_mut() {
- // `Option<T>` produces `type: [T, "null"]`. Convert to OpenAPI 3.0's
- // `nullable: true` so nullability isn't silently dropped.
- convert_null_in_types_to_nullable(obj);
+/// Removes redundant root metadata, inlines same-document references, and makes
+/// empty object inputs explicit.
+pub fn normalize_tool_schema(schema: &mut Value) {
+ inline_refs(schema);
- // Any remaining multi-type array (uncommon in Rust-generated schemas)
- // is collapsed to its first entry to keep this schema subset-compatible.
- if let Some(type_field) = obj.get_mut("type")
- && let Some(types) = type_field.as_array()
- && let Some(first_type) = types.first().cloned()
- {
- *type_field = first_type;
- }
-
- // oneOf is not supported, use anyOf instead
- if let Some(one_of) = obj.remove("oneOf") {
- obj.insert("anyOf".to_string(), one_of);
- }
- }
+ let Value::Object(object) = schema else {
+ return;
+ };
- transform_subschemas(self, schema);
+ object.remove("$schema");
+ object.remove("title");
+ object.remove("description");
- if let Some(obj) = schema.as_object_mut() {
- collapse_nullable_only_any_of(obj);
- }
+ if object.get("type").and_then(Value::as_str) == Some("object") {
+ object
+ .entry("properties")
+ .or_insert_with(|| Value::Object(Default::default()));
}
}
-/// Tries to adapt a JSON schema representation to be compatible with the specified format.
+/// Inlines same-document `$ref`s and drops the definitions they came from.
///
-/// If the json cannot be made compatible with the specified format, an error is returned.
-pub fn adapt_schema_to_format(
- json: &mut Value,
- format: LanguageModelToolSchemaFormat,
-) -> Result<()> {
- log::trace!("Adapting schema to format {:?}: {}", format, json);
-
- if let Value::Object(obj) = json {
- obj.remove("$schema");
- obj.remove("title");
- obj.remove("description");
+/// Providers disagree about references and Google rejects them outright, while
+/// context servers routinely ship them: pydantic emits `$defs` for any nested
+/// model or enum, and `zod-to-json-schema` emits `definitions` for reused
+/// shapes. Schemas built by [`root_schema_for`] never contain references,
+/// because it inlines subschemas during generation.
+///
+/// A schema whose references cannot all be resolved is left exactly as it
+/// arrived, so a provider rejects it with a description of what is wrong
+/// instead of receiving something half-rewritten.
+fn inline_refs(schema: &mut Value) {
+ let Some(root) = schema.as_object() else {
+ return;
+ };
+ if !root.contains_key("$defs") && !root.contains_key("definitions") {
+ return;
}
- resolve_refs(json)?;
-
- match format {
- LanguageModelToolSchemaFormat::JsonSchema => preprocess_json_schema(json),
- LanguageModelToolSchemaFormat::JsonSchemaSubset => adapt_to_json_schema_subset(json),
- }?;
-
- log::trace!("Adapted schema: {}", json);
- Ok(())
-}
-
-fn preprocess_json_schema(json: &mut Value) -> Result<()> {
- if let Value::Object(obj) = json
- && matches!(obj.get("type"), Some(Value::String(s)) if s == "object")
- {
- if !obj.contains_key("additionalProperties") {
- obj.insert("additionalProperties".to_string(), Value::Bool(false));
- }
-
- if !obj.contains_key("properties") {
- obj.insert("properties".to_string(), Value::Object(Default::default()));
- }
+ let mut inlined = schema.clone();
+ match inline_refs_fallibly(&mut inlined) {
+ Ok(()) => *schema = inlined,
+ Err(error) => log::warn!("leaving tool schema references unresolved: {error:#}"),
}
- Ok(())
}
-/// Inlines same-document `$ref`s from `$defs`/`definitions` and removes those.
-fn resolve_refs(json: &mut Value) -> Result<()> {
- let Some(root_obj) = json.as_object_mut() else {
+fn inline_refs_fallibly(schema: &mut Value) -> Result<()> {
+ let Some(root) = schema.as_object_mut() else {
return Ok(());
};
+ let defs = root.remove("$defs");
+ let legacy_defs = root.remove("definitions");
- let defs = root_obj.remove("$defs");
- let legacy_defs = root_obj.remove("definitions");
- if defs.is_none() && legacy_defs.is_none() {
- return Ok(());
- }
-
- resolve_refs_recursive(json, defs.as_ref(), legacy_defs.as_ref(), &mut Vec::new())
+ inline_refs_recursive(schema, defs.as_ref(), legacy_defs.as_ref(), &mut Vec::new())
}
-fn resolve_refs_recursive(
+fn inline_refs_recursive(
value: &mut Value,
defs: Option<&Value>,
legacy_defs: Option<&Value>,
visiting: &mut Vec<String>,
) -> Result<()> {
match value {
- Value::Object(obj) => {
- if let Some(ref_str) = obj.get("$ref").and_then(|v| v.as_str()) {
- // Guard against cycles (A -> B -> A, or self-referential
- // schemas like a Tree node whose children are Trees)
- if visiting.iter().any(|v| v == ref_str) {
- *obj = Map::new();
+ Value::Object(object) => {
+ if let Some(reference) = object.get("$ref").and_then(Value::as_str) {
+ // A self-referential schema, such as a tree node whose children
+ // are trees, cannot be inlined to any finite depth; an empty
+ // schema accepts anything, which is the closest available
+ // approximation.
+ if visiting.iter().any(|visited| visited == reference) {
+ *object = Map::new();
return Ok(());
}
- let (defs_key, name) = parse_ref(ref_str)?;
- let defs_for_key = match defs_key {
+ let reference = reference.to_string();
+ let (definitions_key, name) = parse_ref(&reference)?;
+ let definition = match definitions_key {
"$defs" => defs,
- "definitions" => legacy_defs,
- _ => None,
- };
- let Some(def) = defs_for_key.and_then(|defs| defs.get(name)) else {
- anyhow::bail!("$ref target not found in {defs_key}: {ref_str}");
- };
-
- let ref_owned = ref_str.to_string();
+ _ => legacy_defs,
+ }
+ .and_then(|definitions| definitions.get(name))
+ .with_context(|| format!("no {definitions_key} entry for {reference}"))?;
- // Inline the referenced definition into the current object.
- let mut resolved = def.clone();
- if let Value::Object(resolved_obj) = &mut resolved {
- for (key, val) in obj.iter() {
+ let mut resolved = definition.clone();
+ if let Value::Object(resolved) = &mut resolved {
+ for (key, sibling) in object.iter() {
if key != "$ref" {
- resolved_obj.insert(key.clone(), val.clone());
+ resolved.insert(key.clone(), sibling.clone());
}
}
}
*value = resolved;
- visiting.push(ref_owned);
- let result = resolve_refs_recursive(value, defs, legacy_defs, visiting);
+ visiting.push(reference);
+ let result = inline_refs_recursive(value, defs, legacy_defs, visiting);
visiting.pop();
return result;
}
- let keys: Vec<String> = obj.keys().cloned().collect();
- for key in keys {
- if let Some(child) = obj.get_mut(&key) {
- resolve_refs_recursive(child, defs, legacy_defs, visiting)?;
- }
+ for child in object.values_mut() {
+ inline_refs_recursive(child, defs, legacy_defs, visiting)?;
}
}
- Value::Array(arr) => {
- for item in arr.iter_mut() {
- resolve_refs_recursive(item, defs, legacy_defs, visiting)?;
+ Value::Array(items) => {
+ for item in items {
+ inline_refs_recursive(item, defs, legacy_defs, visiting)?;
}
}
_ => {}
@@ -185,880 +125,84 @@ fn resolve_refs_recursive(
Ok(())
}
-/// Parses a same-document `$ref` like `#/$defs/Foo` or `#/definitions/Foo`.
-/// Returns `(defs_key, name)` where `defs_key` is the top-level key the
-/// definition was looked up under, and `name` is the definition name.
-fn parse_ref(ref_str: &str) -> Result<(&'static str, &str)> {
- if let Some(name) = ref_str.strip_prefix("#/$defs/") {
+fn parse_ref(reference: &str) -> Result<(&'static str, &str)> {
+ if let Some(name) = reference.strip_prefix("#/$defs/") {
return Ok(("$defs", name));
}
- if let Some(name) = ref_str.strip_prefix("#/definitions/") {
+ if let Some(name) = reference.strip_prefix("#/definitions/") {
return Ok(("definitions", name));
}
- anyhow::bail!(
- "Unsupported $ref format (only `#/$defs/<name>` and `#/definitions/<name>` are supported): {ref_str}"
- );
-}
-
-fn adapt_to_json_schema_subset(json: &mut Value) -> Result<()> {
- if let Value::Object(obj) = json {
- const UNSUPPORTED_KEYS: [&str; 4] = ["if", "then", "else", "$ref"];
-
- for key in UNSUPPORTED_KEYS {
- anyhow::ensure!(
- !obj.contains_key(key),
- "Schema cannot be made compatible because it contains \"{key}\""
- );
- }
-
- const KEYS_TO_REMOVE: [(&str, fn(&Value) -> bool); 6] = [
- ("format", |value| value.is_string()),
- ("additionalProperties", |_| true),
- ("propertyNames", |_| true),
- ("exclusiveMinimum", |value| value.is_number()),
- ("exclusiveMaximum", |value| value.is_number()),
- ("optional", |value| value.is_boolean()),
- ];
- for (key, predicate) in KEYS_TO_REMOVE {
- if let Some(value) = obj.get(key)
- && predicate(value)
- {
- obj.remove(key);
- }
- }
-
- convert_null_in_types_to_nullable(obj);
- convert_types_to_any_of_defs(obj);
-
- // After the conversions above, `type` should only ever still be an array
- // if a malformed input had a single-element type array (e.g. `["string"]`).
- // Collapse it to a single value so downstream consumers see a scalar.
- if let Some(type_value) = obj.get_mut("type")
- && let Some(types) = type_value.as_array()
- && let Some(first_type) = types.first().cloned()
- {
- *type_value = first_type;
- }
-
- if matches!(obj.get("description"), Some(Value::String(_)))
- && !obj.contains_key("type")
- && !(obj.contains_key("anyOf")
- || obj.contains_key("oneOf")
- || obj.contains_key("allOf"))
- {
- obj.insert("type".to_string(), Value::String("string".to_string()));
- }
-
- if let Some(subschemas) = obj.get_mut("oneOf")
- && subschemas.is_array()
- {
- let subschemas_clone = subschemas.clone();
- obj.remove("oneOf");
- push_any_of_constraint(obj, subschemas_clone);
- }
-
- for (_, value) in obj.iter_mut() {
- if let Value::Object(_) | Value::Array(_) = value {
- adapt_to_json_schema_subset(value)?;
- }
- }
-
- // Children may have been rewritten from `{"type": "null"}` into
- // `{"nullable": true}`. Fold those into the parent so the result matches
- // OpenAPI 3.0's convention of `nullable: true` as a sibling of `type`.
- collapse_nullable_only_any_of(obj);
- } else if let Value::Array(arr) = json {
- for item in arr.iter_mut() {
- adapt_to_json_schema_subset(item)?;
- }
- }
- Ok(())
-}
-
-fn convert_null_in_types_to_nullable(obj: &mut Map<String, Value>) {
- let mut nullable_found_in_type = false;
-
- if let Some(type_entry) = obj.get_mut("type") {
- if let Some(types) = type_entry.as_array_mut() {
- let mut had_null_type = false;
- types.retain(|t| {
- if t.as_str() == Some("null") {
- had_null_type = true;
- false
- } else {
- true
- }
- });
-
- if had_null_type {
- nullable_found_in_type = true;
- if types.len() == 1 {
- *type_entry = types.remove(0);
- } else if types.is_empty() {
- obj.remove("type");
- }
- }
- } else if let Some(type_str) = type_entry.as_str() {
- if type_str == "null" {
- nullable_found_in_type = true;
- obj.remove("type");
- }
- }
- }
- if nullable_found_in_type {
- obj.insert("nullable".to_string(), Value::Bool(true));
- }
-}
-
-fn convert_types_to_any_of_defs(obj: &mut Map<String, Value>) {
- let is_multi_type = obj
- .get("type")
- .and_then(|v| v.as_array())
- .is_some_and(|types| types.len() > 1);
- if !is_multi_type {
- return;
- }
-
- let Some(Value::Array(types)) = obj.remove("type") else {
- return;
- };
- let any_of_schemas = types.into_iter().map(|t| json!({"type": t})).collect();
- push_any_of_constraint(obj, Value::Array(any_of_schemas));
-}
-
-fn push_any_of_constraint(obj: &mut Map<String, Value>, any_of_schemas: Value) {
- if let Some(existing_any_of) = obj.remove("anyOf") {
- let mut all_of = match obj.remove("allOf") {
- Some(Value::Array(arr)) => arr,
- _ => Vec::new(),
- };
- // Always preserve the pre-existing `anyOf` — earlier this push was
- // skipped when `allOf` was non-empty, which silently dropped it.
- all_of.push(json!({"anyOf": existing_any_of}));
- all_of.push(json!({"anyOf": any_of_schemas}));
- obj.insert("allOf".to_string(), Value::Array(all_of));
- } else if let Some(all_of) = obj.get_mut("allOf").and_then(|v| v.as_array_mut()) {
- all_of.push(json!({"anyOf": any_of_schemas}));
- } else {
- obj.insert("anyOf".to_string(), any_of_schemas);
- }
-}
-
-/// Folds `{nullable: true}`-only entries out of an `anyOf` array and onto the
-/// parent object. This matches OpenAPI 3.0 semantics, where nullability is
-/// expressed as a sibling of `type` rather than a separate variant.
-fn collapse_nullable_only_any_of(obj: &mut Map<String, Value>) {
- let Some(Value::Array(mut any_of)) = obj.remove("anyOf") else {
- return;
- };
-
- let mut found_nullable_only = false;
- any_of.retain(|entry| {
- let is_nullable_only = entry
- .as_object()
- .is_some_and(|m| m.len() == 1 && matches!(m.get("nullable"), Some(Value::Bool(true))));
- if is_nullable_only {
- found_nullable_only = true;
- false
- } else {
- true
- }
- });
-
- if !found_nullable_only {
- obj.insert("anyOf".to_string(), Value::Array(any_of));
- return;
- }
-
- obj.insert("nullable".to_string(), Value::Bool(true));
-
- if any_of.is_empty() {
- return;
- }
-
- // If a single variant remains and its keys don't collide with the parent's
- // existing keys, inline it. `anyOf` with a single entry is equivalent to
- // just that entry, and inlining produces the canonical OpenAPI form
- // (e.g. `{type: "string", nullable: true}`).
- if any_of.len() == 1
- && let Value::Object(entry_obj) = &any_of[0]
- && entry_obj.keys().all(|k| !obj.contains_key(k))
- {
- let entry = any_of.remove(0);
- if let Value::Object(entry_obj) = entry {
- for (k, v) in entry_obj {
- obj.insert(k, v);
- }
- }
- return;
- }
-
- obj.insert("anyOf".to_string(), Value::Array(any_of));
+ bail!("only `#/$defs/<name>` and `#/definitions/<name>` are supported, got {reference}")
}
#[cfg(test)]
mod tests {
- use super::*;
use pretty_assertions::assert_eq;
use serde_json::json;
- fn obj(value: Value) -> Map<String, Value> {
- match value {
- Value::Object(map) => map,
- other => panic!("expected JSON object, got {other}"),
- }
- }
-
- #[test]
- fn test_convert_null_in_types_to_nullable() {
- // ["string", "null"] -> "string", nullable: true
- let mut o = obj(json!({"type": ["string", "null"]}));
- convert_null_in_types_to_nullable(&mut o);
- assert_eq!(o, obj(json!({"type": "string", "nullable": true})));
-
- // "null" -> nullable: true
- let mut o = obj(json!({"type": "null"}));
- convert_null_in_types_to_nullable(&mut o);
- assert_eq!(o, obj(json!({"nullable": true})));
-
- // ["string", "number", "null"] -> ["string", "number"], nullable: true (anyOf handled elsewhere)
- let mut o = obj(json!({"type": ["string", "number", "null"]}));
- convert_null_in_types_to_nullable(&mut o);
- assert_eq!(
- o,
- obj(json!({"type": ["string", "number"], "nullable": true}))
- );
-
- // "string" (no change, not nullable)
- let mut o = obj(json!({"type": "string"}));
- convert_null_in_types_to_nullable(&mut o);
- assert_eq!(o, obj(json!({"type": "string"})));
-
- // ["string", "number"] (no change, not nullable)
- let mut o = obj(json!({"type": ["string", "number"]}));
- convert_null_in_types_to_nullable(&mut o);
- assert_eq!(o, obj(json!({"type": ["string", "number"]})));
-
- // object with other properties, ["boolean", "null"]
- let mut o = obj(json!({
- "description": "A test field",
- "type": ["boolean", "null"]
- }));
- convert_null_in_types_to_nullable(&mut o);
- assert_eq!(
- o,
- obj(json!({
- "description": "A test field",
- "type": "boolean",
- "nullable": true
- }))
- );
- }
-
- #[test]
- fn test_convert_types_to_any_of_defs() {
- // ["string", "number"] -> anyOf with string and number
- let mut o = obj(json!({"type": ["string", "number"]}));
- convert_types_to_any_of_defs(&mut o);
- assert_eq!(
- o,
- obj(json!({
- "anyOf": [
- {"type": "string"},
- {"type": "number"}
- ]
- }))
- );
-
- // "string" (no change)
- let mut o = obj(json!({"type": "string"}));
- convert_types_to_any_of_defs(&mut o);
- assert_eq!(o, obj(json!({"type": "string"})));
-
- // single-element array (no change, fallback in caller collapses it)
- let mut o = obj(json!({"type": ["string"]}));
- convert_types_to_any_of_defs(&mut o);
- assert_eq!(o, obj(json!({"type": ["string"]})));
-
- // object with other properties, ["string", "number"]
- let mut o = obj(json!({
- "description": "A test field",
- "type": ["string", "number"]
- }));
- convert_types_to_any_of_defs(&mut o);
- assert_eq!(
- o,
- obj(json!({
- "description": "A test field",
- "anyOf": [
- {"type": "string"},
- {"type": "number"}
- ]
- }))
- );
-
- // anyOf already present (no change)
- let mut o = obj(json!({
- "anyOf": [
- {"type": "string"},
- {"type": "number"}
- ]
- }));
- convert_types_to_any_of_defs(&mut o);
- assert_eq!(
- o,
- obj(json!({
- "anyOf": [
- {"type": "string"},
- {"type": "number"}
- ]
- }))
- );
-
- // both type array and anyOf present
- let mut o = obj(json!({
- "type": ["string", "number"],
- "anyOf": [
- {"format": "email"}
- ]
- }));
- convert_types_to_any_of_defs(&mut o);
- assert_eq!(
- o,
- obj(json!({
- "allOf": [
- {
- "anyOf": [
- {"format": "email"}
- ]
- },
- {
- "anyOf": [
- {"type": "string"},
- {"type": "number"}
- ]
- }
- ]
- }))
- );
-
- // type array + anyOf + pre-existing allOf: pre-existing anyOf must not
- // be silently dropped just because allOf is non-empty.
- let mut o = obj(json!({
- "type": ["string", "number"],
- "anyOf": [{"minLength": 5}],
- "allOf": [{"maxLength": 100}]
- }));
- convert_types_to_any_of_defs(&mut o);
- assert_eq!(
- o,
- obj(json!({
- "allOf": [
- {"maxLength": 100},
- {"anyOf": [{"minLength": 5}]},
- {"anyOf": [{"type": "string"}, {"type": "number"}]}
- ]
- }))
- );
- }
-
- #[test]
- fn test_transform_adds_type_when_missing() {
- let mut json = json!({
- "description": "A test field without type"
- });
-
- adapt_to_json_schema_subset(&mut json).unwrap();
-
- assert_eq!(
- json,
- json!({
- "description": "A test field without type",
- "type": "string"
- })
- );
-
- let mut json = json!({
- "description": {
- "value": "abc",
- "type": "string"
- }
- });
-
- adapt_to_json_schema_subset(&mut json).unwrap();
-
- assert_eq!(
- json,
- json!({
- "description": {
- "value": "abc",
- "type": "string"
- }
- })
- );
- }
-
- #[test]
- fn test_transform_removes_unsupported_keys() {
- let mut json = json!({
- "description": "A test field",
- "type": "integer",
- "format": "uint32",
- "exclusiveMinimum": 0,
- "exclusiveMaximum": 100,
- "additionalProperties": false,
- "optional": true
- });
-
- adapt_to_json_schema_subset(&mut json).unwrap();
-
- assert_eq!(
- json,
- json!({
- "description": "A test field",
- "type": "integer"
- })
- );
-
- let mut json = json!({
- "description": "A test field",
- "type": "integer",
- "format": {},
- });
-
- adapt_to_json_schema_subset(&mut json).unwrap();
-
- assert_eq!(
- json,
- json!({
- "description": "A test field",
- "type": "integer",
- "format": {},
- })
- );
-
- let mut json = json!({
- "type": "object",
- "properties": {
- "name": { "type": "string" }
- },
- "additionalProperties": { "type": "string" },
- "propertyNames": { "pattern": "^[A-Za-z]+$" }
- });
-
- adapt_to_json_schema_subset(&mut json).unwrap();
-
- assert_eq!(
- json,
- json!({
- "type": "object",
- "properties": {
- "name": { "type": "string" }
- }
- })
- );
- }
-
- #[test]
- fn test_transform_null_in_any_of() {
- let mut json = json!({
- "anyOf": [
- { "type": "string" },
- { "type": "null" }
- ]
- });
-
- adapt_to_json_schema_subset(&mut json).unwrap();
-
- // Should collapse to the canonical OpenAPI 3.0 form: `nullable: true`
- // as a sibling of `type`, rather than a separate anyOf variant.
- assert_eq!(
- json,
- json!({
- "type": "string",
- "nullable": true
- })
- );
- }
-
- #[test]
- fn test_transform_null_in_any_of_with_multiple_non_null_variants() {
- let mut json = json!({
- "anyOf": [
- { "type": "string" },
- { "type": "number" },
- { "type": "null" }
- ]
- });
-
- adapt_to_json_schema_subset(&mut json).unwrap();
-
- // When more than one non-null variant remains, keep the anyOf and just
- // hoist `nullable: true` onto the parent.
- assert_eq!(
- json,
- json!({
- "nullable": true,
- "anyOf": [
- { "type": "string" },
- { "type": "number" }
- ]
- })
- );
- }
-
- #[test]
- fn test_transform_conflicting_any_of_sources() {
- let mut json = json!({
- "type": ["string", "number"],
- "anyOf": [
- { "minLength": 5 }
- ],
- "oneOf": [
- { "pattern": "^a" },
- { "pattern": "^b" }
- ]
- });
-
- adapt_to_json_schema_subset(&mut json).unwrap();
-
- assert_eq!(
- json,
- json!({
- "allOf": [
- {
- "anyOf": [
- { "minLength": 5 },
- ]
- },
- {
- "anyOf": [
- {"type": "string"},
- {"type": "number"}
- ]
- },
- {
- "anyOf": [
- { "pattern": "^a" },
- { "pattern": "^b" }
- ]
- }
- ]
- })
- );
- }
-
- #[test]
- fn test_transform_one_of_to_any_of() {
- let mut json = json!({
- "description": "A test field",
- "oneOf": [
- { "type": "string" },
- { "type": "integer" }
- ]
- });
-
- adapt_to_json_schema_subset(&mut json).unwrap();
-
- assert_eq!(
- json,
- json!({
- "description": "A test field",
- "anyOf": [
- { "type": "string" },
- { "type": "integer" }
- ]
- })
- );
- }
-
- #[test]
- fn test_transform_nested_objects() {
- let mut json = json!({
- "type": "object",
- "properties": {
- "nested": {
- "oneOf": [
- { "type": "string" },
- { "type": "null" }
- ],
- "format": "email"
- }
- }
- });
-
- adapt_to_json_schema_subset(&mut json).unwrap();
-
- assert_eq!(
- json,
- json!({
- "type": "object",
- "properties": {
- "nested": {
- "type": "string",
- "nullable": true
- }
- }
- })
- );
- }
+ use super::*;
#[test]
- fn test_transform_array_type_to_single_type() {
- let mut json = json!({
+ fn normalizes_tool_schema_without_changing_validation_keywords() {
+ let mut schema = json!({
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
+ "title": "Search input",
+ "description": "Searches files",
"type": "object",
- "properties": {
- "projectSlugOrId": {
- "type": ["string", "number"],
- "description": "Project slug or numeric ID"
- },
- "optionalName": {
- "type": ["string", "null"],
- "description": "An optional name"
- }
- }
+ "additionalProperties": true,
+ "if": { "required": ["query"] },
+ "then": { "required": ["limit"] }
});
- adapt_to_json_schema_subset(&mut json).unwrap();
+ normalize_tool_schema(&mut schema);
assert_eq!(
- json,
+ schema,
json!({
"type": "object",
- "properties": {
- "projectSlugOrId": {
- "anyOf": [
- {"type": "string"},
- {"type": "number"}
- ],
- "description": "Project slug or numeric ID"
- },
- "optionalName": {
- "type": "string",
- "description": "An optional name",
- "nullable": true
- }
- }
+ "properties": {},
+ "additionalProperties": true,
+ "if": { "required": ["query"] },
+ "then": { "required": ["limit"] }
})
);
}
#[test]
- fn test_transform_fails_if_unsupported_keys_exist() {
- let mut json = json!({
- "type": "object",
- "properties": {
- "$ref": "#/definitions/User",
- }
- });
-
- assert!(adapt_to_json_schema_subset(&mut json).is_err());
-
- let mut json = json!({
- "type": "object",
- "properties": {
- "if": "...",
- }
- });
-
- assert!(adapt_to_json_schema_subset(&mut json).is_err());
-
- let mut json = json!({
- "type": "object",
- "properties": {
- "then": "...",
- }
- });
-
- assert!(adapt_to_json_schema_subset(&mut json).is_err());
-
- let mut json = json!({
- "type": "object",
- "properties": {
- "else": "...",
- }
- });
-
- assert!(adapt_to_json_schema_subset(&mut json).is_err());
- }
-
- #[test]
- fn test_refs_are_resolved_via_adapt_schema_to_format() {
- let mut json = json!({
- "type": "object",
- "properties": {
- "parent": {
- "$ref": "#/$defs/pageParent"
- },
- "title": {
- "type": "string",
- "description": "Page title"
- }
- },
- "required": ["parent"],
+ fn inlines_the_references_context_servers_generate() {
+ // The shape pydantic produces for a tool taking a nested model with an
+ // enum field, which the MCP Python SDK derives from a typed handler.
+ let mut schema = json!({
"$defs": {
- "pageParent": {
+ "Filter": {
"type": "object",
- "properties": {
- "type": {
- "type": "string",
- "description": "Parent type"
- }
- },
- "required": ["type"]
- }
- }
- });
-
- adapt_schema_to_format(&mut json, LanguageModelToolSchemaFormat::JsonSchemaSubset).unwrap();
-
- let expected = json!({
- "type": "object",
- "properties": {
- "parent": {
- "type": "object",
- "properties": {
- "type": {
- "type": "string",
- "description": "Parent type"
- }
- },
- "required": ["type"]
+ "properties": { "severity": { "$ref": "#/$defs/Severity" } }
},
- "title": {
- "type": "string",
- "description": "Page title"
- }
+ "Severity": { "type": "string", "enum": ["low", "high"] }
},
- "required": ["parent"],
- });
- assert_eq!(json, expected);
- }
-
- #[test]
- fn test_refs_fail_for_unsupported_prefix() {
- let mut json = json!({
"type": "object",
"properties": {
- "child": {
- "$ref": "https://example.com/schema.json#/User"
- }
- },
- "$defs": {
- "User": { "type": "string" }
+ "filter": { "anyOf": [{ "$ref": "#/$defs/Filter" }, { "type": "null" }] }
}
});
- assert!(
- adapt_schema_to_format(&mut json, LanguageModelToolSchemaFormat::JsonSchemaSubset)
- .is_err()
- );
- }
-
- #[test]
- fn test_refs_fail_for_missing_definition() {
- let mut json = json!({
- "type": "object",
- "properties": {
- "child": {
- "$ref": "#/$defs/NonExistent"
- }
- },
- "$defs": {
- "Existing": { "type": "string" }
- }
- });
+ normalize_tool_schema(&mut schema);
- assert!(
- adapt_schema_to_format(&mut json, LanguageModelToolSchemaFormat::JsonSchemaSubset)
- .is_err()
- );
- }
-
- #[test]
- fn test_refs_in_defs_are_resolved() {
- // A definition that itself references another definition.
- let mut json = json!({
- "type": "object",
- "properties": {
- "parent": {
- "$ref": "#/$defs/pageParent"
- }
- },
- "$defs": {
- "pageParent": {
- "type": "object",
- "properties": {
- "database_id": {
- "$ref": "#/$defs/databaseId"
- }
- }
- },
- "databaseId": {
- "type": "string",
- "description": "A database ID"
- }
- }
- });
-
- adapt_schema_to_format(&mut json, LanguageModelToolSchemaFormat::JsonSchemaSubset).unwrap();
-
- // The nested $ref in pageParent -> databaseId should be resolved.
assert_eq!(
- json,
+ schema,
json!({
"type": "object",
"properties": {
- "parent": {
- "type": "object",
- "properties": {
- "database_id": {
- "type": "string",
- "description": "A database ID"
- }
- }
- }
- }
- })
- );
- }
-
- #[test]
- fn test_refs_resolve_when_both_defs_and_definitions_exist() {
- let mut json = json!({
- "type": "object",
- "properties": {
- "modern": {
- "$ref": "#/$defs/Modern"
- },
- "legacy": {
- "$ref": "#/definitions/Legacy"
- }
- },
- "$defs": {
- "Modern": {
- "type": "string"
- }
- },
- "definitions": {
- "Legacy": {
- "type": "number"
- }
- }
- });
-
- adapt_schema_to_format(&mut json, LanguageModelToolSchemaFormat::JsonSchemaSubset).unwrap();
-
- assert_eq!(
- json,
- json!({
- "type": "object",
- "properties": {
- "modern": {
- "type": "string"
- },
- "legacy": {
- "type": "number"
+ "filter": {
+ "anyOf": [
+ {
+ "type": "object",
+ "properties": {
+ "severity": { "type": "string", "enum": ["low", "high"] }
+ }
+ },
+ { "type": "null" }
+ ]
}
}
})
@@ -1066,175 +210,83 @@ mod tests {
}
#[test]
- fn test_refs_in_array_items_are_resolved() {
- let mut json = json!({
+ fn inlines_legacy_definitions_and_keeps_sibling_keywords() {
+ let mut schema = json!({
+ "definitions": { "Glob": { "type": "string" } },
"type": "object",
"properties": {
- "items": {
- "type": "array",
- "items": {
- "$ref": "#/$defs/itemDef"
- }
- }
- },
- "$defs": {
- "itemDef": {
- "type": "string",
- "description": "An item"
- }
+ "glob": { "$ref": "#/definitions/Glob", "description": "a pattern" }
}
});
- adapt_schema_to_format(&mut json, LanguageModelToolSchemaFormat::JsonSchemaSubset).unwrap();
+ normalize_tool_schema(&mut schema);
assert_eq!(
- json,
- json!({
- "type": "object",
- "properties": {
- "items": {
- "type": "array",
- "items": {
- "type": "string",
- "description": "An item"
- }
- }
- }
- })
+ schema["properties"]["glob"],
+ json!({ "type": "string", "description": "a pattern" })
);
+ assert_eq!(schema.get("definitions"), None);
}
#[test]
- fn test_self_referential_ref_is_replaced_with_empty_schema() {
- // A common pattern: a Tree node with children of the same type.
- let mut json = json!({
- "type": "object",
- "properties": {
- "root": { "$ref": "#/$defs/Tree" }
- },
+ fn replaces_a_recursive_reference_with_an_unconstrained_schema() {
+ let mut schema = json!({
"$defs": {
- "Tree": {
+ "Node": {
"type": "object",
- "properties": {
- "value": { "type": "string" },
- "children": {
- "type": "array",
- "items": { "$ref": "#/$defs/Tree" }
- }
- }
+ "properties": { "child": { "$ref": "#/$defs/Node" } }
}
- }
+ },
+ "type": "object",
+ "properties": { "root": { "$ref": "#/$defs/Node" } }
});
- adapt_schema_to_format(&mut json, LanguageModelToolSchemaFormat::JsonSchemaSubset)
- .expect("self-referential $ref should not error");
+ normalize_tool_schema(&mut schema);
assert_eq!(
- json,
- json!({
- "type": "object",
- "properties": {
- "root": {
- "type": "object",
- "properties": {
- "value": { "type": "string" },
- "children": {
- "type": "array",
- "items": {}
- }
- }
- }
- }
- })
+ schema["properties"]["root"],
+ json!({ "type": "object", "properties": { "child": {} } })
);
}
#[test]
- fn test_ref_sibling_properties_are_preserved() {
- // JSON Schema draft 2019-09+ allows sibling properties alongside
- // `$ref`. They must be merged into the resolved definition rather than
- // discarded, with siblings overriding the definition's keys.
- let mut json = json!({
+ fn leaves_a_schema_untouched_when_a_reference_cannot_be_resolved() {
+ let unresolvable = json!({
+ "$defs": { "Known": { "type": "string" } },
"type": "object",
- "properties": {
- "child": {
- "$ref": "#/$defs/childDef",
- "description": "Local description overrides def"
- }
- },
- "$defs": {
- "childDef": {
- "type": "string",
- "description": "Def description",
- "minLength": 1
- }
- }
+ "properties": { "value": { "$ref": "#/$defs/Missing" } }
});
+ let mut schema = unresolvable.clone();
- adapt_schema_to_format(&mut json, LanguageModelToolSchemaFormat::JsonSchemaSubset).unwrap();
+ normalize_tool_schema(&mut schema);
- assert_eq!(
- json["properties"]["child"],
- json!({
- "type": "string",
- "description": "Local description overrides def",
- "minLength": 1
- })
- );
+ assert_eq!(schema, unresolvable);
}
#[test]
- fn test_preprocess_json_schema_adds_additional_properties() {
- let mut json = json!({
- "type": "object",
- "properties": {
- "name": {
- "type": "string"
- }
- }
+ fn normalization_is_idempotent() {
+ let mut schema = json!({
+ "title": "Empty input",
+ "type": "object"
});
- preprocess_json_schema(&mut json).unwrap();
+ normalize_tool_schema(&mut schema);
+ let normalized = schema.clone();
+ normalize_tool_schema(&mut schema);
- assert_eq!(
- json,
- json!({
- "type": "object",
- "properties": {
- "name": {
- "type": "string"
- }
- },
- "additionalProperties": false
- })
- );
+ assert_eq!(schema, normalized);
}
#[test]
- fn test_preprocess_json_schema_preserves_additional_properties() {
- let mut json = json!({
- "type": "object",
- "properties": {
- "name": {
- "type": "string"
- }
- },
- "additionalProperties": true
+ fn normalization_leaves_non_object_schemas_unchanged_except_for_metadata() {
+ let mut schema = json!({
+ "title": "Text input",
+ "description": "Accepts text",
+ "type": "string"
});
- preprocess_json_schema(&mut json).unwrap();
+ normalize_tool_schema(&mut schema);
- assert_eq!(
- json,
- json!({
- "type": "object",
- "properties": {
- "name": {
- "type": "string"
- }
- },
- "additionalProperties": true
- })
- );
+ assert_eq!(schema, json!({ "type": "string" }));
}
}
diff --git a/crates/language_models/src/provider/bedrock.rs b/crates/language_models/src/provider/bedrock.rs
index d8577fcae14..5710b7ef92c 100644
--- a/crates/language_models/src/provider/bedrock.rs
+++ b/crates/language_models/src/provider/bedrock.rs
@@ -44,9 +44,9 @@ use language_model::{
LanguageModelCompletionError, LanguageModelCompletionEvent, LanguageModelEffortLevel,
LanguageModelId, LanguageModelName, LanguageModelProvider, LanguageModelProviderId,
LanguageModelProviderName, LanguageModelProviderState, LanguageModelRequest,
- LanguageModelToolChoice, LanguageModelToolResultContent, LanguageModelToolSchemaFormat,
- LanguageModelToolUse, MessageContent, ProviderErrorCategory, ProviderSettingsView, RateLimiter,
- Role, SubPageProviderSettings, TokenUsage, env_var,
+ LanguageModelToolChoice, LanguageModelToolResultContent, LanguageModelToolUse, MessageContent,
+ ProviderErrorCategory, ProviderSettingsView, RateLimiter, Role, SubPageProviderSettings,
+ TokenUsage, env_var,
};
use open_ai::responses::Request as OpenAiResponseRequest;
use open_ai::responses::{ResponseOutputItem, StreamEvent as OpenAiResponseStreamEvent};
@@ -1862,10 +1862,6 @@ impl LanguageModel for BedrockMantleModel {
self.model.supports_tools()
}
- fn tool_input_format(&self) -> LanguageModelToolSchemaFormat {
- LanguageModelToolSchemaFormat::JsonSchemaSubset
- }
-
fn supports_images(&self) -> bool {
self.model.supports_images()
}
diff --git a/crates/language_models/src/provider/google.rs b/crates/language_models/src/provider/google.rs
index e930405c118..f7a50f8ee94 100644
--- a/crates/language_models/src/provider/google.rs
+++ b/crates/language_models/src/provider/google.rs
@@ -8,7 +8,7 @@ use gpui::{App, AppContext, AsyncApp, Context, Entity, SharedString, Task};
use http_client::{CustomHeaders, HttpClient};
use language_model::{
ApiKeyConfiguration, AuthenticateError, EnvVar, LanguageModelCompletionError,
- LanguageModelCompletionEvent, LanguageModelToolChoice, LanguageModelToolSchemaFormat,
+ LanguageModelCompletionEvent, LanguageModelToolChoice,
};
use language_model::{
GOOGLE_PROVIDER_ID, GOOGLE_PROVIDER_NAME, IconOrSvg, LanguageModel, LanguageModelEffortLevel,
@@ -328,10 +328,6 @@ impl LanguageModel for GoogleLanguageModel {
}
}
- fn tool_input_format(&self) -> LanguageModelToolSchemaFormat {
- LanguageModelToolSchemaFormat::JsonSchemaSubset
- }
-
fn telemetry_id(&self) -> String {
format!("google/{}", self.model.request_id())
}
diff --git a/crates/language_models/src/provider/open_ai_compatible.rs b/crates/language_models/src/provider/open_ai_compatible.rs
index f725ba981fe..47a109b04db 100644
--- a/crates/language_models/src/provider/open_ai_compatible.rs
+++ b/crates/language_models/src/provider/open_ai_compatible.rs
@@ -8,7 +8,7 @@ use language_model::{
LanguageModelCompletionEvent, LanguageModelEffortLevel, LanguageModelId, LanguageModelName,
LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderName,
LanguageModelProviderState, LanguageModelRequest, LanguageModelToolChoice,
- LanguageModelToolSchemaFormat, ProviderSettingsView, RateLimiter, SubPageProviderSettings,
+ ProviderSettingsView, RateLimiter, SubPageProviderSettings,
};
use open_ai::{
ResponseStreamEvent,
@@ -358,10 +358,6 @@ impl LanguageModel for OpenAiCompatibleLanguageModel {
self.model.capabilities.tools
}
- fn tool_input_format(&self) -> LanguageModelToolSchemaFormat {
- LanguageModelToolSchemaFormat::JsonSchemaSubset
- }
-
fn supports_images(&self) -> bool {
self.model.capabilities.images
}
diff --git a/crates/language_models/src/provider/open_router.rs b/crates/language_models/src/provider/open_router.rs
index e05c47beccf..381ff36bd1d 100644
--- a/crates/language_models/src/provider/open_router.rs
+++ b/crates/language_models/src/provider/open_router.rs
@@ -9,9 +9,8 @@ use language_model::{
LanguageModelCompletionError, LanguageModelCompletionEvent, LanguageModelEffortLevel,
LanguageModelId, LanguageModelName, LanguageModelProvider, LanguageModelProviderId,
LanguageModelProviderName, LanguageModelProviderState, LanguageModelRequest,
- LanguageModelToolChoice, LanguageModelToolResultContent, LanguageModelToolSchemaFormat,
- LanguageModelToolUse, MessageContent, ProviderSettingsView, RateLimiter, Role, StopReason,
- TokenUsage, env_var,
+ LanguageModelToolChoice, LanguageModelToolResultContent, LanguageModelToolUse, MessageContent,
+ ProviderSettingsView, RateLimiter, Role, StopReason, TokenUsage, env_var,
};
use open_ai::completion::ReasoningDetailsAccumulator;
use open_router::{
@@ -400,15 +399,6 @@ impl LanguageModel for OpenRouterLanguageModel {
.collect()
}
- fn tool_input_format(&self) -> LanguageModelToolSchemaFormat {
- let model_id = self.model.id().trim().to_lowercase();
- if model_id.contains("gemini") || model_id.contains("grok") {
- LanguageModelToolSchemaFormat::JsonSchemaSubset
- } else {
- LanguageModelToolSchemaFormat::JsonSchema
- }
- }
-
fn telemetry_id(&self) -> String {
format!("openrouter/{}", self.model.id())
}
diff --git a/crates/language_models/src/provider/vercel_ai_gateway.rs b/crates/language_models/src/provider/vercel_ai_gateway.rs
index b13b022aa9d..ede1e45a341 100644
--- a/crates/language_models/src/provider/vercel_ai_gateway.rs
+++ b/crates/language_models/src/provider/vercel_ai_gateway.rs
@@ -11,7 +11,7 @@ use language_model::{
LanguageModelCompletionError, LanguageModelCompletionEvent, LanguageModelId, LanguageModelName,
LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderName,
LanguageModelProviderState, LanguageModelRequest, LanguageModelToolChoice,
- LanguageModelToolSchemaFormat, ProviderSettingsView, RateLimiter, env_var,
+ ProviderSettingsView, RateLimiter, env_var,
};
use open_ai::ResponseStreamEvent;
use serde::Deserialize;
@@ -401,10 +401,6 @@ impl LanguageModel for VercelAiGatewayLanguageModel {
self.model.capabilities.tools
}
- fn tool_input_format(&self) -> LanguageModelToolSchemaFormat {
- LanguageModelToolSchemaFormat::JsonSchemaSubset
- }
-
fn supports_images(&self) -> bool {
self.model.capabilities.images
}
diff --git a/crates/language_models/src/provider/x_ai.rs b/crates/language_models/src/provider/x_ai.rs
index e7ba81ef706..711f71afa0f 100644
--- a/crates/language_models/src/provider/x_ai.rs
+++ b/crates/language_models/src/provider/x_ai.rs
@@ -9,8 +9,7 @@ use language_model::{
LanguageModelCompletionError, LanguageModelCompletionEvent, LanguageModelEffortLevel,
LanguageModelId, LanguageModelName, LanguageModelProvider, LanguageModelProviderId,
LanguageModelProviderName, LanguageModelProviderState, LanguageModelRequest,
- LanguageModelToolChoice, LanguageModelToolSchemaFormat, ProviderSettingsView, RateLimiter,
- env_var,
+ LanguageModelToolChoice, ProviderSettingsView, RateLimiter, env_var,
};
use open_ai::ResponseStreamEvent;
pub use settings::XaiAvailableModel as AvailableModel;
@@ -384,14 +383,6 @@ impl LanguageModel for XAiLanguageModel {
supported_thinking_effort_levels(&self.model)
}
- fn tool_input_format(&self) -> LanguageModelToolSchemaFormat {
- if self.model.requires_json_schema_subset() {
- LanguageModelToolSchemaFormat::JsonSchemaSubset
- } else {
- LanguageModelToolSchemaFormat::JsonSchema
- }
- }
-
fn telemetry_id(&self) -> String {
format!("x_ai/{}", self.model.id())
}
diff --git a/crates/language_models_cloud/src/language_models_cloud.rs b/crates/language_models_cloud/src/language_models_cloud.rs
index 8c5ce5104b7..50a69171e0f 100644
--- a/crates/language_models_cloud/src/language_models_cloud.rs
+++ b/crates/language_models_cloud/src/language_models_cloud.rs
@@ -23,9 +23,9 @@ use language_model::{
CompactionResult, DisabledReason, GOOGLE_PROVIDER_ID, GOOGLE_PROVIDER_NAME, LanguageModel,
LanguageModelCompletionError, LanguageModelCompletionEvent, LanguageModelEffortLevel,
LanguageModelId, LanguageModelName, LanguageModelProviderId, LanguageModelProviderName,
- LanguageModelRequest, LanguageModelToolChoice, LanguageModelToolSchemaFormat,
- OPEN_AI_PROVIDER_ID, OPEN_AI_PROVIDER_NAME, ProviderErrorCategory, RateLimiter,
- X_AI_PROVIDER_ID, X_AI_PROVIDER_NAME, ZED_CLOUD_PROVIDER_ID, ZED_CLOUD_PROVIDER_NAME,
+ LanguageModelRequest, LanguageModelToolChoice, OPEN_AI_PROVIDER_ID, OPEN_AI_PROVIDER_NAME,
+ ProviderErrorCategory, RateLimiter, X_AI_PROVIDER_ID, X_AI_PROVIDER_NAME,
+ ZED_CLOUD_PROVIDER_ID, ZED_CLOUD_PROVIDER_NAME,
};
use schemars::JsonSchema;
@@ -680,20 +680,6 @@ impl<TP: CloudLlmTokenProvider + 'static> LanguageModel for CloudLanguageModel<T
format!("zed.dev/{}", self.model.id)
}
- fn tool_input_format(&self) -> LanguageModelToolSchemaFormat {
- match self.model.provider {
- cloud_llm_client::LanguageModelProvider::Anthropic
- | cloud_llm_client::LanguageModelProvider::Baseten
- | cloud_llm_client::LanguageModelProvider::OpenAi => {
- LanguageModelToolSchemaFormat::JsonSchema
- }
- cloud_llm_client::LanguageModelProvider::Google
- | cloud_llm_client::LanguageModelProvider::XAi => {
- LanguageModelToolSchemaFormat::JsonSchemaSubset
- }
- }
- }
-
fn max_token_count(&self) -> u64 {
self.model.max_token_count as u64
}
diff --git a/crates/x_ai/src/x_ai.rs b/crates/x_ai/src/x_ai.rs
index 4c646efff03..ab254c6f630 100644
--- a/crates/x_ai/src/x_ai.rs
+++ b/crates/x_ai/src/x_ai.rs
@@ -106,17 +106,6 @@ impl Model {
}
}
- pub fn requires_json_schema_subset(&self) -> bool {
- match self {
- Self::Grok43
- | Self::Grok45
- | Self::Grok46
- | Self::Grok420Reasoning
- | Self::Grok420NonReasoning => true,
- Self::Custom { .. } => false,
- }
- }
-
pub fn supports_prompt_cache_key(&self) -> bool {
false
}