Module: LLM::Schema::Parser

Included in:
LLM::Schema
Defined in:
lib/llm/schema/parser.rb

Overview

The LLM::Schema::Parser module provides methods for parsing a JSON schema into Leaf objects. It is used by LLM::Schema to convert external JSON schema definitions into the schema objects used throughout llm.rb.

Instance Method Summary collapse

Instance Method Details

#parse(schema, root = nil) ⇒ LLM::Schema::Leaf

Parses a JSON schema into an Leaf.

Parameters:

  • schema (Hash)

    The JSON schema to parse

Returns:

Raises:

  • (TypeError)

    When the schema is not supported



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/llm/schema/parser.rb', line 18

def parse(schema, root = nil)
  schema = normalize_schema(schema)
  root ||= schema
  schema = resolve_ref(schema, root)
  case schema["type"]
  when "object" then apply(parse_object(schema, root), schema)
  when "array" then apply(parse_array(schema, root), schema)
  when "string" then apply(parse_string(schema), schema)
  when "integer" then apply(parse_integer(schema), schema)
  when "number" then apply(parse_number(schema), schema)
  when "boolean" then apply(schema().boolean, schema)
  when "null" then apply(schema().null, schema)
  else raise TypeError, "unsupported schema type #{schema["type"].inspect}"
  end
end