Class: Agentic::StructuredOutputs::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/agentic/structured_outputs.rb

Overview

Schema class for defining JSON schemas

Constant Summary collapse

MAX_OBJECT_PROPERTIES =
100
MAX_NESTING_DEPTH =
5

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) {|_self| ... } ⇒ Schema

Returns a new instance of Schema.

Yields:

  • (_self)

Yield Parameters:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/agentic/structured_outputs.rb', line 14

def initialize(name = nil, &block)
  # Use the provided name or derive from class name
  @name = name || self.class.name.split("::").last.downcase
  # Initialize the base schema structure
  @schema = {
    type: "object",
    properties: {},
    required: [],
    additionalProperties: false,
    strict: true
  }
  @definitions = {}
  # Execute the provided block to define the schema
  yield(self) if block
  validate_schema
end

Instance Method Details

#any_of(name, schemas) ⇒ Object

Define an anyOf property



73
74
75
# File 'lib/agentic/structured_outputs.rb', line 73

def any_of(name, schemas)
  add_property(name, {anyOf: schemas})
end

#array(name, items:) ⇒ Object

Define an array property



68
69
70
# File 'lib/agentic/structured_outputs.rb', line 68

def array(name, items:)
  add_property(name, {type: "array", items: items})
end

#boolean(name) ⇒ Object

Define a boolean property



51
52
53
# File 'lib/agentic/structured_outputs.rb', line 51

def boolean(name)
  add_property(name, {type: "boolean"})
end

#define(name, &block) ⇒ Object

Define a reusable schema component



78
79
80
# File 'lib/agentic/structured_outputs.rb', line 78

def define(name, &block)
  @definitions[name] = Schema.new(&block).instance_variable_get(:@schema)
end

#number(name) ⇒ Object

Define a number property



46
47
48
# File 'lib/agentic/structured_outputs.rb', line 46

def number(name)
  add_property(name, {type: "number"})
end

#object(name, &block) ⇒ Object

Define an object property



56
57
58
59
60
61
62
63
64
65
# File 'lib/agentic/structured_outputs.rb', line 56

def object(name, &block)
  properties = {}
  required = []
  Schema.new.tap do |s|
    s.instance_eval(&block)
    properties = s.instance_variable_get(:@schema)[:properties]
    required = s.instance_variable_get(:@schema)[:required]
  end
  add_property(name, {type: "object", properties: properties, required: required, additionalProperties: false})
end

#ref(name) ⇒ Object

Reference a defined schema component



83
84
85
# File 'lib/agentic/structured_outputs.rb', line 83

def ref(name)
  {"$ref" => "#/$defs/#{name}"}
end

#string(name, enum: nil) ⇒ Object

Define a string property



41
42
43
# File 'lib/agentic/structured_outputs.rb', line 41

def string(name, enum: nil)
  add_property(name, enum ? {type: "string", enum: enum} : {type: "string"})
end

#to_hashObject

Convert the schema to a hash format



32
33
34
35
36
37
38
# File 'lib/agentic/structured_outputs.rb', line 32

def to_hash
  {
    name: @name,
    description: "Schema for the structured response",
    schema: @schema
  }
end