Class: RubyLLM::Schema

Inherits:
Object
  • Object
show all
Extended by:
DSL
Includes:
JsonOutput
Defined in:
lib/ruby_llm/schema.rb,
lib/ruby_llm/schema/dsl.rb,
lib/ruby_llm/schema/errors.rb,
lib/ruby_llm/schema/validator.rb,
lib/ruby_llm/schema/json_output.rb,
lib/ruby_llm/schema/dsl/utilities.rb,
lib/ruby_llm/schema/dsl/complex_types.rb,
lib/ruby_llm/schema/dsl/primitive_types.rb,
lib/ruby_llm/schema/dsl/schema_builders.rb

Defined Under Namespace

Modules: DSL, JsonOutput Classes: Error, InvalidArrayTypeError, InvalidObjectTypeError, InvalidSchemaError, InvalidSchemaTypeError, LimitExceededError, ValidationError, Validator

Constant Summary collapse

PRIMITIVE_TYPES =
i[string number integer boolean null].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL::Utilities

#define, #reference

Methods included from DSL::ComplexTypes

#any_of, #array, #object, #optional

Methods included from DSL::PrimitiveTypes

#boolean, #integer, #null, #number, #string

Methods included from DSL::SchemaBuilders

#any_of_schema, #array_schema, #boolean_schema, #integer_schema, #null_schema, #number_schema, #object_schema, #string_schema

Methods included from JsonOutput

#to_json, #to_json_schema

Constructor Details

#initialize(name = nil, description: nil) ⇒ Schema

Returns a new instance of Schema.



65
66
67
68
# File 'lib/ruby_llm/schema.rb', line 65

def initialize(name = nil, description: nil)
  @name = name || self.class.name || "Schema"
  @description = description
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/ruby_llm/schema.rb', line 78

def method_missing(method_name, ...)
  if respond_to_missing?(method_name)
    self.class.send(method_name, ...)
  else
    super
  end
end

Class Method Details

.additional_properties(value = nil) ⇒ Object



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

def additional_properties(value = nil)
  return @additional_properties ||= false if value.nil?
  @additional_properties = value
end

.create(&block) ⇒ Object



19
20
21
22
23
# File 'lib/ruby_llm/schema.rb', line 19

def create(&block)
  schema_class = Class.new(Schema)
  schema_class.class_eval(&block)
  schema_class
end

.definitionsObject



33
34
35
# File 'lib/ruby_llm/schema.rb', line 33

def definitions
  @definitions ||= {}
end

.description(description = nil) ⇒ Object



37
38
39
40
# File 'lib/ruby_llm/schema.rb', line 37

def description(description = nil)
  @description = description if description
  @description
end

.propertiesObject



25
26
27
# File 'lib/ruby_llm/schema.rb', line 25

def properties
  @properties ||= {}
end

.required_propertiesObject



29
30
31
# File 'lib/ruby_llm/schema.rb', line 29

def required_properties
  @required_properties ||= []
end

.strict(value = nil) ⇒ Object



47
48
49
50
51
52
# File 'lib/ruby_llm/schema.rb', line 47

def strict(value = nil)
  if value.nil?
    return @strict.nil? ? (@strict = true) : @strict
  end
  @strict = value
end

.valid?Boolean

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/ruby_llm/schema.rb', line 59

def valid?
  validator = Validator.new(self)
  validator.valid?
end

.validate!Object



54
55
56
57
# File 'lib/ruby_llm/schema.rb', line 54

def validate!
  validator = Validator.new(self)
  validator.validate!
end

Instance Method Details

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/ruby_llm/schema.rb', line 86

def respond_to_missing?(method_name, include_private = false)
  i[string number integer boolean array object any_of null].include?(method_name) || super
end

#valid?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/ruby_llm/schema.rb', line 74

def valid?
  self.class.valid?
end

#validate!Object



70
71
72
# File 'lib/ruby_llm/schema.rb', line 70

def validate!
  self.class.validate!
end