Module: Anthropic::Helpers::Messages

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

Class Method Summary collapse

Class Method Details

.distill_input_schema_models!(data, strict:) ⇒ 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:

Returns:

API:

  • private



17
18
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
# File 'lib/anthropic/helpers/messages.rb', line 17

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

  case data
  in {tools: Array => tool_array}
    mapped = tool_array.map do |tool|
      case tool
      # Direct tool class:
      in Anthropic::Helpers::InputSchema::JsonSchemaConverter
        classname = tool.is_a?(Anthropic::Helpers::Tools::BaseTool) ? tool.class.name : tool.name
        name = model_name(classname)

        description =
          case tool
          in Anthropic::Helpers::Tools::BaseTool
            tool.class.doc_string || name
          in Class if tool <= Anthropic::Helpers::InputSchema::BaseModel
            tool.doc_string || name
          else
            name
          end

        tools.store(name, tool)
        input_schema = Anthropic::Helpers::InputSchema::JsonSchemaConverter.to_json_schema(tool)
        {name:, description:, input_schema:}.tap { _1.update(strict:) if strict }
      # Tool with explicit name/description and BaseModel as input_schema:
      in {name: String => name,
          input_schema: Anthropic::Helpers::InputSchema::JsonSchemaConverter => tool,
          **rest}
        tools.store(name, tool)
        input_schema = Anthropic::Helpers::InputSchema::JsonSchemaConverter.to_json_schema(tool)
        rest.merge(name:, input_schema:).tap { _1.update(strict:) if strict }
      else
        # Any other format (pass through unchanged)
        # This includes raw JSON schemas and any other tool definitions.
        tool
      end
    end
    tool_array.replace(mapped)
  in {output_format: Anthropic::Helpers::InputSchema::JsonSchemaConverter => model}
    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})
  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{Sybmol=>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:

Returns:

API:

  • private



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
109
# File 'lib/anthropic/helpers/messages.rb', line 84

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