Sinter.JsonSchema (Sinter v0.0.1)

View Source

Unified JSON Schema generation for Sinter.

This module provides the single JSON Schema generation engine that handles all JSON Schema creation in Sinter. It converts Sinter schemas into standard JSON Schema format with optional provider-specific optimizations.

Features

  • Standard JSON Schema generation
  • Provider-specific optimizations (OpenAI, Anthropic, etc.)
  • Reference resolution and flattening
  • Constraint mapping
  • Metadata preservation

Usage

schema = Sinter.Schema.define([
  {:name, :string, [required: true, min_length: 2]},
  {:age, :integer, [optional: true, gt: 0]}
])

# Basic JSON Schema generation
json_schema = Sinter.JsonSchema.generate(schema)

# Provider-specific optimization
openai_schema = Sinter.JsonSchema.generate(schema, optimize_for_provider: :openai)

# For specific providers
anthropic_schema = Sinter.JsonSchema.for_provider(schema, :anthropic)

Summary

Functions

Generates a JSON Schema optimized for a specific LLM provider.

Generates a JSON Schema from a Sinter schema.

Validates a JSON Schema for correctness and compatibility.

Types

generation_opts()

@type generation_opts() :: [
  optimize_for_provider: :openai | :anthropic | :generic,
  flatten: boolean(),
  include_descriptions: boolean(),
  strict: boolean()
]

Functions

for_provider(schema, provider, additional_opts \\ [])

@spec for_provider(Sinter.Schema.t(), :openai | :anthropic | :generic, keyword()) ::
  map()

Generates a JSON Schema optimized for a specific LLM provider.

This is a convenience function that applies provider-specific optimizations and returns a schema ready for use with that provider's API.

Examples

iex> openai_schema = Sinter.JsonSchema.for_provider(schema, :openai)
iex> anthropic_schema = Sinter.JsonSchema.for_provider(schema, :anthropic)

generate(schema, opts \\ [])

@spec generate(Sinter.Schema.t(), generation_opts()) :: map()

Generates a JSON Schema from a Sinter schema.

This is the core JSON Schema generation function that converts Sinter schemas into standard JSON Schema format.

Parameters

Options

  • :optimize_for_provider - Apply provider-specific optimizations
    • :openai - Optimize for OpenAI function calling
    • :anthropic - Optimize for Anthropic tool use
    • :generic - Standard JSON Schema (default)
  • :flatten - Resolve all references inline (default: false)
  • :include_descriptions - Include field descriptions (default: true)
  • :strict - Override schema's strict setting for additionalProperties

Returns

  • JSON Schema map

Examples

iex> schema = Sinter.Schema.define([
...>   {:name, :string, [required: true, min_length: 2]},
...>   {:age, :integer, [optional: true, gt: 0]}
...> ], title: "User Schema")
iex> Sinter.JsonSchema.generate(schema)
%{
  "type" => "object",
  "title" => "User Schema",
  "properties" => %{
    "name" => %{"type" => "string", "minLength" => 2},
    "age" => %{"type" => "integer", "exclusiveMinimum" => 0}
  },
  "required" => ["name"],
  "additionalProperties" => false
}

# Provider-specific optimization
iex> Sinter.JsonSchema.generate(schema, optimize_for_provider: :openai)
%{
  "type" => "object",
  "properties" => %{...},
  "additionalProperties" => false,
  "required" => ["name"]
}

validate_schema(json_schema, opts \\ [])

@spec validate_schema(
  map(),
  keyword()
) :: :ok | {:error, [String.t()]}

Validates a JSON Schema for correctness and compatibility.

Parameters

  • json_schema - The JSON Schema to validate
  • opts - Validation options

Returns

  • :ok if schema is valid
  • {:error, issues} if problems are found