class RubyLLM::Tool::SchemaDefinition
Wraps schema handling for tool parameters, supporting JSON Schema hashes, RubyLLM::Schema instances/classes, and DSL blocks.
Public Class Methods
Source
# File lib/ruby_llm/tool.rb, line 222 def self.default_items_schema { type: 'string' } end
# File lib/ruby_llm/tool.rb, line 183 def self.from_parameters(parameters, allow_empty: false) return nil if parameters.nil? || (parameters.empty? && !allow_empty) properties = parameters.to_h do |name, param| schema = { type: map_type(param.type), description: param.description }.compact schema[:items] = default_items_schema if schema[:type] == 'array' [name.to_s, schema] end required = parameters.select { |_, param| param.required }.keys.map(&:to_s) json_schema = { type: 'object', properties: properties, required: required, additionalProperties: false, strict: true } new(schema: json_schema) end
Source
# File lib/ruby_llm/tool.rb, line 210 def self.map_type(type) case type.to_s when 'integer', 'int' then 'integer' when 'number', 'float', 'double' then 'number' when 'boolean' then 'boolean' when 'array' then 'array' when 'object' then 'object' else 'string' end end
# File lib/ruby_llm/tool.rb, line 226 def initialize(schema: nil, block: nil) @schema = schema @block = block end
Public Instance Methods
Source
# File lib/ruby_llm/tool.rb, line 235 def json_schema @json_schema ||= RubyLLM::Utils.deep_stringify_keys(resolve_schema) end