Class SchemaUtils
java.lang.Object
com.google.adk.SchemaUtils
Utility class for reading JSON and validating values against schemas.
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleandescribesObject(com.google.genai.types.Schema schema) Returns whetherschemais an object: typedOBJECT, or without a known type unless it hasitemsand neitherpropertiesnorrequired.findMismatch(@Nullable Object value, com.google.genai.types.Schema schema) Returns whyvaluedoes not matchschema, enum values included, or empty if it does.static @Nullable ObjectparseModelOutput(String output, com.google.genai.types.Schema schema) Parses model text as one JSON value againstschema, or returns the text itself if that fails and the schema accepts a string.static voidvalidateMapOnSchema(Map<String, Object> args, com.google.genai.types.Schema schema, Boolean isInput) Validates a map against a schema.static @Nullable ObjectvalidateOutput(String output, com.google.genai.types.Schema schema) Parsesoutputand validates it againstschema.validateOutputSchema(String output, com.google.genai.types.Schema schema) Validates an output string against a schema.static @Nullable ObjectvalueWithDefaults(@Nullable Object value, com.google.genai.types.Schema schema) Returnsvalue, any JSON value, aswithDefaults(Map, Schema)fills it; null stays null.withDefaults(Map<String, Object> args, com.google.genai.types.Schema schema) Returnsargswith defaults added for omitted (not explicitly null) properties across nested objects, array items, and the first matchinganyOfalternative.
-
Method Details
-
validateMapOnSchema
public static void validateMapOnSchema(Map<String, Object> args, com.google.genai.types.Schema schema, Boolean isInput) Validates a map against a schema.- Parameters:
args- The map to validate.schema- The schema to validate against.isInput- Whether the map is an input or output.- Throws:
IllegalArgumentException- If the map does not match the schema.
-
parseModelOutput
public static @Nullable Object parseModelOutput(String output, com.google.genai.types.Schema schema) Parses model text as one JSON value againstschema, or returns the text itself if that fails and the schema accepts a string. Returnsnullfor blank text or a JSON null the schema allows, and fills omitted properties of a parsed value with their schema defaults.This method is only for use by Agent Development Kit.
- Throws:
IllegalArgumentException- if the text does not match the schema
-
findMismatch
public static Optional<String> findMismatch(@Nullable Object value, com.google.genai.types.Schema schema) Returns whyvaluedoes not matchschema, enum values included, or empty if it does. The reason names parts of the schema but never the value, which comes from the user.This method is only for use by Agent Development Kit.
-
describesObject
public static boolean describesObject(com.google.genai.types.Schema schema) Returns whetherschemais an object: typedOBJECT, or without a known type unless it hasitemsand neitherpropertiesnorrequired.This method is only for use by Agent Development Kit.
-
validateOutput
public static @Nullable Object validateOutput(String output, com.google.genai.types.Schema schema) throws com.fasterxml.jackson.core.JsonProcessingException Parsesoutputand validates it againstschema. An object schema goes throughvalidateOutputSchema(String, Schema); for an array or primitive schema this returns the parsed value, such as a list, which may be null.This method is only for use by Agent Development Kit.
- Throws:
IllegalArgumentException- If the output does not match the schema.com.fasterxml.jackson.core.JsonProcessingException- If the output cannot be parsed.
-
validateOutputSchema
public static Map<String,Object> validateOutputSchema(String output, com.google.genai.types.Schema schema) throws com.fasterxml.jackson.core.JsonProcessingException Validates an output string against a schema.- Parameters:
output- The output string to validate.schema- The schema to validate against.- Returns:
- The output map.
- Throws:
IllegalArgumentException- If the output string does not match the schema.com.fasterxml.jackson.core.JsonProcessingException- If the output string cannot be parsed.
-
withDefaults
public static Map<String,Object> withDefaults(Map<String, Object> args, com.google.genai.types.Schema schema) Returnsargswith defaults added for omitted (not explicitly null) properties across nested objects, array items, and the first matchinganyOfalternative. The result may beargsitself or share its maps and lists; this method does not validate the result or the defaults.This method is only for use by Agent Development Kit.
-
valueWithDefaults
public static @Nullable Object valueWithDefaults(@Nullable Object value, com.google.genai.types.Schema schema) Returnsvalue, any JSON value, aswithDefaults(Map, Schema)fills it; null stays null.This method is only for use by Agent Development Kit.
-