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.
Constant Summary collapse
- METADATA_KEYS =
%w[description default enum const].freeze
Instance Method Summary collapse
-
#parse(schema, root = nil) ⇒ LLM::Schema::Leaf
Parses a JSON schema into an Leaf.
Instance Method Details
#parse(schema, root = nil) ⇒ LLM::Schema::Leaf
Parses a JSON schema into an Leaf.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/llm/schema/parser.rb', line 20 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) when ::Array then apply(schema().any_of(*schema["type"].map { parse(schema.except("type", *METADATA_KEYS).merge("type" => _1), root) }), schema.except("type")) when nil then parse_union(schema, root) else raise TypeError, "unsupported schema type #{schema["type"].inspect}" end end |