Class SchemaUtils

java.lang.Object
com.google.adk.SchemaUtils

public final class SchemaUtils extends Object
Utility class for reading JSON and validating values against schemas.
  • 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 against schema, or returns the text itself if that fails and the schema accepts a string. Returns null for 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 why value does not match schema, 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 whether schema is an object: typed OBJECT, or without a known type unless it has items and neither properties nor required.

      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
      Parses output and validates it against schema. An object schema goes through validateOutputSchema(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)
      Returns args with defaults added for omitted (not explicitly null) properties across nested objects, array items, and the first matching anyOf alternative. The result may be args itself 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)
      Returns value, any JSON value, as withDefaults(Map, Schema) fills it; null stays null.

      This method is only for use by Agent Development Kit.