Module: Anthropic::Helpers::Messages

Defined in:
lib/anthropic/helpers/messages.rb

Class Method Summary collapse

Class Method Details

.distill_input_schema_models!(data, strict:, is_beta: false) ⇒ Hash{String=>Class}

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.

Extract tool models from the request and convert them to JSON Schema Returns a hash mapping tool name to Ruby model.

Parameters:

  • data (Hash{Symbol=>Object})
  • strict (Boolean, nil)
  • is_beta (Boolean) (defaults to: false)

Returns:

  • (Hash{String=>Class}, Hash{String=>Class})


19
20
21
22
23
24
25
26
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/anthropic/helpers/messages.rb', line 19

def distill_input_schema_models!(data, strict:, is_beta: false)
  tools = {}
  models = {}

  # Check for invalid output_config type before pattern matching
  if data.key?(:output_config) && !data[:output_config].is_a?(Hash)
    raise ArgumentError,
          "output_config must be a Hash, got #{data[:output_config].class}"
  end

  # Check for conflicting output_format and output_config[:format] before pattern matching
  if data.key?(:output_format) && data.dig(:output_config, :format)
    raise ArgumentError,
          "Both output_format and output_config[:format] were provided. " \
          "Please use only output_config[:format] (output_format is deprecated)."
  end

  case data
  in {tools: Array => tool_array}
    tool_array.replace(tool_array.map { tool_to_param(_1, tools:, strict:) })
  else
  end

  # A `tool_addition` block defines a tool by value, in the shape of a `tools` entry.
  case data
  in {messages: Array => messages}
    messages.each do |message|
      case message
      in {role: :system | "system", content: Array => blocks}
        blocks.each do |block|
          case block
          in {
            type: :tool_addition | "tool_addition",
            tool: {type: :tool_definition | "tool_definition", definition:} => tool
          }
            tool.store(:definition, tool_to_param(definition, tools:, strict:))
          else
          end
        end
      else
      end
    end
  else
  end

  # Matched separately from `tools:` so a request can carry both tools and a structured output format.
  case data
  # GA: output_config with BaseModel class as format
  in {output_config: {format: Anthropic::Helpers::InputSchema::JsonSchemaConverter => model} => output_config}
    name = model_name(model.name)
    models.store(name, model)
    schema = model.to_json_schema
    Anthropic::Helpers::InputSchema::SupportedSchemas.transform_schema!(schema)
    output_config.update(format: {type: :json_schema, schema:})
    inject_structured_output_beta_header!(data) if is_beta
  # GA: output_config.format.schema as BaseModel class
  in {output_config: {format: {schema: Anthropic::Helpers::InputSchema::JsonSchemaConverter => model} => format_} => _output_config}
    name = model_name(model.name)
    models.store(name, model)
    schema = model.to_json_schema
    Anthropic::Helpers::InputSchema::SupportedSchemas.transform_schema!(schema)
    format_.update(type: :json_schema, schema:)
    inject_structured_output_beta_header!(data) if is_beta
  # Beta: output_format as BaseModel class (deprecated)
  in {output_format: Anthropic::Helpers::InputSchema::JsonSchemaConverter => model}
    transform_output_format_to_output_config!(data, model:, models:, is_beta:)
  # rubocop:disable Lint/DuplicateBranch
  in {output_format: {schema: Anthropic::Helpers::InputSchema::JsonSchemaConverter => model}}
    # This branch handles output_format: {schema: Model} vs output_format: Model above
    transform_output_format_to_output_config!(data, model:, models:, is_beta:)
  # rubocop:enable Lint/DuplicateBranch
  in {output_config: {format: Anthropic::Helpers::InputSchema::JsonSchemaConverter => model}}
    # New API: output_config.format with model class
    name = model_name(model.name)
    models.store(name, model)
    schema = model.to_json_schema
    Anthropic::Helpers::InputSchema::SupportedSchemas.transform_schema!(schema)
    data.update(output_format: {type: :json_schema, schema: schema})
  # Beta: output_format.schema as BaseModel class (deprecated)
  in {output_format: {schema: Anthropic::Helpers::InputSchema::JsonSchemaConverter => model} => output_format}
    name = model_name(model.name)
    models.store(name, model)
    schema = model.to_json_schema
    Anthropic::Helpers::InputSchema::SupportedSchemas.transform_schema!(schema)
    output_format.update(type: :json_schema, schema:)
  else
  end

  [tools, models]
end

.parse_input_schemas!(raw, tools:, models:) ⇒ 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:

  • raw (Hash{Symbol=>Object})
  • tools (Hash{String=>Class})
  • models (Hash{String=>Class})

Returns:

  • (Hash{Symbol=>Object})


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/anthropic/helpers/messages.rb', line 119

def parse_input_schemas!(raw, tools:, models:)
  raw[:content]&.each do |content|
    case content
    in {type: "tool_use", name:, input:} if (tool = tools[name])
      begin
        coerced = Anthropic::Internal::Type::Converter.coerce(tool, input)

        content.store(:parsed, coerced)
      rescue StandardError => e
        content.store(:parsed, {error: e.message})
      end
    in {type: "text", text:} if (model = models.first&.last)
      begin
        json = JSON.parse(text, symbolize_names: true)
        coerced = Anthropic::Internal::Type::Converter.coerce(model, json)

        content.store(:parsed, coerced)
      rescue StandardError => e
        content.store(:parsed, {error: e.message})
      end
    else
    end
  end

  raw
end

.tool_api_name(tool) ⇒ String

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.

The name a runnable tool is serialized under in tools[].

Parameters:

Returns:

  • (String)


255
# File 'lib/anthropic/helpers/messages.rb', line 255

def tool_api_name(tool) = tool.class.tool_name || model_name(tool.class.name)