Support anyOf and enum in JsonSchema (#16875)

This brings us into better alignment with the JSON schema subset that is
supported in
<https://developers.openai.com/api/docs/guides/structured-outputs#supported-schemas>,
and also allows us to render richer function signatures in code mode
(e.g., anyOf{null, OtherObjectType})
This commit is contained in:
Vivian Fang authored and GitHub committed 2026-04-08 01:07:55 -07:00
1 parent abc678f9e8
commit ea516f9a40
36 files changed
+1797 -1657

No files matched your search

+424 -100
View File
@@ -1,5 +1,7 @@
use super::AdditionalProperties;
use super::JsonSchema;
use super::JsonSchemaPrimitiveType;
use super::JsonSchemaType;
use super::parse_tool_input_schema;
use pretty_assertions::assert_eq;
use std::collections::BTreeMap;
@@ -18,7 +20,7 @@ fn parse_tool_input_schema_coerces_boolean_schemas() {
// semantics directly.
let schema = parse_tool_input_schema(&serde_json::json!(true)).expect("parse schema");
assert_eq!(schema, JsonSchema::String { description: None });
assert_eq!(schema, JsonSchema::string(/*description*/ None));
}
#[test]
@@ -42,21 +44,19 @@ fn parse_tool_input_schema_infers_object_shape_and_defaults_properties() {
assert_eq!(
schema,
JsonSchema::Object {
properties: BTreeMap::from([(
JsonSchema::object(
BTreeMap::from([(
"query".to_string(),
JsonSchema::String {
description: Some("search query".to_string()),
},
JsonSchema::string(Some("search query".to_string())),
)]),
required: None,
additional_properties: None,
}
/*required*/ None,
/*additional_properties*/ None
)
);
}
#[test]
fn parse_tool_input_schema_normalizes_integer_and_missing_array_items() {
fn parse_tool_input_schema_preserves_integer_and_defaults_array_items() {
// Example schema shape:
// {
// "type": "object",
@@ -67,8 +67,7 @@ fn parse_tool_input_schema_normalizes_integer_and_missing_array_items() {
// }
//
// Expected normalization behavior:
// - `"integer"` is accepted by the baseline model through the legacy
// number/integer alias.
// - `"integer"` is preserved distinctly from `"number"`.
// - Arrays missing `items` receive a permissive string `items` schema.
let schema = parse_tool_input_schema(&serde_json::json!({
"type": "object",
@@ -81,20 +80,23 @@ fn parse_tool_input_schema_normalizes_integer_and_missing_array_items() {
assert_eq!(
schema,
JsonSchema::Object {
properties: BTreeMap::from([
("page".to_string(), JsonSchema::Number { description: None }),
JsonSchema::object(
BTreeMap::from([
(
"page".to_string(),
JsonSchema::integer(/*description*/ None),
),
(
"tags".to_string(),
JsonSchema::Array {
items: Box::new(JsonSchema::String { description: None }),
description: None,
},
JsonSchema::array(
JsonSchema::string(/*description*/ None),
/*description*/ None,
)
),
]),
required: None,
additional_properties: None,
}
/*required*/ None,
/*additional_properties*/ None
)
);
}
@@ -118,9 +120,7 @@ fn parse_tool_input_schema_sanitizes_additional_properties_schema() {
//
// Expected normalization behavior:
// - `additionalProperties` schema objects are recursively sanitized.
// - The nested schema is normalized into the baseline object form.
// - In the baseline model, the nested `anyOf` degrades to a plain string
// field because richer combiners are not preserved.
// - The nested schema is normalized into the current object/anyOf form.
let schema = parse_tool_input_schema(&serde_json::json!({
"type": "object",
"additionalProperties": {
@@ -134,20 +134,24 @@ fn parse_tool_input_schema_sanitizes_additional_properties_schema() {
assert_eq!(
schema,
JsonSchema::Object {
properties: BTreeMap::new(),
required: None,
additional_properties: Some(AdditionalProperties::Schema(Box::new(
JsonSchema::Object {
properties: BTreeMap::from([(
"value".to_string(),
JsonSchema::String { description: None },
)]),
required: Some(vec!["value".to_string()]),
additional_properties: None,
},
))),
}
JsonSchema::object(
BTreeMap::new(),
/*required*/ None,
Some(AdditionalProperties::Schema(Box::new(JsonSchema::object(
BTreeMap::from([(
"value".to_string(),
JsonSchema::any_of(
vec![
JsonSchema::string(/*description*/ None),
JsonSchema::number(/*description*/ None),
],
/*description*/ None,
),
)]),
Some(vec!["value".to_string()]),
/*additional_properties*/ None,
))))
)
);
}
@@ -168,11 +172,7 @@ fn parse_tool_input_schema_infers_object_shape_from_boolean_additional_propertie
assert_eq!(
schema,
JsonSchema::Object {
properties: BTreeMap::new(),
required: None,
additional_properties: Some(false.into()),
}
JsonSchema::object(BTreeMap::new(), /*required*/ None, Some(false.into()))
);
}
@@ -191,7 +191,7 @@ fn parse_tool_input_schema_infers_number_from_numeric_keywords() {
}))
.expect("parse schema");
assert_eq!(schema, JsonSchema::Number { description: None });
assert_eq!(schema, JsonSchema::number(/*description*/ None));
}
#[test]
@@ -209,7 +209,7 @@ fn parse_tool_input_schema_infers_number_from_multiple_of() {
}))
.expect("parse schema");
assert_eq!(schema, JsonSchema::Number { description: None });
assert_eq!(schema, JsonSchema::number(/*description*/ None));
}
#[test]
@@ -220,7 +220,8 @@ fn parse_tool_input_schema_infers_string_from_enum_const_and_format_keywords() {
// { "format": "date-time" }
//
// Expected normalization behavior:
// - Each of these keywords implies a string schema when `type` is omitted.
// - `enum` and `const` normalize into explicit string-enum schemas.
// - `format` still falls back to a plain string schema.
let enum_schema = parse_tool_input_schema(&serde_json::json!({
"enum": ["fast", "safe"]
}))
@@ -234,9 +235,18 @@ fn parse_tool_input_schema_infers_string_from_enum_const_and_format_keywords() {
}))
.expect("parse format schema");
assert_eq!(enum_schema, JsonSchema::String { description: None });
assert_eq!(const_schema, JsonSchema::String { description: None });
assert_eq!(format_schema, JsonSchema::String { description: None });
assert_eq!(
enum_schema,
JsonSchema::string_enum(
vec![serde_json::json!("fast"), serde_json::json!("safe")],
/*description*/ None,
)
);
assert_eq!(
const_schema,
JsonSchema::string_enum(vec![serde_json::json!("file")], /*description*/ None)
);
assert_eq!(format_schema, JsonSchema::string(/*description*/ None));
}
#[test]
@@ -245,11 +255,11 @@ fn parse_tool_input_schema_defaults_empty_schema_to_string() {
// {}
//
// Expected normalization behavior:
// - With no structural hints at all, the baseline normalizer falls back to
// a permissive string schema.
// - With no structural hints at all, the normalizer falls back to a
// permissive string schema.
let schema = parse_tool_input_schema(&serde_json::json!({})).expect("parse schema");
assert_eq!(schema, JsonSchema::String { description: None });
assert_eq!(schema, JsonSchema::string(/*description*/ None));
}
#[test]
@@ -263,8 +273,8 @@ fn parse_tool_input_schema_infers_array_from_prefix_items() {
//
// Expected normalization behavior:
// - `prefixItems` implies an array schema when `type` is omitted.
// - The baseline model still stores the normalized result as a regular
// array schema with string items.
// - The normalized result is stored as a regular array schema with string
// items.
let schema = parse_tool_input_schema(&serde_json::json!({
"prefixItems": [
{"type": "string"}
@@ -274,10 +284,10 @@ fn parse_tool_input_schema_infers_array_from_prefix_items() {
assert_eq!(
schema,
JsonSchema::Array {
items: Box::new(JsonSchema::String { description: None }),
description: None,
}
JsonSchema::array(
JsonSchema::string(/*description*/ None),
/*description*/ None,
)
);
}
@@ -309,18 +319,14 @@ fn parse_tool_input_schema_preserves_boolean_additional_properties_on_inferred_o
assert_eq!(
schema,
JsonSchema::Object {
properties: BTreeMap::from([(
JsonSchema::object(
BTreeMap::from([(
"metadata".to_string(),
JsonSchema::Object {
properties: BTreeMap::new(),
required: None,
additional_properties: Some(AdditionalProperties::Boolean(true)),
},
JsonSchema::object(BTreeMap::new(), /*required*/ None, Some(true.into())),
)]),
required: None,
additional_properties: None,
}
/*required*/ None,
/*additional_properties*/ None
)
);
}
@@ -347,22 +353,202 @@ fn parse_tool_input_schema_infers_object_shape_from_schema_additional_properties
assert_eq!(
schema,
JsonSchema::Object {
properties: BTreeMap::new(),
required: None,
additional_properties: Some(AdditionalProperties::Schema(Box::new(
JsonSchema::String { description: None },
))),
JsonSchema::object(
BTreeMap::new(),
/*required*/ None,
Some(JsonSchema::string(/*description*/ None).into())
)
);
}
#[test]
fn parse_tool_input_schema_rewrites_const_to_single_value_enum() {
// Example schema shape:
// {
// "const": "tagged"
// }
//
// Expected normalization behavior:
// - `const` is rewritten through the sanitizer's `map.remove("const")`
// path into an equivalent single-value string enum schema.
let schema = parse_tool_input_schema(&serde_json::json!({
"const": "tagged"
}))
.expect("parse schema");
assert_eq!(
schema,
JsonSchema::string_enum(vec![serde_json::json!("tagged")], /*description*/ None)
);
}
#[test]
fn parse_tool_input_schema_rejects_singleton_null_type() {
let err = parse_tool_input_schema(&serde_json::json!({
"type": "null"
}))
.expect_err("singleton null should be rejected");
assert!(
err.to_string()
.contains("tool input schema must not be a singleton null type"),
"unexpected error: {err}"
);
}
#[test]
fn parse_tool_input_schema_fills_default_properties_for_nullable_object_union() {
// Example schema shape:
// {
// "type": ["object", "null"]
// }
//
// Expected normalization behavior:
// - The full union is preserved.
// - Object members of the union still receive default `properties`.
let schema = parse_tool_input_schema(&serde_json::json!({
"type": ["object", "null"]
}))
.expect("parse schema");
assert_eq!(
schema,
JsonSchema {
schema_type: Some(JsonSchemaType::Multiple(vec![
JsonSchemaPrimitiveType::Object,
JsonSchemaPrimitiveType::Null,
])),
properties: Some(BTreeMap::new()),
..Default::default()
}
);
}
#[test]
fn parse_tool_input_schema_fills_default_items_for_nullable_array_union() {
// Example schema shape:
// {
// "type": ["array", "null"]
// }
//
// Expected normalization behavior:
// - The full union is preserved.
// - Array members of the union still receive default `items`.
let schema = parse_tool_input_schema(&serde_json::json!({
"type": ["array", "null"]
}))
.expect("parse schema");
assert_eq!(
schema,
JsonSchema {
schema_type: Some(JsonSchemaType::Multiple(vec![
JsonSchemaPrimitiveType::Array,
JsonSchemaPrimitiveType::Null,
])),
items: Some(Box::new(JsonSchema::string(/*description*/ None))),
..Default::default()
}
);
}
// Schemas that should be preserved for Responses API compatibility rather than
// being rewritten into a different shape. These currently fail on the baseline
// normalizer and are the intended signal for the new JsonSchema work.
// being rewritten into a different shape.
#[test]
fn parse_tool_input_schema_preserves_nested_nullable_any_of_shape() {
// Example schema shape:
// {
// "type": "object",
// "properties": {
// "open": {
// "anyOf": [
// {
// "type": "array",
// "items": {
// "type": "object",
// "properties": {
// "ref_id": { "type": "string" },
// "lineno": { "anyOf": [{ "type": "integer" }, { "type": "null" }] }
// },
// "required": ["ref_id"],
// "additionalProperties": false
// }
// },
// { "type": "null" }
// ]
// }
// }
// }
//
// Expected normalization behavior:
// - Nested nullable `anyOf` shapes are preserved all the way down.
let schema = parse_tool_input_schema(&serde_json::json!({
"type": "object",
"properties": {
"open": {
"anyOf": [
{
"type": "array",
"items": {
"type": "object",
"properties": {
"ref_id": {"type": "string"},
"lineno": {"anyOf": [{"type": "integer"}, {"type": "null"}]}
},
"required": ["ref_id"],
"additionalProperties": false
}
},
{"type": "null"}
]
}
}
}))
.expect("parse schema");
assert_eq!(
schema,
JsonSchema::object(
BTreeMap::from([(
"open".to_string(),
JsonSchema::any_of(
vec![
JsonSchema::array(
JsonSchema::object(
BTreeMap::from([
(
"lineno".to_string(),
JsonSchema::any_of(
vec![
JsonSchema::integer(/*description*/ None),
JsonSchema::null(/*description*/ None),
],
/*description*/ None,
),
),
(
"ref_id".to_string(),
JsonSchema::string(/*description*/ None),
),
]),
Some(vec!["ref_id".to_string()]),
Some(false.into()),
),
/*description*/ None,
),
JsonSchema::null(/*description*/ None),
],
/*description*/ None,
),
),]),
/*required*/ None,
/*additional_properties*/ None
)
);
}
#[test]
#[ignore = "Expected to pass after the new JsonSchema preserves nullable type unions"]
fn parse_tool_input_schema_preserves_nested_nullable_type_union() {
// Example schema shape:
// {
@@ -395,23 +581,25 @@ fn parse_tool_input_schema_preserves_nested_nullable_type_union() {
assert_eq!(
schema,
JsonSchema::Object {
properties: BTreeMap::from([(
JsonSchema::object(
BTreeMap::from([(
"nickname".to_string(),
serde_json::from_value(serde_json::json!({
"type": ["string", "null"],
"description": "Optional nickname"
}))
.expect("nested nullable schema"),
JsonSchema {
schema_type: Some(JsonSchemaType::Multiple(vec![
JsonSchemaPrimitiveType::String,
JsonSchemaPrimitiveType::Null,
])),
description: Some("Optional nickname".to_string()),
..Default::default()
},
)]),
required: Some(vec!["nickname".to_string()]),
additional_properties: Some(false.into()),
}
Some(vec!["nickname".to_string()]),
Some(false.into()),
)
);
}
#[test]
#[ignore = "Expected to pass after the new JsonSchema preserves nested anyOf schemas"]
fn parse_tool_input_schema_preserves_nested_any_of_property() {
// Example schema shape:
// {
@@ -444,19 +632,155 @@ fn parse_tool_input_schema_preserves_nested_any_of_property() {
assert_eq!(
schema,
JsonSchema::Object {
properties: BTreeMap::from([(
JsonSchema::object(
BTreeMap::from([(
"query".to_string(),
serde_json::from_value(serde_json::json!({
"anyOf": [
{ "type": "string" },
{ "type": "number" }
]
}))
.expect("nested anyOf schema"),
JsonSchema::any_of(
vec![
JsonSchema::string(/*description*/ None),
JsonSchema::number(/*description*/ None),
],
/*description*/ None,
),
)]),
required: None,
additional_properties: None,
/*required*/ None,
/*additional_properties*/ None
)
);
}
#[test]
fn parse_tool_input_schema_preserves_type_unions_without_rewriting_to_any_of() {
// Example schema shape:
// {
// "type": ["string", "null"],
// "description": "optional string"
// }
//
// Expected normalization behavior:
// - Explicit type unions are preserved as unions rather than rewritten to
// `anyOf`.
let schema = parse_tool_input_schema(&serde_json::json!({
"type": ["string", "null"],
"description": "optional string"
}))
.expect("parse schema");
assert_eq!(
schema,
JsonSchema {
schema_type: Some(JsonSchemaType::Multiple(vec![
JsonSchemaPrimitiveType::String,
JsonSchemaPrimitiveType::Null,
])),
description: Some("optional string".to_string()),
..Default::default()
}
);
}
#[test]
fn parse_tool_input_schema_preserves_explicit_enum_type_union() {
// Example schema shape:
// {
// "type": ["string", "null"],
// "enum": ["short", "medium", "long"],
// "description": "optional response length"
// }
//
// Expected normalization behavior:
// - The explicit string/null union is preserved alongside the enum values.
let schema = super::parse_tool_input_schema(&serde_json::json!({
"type": ["string", "null"],
"enum": ["short", "medium", "long"],
"description": "optional response length"
}))
.expect("parse schema");
assert_eq!(
schema,
JsonSchema {
schema_type: Some(JsonSchemaType::Multiple(vec![
JsonSchemaPrimitiveType::String,
JsonSchemaPrimitiveType::Null,
])),
description: Some("optional response length".to_string()),
enum_values: Some(vec![
serde_json::json!("short"),
serde_json::json!("medium"),
serde_json::json!("long"),
]),
..Default::default()
}
);
}
#[test]
fn parse_tool_input_schema_preserves_string_enum_constraints() {
// Example schema shape:
// {
// "type": "object",
// "properties": {
// "response_length": { "type": "enum", "enum": ["short", "medium", "long"] },
// "kind": { "type": "const", "const": "tagged" },
// "scope": { "type": "enum", "enum": ["one", "two"] }
// }
// }
//
// Expected normalization behavior:
// - Legacy `type: "enum"` and `type: "const"` inputs are normalized into
// the current string-enum representation.
let schema = super::parse_tool_input_schema(&serde_json::json!({
"type": "object",
"properties": {
"response_length": {
"type": "enum",
"enum": ["short", "medium", "long"]
},
"kind": {
"type": "const",
"const": "tagged"
},
"scope": {
"type": "enum",
"enum": ["one", "two"]
}
}
}))
.expect("parse schema");
assert_eq!(
schema,
JsonSchema::object(
BTreeMap::from([
(
"kind".to_string(),
JsonSchema::string_enum(
vec![serde_json::json!("tagged")],
/*description*/ None,
),
),
(
"response_length".to_string(),
JsonSchema::string_enum(
vec![
serde_json::json!("short"),
serde_json::json!("medium"),
serde_json::json!("long"),
],
/*description*/ None,
),
),
(
"scope".to_string(),
JsonSchema::string_enum(
vec![serde_json::json!("one"), serde_json::json!("two")],
/*description*/ None,
),
),
]),
/*required*/ None,
/*additional_properties*/ None
)
);
}