Class JsonSchemaUtils
java.lang.Object
org.springframework.ai.util.json.schema.JsonSchemaUtils
Utility methods for working with JSON schemas.
- Since:
- 1.0.0
- Author:
- Guangdong Liu, Ilayaperumal Gopinathan, Sebastien Deleuze
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringensureValidInputSchema(String inputSchema) Ensures that the input schema is valid for AI model APIs.static tools.jackson.databind.node.ObjectNodegetJsonSchema(Type inputType) Generates JSON Schema (version 2020_12) for the given class.static voidhoistDefsToRoot(tools.jackson.databind.node.ObjectNode rootSchema, tools.jackson.databind.node.ObjectNode subSchema) Moves any$defsblock found onsubSchemainto the$defsblock ofrootSchema, creating one on the root if needed.
-
Method Details
-
hoistDefsToRoot
public static void hoistDefsToRoot(tools.jackson.databind.node.ObjectNode rootSchema, tools.jackson.databind.node.ObjectNode subSchema) Moves any$defsblock found onsubSchemainto the$defsblock ofrootSchema, creating one on the root if needed. On key collisions the existing root entry is reused when the two definitions are structurally equal; otherwise the incoming entry is renamed with a numeric suffix and every"#/$defs/<oldKey>"reference inside the inlined sub-schema and inside every definition inserted by this call is rewritten to point at the new key.This is needed because victools generates self-contained schemas where
$defsand the$refpointers into them are rooted at the sub-schema. Inlining the sub-schema underproperties.<paramName>re-parents existing"#/$defs/<Name>"refs to the outer root, leaving them unresolvable unless$defsis hoisted first.- Parameters:
rootSchema- the wrapper schema that will receive the hoisted definitionssubSchema- the per-parameter sub-schema whose$defsblock is consumed
-
ensureValidInputSchema
Ensures that the input schema is valid for AI model APIs. Many AI models require that the parameters object must have a "properties" field, even if it's empty. This method normalizes schemas from external sources (like MCP tools) that may not include this field.- Parameters:
inputSchema- the input schema as a JSON string- Returns:
- a valid input schema as a JSON string with required fields
-
getJsonSchema
Generates JSON Schema (version 2020_12) for the given class.- Parameters:
inputType- the inputTypeto generate JSON Schema from.- Returns:
- the generated JSON Schema as a String.
- Since:
- 2.0.0
-