Module: RubyLLM::Schema::DSL::SchemaBuilders

Included in:
RubyLLM::Schema::DSL
Defined in:
lib/ruby_llm/schema/dsl/schema_builders.rb

Instance Method Summary collapse

Instance Method Details

#any_of_schema(description: nil, &block) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/ruby_llm/schema/dsl/schema_builders.rb', line 90

def any_of_schema(description: nil, &block)
  schemas = collect_schemas_from_block(&block)

  {
    description: description,
    anyOf: schemas
  }.compact
end

#array_schema(description: nil, of: nil, min_items: nil, max_items: nil, &block) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ruby_llm/schema/dsl/schema_builders.rb', line 78

def array_schema(description: nil, of: nil, min_items: nil, max_items: nil, &block)
  items = determine_array_items(of, &block)

  {
    type: "array",
    description: description,
    items: items,
    minItems: min_items,
    maxItems: max_items
  }.compact
end

#boolean_schema(description: nil) ⇒ Object



39
40
41
# File 'lib/ruby_llm/schema/dsl/schema_builders.rb', line 39

def boolean_schema(description: nil)
  {type: "boolean", description: description}.compact
end

#integer_schema(description: nil, minimum: nil, maximum: nil, multiple_of: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/ruby_llm/schema/dsl/schema_builders.rb', line 29

def integer_schema(description: nil, minimum: nil, maximum: nil, multiple_of: nil)
  {
    type: "integer",
    description: description,
    minimum: minimum,
    maximum: maximum,
    multipleOf: multiple_of
  }.compact
end

#null_schema(description: nil) ⇒ Object



43
44
45
# File 'lib/ruby_llm/schema/dsl/schema_builders.rb', line 43

def null_schema(description: nil)
  {type: "null", description: description}.compact
end

#number_schema(description: nil, minimum: nil, maximum: nil, multiple_of: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/ruby_llm/schema/dsl/schema_builders.rb', line 19

def number_schema(description: nil, minimum: nil, maximum: nil, multiple_of: nil)
  {
    type: "number",
    description: description,
    minimum: minimum,
    maximum: maximum,
    multipleOf: multiple_of
  }.compact
end

#object_schema(description: nil, of: nil, reference: nil, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ruby_llm/schema/dsl/schema_builders.rb', line 47

def object_schema(description: nil, of: nil, reference: nil, &block)
  if reference
    warn "[DEPRECATION] The `reference` option will be deprecated. Please use `of` instead."
    of = reference
  end

  if of
    determine_object_reference(of, description)
  else
    sub_schema = Class.new(Schema)
    result = sub_schema.class_eval(&block)

    # If the block returned a reference and no properties were added, use the reference
    if result.is_a?(Hash) && result["$ref"] && sub_schema.properties.empty?
      result.merge(description ? {description: description} : {})
    # If the block returned a Schema class or instance, convert it to inline schema
    elsif schema_class?(result) && sub_schema.properties.empty?
      schema_class_to_inline_schema(result).merge(description ? {description: description} : {})
    # Block didn't return reference or schema, so we build an inline object schema
    else
      {
        type: "object",
        properties: sub_schema.properties,
        required: sub_schema.required_properties,
        additionalProperties: sub_schema.additional_properties,
        description: description
      }.compact
    end
  end
end

#string_schema(description: nil, enum: nil, min_length: nil, max_length: nil, pattern: nil, format: nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/ruby_llm/schema/dsl/schema_builders.rb', line 7

def string_schema(description: nil, enum: nil, min_length: nil, max_length: nil, pattern: nil, format: nil)
  {
    type: "string",
    enum: enum,
    description: description,
    minLength: min_length,
    maxLength: max_length,
    pattern: pattern,
    format: format
  }.compact
end