Class: Anthropic::Helpers::InputSchema::BaseModel

Inherits:
Internal::Type::BaseModel show all
Extended by:
JsonSchemaConverter
Defined in:
lib/anthropic/helpers/input_schema/base_model.rb

Overview

Represents a response from Anthropic's API where the model's output has been structured according to a schema predefined by the user.

This class is specifically used when making requests with the input_schema parameter.

See examples/input_schemasexamples/input_schemas.rb for a complete example of use

Constant Summary

Constants included from JsonSchemaConverter

JsonSchemaConverter::NO_REF, JsonSchemaConverter::POINTERS

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from JsonSchemaConverter

assoc_meta!, cache_def!, to_json_schema_inner, to_nilable

Methods inherited from Internal::Type::BaseModel

==, #==, #[], coerce, #deconstruct_keys, #deep_to_h, dump, fields, #hash, hash, inherited, #initialize, #inspect, inspect, known_fields, recursively_to_h, #to_h, #to_json, #to_s, to_sorbet_type, #to_yaml

Methods included from Internal::Type::Converter

#coerce, coerce, #dump, dump, inspect, #inspect, meta_info, new_coerce_state, type_info

Methods included from Internal::Util::SorbetRuntimeSupport

#const_missing, #define_sorbet_constant!, #sorbet_constant_defined?, #to_sorbet_type, to_sorbet_type

Constructor Details

This class inherits a constructor from Anthropic::Internal::Type::BaseModel

Class Attribute Details

.doc_stringvoid (readonly)

Returns the value of attribute doc_string.



71
72
73
# File 'lib/anthropic/helpers/input_schema/base_model.rb', line 71

def doc_string
  @doc_string
end

Class Method Details

.doc(description) ⇒ void

Parameters:

  • description (String)


74
75
76
# File 'lib/anthropic/helpers/input_schema/base_model.rb', line 74

def doc(description)
  @doc_string = description
end

.optional(name_sym, type_info, *args) ⇒ void



66
67
68
69
# File 'lib/anthropic/helpers/input_schema/base_model.rb', line 66

def optional(name_sym, type_info, *args)
  spec = process_field_args(args)
  super(name_sym, type_info, spec)
end

.required(name_sym, type_info, *args) ⇒ void



61
62
63
64
# File 'lib/anthropic/helpers/input_schema/base_model.rb', line 61

def required(name_sym, type_info, *args)
  spec = process_field_args(args)
  super(name_sym, type_info, spec)
end

.to_json_schemaHash{Symbol=>Object}

Returns:

  • (Hash{Symbol=>Object})


16
# File 'lib/anthropic/helpers/input_schema/base_model.rb', line 16

def to_json_schema = Anthropic::Helpers::InputSchema::JsonSchemaConverter.to_json_schema(self)

.to_json_schema_inner(state:) ⇒ Hash{Symbol=>Object}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • state (Hash{Symbol=>Object})

    @option state [HashObject=>String] :defs

    @option state [Array] :path

Returns:

  • (Hash{Symbol=>Object})


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/anthropic/helpers/input_schema/base_model.rb', line 27

def to_json_schema_inner(state:)
  Anthropic::Helpers::InputSchema::JsonSchemaConverter.cache_def!(state, type: self) do
    path = state.fetch(:path)
    properties = fields.to_h do |name, field|
      type, nilable, meta = field.fetch_values(:type, :nilable, :meta)
      new_state = {**state, path: [*path, ".#{name}"]}

      schema =
        case type
        in Anthropic::Helpers::InputSchema::JsonSchemaConverter
          type.to_json_schema_inner(state: new_state)
        else
          Anthropic::Helpers::InputSchema::JsonSchemaConverter.to_json_schema_inner(
            type,
            state: new_state
          )
        end
      Anthropic::Helpers::InputSchema::JsonSchemaConverter.assoc_meta!(schema, meta: meta)
      schema = Anthropic::Helpers::InputSchema::JsonSchemaConverter.to_nilable(schema) if nilable

      [name, schema]
    end

    {
      type: "object",
      properties: properties,
      required: fields.select { _2.fetch(:required) }.keys.map(&:to_s),
      additionalProperties: false
    }.tap { _1.store(:description, @doc_string) unless @doc_string.nil? }
  end
end