Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

Add and use schemars lib (#657)

Aleksandr Golokoz6838296ee1a0
diff --git a/Cargo.lock b/Cargo.lock
index 63d10689d..063b3031c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3766,6 +3766,7 @@ dependencies = [
"pyo3-build-config",
"pyo3-stub-gen",
"reqwest 0.13.4",
+ "schemars 1.2.1",
"serde",
"serde-wasm-bindgen",
"serde_json",
@@ -4678,7 +4679,6 @@ dependencies = [
"proc-macro-crate",
"proc-macro2",
"quote",
- "serde_derive_internals",
"syn 2.0.118",
]
diff --git a/Cargo.toml b/Cargo.toml
index c1af03faa..21a27e8c6 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -66,7 +66,6 @@ xgrammar = { package = "xgrammar-rs", git = "https://github.com/trymirai/xgramma
half = { version = "2.7", features = ["bytemuck", "num-traits"] }
ndarray = "0.17"
serde = { version = "1.0", features = ["derive"] }
-serde_derive_internals = "0.29"
serde_json = "1.0"
monostate = "0.1"
thiserror = "2.0"
@@ -97,7 +96,7 @@ rand = "0.10"
rand_distr = "0.6.0"
sysinfo = "0.38"
itertools = "0.14"
-schemars = { version = "1.2", features = ["derive"] }
+schemars = { version = "1.2", features = ["derive", "preserve_order"] }
is_close = "0.1"
anyhow = "1.0.102"
async-trait = "0.1"
diff --git a/crates/nagare/Cargo.toml b/crates/nagare/Cargo.toml
index c10399daa..97c284a86 100644
--- a/crates/nagare/Cargo.toml
+++ b/crates/nagare/Cargo.toml
@@ -57,6 +57,7 @@ shoji.workspace = true
hanashi.workspace = true
serde.workspace = true
serde_json.workspace = true
+schemars.workspace = true
chrono.workspace = true
tracing.workspace = true
thiserror.workspace = true
diff --git a/crates/nagare/src/tool/func_def.rs b/crates/nagare/src/tool/func_def.rs
index 24c8c20b6..9126303fa 100644
--- a/crates/nagare/src/tool/func_def.rs
+++ b/crates/nagare/src/tool/func_def.rs
@@ -85,16 +85,46 @@ pub fn null_result() -> Value {
fn coerce_to_schema(
value: serde_json::Value,
schema: &serde_json::Value,
+) -> serde_json::Value {
+ coerce_to_schema_with_root(value, schema, schema)
+}
+
+fn coerce_to_schema_with_root(
+ value: serde_json::Value,
+ schema: &serde_json::Value,
+ root_schema: &serde_json::Value,
) -> serde_json::Value {
use serde_json::Value as Json;
- match schema.get("type").and_then(Json::as_str) {
+ let schema = resolve_local_schema(schema, root_schema);
+
+ if let Some(branches) = schema.get("anyOf").and_then(Json::as_array)
+ && branches.len() == 2
+ && branches.iter().any(|branch| branch.get("type").and_then(Json::as_str) == Some("null"))
+ && let Some(non_null_schema) =
+ branches.iter().find(|branch| branch.get("type").and_then(Json::as_str) != Some("null"))
+ {
+ return coerce_to_schema_with_root(value, non_null_schema, root_schema);
+ }
+
+ let schema_type = match schema.get("type") {
+ Some(Json::String(schema_type)) => Some(schema_type.as_str()),
+ Some(Json::Array(schema_types))
+ if schema_types.len() == 2
+ && schema_types.iter().any(|schema_type| schema_type.as_str() == Some("null")) =>
+ {
+ schema_types.iter().filter_map(Json::as_str).find(|schema_type| *schema_type != "null")
+ },
+ _ => None,
+ };
+
+ match schema_type {
Some("object") => match value {
Json::Object(map) => Json::Object(
map.into_iter()
.map(|(key, value)| {
let value = match schema.get("properties").and_then(|properties| properties.get(&key)) {
- Some(property_schema) => coerce_to_schema(value, property_schema),
+ Some(property_schema) => coerce_to_schema_with_root(value, property_schema, root_schema),
None => value,
};
(key, value)
@@ -104,9 +134,9 @@ fn coerce_to_schema(
other => other,
},
Some("array") => match (value, schema.get("items")) {
- (Json::Array(items), Some(item_schema)) => {
- Json::Array(items.into_iter().map(|item| coerce_to_schema(item, item_schema)).collect())
- },
+ (Json::Array(items), Some(item_schema)) => Json::Array(
+ items.into_iter().map(|item| coerce_to_schema_with_root(item, item_schema, root_schema)).collect(),
+ ),
(other, _) => other,
},
Some("number") => match &value {
@@ -142,3 +172,35 @@ fn coerce_to_schema(
_ => value,
}
}
+
+fn resolve_local_schema<'a>(
+ mut schema: &'a serde_json::Value,
+ root_schema: &'a serde_json::Value,
+) -> &'a serde_json::Value {
+ use serde_json::Value as Json;
+
+ let mut visited = Vec::new();
+ while let Some(reference) = schema.get("$ref").and_then(Json::as_str) {
+ if visited.contains(&reference) {
+ break;
+ }
+ visited.push(reference);
+
+ let Some(pointer) = reference.strip_prefix('#') else {
+ break;
+ };
+ let target = if pointer.is_empty() {
+ Some(root_schema)
+ } else if pointer.starts_with('/') {
+ root_schema.pointer(pointer)
+ } else {
+ // Anchor fragments are not JSON Pointers.
+ None
+ };
+ let Some(target) = target else {
+ break;
+ };
+ schema = target;
+ }
+ schema
+}
diff --git a/crates/nagare/src/tool/schema.rs b/crates/nagare/src/tool/schema.rs
index d6113ec08..96040a612 100644
--- a/crates/nagare/src/tool/schema.rs
+++ b/crates/nagare/src/tool/schema.rs
@@ -1,181 +1,111 @@
-use indexmap::IndexMap;
-pub use proc_macros::UzuToolSchema;
-use serde::{Deserialize, Serialize};
+pub use schemars::{JsonSchema, Schema};
+use schemars::{
+ generate::SchemaSettings,
+ transform::{Transform, transform_subschemas},
+};
+use serde_json::{Map, Value as JsonValue};
use shoji::types::basic::Value;
-pub trait ToolSchema {
- fn json_schema() -> JsonSchema;
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum SchemaContract {
+ /// Schema for arguments accepted by Serde deserialization.
+ Input,
+ /// Schema for values produced by Serde serialization.
+ Output,
}
-macro_rules! impl_tool_schema {
- ($constructor:ident => $($ty:ty),+) => {
- $(
- impl ToolSchema for $ty {
- fn json_schema() -> JsonSchema {
- JsonSchema::$constructor()
- }
- }
- )+
+pub fn tool_schema<T: JsonSchema>(contract: SchemaContract) -> Schema {
+ let settings = SchemaSettings::draft07().with(|settings| {
+ settings.meta_schema = None;
+ settings.inline_subschemas = true;
+ });
+ let settings = match contract {
+ SchemaContract::Input => settings.for_deserialize(),
+ SchemaContract::Output => settings.for_serialize(),
};
-}
-
-impl_tool_schema!(string => String);
-impl_tool_schema!(number => f32, f64);
-impl_tool_schema!(integer => i8, i16, i32, i64, isize, u8, u16, u32, u64, usize);
-impl_tool_schema!(boolean => bool);
-impl<T: ToolSchema> ToolSchema for Vec<T> {
- fn json_schema() -> JsonSchema {
- JsonSchema::array(T::json_schema())
- }
+ let mut schema = settings.into_generator().into_root_schema_for::<T>();
+ schema.remove("title");
+ schema
}
-impl<T: ToolSchema> ToolSchema for Option<T> {
- fn json_schema() -> JsonSchema {
- T::json_schema()
- }
+pub fn tool_schema_value<T: JsonSchema>(contract: SchemaContract) -> Value {
+ schema_value(tool_schema::<T>(contract))
}
-#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
-pub struct JsonSchema {
- #[serde(flatten)]
- pub schema_type: JsonSchemaType,
- #[serde(skip_serializing_if = "Option::is_none")]
- pub description: Option<String>,
+/// Builds the outer object schema owned by the function macro.
+pub fn object_schema<PK, P, RK, R>(
+ properties: P,
+ required: R,
+) -> Schema
+where
+ PK: Into<String>,
+ P: IntoIterator<Item = (PK, Schema)>,
+ RK: Into<String>,
+ R: IntoIterator<Item = RK>,
+{
+ let properties = properties
+ .into_iter()
+ .map(|(name, schema)| {
+ let name = name.into();
+ let mut schema = schema;
+ let property_pointer = format!("/properties/{}", escape_json_pointer_token(&name));
+ RebaseLocalReferences {
+ new_root: &property_pointer,
+ }
+ .transform(&mut schema);
+ (name, schema.to_value())
+ })
+ .collect::<Map<_, _>>();
+ let required: Vec<JsonValue> = required.into_iter().map(|name| JsonValue::String(name.into())).collect();
+
+ Schema::try_from(serde_json::json!({
+ "type": "object",
+ "properties": properties,
+ "required": required,
+ }))
+ .expect("the generated tool parameter schema is an object")
}
-#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
-#[serde(tag = "type", rename_all = "lowercase")]
-pub enum JsonSchemaType {
- String {
- #[serde(rename = "enum", skip_serializing_if = "Option::is_none")]
- enum_values: Option<Vec<String>>,
- },
- Number,
- Integer,
- Boolean,
- Array {
- items: Box<JsonSchema>,
- },
- Object {
- #[serde(default)]
- properties: IndexMap<String, JsonSchema>,
- #[serde(default)]
- required: Vec<String>,
- #[serde(rename = "additionalProperties", skip_serializing_if = "Option::is_none")]
- additional_properties: Option<bool>,
- },
+fn escape_json_pointer_token(token: &str) -> String {
+ token.replace('~', "~0").replace('/', "~1")
}
-impl JsonSchema {
- pub fn string() -> Self {
- Self {
- schema_type: JsonSchemaType::String {
- enum_values: None,
- },
- description: None,
- }
- }
-
- pub fn number() -> Self {
- Self {
- schema_type: JsonSchemaType::Number,
- description: None,
- }
- }
-
- pub fn integer() -> Self {
- Self {
- schema_type: JsonSchemaType::Integer,
- description: None,
- }
- }
-
- pub fn boolean() -> Self {
- Self {
- schema_type: JsonSchemaType::Boolean,
- description: None,
- }
- }
-
- pub fn array(items: JsonSchema) -> Self {
- Self {
- schema_type: JsonSchemaType::Array {
- items: Box::new(items),
- },
- description: None,
- }
- }
-
- pub fn object<PK, P, RK, R>(
- properties: P,
- required: R,
- ) -> Self
- where
- PK: Into<String>,
- P: IntoIterator<Item = (PK, JsonSchema)>,
- RK: Into<String>,
- R: IntoIterator<Item = RK>,
- {
- Self {
- schema_type: JsonSchemaType::Object {
- properties: properties.into_iter().map(|(key, schema)| (key.into(), schema)).collect(),
- required: required.into_iter().map(Into::into).collect(),
- additional_properties: None,
- },
- description: None,
- }
- }
+struct RebaseLocalReferences<'a> {
+ new_root: &'a str,
+}
- pub fn empty_object() -> Self {
- Self {
- schema_type: JsonSchemaType::Object {
- properties: IndexMap::new(),
- required: Vec::new(),
- additional_properties: None,
- },
- description: None,
+impl Transform for RebaseLocalReferences<'_> {
+ fn transform(
+ &mut self,
+ schema: &mut Schema,
+ ) {
+ // A nested `$id` starts a new schema resource whose references keep their own scope.
+ if schema.get("$id").and_then(JsonValue::as_str).is_some() {
+ return;
}
- }
- pub fn with_additional_properties(
- mut self,
- allowed: bool,
- ) -> Self {
- if let JsonSchemaType::Object {
- additional_properties,
- ..
- } = &mut self.schema_type
- {
- *additional_properties = Some(allowed);
+ // Rebase JSON Pointer fragments. URI references and anchor fragments are unchanged.
+ if let Some(JsonValue::String(reference)) = schema.get_mut("$ref") {
+ if reference == "#" {
+ *reference = format!("#{}", self.new_root);
+ } else if let Some(pointer) = reference.strip_prefix("#/") {
+ *reference = format!("#{}/{pointer}", self.new_root);
+ }
}
- self
- }
- pub fn with_description(
- mut self,
- description: impl Into<String>,
- ) -> Self {
- self.description = Some(description.into());
- self
+ transform_subschemas(self, schema);
}
+}
- pub fn with_enum_values(
- mut self,
- values: Vec<String>,
- ) -> Self {
- if let JsonSchemaType::String {
- enum_values,
- } = &mut self.schema_type
- {
- *enum_values = Some(values);
- }
- self
- }
+pub fn with_description(
+ mut schema: Schema,
+ description: impl Into<String>,
+) -> Schema {
+ schema.insert("description".into(), description.into().into());
+ schema
+}
- pub fn to_value(&self) -> Value {
- Value {
- json: serde_json::to_string(self).expect("JsonSchema serialization is infallible"),
- }
- }
+pub fn schema_value(schema: Schema) -> Value {
+ Value::from(schema.to_value())
}
diff --git a/crates/proc-macros/Cargo.toml b/crates/proc-macros/Cargo.toml
index 6654b3d99..d6a339434 100644
--- a/crates/proc-macros/Cargo.toml
+++ b/crates/proc-macros/Cargo.toml
@@ -15,7 +15,6 @@ proc-macro = true
proc-macro2.workspace = true
proc-macro-crate.workspace = true
quote.workspace = true
-serde_derive_internals.workspace = true
syn.workspace = true
[lints]
diff --git a/crates/proc-macros/src/lib.rs b/crates/proc-macros/src/lib.rs
index e71852e77..f30df640c 100644
--- a/crates/proc-macros/src/lib.rs
+++ b/crates/proc-macros/src/lib.rs
@@ -61,8 +61,3 @@ pub fn uzu_tool_function(
) -> TokenStream {
uzu_tool::uzu_tool_function(args, input)
}
-
-#[proc_macro_derive(UzuToolSchema, attributes(serde))]
-pub fn uzu_tool_schema(input: TokenStream) -> TokenStream {
- uzu_tool::uzu_derive_tool_schema(input)
-}
diff --git a/crates/proc-macros/src/uzu_tool/mod.rs b/crates/proc-macros/src/uzu_tool/mod.rs
index f33cdf7d0..4c16fa864 100644
--- a/crates/proc-macros/src/uzu_tool/mod.rs
+++ b/crates/proc-macros/src/uzu_tool/mod.rs
@@ -1,8 +1,6 @@
mod closure;
mod function;
-mod schema;
mod util;
pub use closure::uzu_tool_closure;
pub use function::uzu_tool_function;
-pub use schema::uzu_derive_tool_schema;
diff --git a/crates/proc-macros/src/uzu_tool/schema.rs b/crates/proc-macros/src/uzu_tool/schema.rs
deleted file mode 100644
index 790fcd2a1..000000000
--- a/crates/proc-macros/src/uzu_tool/schema.rs
+++ /dev/null
@@ -1,150 +0,0 @@
-use proc_macro::TokenStream;
-use quote::quote;
-use serde_derive_internals::{
- Ctxt, Derive,
- ast::{Container as SerdeContainer, Data as SerdeData},
-};
-use syn::{__private::TokenStream2, Data, DeriveInput, Error, Fields, parse_macro_input};
-
-use crate::uzu_tool::util::{doc_string, is_option, nagare_path};
-
-pub fn uzu_derive_tool_schema(input: TokenStream) -> TokenStream {
- let input = parse_macro_input!(input as DeriveInput);
- match expand_tool_schema(input) {
- Ok(tokens) => tokens.into(),
- Err(error) => error.to_compile_error().into(),
- }
-}
-
-fn expand_tool_schema(input: DeriveInput) -> syn::Result<TokenStream2> {
- let nagare = nagare_path()?;
-
- let Data::Struct(data) = &input.data else {
- return Err(Error::new_spanned(&input.ident, "#[derive(UzuToolSchema)] only supports structs"));
- };
- let Fields::Named(_) = &data.fields else {
- return Err(Error::new_spanned(
- &input.ident,
- "#[derive(UzuToolSchema)] only supports structs with named fields",
- ));
- };
-
- let serde_context = Ctxt::new();
- let serde_container = SerdeContainer::from_ast(&serde_context, &input, Derive::Deserialize);
- serde_context.check()?;
- let Some(serde_container) = serde_container else {
- return Err(Error::new_spanned(&input.ident, "failed to parse Serde attributes"));
- };
- let SerdeData::Struct(_, serde_fields) = &serde_container.data else {
- unreachable!();
- };
-
- if serde_container.attrs.transparent()
- || serde_container.attrs.type_from().is_some()
- || serde_container.attrs.type_try_from().is_some()
- || serde_container.attrs.type_into().is_some()
- || serde_container.attrs.remote().is_some()
- {
- return Err(Error::new_spanned(
- &input.ident,
- "UzuToolSchema does not support Serde container attributes that change the serialized representation",
- ));
- }
-
- let schema_fields = serde_fields
- .iter()
- .filter_map(|field| {
- let attrs = &field.attrs;
- if attrs.skip_serializing() != attrs.skip_deserializing() {
- return Some(Err(Error::new_spanned(
- field.original,
- "UzuToolSchema requires fields to be skipped in both Serialize and Deserialize; use #[serde(skip)]",
- )));
- }
- if attrs.skip_serializing() {
- return None;
- }
- if attrs.flatten() {
- return Some(Err(Error::new_spanned(
- field.original,
- "UzuToolSchema does not support #[serde(flatten)]",
- )));
- }
- if attrs.skip_serializing_if().is_some() {
- return Some(Err(Error::new_spanned(
- field.original,
- "UzuToolSchema does not support #[serde(skip_serializing_if = \"...\")]",
- )));
- }
- if attrs.serialize_with().is_some() || attrs.deserialize_with().is_some() || attrs.getter().is_some() {
- return Some(Err(Error::new_spanned(
- field.original,
- "UzuToolSchema does not support Serde field attributes that change the serialized representation",
- )));
- }
-
- let serialize_name = attrs.name().serialize_name();
- let deserialize_name = attrs.name().deserialize_name();
- if serialize_name != deserialize_name {
- return Some(Err(Error::new_spanned(
- field.original,
- "UzuToolSchema requires matching Serialize and Deserialize field names",
- )));
- }
-
- let required =
- !is_option(field.ty) && attrs.default().is_none() && serde_container.attrs.default().is_none();
- Some(Ok((field.original, serialize_name.to_string(), required)))
- })
- .collect::<syn::Result<Vec<_>>>()?;
-
- let ident = &input.ident;
- let mut generics = input.generics.clone();
- for (field, _, _) in &schema_fields {
- let ty = &field.ty;
- generics.make_where_clause().predicates.push(syn::parse_quote!(#ty: #nagare::tool::schema::ToolSchema));
- }
- let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
-
- let schema = if schema_fields.is_empty() {
- quote!(#nagare::tool::schema::JsonSchema::empty_object())
- } else {
- let properties = schema_fields.iter().map(|(field, name, _)| {
- let ty = &field.ty;
- let schema = quote!(<#ty as #nagare::tool::schema::ToolSchema>::json_schema());
- let description = doc_string(&field.attrs);
- if description.is_empty() {
- quote!((#name, #schema))
- } else {
- quote!((#name, #schema.with_description(#description)))
- }
- });
- let required = schema_fields.iter().filter(|(_, _, required)| *required).map(|(_, name, _)| name);
- quote! {
- #nagare::tool::schema::JsonSchema::object(
- [#(#properties),*],
- ::std::vec::Vec::<&str>::from([#(#required),*]),
- )
- }
- };
- let with_additional_properties = if serde_container.attrs.deny_unknown_fields() {
- quote!(.with_additional_properties(false))
- } else {
- quote!()
- };
-
- let struct_description = doc_string(&input.attrs);
- let with_description = if struct_description.is_empty() {
- quote!()
- } else {
- quote!(.with_description(#struct_description))
- };
-
- Ok(quote! {
- impl #impl_generics #nagare::tool::schema::ToolSchema for #ident #ty_generics #where_clause {
- fn json_schema() -> #nagare::tool::schema::JsonSchema {
- #schema #with_additional_properties #with_description
- }
- }
- })
-}
diff --git a/crates/proc-macros/src/uzu_tool/util.rs b/crates/proc-macros/src/uzu_tool/util.rs
index e4d094405..aa699fd84 100644
--- a/crates/proc-macros/src/uzu_tool/util.rs
+++ b/crates/proc-macros/src/uzu_tool/util.rs
@@ -99,22 +99,30 @@ pub fn parameters_tokens(
let properties = params.iter().map(|param| {
let name = param.ident.unraw().to_string();
let ty = &param.ty;
- let schema = quote!(<#ty as #nagare::tool::schema::ToolSchema>::json_schema());
+ let schema = quote!(
+ #nagare::tool::schema::tool_schema::<#ty>(
+ #nagare::tool::schema::SchemaContract::Input,
+ )
+ );
if param.description.is_empty() {
quote!((#name, #schema))
} else {
let description = &param.description;
- quote!((#name, #schema.with_description(#description)))
+ quote!((
+ #name,
+ #nagare::tool::schema::with_description(#schema, #description),
+ ))
}
});
let required = params.iter().filter(|param| param.required).map(|param| param.ident.unraw().to_string());
quote! {
::core::option::Option::Some(
- #nagare::tool::schema::JsonSchema::object(
- [#(#properties),*],
- ::std::vec::Vec::<&str>::from([#(#required),*]),
+ #nagare::tool::schema::schema_value(
+ #nagare::tool::schema::object_schema(
+ [#(#properties),*],
+ ::std::vec::Vec::<&str>::from([#(#required),*]),
+ ),
)
- .to_value(),
)
}
}
@@ -142,7 +150,11 @@ pub fn return_definition_tokens(
) -> TokenStream2 {
match ok_type {
Some(ty) => quote! {
- ::core::option::Option::Some(<#ty as #nagare::tool::schema::ToolSchema>::json_schema().to_value())
+ ::core::option::Option::Some(
+ #nagare::tool::schema::tool_schema_value::<#ty>(
+ #nagare::tool::schema::SchemaContract::Output,
+ ),
+ )
},
None => quote!(::core::option::Option::None),
}
diff --git a/crates/uzu/examples/tool_calls.rs b/crates/uzu/examples/tool_calls.rs
index c5d58c8fb..b56a38631 100644
--- a/crates/uzu/examples/tool_calls.rs
+++ b/crates/uzu/examples/tool_calls.rs
@@ -1,4 +1,5 @@
-use nagare::tool::{func_def::ErrorFuture, schema::UzuToolSchema, uzu_tool_closure, uzu_tool_function};
+use nagare::tool::{func_def::ErrorFuture, uzu_tool_closure, uzu_tool_function};
+use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use shoji::types::{
basic::{SamplingMethod, SamplingPolicy},
@@ -7,7 +8,7 @@ use shoji::types::{
use uzu::engine::{Engine, EngineConfig};
/// A geographic coordinate.
-#[derive(Serialize, Deserialize, UzuToolSchema)]
+#[derive(Serialize, Deserialize, JsonSchema)]
struct Coordinate {
/// Latitude in decimal degrees.
latitude: f64,
diff --git a/crates/uzu/tests/tool/calls.rs b/crates/uzu/tests/tool/calls.rs
index 9cea90081..f81a07289 100644
--- a/crates/uzu/tests/tool/calls.rs
+++ b/crates/uzu/tests/tool/calls.rs
@@ -1,9 +1,10 @@
#![cfg(not(target_family = "wasm"))]
+use schemars::JsonSchema;
use serde::Serialize;
use uzu::{
engine::{Engine, EngineConfig},
- session::tool::{func_def::ToolDescriptor, schema::UzuToolSchema, uzu_tool_function},
+ session::tool::{func_def::ToolDescriptor, uzu_tool_function},
types::{
basic::{SamplingMethod, SamplingPolicy},
session::chat::{ChatConfig, ChatMessage, ChatReplyConfig, ChatReplyFinishReason, ChatRole},
@@ -11,7 +12,7 @@ use uzu::{
};
/// A geographic coordinate.
-#[derive(Serialize, UzuToolSchema)]
+#[derive(Serialize, JsonSchema)]
struct Coordinate {
/// Latitude in decimal degrees.
latitude: f64,
@@ -20,14 +21,14 @@ struct Coordinate {
}
/// The current temperature at the requested location.
-#[derive(Serialize, UzuToolSchema)]
+#[derive(Serialize, JsonSchema)]
struct Temperature {
/// Current temperature in degrees Celsius.
temperature: f64,
}
/// The current time.
-#[derive(Serialize, UzuToolSchema)]
+#[derive(Serialize, JsonSchema)]
struct Time {
/// Current time.
time: String,
diff --git a/crates/uzu/tests/tool/macro_closure.rs b/crates/uzu/tests/tool/macro_closure.rs
index 456c793cb..34dc9e97a 100644
--- a/crates/uzu/tests/tool/macro_closure.rs
+++ b/crates/uzu/tests/tool/macro_closure.rs
@@ -30,7 +30,11 @@ async fn closure_with_captured_state() {
serde_json::json!({
"type": "object",
"properties": {
- "amount": { "type": "integer", "description": "Amount to add to the total." }
+ "amount": {
+ "type": "integer",
+ "format": "int64",
+ "description": "Amount to add to the total."
+ }
},
"required": ["amount"]
})
@@ -38,7 +42,7 @@ async fn closure_with_captured_state() {
let return_definition: serde_json::Value =
serde_json::from_str(&definition.return_definition.as_ref().unwrap().json).unwrap();
- assert_eq!(return_definition, serde_json::json!({ "type": "integer" }));
+ assert_eq!(return_definition, serde_json::json!({ "type": "integer", "format": "int64" }));
let result: serde_json::Value =
definition.execute(serde_json::json!({ "amount": 40 }).into()).await.unwrap().try_into().unwrap();
@@ -81,7 +85,7 @@ async fn async_closure_with_optional_parameter() {
"properties": {
"name": { "type": "string", "description": "Name of the person to greet." },
"punctuation": {
- "type": "string",
+ "type": ["string", "null"],
"description": "Optional punctuation to end the greeting with."
}
},
@@ -117,7 +121,7 @@ async fn result_closure_propagates_errors() {
let return_definition: serde_json::Value =
serde_json::from_str(&definition.return_definition.as_ref().unwrap().json).unwrap();
- assert_eq!(return_definition, serde_json::json!({ "type": "number" }));
+ assert_eq!(return_definition, serde_json::json!({ "type": "number", "format": "double" }));
let result: serde_json::Value = definition
.execute(serde_json::json!({ "dividend": 10.0, "divisor": 4.0 }).into())
diff --git a/crates/uzu/tests/tool/macro_function.rs b/crates/uzu/tests/tool/macro_function.rs
index c62bb0f04..d298abda1 100644
--- a/crates/uzu/tests/tool/macro_function.rs
+++ b/crates/uzu/tests/tool/macro_function.rs
@@ -1,10 +1,11 @@
#![cfg(not(target_family = "wasm"))]
-use nagare::tool::{func_def::ToolDescriptor, schema::UzuToolSchema, uzu_tool_function};
+use nagare::tool::{func_def::ToolDescriptor, uzu_tool_function};
+use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
/// The current weather at the requested location.
-#[derive(Serialize, UzuToolSchema)]
+#[derive(Serialize, JsonSchema)]
struct Weather {
/// Temperature in degrees Celsius.
temperature: f64,
@@ -44,8 +45,16 @@ async fn check_generated_definition() {
serde_json::json!({
"type": "object",
"properties": {
- "latitude": { "type": "number", "description": "Latitude in decimal degrees." },
- "longitude": { "type": "number", "description": "Longitude in decimal degrees." }
+ "latitude": {
+ "type": "number",
+ "format": "double",
+ "description": "Latitude in decimal degrees."
+ },
+ "longitude": {
+ "type": "number",
+ "format": "double",
+ "description": "Longitude in decimal degrees."
+ }
},
"required": ["latitude", "longitude"]
})
@@ -58,10 +67,17 @@ async fn check_generated_definition() {
serde_json::json!({
"type": "object",
"properties": {
- "temperature": { "type": "number", "description": "Temperature in degrees Celsius." },
- "summary": { "type": "string", "description": "Optional human-readable summary." }
+ "temperature": {
+ "type": "number",
+ "format": "double",
+ "description": "Temperature in degrees Celsius."
+ },
+ "summary": {
+ "type": ["string", "null"],
+ "description": "Optional human-readable summary."
+ }
},
- "required": ["temperature"],
+ "required": ["temperature", "summary"],
"description": "The current weather at the requested location."
})
);
@@ -83,7 +99,7 @@ async fn check_generated_definition() {
}
/// A geographic coordinate.
-#[derive(Serialize, Deserialize, UzuToolSchema)]
+#[derive(Serialize, Deserialize, JsonSchema)]
struct Coordinate {
/// Latitude in decimal degrees.
latitude: f64,
@@ -92,7 +108,7 @@ struct Coordinate {
}
/// A weather forecast request.
-#[derive(Deserialize, UzuToolSchema)]
+#[derive(Deserialize, JsonSchema)]
struct ForecastRequest {
/// Location to forecast.
location: Coordinate,
@@ -101,7 +117,7 @@ struct ForecastRequest {
}
/// The forecast for a single day.
-#[derive(Serialize, UzuToolSchema)]
+#[derive(Serialize, JsonSchema)]
struct DayForecast {
/// Temperature in degrees Celsius.
temperature: f64,
@@ -110,7 +126,7 @@ struct DayForecast {
}
/// A forecast for the requested location.
-#[derive(Serialize, UzuToolSchema)]
+#[derive(Serialize, JsonSchema)]
struct Forecast {
location: Coordinate,
/// Forecasts per day.
@@ -138,8 +154,16 @@ async fn structured_input_and_output() {
assert_eq!(definition.description, "Get the weather forecast for a location.");
let coordinate_properties = serde_json::json!({
- "latitude": { "type": "number", "description": "Latitude in decimal degrees." },
- "longitude": { "type": "number", "description": "Longitude in decimal degrees." }
+ "latitude": {
+ "type": "number",
+ "format": "double",
+ "description": "Latitude in decimal degrees."
+ },
+ "longitude": {
+ "type": "number",
+ "format": "double",
+ "description": "Longitude in decimal degrees."
+ }
});
let parameters: serde_json::Value = serde_json::from_str(&definition.parameters.as_ref().unwrap().json).unwrap();
@@ -158,7 +182,12 @@ async fn structured_input_and_output() {
// The field doc overrides the struct-level description of `Coordinate`.
"description": "Location to forecast."
},
- "days": { "type": "integer", "description": "Number of days ahead." }
+ "days": {
+ "type": "integer",
+ "format": "uint32",
+ "minimum": 0,
+ "description": "Number of days ahead."
+ }
},
"required": ["location", "days"],
"description": "A weather forecast request."
@@ -187,8 +216,16 @@ async fn structured_input_and_output() {
"items": {
"type": "object",
"properties": {
- "temperature": { "type": "number", "description": "Temperature in degrees Celsius." },
- "rain_probability": { "type": "number", "description": "Chance of rain from 0 to 1." }
+ "temperature": {
+ "type": "number",
+ "format": "double",
+ "description": "Temperature in degrees Celsius."
+ },
+ "rain_probability": {
+ "type": "number",
+ "format": "double",
+ "description": "Chance of rain from 0 to 1."
+ }
},
"required": ["temperature", "rain_probability"],
"description": "The forecast for a single day."
@@ -232,7 +269,7 @@ async fn structured_input_and_output() {
assert!(error.to_string().contains("request"), "unexpected error: {error}");
}
-#[derive(Serialize, Deserialize, UzuToolSchema)]
+#[derive(Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
struct UserLookup {
#[serde(rename = "id")]
@@ -265,7 +302,12 @@ async fn serde_field_attributes_match_generated_schema() {
"properties": {
"id": { "type": "string" },
"displayName": { "type": "string" },
- "maxResults": { "type": "integer" }
+ "maxResults": {
+ "type": "integer",
+ "format": "uint32",
+ "minimum": 0,
+ "default": 0
+ }
},
"required": ["id", "displayName"]
}
@@ -291,7 +333,7 @@ async fn serde_field_attributes_match_generated_schema() {
assert_eq!(result, serde_json::json!("00123:Ada:0"));
}
-#[derive(Deserialize, UzuToolSchema)]
+#[derive(Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
struct StrictRequest {
query: String,
@@ -355,8 +397,8 @@ async fn primitive_input_and_output() {
serde_json::json!({
"type": "object",
"properties": {
- "a": { "type": "integer", "description": "The first addend." },
- "b": { "type": "integer", "description": "The second addend." }
+ "a": { "type": "integer", "format": "int64", "description": "The first addend." },
+ "b": { "type": "integer", "format": "int64", "description": "The second addend." }
},
"required": ["a", "b"]
})
@@ -364,13 +406,116 @@ async fn primitive_input_and_output() {
let return_definition: serde_json::Value =
serde_json::from_str(&definition.return_definition.as_ref().unwrap().json).unwrap();
- assert_eq!(return_definition, serde_json::json!({ "type": "integer" }));
+ assert_eq!(return_definition, serde_json::json!({ "type": "integer", "format": "int64" }));
let result: serde_json::Value =
definition.execute(serde_json::json!({ "a": 40, "b": 2 }).into()).await.unwrap().try_into().unwrap();
assert_eq!(result, serde_json::json!(42));
}
+#[uzu_tool_function]
+fn increment_optional(value: Option<i64>) -> Option<i64> {
+ value.map(|value| value + 1)
+}
+
+#[tokio::test]
+async fn nullable_argument_schema_is_coerced() {
+ let definition: ToolDescriptor = increment_optional.into();
+ let parameters: serde_json::Value = serde_json::from_str(&definition.parameters.as_ref().unwrap().json).unwrap();
+ assert_eq!(
+ parameters,
+ serde_json::json!({
+ "type": "object",
+ "properties": {
+ "value": {
+ "type": ["integer", "null"],
+ "format": "int64"
+ }
+ },
+ "required": []
+ })
+ );
+
+ let result: serde_json::Value =
+ definition.execute(serde_json::json!({ "value": "41" }).into()).await.unwrap().try_into().unwrap();
+ assert_eq!(result, serde_json::json!(42));
+}
+
+#[derive(Serialize, Deserialize, JsonSchema)]
+struct AsymmetricValue {
+ #[serde(skip_serializing)]
+ input_only: String,
+ #[serde(skip_deserializing)]
+ output_only: String,
+}
+
+#[uzu_tool_function]
+fn transform_asymmetric(value: AsymmetricValue) -> AsymmetricValue {
+ AsymmetricValue {
+ output_only: value.input_only.clone(),
+ ..value
+ }
+}
+
+#[test]
+fn input_and_output_use_separate_serde_contracts() {
+ let definition: ToolDescriptor = transform_asymmetric.into();
+ let parameters: serde_json::Value = serde_json::from_str(&definition.parameters.as_ref().unwrap().json).unwrap();
+ let input_schema = &parameters["properties"]["value"];
+ assert!(input_schema["properties"].get("input_only").is_some());
+ assert!(input_schema["properties"].get("output_only").is_none());
+ assert_eq!(input_schema["required"], serde_json::json!(["input_only"]));
+
+ let output_schema: serde_json::Value =
+ serde_json::from_str(&definition.return_definition.as_ref().unwrap().json).unwrap();
+ assert!(output_schema["properties"].get("input_only").is_none());
+ assert!(output_schema["properties"].get("output_only").is_some());
+ assert_eq!(output_schema["required"], serde_json::json!(["output_only"]));
+}
+
+#[derive(Deserialize, JsonSchema)]
+struct RecursiveRequest {
+ value: String,
+ count: i64,
+ child: Option<Box<RecursiveRequest>>,
+}
+
+#[uzu_tool_function]
+fn recursive_count(request: RecursiveRequest) -> i64 {
+ let child_count = request.child.map_or(0, |child| recursive_count::call(*child));
+ let _ = request.value;
+ request.count + child_count
+}
+
+#[tokio::test]
+async fn recursive_parameter_references_and_coercion_use_the_nested_parameter_root() {
+ let definition: ToolDescriptor = recursive_count.into();
+ let parameters: serde_json::Value = serde_json::from_str(&definition.parameters.as_ref().unwrap().json).unwrap();
+
+ assert_eq!(parameters["properties"]["request"]["properties"]["child"]["anyOf"][0]["$ref"], "#/properties/request");
+
+ let result: serde_json::Value = definition
+ .execute(
+ serde_json::json!({
+ "request": {
+ "value": "root",
+ "count": 1,
+ "child": {
+ "value": "leaf",
+ "count": "41",
+ "child": null
+ }
+ }
+ })
+ .into(),
+ )
+ .await
+ .unwrap()
+ .try_into()
+ .unwrap();
+ assert_eq!(result, serde_json::json!(42));
+}
+
/// Reset the session state.
#[uzu_tool_function]
fn reset() {}