Skip to content

armgit / sie

Public Imported from GitHub
0

Open-source inference server and production cluster for all the models your agent needs.

fix(gateway): refuse structurally invalid JSON schemas before dispatch (#1011)

Shai Shmuel3cfed236919e
diff --git a/packages/sie_gateway/src/handlers/grammar.rs b/packages/sie_gateway/src/handlers/grammar.rs
index 561fcecea..62b99d419 100644
--- a/packages/sie_gateway/src/handlers/grammar.rs
+++ b/packages/sie_gateway/src/handlers/grammar.rs
@@ -11,6 +11,9 @@
//! * Internal JSON Schema ``$ref`` dereferencing (``#/...`` only)
//! * JSON Schema reject-list (``$dynamicRef``, ``if/then/else``,
//! ``unevaluatedProperties``, ``dependentSchemas``)
+//! * Keyword value shapes every JSON Schema draft refuses (an unknown
+//! ``type`` name, ``properties`` that is not an object, ``required``
+//! that is not an array of strings, a string where a schema belongs, …)
//! * Mutual exclusivity of ``json_schema`` and ``regex``
//! * ``strict: true`` only on ``json_schema`` and ``regex`` grammars, whose
//! finished output the worker verifies
@@ -204,6 +207,14 @@ impl SchemaRejection {
/// worker via the resulting :class:`GrammarSpec`. They never affect the
/// cache key — see :func:`sie_server.types.grammar.hash_grammar`.
pub fn parse_grammar(v: &Value) -> GrammarParseResult {
+ parse_grammar_at(v, "grammar.json_schema")
+}
+
+/// :func:`parse_grammar` with the ``param`` path that schema rejections
+/// name for the ``json_schema`` variant. Callers that translate another
+/// request field into a grammar pass that field's path, so a refused
+/// schema is reported where the client sent it.
+pub fn parse_grammar_at(v: &Value, schema_param: &str) -> GrammarParseResult {
let Some(obj) = v.as_object() else {
return GrammarParseResult::Err(bad_request(
"'grammar' must be a JSON object".to_string(),
@@ -260,7 +271,7 @@ pub fn parse_grammar(v: &Value) -> GrammarParseResult {
if has_schema {
let schema = obj.get("json_schema").expect("checked above");
- let resolved_schema = match dereference_schema_refs(schema, "grammar.json_schema") {
+ let resolved_schema = match dereference_schema_refs(schema, schema_param) {
Ok(schema) => schema,
Err(rejection) => return GrammarParseResult::Err(rejection.into_response()),
};
@@ -275,7 +286,7 @@ pub fn parse_grammar(v: &Value) -> GrammarParseResult {
oai_code::INVALID_REQUEST,
));
}
- if let Err(rejection) = walk_schema(&resolved_schema, "grammar.json_schema", None) {
+ if let Err(rejection) = walk_schema(&resolved_schema, schema_param, None) {
return GrammarParseResult::Err(rejection.into_response());
}
GrammarParseResult::Ok(GrammarSpec::JsonSchema {
@@ -471,6 +482,11 @@ fn dereference_schema_refs_inner(
match v {
Value::Object(map) => {
+ // Definitions are dropped below once their references are
+ // inlined, so their shape is checked here, before they go.
+ if context == SchemaContext::Schema {
+ check_definitions_shape(map, path)?;
+ }
if context == SchemaContext::Schema && map.contains_key("$ref") {
let ref_param = format!("{path}.$ref");
let (ref_str, target) = resolve_internal_ref(
@@ -580,6 +596,39 @@ fn dereference_schema_refs_inner(
}
}
+/// ``$defs`` / ``definitions`` must map names to schemas.
+fn check_definitions_shape(
+ map: &serde_json::Map<String, Value>,
+ path: &str,
+) -> Result<(), SchemaRejection> {
+ for kw in ["$defs", "definitions"] {
+ if let Some(v) = map.get(kw).filter(|v| !v.is_null()) {
+ if !v.is_object() {
+ return Err(invalid_schema(
+ format!(
+ "'{kw}' must be an object mapping names to schemas, got {}",
+ json_kind(v)
+ ),
+ &format!("{path}.{kw}"),
+ ));
+ }
+ }
+ }
+ Ok(())
+}
+
+/// An array where a single schema belongs is never a schema. The caller
+/// excludes the tuple form of ``items`` and the members of definition maps.
+fn reject_array_schema(v: &Value, path: &str) -> Result<(), SchemaRejection> {
+ if v.is_array() {
+ return Err(invalid_schema(
+ "a JSON Schema must be an object or a boolean".to_string(),
+ path,
+ ));
+ }
+ Ok(())
+}
+
fn schema_child_context(parent: SchemaContext, key: &str) -> SchemaContext {
match parent {
SchemaContext::Schema => match key {
@@ -613,6 +662,7 @@ fn schema_child_context(parent: SchemaContext, key: &str) -> SchemaContext {
/// With ``refs_root``, every ``$ref`` must resolve inside it (see
/// :func:`resolve_internal_ref`); refs are checked, not followed.
fn walk_schema(v: &Value, path: &str, refs_root: Option<&Value>) -> Result<(), SchemaRejection> {
+ reject_array_schema(v, path)?;
let mut visited: usize = 0;
walk_schema_inner(
v,
@@ -678,6 +728,7 @@ fn walk_schema_inner(
if let (Some(root), Some(ref_value)) = (refs_root, map.get("$ref")) {
resolve_internal_ref(root, ref_value, &format!("{path}.$ref"))?;
}
+ check_schema_keyword_shapes(map, path)?;
}
for (k, child) in map {
let child_path = format!("{path}.{k}");
@@ -687,6 +738,25 @@ fn walk_schema_inner(
depth
};
let child_context = schema_child_context(context, k);
+ if context == SchemaContext::Schema {
+ // ``items`` also takes the tuple form, an array of schemas.
+ if child_context == SchemaContext::Schema && k != "items" {
+ reject_array_schema(child, &child_path)?;
+ }
+ // Members of a property map are schemas. Definition maps
+ // are left alone: a ``$ref`` may point into an array held
+ // there, and that is reported as a reference error.
+ if matches!(
+ k.as_str(),
+ "properties" | "patternProperties" | "dependentSchemas"
+ ) {
+ if let Some(members) = child.as_object() {
+ for (name, member) in members {
+ reject_array_schema(member, &format!("{child_path}.{name}"))?;
+ }
+ }
+ }
+ }
walk_schema_inner(
child,
&child_path,
@@ -707,6 +777,9 @@ fn walk_schema_inner(
};
for (i, child) in arr.iter().enumerate() {
let child_path = format!("{path}[{i}]");
+ if child_context == SchemaContext::Schema {
+ reject_array_schema(child, &child_path)?;
+ }
walk_schema_inner(
child,
&child_path,
@@ -718,14 +791,258 @@ fn walk_schema_inner(
)?;
}
}
- // Scalars (string / number / bool / null) cannot host
- // unsupported keywords and do not contribute to depth.
+ // A schema is an object or a boolean. A string or number where a
+ // schema belongs (``"properties": {"a": "string"}``) is never a
+ // schema. ``null`` is left alone: it is read as an absent keyword.
+ Value::String(_) | Value::Number(_) if context == SchemaContext::Schema => {
+ return Err(invalid_schema(
+ "a JSON Schema must be an object or a boolean".to_string(),
+ path,
+ ));
+ }
+ // Other scalars cannot host unsupported keywords and do not
+ // contribute to depth.
_ => {}
}
Ok(())
}
+/// The type names JSON Schema defines for ``type``.
+const SCHEMA_TYPE_NAMES: &[&str] = &[
+ "array", "boolean", "integer", "null", "number", "object", "string",
+];
+
+/// Keywords whose value is a map from names to schemas.
+const SCHEMA_MAP_KEYWORDS: &[&str] = &["properties", "patternProperties", "$defs", "definitions"];
+
+/// Keywords whose value is a non-empty array of schemas.
+const SCHEMA_ARRAY_KEYWORDS: &[&str] = &["allOf", "anyOf", "oneOf"];
+
+/// Keywords whose value is a non-negative integer.
+const NON_NEGATIVE_INTEGER_KEYWORDS: &[&str] = &[
+ "minLength",
+ "maxLength",
+ "minItems",
+ "maxItems",
+ "minProperties",
+ "maxProperties",
+ "minContains",
+ "maxContains",
+];
+
+/// Keywords whose value is a number.
+const NUMBER_KEYWORDS: &[&str] = &["minimum", "maximum", "multipleOf"];
+
+fn invalid_schema(message: String, param: &str) -> SchemaRejection {
+ SchemaRejection::new(message, param, oai_code::INVALID_REQUEST)
+}
+
+fn json_kind(v: &Value) -> &'static str {
+ match v {
+ Value::Null => "null",
+ Value::Bool(_) => "a boolean",
+ Value::Number(_) => "a number",
+ Value::String(_) => "a string",
+ Value::Array(_) => "an array",
+ Value::Object(_) => "an object",
+ }
+}
+
+fn check_type_name(v: &Value, param: &str) -> Result<(), SchemaRejection> {
+ match v.as_str() {
+ Some(name) if SCHEMA_TYPE_NAMES.contains(&name) => Ok(()),
+ Some(name) => Err(invalid_schema(
+ format!(
+ "'type' {name:?} is not a JSON Schema type; expected one of {}",
+ SCHEMA_TYPE_NAMES.join(", ")
+ ),
+ param,
+ )),
+ None => Err(invalid_schema(
+ format!("'type' entries must be strings, got {}", json_kind(v)),
+ param,
+ )),
+ }
+}
+
+/// Refuse keyword values that no JSON Schema draft allows, one schema
+/// object at a time (the caller walks the children). Only shapes every
+/// draft's metaschema refuses are checked; unknown keywords, annotations
+/// and ``null`` values pass.
+fn check_schema_keyword_shapes(
+ map: &serde_json::Map<String, Value>,
+ path: &str,
+) -> Result<(), SchemaRejection> {
+ let present = |key: &str| map.get(key).filter(|v| !v.is_null());
+
+ if let Some(ty) = present("type") {
+ let param = format!("{path}.type");
+ match ty {
+ Value::String(_) => check_type_name(ty, &param)?,
+ Value::Array(names) => {
+ if names.is_empty() {
+ return Err(invalid_schema(
+ "'type' must not be an empty array".to_string(),
+ &param,
+ ));
+ }
+ for (i, name) in names.iter().enumerate() {
+ check_type_name(name, &format!("{param}[{i}]"))?;
+ }
+ }
+ other => {
+ return Err(invalid_schema(
+ format!(
+ "'type' must be a string or an array of strings, got {}",
+ json_kind(other)
+ ),
+ &param,
+ ));
+ }
+ }
+ }
+
+ for &kw in SCHEMA_MAP_KEYWORDS {
+ if let Some(v) = present(kw) {
+ if !v.is_object() {
+ return Err(invalid_schema(
+ format!(
+ "'{kw}' must be an object mapping names to schemas, got {}",
+ json_kind(v)
+ ),
+ &format!("{path}.{kw}"),
+ ));
+ }
+ }
+ }
+
+ if let Some(v) = present("required") {
+ let param = format!("{path}.required");
+ let Some(names) = v.as_array() else {
+ return Err(invalid_schema(
+ format!(
+ "'required' must be an array of property names, got {}",
+ json_kind(v)
+ ),
+ &param,
+ ));
+ };
+ for (i, name) in names.iter().enumerate() {
+ if !name.is_string() {
+ return Err(invalid_schema(
+ format!(
+ "'required' entries must be strings, got {}",
+ json_kind(name)
+ ),
+ &format!("{param}[{i}]"),
+ ));
+ }
+ }
+ }
+
+ if let Some(v) = present("enum") {
+ if !v.is_array() {
+ return Err(invalid_schema(
+ format!("'enum' must be an array, got {}", json_kind(v)),
+ &format!("{path}.enum"),
+ ));
+ }
+ }
+
+ for &kw in SCHEMA_ARRAY_KEYWORDS {
+ if let Some(v) = present(kw) {
+ let param = format!("{path}.{kw}");
+ match v.as_array() {
+ Some(items) if items.is_empty() => {
+ return Err(invalid_schema(
+ format!("'{kw}' must be a non-empty array of schemas"),
+ &param,
+ ));
+ }
+ Some(_) => {}
+ None => {
+ return Err(invalid_schema(
+ format!(
+ "'{kw}' must be a non-empty array of schemas, got {}",
+ json_kind(v)
+ ),
+ &param,
+ ));
+ }
+ }
+ }
+ }
+
+ if let Some(v) = present("prefixItems") {
+ if !v.is_array() {
+ return Err(invalid_schema(
+ format!(
+ "'prefixItems' must be an array of schemas, got {}",
+ json_kind(v)
+ ),
+ &format!("{path}.prefixItems"),
+ ));
+ }
+ }
+
+ for &kw in NON_NEGATIVE_INTEGER_KEYWORDS {
+ if let Some(v) = present(kw) {
+ // ``5.0`` counts as an integer, as JSON Schema validators read it.
+ let ok = v
+ .as_f64()
+ .is_some_and(|n| n >= 0.0 && n.fract() == 0.0 && n.is_finite());
+ if !ok {
+ return Err(invalid_schema(
+ format!("'{kw}' must be a non-negative integer, got {v}"),
+ &format!("{path}.{kw}"),
+ ));
+ }
+ }
+ }
+
+ for &kw in NUMBER_KEYWORDS {
+ if let Some(v) = present(kw) {
+ if !v.is_number() {
+ return Err(invalid_schema(
+ format!("'{kw}' must be a number, got {}", json_kind(v)),
+ &format!("{path}.{kw}"),
+ ));
+ }
+ }
+ }
+ // A number in current drafts, a boolean modifier in draft 4.
+ for kw in ["exclusiveMinimum", "exclusiveMaximum"] {
+ if let Some(v) = present(kw) {
+ if !v.is_number() && !v.is_boolean() {
+ return Err(invalid_schema(
+ format!("'{kw}' must be a number or a boolean, got {}", json_kind(v)),
+ &format!("{path}.{kw}"),
+ ));
+ }
+ }
+ }
+ if let Some(v) = present("multipleOf").and_then(Value::as_f64) {
+ if v <= 0.0 {
+ return Err(invalid_schema(
+ format!("'multipleOf' must be greater than 0, got {v}"),
+ &format!("{path}.multipleOf"),
+ ));
+ }
+ }
+
+ if let Some(v) = present("pattern") {
+ if !v.is_string() {
+ return Err(invalid_schema(
+ format!("'pattern' must be a string, got {}", json_kind(v)),
+ &format!("{path}.pattern"),
+ ));
+ }
+ }
+
+ Ok(())
+}
+
fn is_schema_nesting_key(key: &str) -> bool {
matches!(
key,
@@ -1545,4 +1862,250 @@ mod tests {
format!("JSON Schema node count exceeds limit ({MAX_SCHEMA_NODES})")
);
}
+
+ /// Schemas that every JSON Schema draft refuses, with the ``param``
+ /// each refusal names (relative to the schema root ``p``).
+ fn structurally_invalid_schemas() -> Vec<(Value, &'static str, &'static str)> {
+ vec![
+ (
+ json!({"type": "objekt"}),
+ "p.type",
+ "'type' \"objekt\" is not a JSON Schema type",
+ ),
+ (
+ json!({"type": 3}),
+ "p.type",
+ "'type' must be a string or an array of strings",
+ ),
+ (
+ json!({"type": []}),
+ "p.type",
+ "'type' must not be an empty array",
+ ),
+ (
+ json!({"type": ["string", "nul"]}),
+ "p.type[1]",
+ "'type' \"nul\" is not a JSON Schema type",
+ ),
+ (
+ json!({"type": ["string", 1]}),
+ "p.type[1]",
+ "'type' entries must be strings",
+ ),
+ (
+ json!({"type": "object", "properties": [{"a": {"type": "string"}}]}),
+ "p.properties",
+ "'properties' must be an object mapping names to schemas",
+ ),
+ (
+ json!({"type": "object", "properties": {"a": "string"}}),
+ "p.properties.a",
+ "a JSON Schema must be an object or a boolean",
+ ),
+ (
+ json!({"type": "object", "properties": {"a": {"type": "string"}}, "required": "a"}),
+ "p.required",
+ "'required' must be an array of property names",
+ ),
+ (
+ json!({"type": "object", "properties": {"a": {"type": "string", "required": true}}}),
+ "p.properties.a.required",
+ "'required' must be an array of property names",
+ ),
+ (
+ json!({"type": "object", "required": ["a", 1]}),
+ "p.required[1]",
+ "'required' entries must be strings",
+ ),
+ (
+ json!({"type": "string", "enum": "a"}),
+ "p.enum",
+ "'enum' must be an array",
+ ),
+ (
+ json!({"anyOf": []}),
+ "p.anyOf",
+ "'anyOf' must be a non-empty array of schemas",
+ ),
+ (
+ json!({"oneOf": {"type": "string"}}),
+ "p.oneOf",
+ "'oneOf' must be a non-empty array of schemas",
+ ),
+ (
+ json!({"type": "string", "minLength": -1}),
+ "p.minLength",
+ "'minLength' must be a non-negative integer",
+ ),
+ (
+ json!({"type": "array", "maxItems": 1.5}),
+ "p.maxItems",
+ "'maxItems' must be a non-negative integer",
+ ),
+ (
+ json!({"type": "number", "minimum": "0"}),
+ "p.minimum",
+ "'minimum' must be a number",
+ ),
+ (
+ json!({"type": "number", "minimum": 0, "exclusiveMinimum": "bad"}),
+ "p.exclusiveMinimum",
+ "'exclusiveMinimum' must be a number or a boolean",
+ ),
+ (
+ json!({"type": "number", "exclusiveMaximum": [1]}),
+ "p.exclusiveMaximum",
+ "'exclusiveMaximum' must be a number or a boolean",
+ ),
+ (
+ json!({"type": "number", "multipleOf": 0}),
+ "p.multipleOf",
+ "'multipleOf' must be greater than 0",
+ ),
+ (
+ json!({"type": "string", "pattern": 1}),
+ "p.pattern",
+ "'pattern' must be a string",
+ ),
+ (
+ json!({"type": "array", "items": "string"}),
+ "p.items",
+ "a JSON Schema must be an object or a boolean",
+ ),
+ (
+ json!("object"),
+ "p",
+ "a JSON Schema must be an object or a boolean",
+ ),
+ (
+ json!({"type": "object", "$defs": 7}),
+ "p.$defs",
+ "'$defs' must be an object mapping names to schemas",
+ ),
+ (
+ json!({"type": "object", "definitions": ["a"]}),
+ "p.definitions",
+ "'definitions' must be an object mapping names to schemas",
+ ),
+ (
+ json!([]),
+ "p",
+ "a JSON Schema must be an object or a boolean",
+ ),
+ (
+ json!({"type": "object", "properties": {"a": []}}),
+ "p.properties.a",
+ "a JSON Schema must be an object or a boolean",
+ ),
+ (
+ json!({"allOf": [[]]}),
+ "p.allOf[0]",
+ "a JSON Schema must be an object or a boolean",
+ ),
+ (
+ json!({"not": [{"type": "string"}]}),
+ "p.not",
+ "a JSON Schema must be an object or a boolean",
+ ),
+ (
+ json!({"type": "array", "items": [[{"type": "string"}]]}),
+ "p.items[0]",
+ "a JSON Schema must be an object or a boolean",
+ ),
+ (
+ json!({"type": "object", "properties": {"a": {"$ref": "#/$defs/A"}}, "$defs": {"A": {"type": "strin"}}}),
+ "p.$defs.A.type",
+ "'type' \"strin\" is not a JSON Schema type",
+ ),
+ (
+ json!({"type": "object", "properties": {"a": {"type": "array", "items": {"anyOf": [{"type": "string"}, {"type": "object", "properties": {"b": {"type": "intger"}}}]}}}}),
+ "p.properties.a.items.anyOf[1].properties.b.type",
+ "'type' \"intger\" is not a JSON Schema type",
+ ),
+ ]
+ }
+
+ /// Valid schemas from common clients that must keep passing.
+ fn structurally_valid_schemas() -> Vec<Value> {
+ vec![
+ json!({}),
+ json!(true),
+ json!({"type": "object", "properties": {"a": {"type": "string"}}, "required": ["a"], "additionalProperties": false}),
+ json!({"type": "object", "properties": {"u": {"type": ["string", "null"], "enum": ["c", "f", null]}}}),
+ json!({"type": "object", "properties": {"a": {"$ref": "#/$defs/A"}}, "$defs": {"A": {"type": "object", "properties": {"b": {"type": "integer", "minimum": 0}}}}}),
+ json!({"type": "object", "properties": {"a": {"$ref": "#/definitions/A"}}, "definitions": {"A": {"type": "string", "minLength": 1, "maxLength": 5.0, "pattern": "^x"}}}),
+ json!({"anyOf": [{"type": "string"}, {"type": "null"}], "default": null, "title": "T"}),
+ json!({"type": "array", "items": [{"type": "string"}, {"type": "number"}], "minItems": 2, "additionalItems": false}),
+ json!({"type": "number", "exclusiveMinimum": true, "minimum": 0, "multipleOf": 0.5}),
+ // Unknown keywords and annotations pass, whatever they hold;
+ // property names that look like keywords are names, not keywords.
+ json!({"type": "object", "x-extra": {"type": "objekt"}, "nullable": true, "properties": {"type": {"type": "string"}, "required": {"type": "boolean"}, "enum": {"type": "array"}}}),
+ // ``null`` keyword values read as absent.
+ json!({"type": "object", "properties": {"a": {"type": "string"}}, "required": null, "items": null}),
+ json!({"type": "string", "format": "custom", "description": "d", "examples": ["x"], "const": "x"}),
+ json!({"type": "number", "exclusiveMinimum": 0, "exclusiveMaximum": 9.5}),
+ json!({"type": "number", "maximum": 9, "exclusiveMaximum": false, "exclusiveMinimum": null}),
+ // A definition may hold an array that a ``$ref`` indexes into.
+ json!({"type": "object", "$defs": {"variants": [{"type": "string"}, {"type": "integer"}]}, "properties": {"a": {"$ref": "#/$defs/variants/1"}}}),
+ ]
+ }
+
+ #[test]
+ fn test_tool_parameters_reject_structurally_invalid_schemas() {
+ for (schema, param, message) in structurally_invalid_schemas() {
+ let rejection = validate_tool_parameters(&schema, "p")
+ .expect_err(&format!("{schema} should be refused"));
+ assert_eq!(rejection.param, param, "{schema}");
+ assert_eq!(rejection.code, oai_code::INVALID_REQUEST, "{schema}");
+ assert!(
+ rejection.message.starts_with(message),
+ "{schema}: {}",
+ rejection.message
+ );
+ }
+ }
+
+ #[test]
+ fn test_tool_parameters_accept_structurally_valid_schemas() {
+ for schema in structurally_valid_schemas() {
+ if let Err(rejection) = validate_tool_parameters(&schema, "p") {
+ panic!("{schema} refused: {rejection:?}");
+ }
+ }
+ }
+
+ #[tokio::test]
+ async fn test_parse_grammar_rejects_structurally_invalid_schemas() {
+ for (schema, param, message) in structurally_invalid_schemas() {
+ // A refused ``$defs`` entry is reported where it is referenced,
+ // since the grammar walks the schema with ``$ref`` inlined.
+ let param = param
+ .replacen('p', "grammar.json_schema", 1)
+ .replace("$defs.A", "properties.a");
+ let body = err_or_panic(parse_grammar(&json!({"json_schema": schema}))).await;
+ assert_eq!(body["error"]["param"], param, "{schema}");
+ assert_eq!(body["error"]["code"], "invalid_request", "{schema}");
+ let got = body["error"]["message"].as_str().unwrap();
+ assert!(got.starts_with(message), "{schema}: {got}");
+ }
+ }
+
+ #[test]
+ fn test_parse_grammar_accepts_structurally_valid_schemas() {
+ for schema in structurally_valid_schemas() {
+ if let GrammarParseResult::Err(_) = parse_grammar(&json!({"json_schema": schema})) {
+ panic!("{schema} refused");
+ }
+ }
+ }
+
+ #[tokio::test]
+ async fn test_parse_grammar_at_names_the_given_schema_path() {
+ let v = json!({"json_schema": {"type": "objekt"}});
+ let body = err_or_panic(parse_grammar_at(&v, "response_format.json_schema.schema")).await;
+ assert_eq!(
+ body["error"]["param"],
+ "response_format.json_schema.schema.type"
+ );
+ }
}
diff --git a/packages/sie_gateway/src/handlers/messages/tests.rs b/packages/sie_gateway/src/handlers/messages/tests.rs
index 6fb47938f..cbecda36c 100644
--- a/packages/sie_gateway/src/handlers/messages/tests.rs
+++ b/packages/sie_gateway/src/handlers/messages/tests.rs
@@ -192,6 +192,19 @@ fn every_captured_claude_code_request_translates_or_fails_with_the_recovery_word
}
}
+#[test]
+fn a_structurally_invalid_tool_schema_is_refused() {
+ let error = messages_to_chat(
+ &json!({"model": "m", "max_tokens": 1, "tools": [{"name": "t", "input_schema": {"type": "objekt"}}], "messages": [{"role": "user", "content": "x"}]}),
+ None,
+ )
+ .unwrap_err();
+ assert_eq!(
+ error.message,
+ "tools.0.input_schema.type: 'type' \"objekt\" is not a JSON Schema type; expected one of array, boolean, integer, null, number, object, string"
+ );
+}
+
#[test]
fn tool_count_over_the_limit_is_refused() {
let tools: Vec<Value> = (0..=MAX_TOOLS)
diff --git a/packages/sie_gateway/src/handlers/proxy.rs b/packages/sie_gateway/src/handlers/proxy.rs
index f80a62ee2..36bd0086f 100644
--- a/packages/sie_gateway/src/handlers/proxy.rs
+++ b/packages/sie_gateway/src/handlers/proxy.rs
@@ -8502,7 +8502,10 @@ fn translate_response_format(
if let Some(s) = js.get("strict") {
wrapped.insert("strict".to_string(), s.clone());
}
- match super::grammar::parse_grammar(&serde_json::Value::Object(wrapped)) {
+ match super::grammar::parse_grammar_at(
+ &serde_json::Value::Object(wrapped),
+ "response_format.json_schema.schema",
+ ) {
super::grammar::GrammarParseResult::Ok(g) => {
// ``parse_grammar`` builds a :class:`GrammarSpec` from the
// wrapped form. Re-attach the OpenAI label/strict in case
@@ -27458,6 +27461,98 @@ mod tests {
assert_eq!(v["error"]["param"], "response_format.json_schema.schema");
}
+ #[tokio::test]
+ async fn test_chat_params_from_json_response_format_invalid_schema_returns_400() {
+ let mut body = _chat_body_min("m");
+ body["response_format"] = serde_json::json!({
+ "type": "json_schema",
+ "json_schema": {
+ "name": "x",
+ "strict": true,
+ "schema": {"type": "object", "properties": {"a": {"type": "objekt"}}},
+ },
+ });
+ let v = _expect_chat_err(body).await;
+ assert_eq!(v["error"]["type"], "invalid_request_error");
+ assert_eq!(v["error"]["code"], "invalid_request");
+ assert_eq!(
+ v["error"]["param"],
+ "response_format.json_schema.schema.properties.a.type"
+ );
+ assert_eq!(
+ v["error"]["message"],
+ "'type' \"objekt\" is not a JSON Schema type; expected one of array, boolean, integer, null, number, object, string"
+ );
+ }
+
+ #[tokio::test]
+ async fn test_chat_params_from_json_tool_invalid_parameters_returns_400() {
+ let mut body = _chat_body_min("m");
+ body["tools"] = serde_json::json!([
+ {"type": "function", "function": {"name": "ok", "parameters": {"type": "object"}}},
+ {"type": "function", "function": {"name": "bad", "parameters": {"type": "objekt"}}},
+ ]);
+ let v = _expect_chat_err(body).await;
+ assert_eq!(v["error"]["type"], "invalid_request_error");
+ assert_eq!(v["error"]["code"], "invalid_request");
+ assert_eq!(v["error"]["param"], "tools[1].function.parameters.type");
+ assert_eq!(
+ v["error"]["message"],
+ "tools[1].function.parameters: 'type' \"objekt\" is not a JSON Schema type; expected one of array, boolean, integer, null, number, object, string"
+ );
+
+ let mut body = _chat_body_min("m");
+ body["tools"] = serde_json::json!([
+ {"type": "function", "function": {"name": "bad", "parameters": {
+ "type": "object",
+ "properties": {"a": {"type": "string"}},
+ "required": "a",
+ }}},
+ ]);
+ let v = _expect_chat_err(body).await;
+ assert_eq!(v["error"]["param"], "tools[0].function.parameters.required");
+ }
+
+ #[test]
+ fn test_chat_params_from_json_valid_schemas_still_pass() {
+ let mut body = _chat_body_min("m");
+ body["stream"] = serde_json::json!(true);
+ body["tools"] = serde_json::json!([
+ {"type": "function", "function": {"name": "no_params"}},
+ {"type": "function", "function": {"name": "empty_params", "parameters": {}}},
+ {"type": "function", "function": {"name": "strict_tool", "strict": true, "parameters": {
+ "type": "object",
+ "properties": {
+ "unit": {"type": ["string", "null"], "enum": ["c", "f", null]},
+ "where": {"$ref": "#/$defs/Place"},
+ },
+ "required": ["unit", "where"],
+ "additionalProperties": false,
+ "$defs": {"Place": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"], "additionalProperties": false}},
+ }}},
+ ]);
+ body["response_format"] = serde_json::json!({
+ "type": "json_schema",
+ "json_schema": {
+ "name": "answer",
+ "strict": true,
+ "schema": {
+ "type": "object",
+ "properties": {
+ "answer": {"anyOf": [{"type": "string"}, {"type": "null"}]},
+ "place": {"$ref": "#/$defs/Place"},
+ },
+ "required": ["answer", "place"],
+ "additionalProperties": false,
+ "$defs": {"Place": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"], "additionalProperties": false}},
+ },
+ },
+ });
+ let p = _expect_chat_ok(body);
+ assert_eq!(p.tools.as_ref().map(Vec::len), Some(3));
+ assert!(p.grammar.is_some());
+ }
+
#[tokio::test]
async fn test_chat_params_from_json_response_format_unknown_type_returns_400() {
let mut body = _chat_body_min("m");