glean/schema

JSON Schema builder for tool input and structured output definitions. Opaque type with explicit builder functions — no macros, no reflection.

Schemas are sent to the model and also used to validate what the model sends back (see validate).

Types

pub opaque type Schema

Values

pub fn array(of items: Schema) -> Schema
pub fn bool() -> Schema
pub fn describe(
  schema: Schema,
  description description: String,
) -> Schema
pub fn float() -> Schema
pub fn int() -> Schema
pub fn int_range(
  minimum minimum: Int,
  maximum maximum: Int,
) -> Schema
pub fn is_strict_compatible(schema: Schema) -> Bool

Whether this schema can be sent with OpenAI’s strict: true.

Strict mode needs an object at the root, every object property listed as required (optional fields are expressed with nullable), and no string length limits. Schemas that don’t qualify are still sent, just without strict, and glean validates the model’s output itself either way.

pub fn nullable(schema: Schema) -> Schema
pub fn object(
  properties properties: List(#(String, Schema)),
  required required: List(String),
) -> Schema
pub fn one_of(variants variants: List(Schema)) -> Schema
pub fn string() -> Schema
pub fn string_enum(values values: List(String)) -> Schema
pub fn string_with_length(
  min_length min_length: option.Option(Int),
  max_length max_length: option.Option(Int),
) -> Schema
pub fn to_json(schema: Schema) -> json.Json
pub fn to_json_strict(schema: Schema) -> json.Json

Convert to JSON with additionalProperties: false on all object schemas. Required by OpenAI strict structured outputs.

pub fn to_json_string(schema: Schema) -> String
pub fn validate(
  schema: Schema,
  value: json_value.JsonValue,
) -> Result(Nil, List(error.ValidationError))

Check a JSON value against this schema.

Returns every violation found, each with the path to the offending value (object keys and array indexes). Objects may carry keys the schema doesn’t declare; they are not checked. Integers are accepted where a number is expected, and whole-number floats (e.g. 3.0) where an integer is.

pub fn validate_json(
  schema: Schema,
  json_string: String,
) -> Result(json_value.JsonValue, List(error.ValidationError))

Parse a JSON string and validate it against this schema. Invalid JSON is reported as a single validation error at the root.

✨ Search Document