anthropic 1.13.0 → 1.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +27 -0
  3. data/README.md +31 -1
  4. data/lib/anthropic/helpers/input_schema/base_model.rb +6 -3
  5. data/lib/anthropic/helpers/input_schema/json_schema_converter.rb +9 -3
  6. data/lib/anthropic/helpers/input_schema/supported_schemas.rb +106 -0
  7. data/lib/anthropic/helpers/input_schema/union_of.rb +3 -1
  8. data/lib/anthropic/helpers/messages.rb +107 -0
  9. data/lib/anthropic/helpers/streaming/message_stream.rb +54 -43
  10. data/lib/anthropic/helpers/tools/base_tool.rb +82 -0
  11. data/lib/anthropic/helpers/tools/runner.rb +156 -0
  12. data/lib/anthropic/helpers/tools.rb +5 -0
  13. data/lib/anthropic/internal/transport/base_client.rb +7 -1
  14. data/lib/anthropic/internal/transport/pooled_net_requester.rb +6 -2
  15. data/lib/anthropic/models/beta/beta_code_execution_tool_20250522.rb +8 -1
  16. data/lib/anthropic/models/beta/beta_code_execution_tool_20250825.rb +8 -1
  17. data/lib/anthropic/models/beta/beta_json_output_format.rb +27 -0
  18. data/lib/anthropic/models/beta/beta_memory_tool_20250818.rb +8 -1
  19. data/lib/anthropic/models/beta/beta_tool.rb +8 -1
  20. data/lib/anthropic/models/beta/beta_tool_bash_20241022.rb +8 -1
  21. data/lib/anthropic/models/beta/beta_tool_bash_20250124.rb +8 -1
  22. data/lib/anthropic/models/beta/beta_tool_computer_use_20241022.rb +8 -1
  23. data/lib/anthropic/models/beta/beta_tool_computer_use_20250124.rb +8 -1
  24. data/lib/anthropic/models/beta/beta_tool_text_editor_20241022.rb +8 -1
  25. data/lib/anthropic/models/beta/beta_tool_text_editor_20250124.rb +8 -1
  26. data/lib/anthropic/models/beta/beta_tool_text_editor_20250429.rb +8 -1
  27. data/lib/anthropic/models/beta/beta_tool_text_editor_20250728.rb +8 -1
  28. data/lib/anthropic/models/beta/beta_tool_use_block.rb +14 -0
  29. data/lib/anthropic/models/beta/beta_web_fetch_tool_20250910.rb +8 -1
  30. data/lib/anthropic/models/beta/beta_web_search_tool_20250305.rb +8 -1
  31. data/lib/anthropic/models/beta/message_count_tokens_params.rb +9 -1
  32. data/lib/anthropic/models/beta/message_create_params.rb +9 -1
  33. data/lib/anthropic/models/beta/messages/batch_create_params.rb +9 -1
  34. data/lib/anthropic/models/tool_use_block.rb +6 -6
  35. data/lib/anthropic/resources/beta/messages/batches.rb +6 -6
  36. data/lib/anthropic/resources/beta/messages.rb +37 -11
  37. data/lib/anthropic/resources/messages/batches.rb +6 -6
  38. data/lib/anthropic/resources/messages.rb +12 -84
  39. data/lib/anthropic/version.rb +1 -1
  40. data/lib/anthropic.rb +16 -10
  41. data/manifest.yaml +1 -0
  42. data/rbi/anthropic/helpers/input_schema/base_model.rbi +7 -2
  43. data/rbi/anthropic/helpers/tools/base_tool.rbi +51 -0
  44. data/rbi/anthropic/helpers/tools/runner.rbi +40 -0
  45. data/rbi/anthropic/helpers/tools.rbi +5 -0
  46. data/rbi/anthropic/internal/transport/base_client.rbi +5 -0
  47. data/rbi/anthropic/internal/transport/pooled_net_requester.rbi +6 -2
  48. data/rbi/anthropic/internal/type/base_model.rbi +8 -4
  49. data/rbi/anthropic/models/beta/beta_code_execution_tool_20250522.rbi +10 -1
  50. data/rbi/anthropic/models/beta/beta_code_execution_tool_20250825.rbi +10 -1
  51. data/rbi/anthropic/models/beta/beta_json_output_format.rbi +46 -0
  52. data/rbi/anthropic/models/beta/beta_memory_tool_20250818.rbi +10 -1
  53. data/rbi/anthropic/models/beta/beta_tool.rbi +9 -0
  54. data/rbi/anthropic/models/beta/beta_tool_bash_20241022.rbi +10 -1
  55. data/rbi/anthropic/models/beta/beta_tool_bash_20250124.rbi +10 -1
  56. data/rbi/anthropic/models/beta/beta_tool_computer_use_20241022.rbi +10 -1
  57. data/rbi/anthropic/models/beta/beta_tool_computer_use_20250124.rbi +10 -1
  58. data/rbi/anthropic/models/beta/beta_tool_text_editor_20241022.rbi +10 -1
  59. data/rbi/anthropic/models/beta/beta_tool_text_editor_20250124.rbi +10 -1
  60. data/rbi/anthropic/models/beta/beta_tool_text_editor_20250429.rbi +10 -1
  61. data/rbi/anthropic/models/beta/beta_tool_text_editor_20250728.rbi +10 -1
  62. data/rbi/anthropic/models/beta/beta_web_fetch_tool_20250910.rbi +10 -1
  63. data/rbi/anthropic/models/beta/beta_web_search_tool_20250305.rbi +9 -0
  64. data/rbi/anthropic/models/beta/message_count_tokens_params.rbi +17 -0
  65. data/rbi/anthropic/models/beta/message_create_params.rbi +17 -0
  66. data/rbi/anthropic/models/beta/messages/batch_create_params.rbi +18 -0
  67. data/rbi/anthropic/models/tool_use_block.rbi +3 -0
  68. data/rbi/anthropic/resources/beta/messages/batches.rbi +6 -6
  69. data/rbi/anthropic/resources/beta/messages.rbi +313 -3
  70. data/rbi/anthropic/resources/messages/batches.rbi +6 -6
  71. data/rbi/anthropic/resources/messages.rbi +5 -3
  72. data/sig/anthropic/internal/transport/base_client.rbs +2 -0
  73. data/sig/anthropic/internal/transport/pooled_net_requester.rbs +4 -1
  74. data/sig/anthropic/models/beta/beta_code_execution_tool_20250522.rbs +9 -2
  75. data/sig/anthropic/models/beta/beta_code_execution_tool_20250825.rbs +9 -2
  76. data/sig/anthropic/models/beta/beta_json_output_format.rbs +23 -0
  77. data/sig/anthropic/models/beta/beta_memory_tool_20250818.rbs +9 -2
  78. data/sig/anthropic/models/beta/beta_tool.rbs +7 -0
  79. data/sig/anthropic/models/beta/beta_tool_bash_20241022.rbs +9 -2
  80. data/sig/anthropic/models/beta/beta_tool_bash_20250124.rbs +9 -2
  81. data/sig/anthropic/models/beta/beta_tool_computer_use_20241022.rbs +9 -2
  82. data/sig/anthropic/models/beta/beta_tool_computer_use_20250124.rbs +9 -2
  83. data/sig/anthropic/models/beta/beta_tool_text_editor_20241022.rbs +9 -2
  84. data/sig/anthropic/models/beta/beta_tool_text_editor_20250124.rbs +9 -2
  85. data/sig/anthropic/models/beta/beta_tool_text_editor_20250429.rbs +9 -2
  86. data/sig/anthropic/models/beta/beta_tool_text_editor_20250728.rbs +9 -2
  87. data/sig/anthropic/models/beta/beta_web_fetch_tool_20250910.rbs +9 -2
  88. data/sig/anthropic/models/beta/beta_web_search_tool_20250305.rbs +7 -0
  89. data/sig/anthropic/models/beta/message_count_tokens_params.rbs +5 -0
  90. data/sig/anthropic/models/beta/message_create_params.rbs +5 -0
  91. data/sig/anthropic/models/beta/messages/batch_create_params.rbs +5 -0
  92. data/sig/anthropic/resources/beta/messages.rbs +3 -0
  93. metadata +14 -4
  94. data/lib/anthropic/helpers/input_schema/property_mapping.rb +0 -47
  95. /data/rbi/anthropic/helpers/{structured_output.rbi → input_schema.rbi} +0 -0
@@ -183,6 +183,12 @@ module Anthropic
183
183
  # @return [Anthropic::Models::Beta::BetaMetadata, nil]
184
184
  optional :metadata, -> { Anthropic::Beta::BetaMetadata }
185
185
 
186
+ # @!attribute output_format
187
+ # A schema to specify Claude's output format in responses.
188
+ #
189
+ # @return [Anthropic::Models::Beta::BetaJSONOutputFormat, nil]
190
+ optional :output_format, -> { Anthropic::Beta::BetaJSONOutputFormat }, nil?: true
191
+
186
192
  # @!attribute service_tier
187
193
  # Determines whether to use priority capacity (if available) or standard capacity
188
194
  # for this request.
@@ -368,7 +374,7 @@ module Anthropic
368
374
  # @return [Float, nil]
369
375
  optional :top_p, Float
370
376
 
371
- # @!method initialize(max_tokens:, messages:, model:, container: nil, context_management: nil, mcp_servers: nil, metadata: nil, service_tier: nil, stop_sequences: nil, stream: nil, system_: nil, temperature: nil, thinking: nil, tool_choice: nil, tools: nil, top_k: nil, top_p: nil)
377
+ # @!method initialize(max_tokens:, messages:, model:, container: nil, context_management: nil, mcp_servers: nil, metadata: nil, output_format: nil, service_tier: nil, stop_sequences: nil, stream: nil, system_: nil, temperature: nil, thinking: nil, tool_choice: nil, tools: nil, top_k: nil, top_p: nil)
372
378
  # Some parameter documentations has been truncated, see
373
379
  # {Anthropic::Models::Beta::Messages::BatchCreateParams::Request::Params} for more
374
380
  # details.
@@ -392,6 +398,8 @@ module Anthropic
392
398
  #
393
399
  # @param metadata [Anthropic::Models::Beta::BetaMetadata] An object describing metadata about the request.
394
400
  #
401
+ # @param output_format [Anthropic::Models::Beta::BetaJSONOutputFormat, nil]
402
+ #
395
403
  # @param service_tier [Symbol, Anthropic::Models::Beta::Messages::BatchCreateParams::Request::Params::ServiceTier] Determines whether to use priority capacity (if available) or standard capacity
396
404
  #
397
405
  # @param stop_sequences [Array<String>] Custom text sequences that will cause the model to stop generating.
@@ -23,18 +23,18 @@ module Anthropic
23
23
  # @return [Symbol, :tool_use]
24
24
  required :type, const: :tool_use
25
25
 
26
- # @api private
27
- #
28
- # Internal buffer for accumulating partial JSON during streaming.
29
- # Used by streaming helpers to reconstruct complete JSON input from deltas.
30
- optional :json_buf, String
31
-
32
26
  response_only do
33
27
  # @api public
34
28
  #
35
29
  # Parsed input data coerced to the tool's input schema model.
36
30
  # Only present when tools are defined using the InputSchema DSL.
37
31
  optional :parsed, Anthropic::Internal::Type::Unknown
32
+
33
+ # @api private
34
+ #
35
+ # Internal buffer for accumulating partial JSON during streaming.
36
+ # Used by streaming helpers to reconstruct complete JSON input from deltas.
37
+ optional :_json_buf, String
38
38
  end
39
39
 
40
40
  # @!method initialize(id:, input:, name:, type: :tool_use)
@@ -15,7 +15,7 @@ module Anthropic
15
15
  # can take up to 24 hours to complete.
16
16
  #
17
17
  # Learn more about the Message Batches API in our
18
- # [user guide](/en/docs/build-with-claude/batch-processing)
18
+ # [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
19
19
  #
20
20
  # @overload create(requests:, betas: nil, request_options: {})
21
21
  #
@@ -46,7 +46,7 @@ module Anthropic
46
46
  # `results_url` field in the response.
47
47
  #
48
48
  # Learn more about the Message Batches API in our
49
- # [user guide](/en/docs/build-with-claude/batch-processing)
49
+ # [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
50
50
  #
51
51
  # @overload retrieve(message_batch_id, betas: nil, request_options: {})
52
52
  #
@@ -77,7 +77,7 @@ module Anthropic
77
77
  # returned first.
78
78
  #
79
79
  # Learn more about the Message Batches API in our
80
- # [user guide](/en/docs/build-with-claude/batch-processing)
80
+ # [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
81
81
  #
82
82
  # @overload list(after_id: nil, before_id: nil, limit: nil, betas: nil, request_options: {})
83
83
  #
@@ -114,7 +114,7 @@ module Anthropic
114
114
  # like to delete an in-progress batch, you must first cancel it.
115
115
  #
116
116
  # Learn more about the Message Batches API in our
117
- # [user guide](/en/docs/build-with-claude/batch-processing)
117
+ # [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
118
118
  #
119
119
  # @overload delete(message_batch_id, betas: nil, request_options: {})
120
120
  #
@@ -149,7 +149,7 @@ module Anthropic
149
149
  # non-interruptible.
150
150
  #
151
151
  # Learn more about the Message Batches API in our
152
- # [user guide](/en/docs/build-with-claude/batch-processing)
152
+ # [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
153
153
  #
154
154
  # @overload cancel(message_batch_id, betas: nil, request_options: {})
155
155
  #
@@ -180,7 +180,7 @@ module Anthropic
180
180
  # requests. Use the `custom_id` field to match results to requests.
181
181
  #
182
182
  # Learn more about the Message Batches API in our
183
- # [user guide](/en/docs/build-with-claude/batch-processing)
183
+ # [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
184
184
  #
185
185
  # @overload results_streaming(message_batch_id, betas: nil, request_options: {})
186
186
  #
@@ -7,6 +7,13 @@ module Anthropic
7
7
  # @return [Anthropic::Resources::Beta::Messages::Batches]
8
8
  attr_reader :batches
9
9
 
10
+ # @param params [Anthropic::Models::Beta::MessageCreateParams]
11
+ #
12
+ # @return [Anthropic::Helpers::Tools::Runner]
13
+ def tool_runner(params)
14
+ Anthropic::Helpers::Tools::Runner.new(@client, params:)
15
+ end
16
+
10
17
  # See {Anthropic::Resources::Beta::Messages#stream_raw} for streaming counterpart.
11
18
  #
12
19
  # Some parameter documentations has been truncated, see
@@ -18,9 +25,10 @@ module Anthropic
18
25
  # The Messages API can be used for either single queries or stateless multi-turn
19
26
  # conversations.
20
27
  #
21
- # Learn more about the Messages API in our [user guide](/en/docs/initial-setup)
28
+ # Learn more about the Messages API in our
29
+ # [user guide](https://docs.claude.com/en/docs/initial-setup)
22
30
  #
23
- # @overload create(max_tokens:, messages:, model:, container: nil, context_management: nil, mcp_servers: nil, metadata: nil, service_tier: nil, stop_sequences: nil, system_: nil, temperature: nil, thinking: nil, tool_choice: nil, tools: nil, top_k: nil, top_p: nil, betas: nil, request_options: {})
31
+ # @overload create(max_tokens:, messages:, model:, container: nil, context_management: nil, mcp_servers: nil, metadata: nil, output_format: nil, service_tier: nil, stop_sequences: nil, system_: nil, temperature: nil, thinking: nil, tool_choice: nil, tools: nil, top_k: nil, top_p: nil, betas: nil, request_options: {})
24
32
  #
25
33
  # @param max_tokens [Integer] Body param: The maximum number of tokens to generate before stopping.
26
34
  #
@@ -36,6 +44,8 @@ module Anthropic
36
44
  #
37
45
  # @param metadata [Anthropic::Models::Beta::BetaMetadata] Body param: An object describing metadata about the request.
38
46
  #
47
+ # @param output_format [Anthropic::Models::Beta::BetaJSONOutputFormat, nil] Body param:
48
+ #
39
49
  # @param service_tier [Symbol, Anthropic::Models::Beta::MessageCreateParams::ServiceTier] Body param: Determines whether to use priority capacity (if available) or standa
40
50
  #
41
51
  # @param stop_sequences [Array<String>] Body param: Custom text sequences that will cause the model to stop generating.
@@ -68,8 +78,12 @@ module Anthropic
68
78
  raise ArgumentError.new(message)
69
79
  end
70
80
 
81
+ tools, models = Anthropic::Helpers::Messages.distill_input_schema_models!(parsed, strict: nil)
82
+
83
+ unwrap = ->(raw) { Anthropic::Helpers::Messages.parse_input_schemas!(raw, tools:, models:) }
84
+
71
85
  if options.empty? && @client.timeout == Anthropic::Client::DEFAULT_TIMEOUT_IN_SECONDS
72
- model = parsed[:model].to_sym
86
+ model = parsed[:model]&.to_sym
73
87
  max_tokens = parsed[:max_tokens].to_i
74
88
  timeout = @client.calculate_nonstreaming_timeout(
75
89
  max_tokens,
@@ -87,6 +101,7 @@ module Anthropic
87
101
  headers: parsed.slice(*header_params.keys).transform_keys(header_params),
88
102
  body: parsed.except(*header_params.keys),
89
103
  model: Anthropic::Beta::BetaMessage,
104
+ unwrap: unwrap,
90
105
  options: options
91
106
  )
92
107
  end
@@ -102,9 +117,10 @@ module Anthropic
102
117
  # The Messages API can be used for either single queries or stateless multi-turn
103
118
  # conversations.
104
119
  #
105
- # Learn more about the Messages API in our [user guide](/en/docs/initial-setup)
120
+ # Learn more about the Messages API in our
121
+ # [user guide](https://docs.claude.com/en/docs/initial-setup)
106
122
  #
107
- # @overload stream_raw(max_tokens:, messages:, model:, container: nil, context_management: nil, mcp_servers: nil, metadata: nil, service_tier: nil, stop_sequences: nil, system_: nil, temperature: nil, thinking: nil, tool_choice: nil, tools: nil, top_k: nil, top_p: nil, betas: nil, request_options: {})
123
+ # @overload stream_raw(max_tokens:, messages:, model:, container: nil, context_management: nil, mcp_servers: nil, metadata: nil, output_format: nil, service_tier: nil, stop_sequences: nil, system_: nil, temperature: nil, thinking: nil, tool_choice: nil, tools: nil, top_k: nil, top_p: nil, betas: nil, request_options: {})
108
124
  #
109
125
  # @param max_tokens [Integer] Body param: The maximum number of tokens to generate before stopping.
110
126
  #
@@ -120,6 +136,8 @@ module Anthropic
120
136
  #
121
137
  # @param metadata [Anthropic::Models::Beta::BetaMetadata] Body param: An object describing metadata about the request.
122
138
  #
139
+ # @param output_format [Anthropic::Models::Beta::BetaJSONOutputFormat, nil] Body param:
140
+ #
123
141
  # @param service_tier [Symbol, Anthropic::Models::Beta::MessageCreateParams::ServiceTier] Body param: Determines whether to use priority capacity (if available) or standa
124
142
  #
125
143
  # @param stop_sequences [Array<String>] Body param: Custom text sequences that will cause the model to stop generating.
@@ -152,16 +170,22 @@ module Anthropic
152
170
  raise ArgumentError.new(message)
153
171
  end
154
172
  parsed.store(:stream, true)
173
+ tools, models = Anthropic::Helpers::Messages.distill_input_schema_models!(parsed, strict: nil)
174
+
175
+ header_params = {betas: "anthropic-beta"}
155
176
  raw_stream = @client.request(
156
177
  method: :post,
157
178
  path: "v1/messages?beta=true",
158
- headers: {"accept" => "text/event-stream"},
159
- body: parsed,
179
+ headers: {
180
+ "accept" => "text/event-stream",
181
+ **parsed.slice(*header_params.keys)
182
+ }.transform_keys(header_params),
183
+ body: parsed.except(*header_params.keys),
160
184
  stream: Anthropic::Internal::Stream,
161
185
  model: Anthropic::Beta::BetaRawMessageStreamEvent,
162
- options: options
186
+ options: {timeout: 600, **options}
163
187
  )
164
- Anthropic::Streaming::MessageStream.new(raw_stream: raw_stream)
188
+ Anthropic::Streaming::MessageStream.new(raw_stream:, tools:, models:)
165
189
  end
166
190
 
167
191
  # See {Anthropic::Resources::Beta::Messages#create} for non-streaming counterpart.
@@ -247,9 +271,9 @@ module Anthropic
247
271
  # including tools, images, and documents, without creating it.
248
272
  #
249
273
  # Learn more about token counting in our
250
- # [user guide](/en/docs/build-with-claude/token-counting)
274
+ # [user guide](https://docs.claude.com/en/docs/build-with-claude/token-counting)
251
275
  #
252
- # @overload count_tokens(messages:, model:, context_management: nil, mcp_servers: nil, system_: nil, thinking: nil, tool_choice: nil, tools: nil, betas: nil, request_options: {})
276
+ # @overload count_tokens(messages:, model:, context_management: nil, mcp_servers: nil, output_format: nil, system_: nil, thinking: nil, tool_choice: nil, tools: nil, betas: nil, request_options: {})
253
277
  #
254
278
  # @param messages [Array<Anthropic::Models::Beta::BetaMessageParam>] Body param: Input messages.
255
279
  #
@@ -259,6 +283,8 @@ module Anthropic
259
283
  #
260
284
  # @param mcp_servers [Array<Anthropic::Models::Beta::BetaRequestMCPServerURLDefinition>] Body param: MCP servers to be utilized in this request
261
285
  #
286
+ # @param output_format [Anthropic::Models::Beta::BetaJSONOutputFormat, nil] Body param:
287
+ #
262
288
  # @param system_ [String, Array<Anthropic::Models::Beta::BetaTextBlockParam>] Body param: System prompt.
263
289
  #
264
290
  # @param thinking [Anthropic::Models::Beta::BetaThinkingConfigEnabled, Anthropic::Models::Beta::BetaThinkingConfigDisabled] Body param: Configuration for enabling Claude's extended thinking.
@@ -14,7 +14,7 @@ module Anthropic
14
14
  # can take up to 24 hours to complete.
15
15
  #
16
16
  # Learn more about the Message Batches API in our
17
- # [user guide](/en/docs/build-with-claude/batch-processing)
17
+ # [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
18
18
  #
19
19
  # @overload create(requests:, request_options: {})
20
20
  #
@@ -41,7 +41,7 @@ module Anthropic
41
41
  # `results_url` field in the response.
42
42
  #
43
43
  # Learn more about the Message Batches API in our
44
- # [user guide](/en/docs/build-with-claude/batch-processing)
44
+ # [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
45
45
  #
46
46
  # @overload retrieve(message_batch_id, request_options: {})
47
47
  #
@@ -68,7 +68,7 @@ module Anthropic
68
68
  # returned first.
69
69
  #
70
70
  # Learn more about the Message Batches API in our
71
- # [user guide](/en/docs/build-with-claude/batch-processing)
71
+ # [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
72
72
  #
73
73
  # @overload list(after_id: nil, before_id: nil, limit: nil, request_options: {})
74
74
  #
@@ -101,7 +101,7 @@ module Anthropic
101
101
  # like to delete an in-progress batch, you must first cancel it.
102
102
  #
103
103
  # Learn more about the Message Batches API in our
104
- # [user guide](/en/docs/build-with-claude/batch-processing)
104
+ # [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
105
105
  #
106
106
  # @overload delete(message_batch_id, request_options: {})
107
107
  #
@@ -132,7 +132,7 @@ module Anthropic
132
132
  # non-interruptible.
133
133
  #
134
134
  # Learn more about the Message Batches API in our
135
- # [user guide](/en/docs/build-with-claude/batch-processing)
135
+ # [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
136
136
  #
137
137
  # @overload cancel(message_batch_id, request_options: {})
138
138
  #
@@ -159,7 +159,7 @@ module Anthropic
159
159
  # requests. Use the `custom_id` field to match results to requests.
160
160
  #
161
161
  # Learn more about the Message Batches API in our
162
- # [user guide](/en/docs/build-with-claude/batch-processing)
162
+ # [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing)
163
163
  #
164
164
  # @overload results_streaming(message_batch_id, request_options: {})
165
165
  #
@@ -17,7 +17,8 @@ module Anthropic
17
17
  # The Messages API can be used for either single queries or stateless multi-turn
18
18
  # conversations.
19
19
  #
20
- # Learn more about the Messages API in our [user guide](/en/docs/initial-setup)
20
+ # Learn more about the Messages API in our
21
+ # [user guide](https://docs.claude.com/en/docs/initial-setup)
21
22
  #
22
23
  # @overload create(max_tokens:, messages:, model:, metadata: nil, service_tier: nil, stop_sequences: nil, system_: nil, temperature: nil, thinking: nil, tool_choice: nil, tools: nil, top_k: nil, top_p: nil, request_options: {})
23
24
  #
@@ -59,14 +60,12 @@ module Anthropic
59
60
  raise ArgumentError.new(message)
60
61
  end
61
62
 
62
- tool_models = get_structured_output_models(parsed)
63
+ tools, models = Anthropic::Helpers::Messages.distill_input_schema_models!(parsed, strict: nil)
63
64
 
64
- unwrap = if tool_models.any?
65
- ->(raw) { parse_structured_outputs!(raw, tool_models) }
66
- end
65
+ unwrap = ->(raw) { Anthropic::Helpers::Messages.parse_input_schemas!(raw, tools:, models:) }
67
66
 
68
67
  if options.empty? && @client.timeout == Anthropic::Client::DEFAULT_TIMEOUT_IN_SECONDS
69
- model = parsed[:model].to_sym
68
+ model = parsed[:model]&.to_sym
70
69
  max_tokens = parsed[:max_tokens].to_i
71
70
  timeout = @client.calculate_nonstreaming_timeout(
72
71
  max_tokens,
@@ -87,6 +86,8 @@ module Anthropic
87
86
  )
88
87
  end
89
88
 
89
+ alias_method :parse, :create
90
+
90
91
  # See {Anthropic::Resources::Messages#create} for non-streaming counterpart.
91
92
  #
92
93
  # Some parameter documentations has been truncated, see
@@ -141,7 +142,7 @@ module Anthropic
141
142
  end
142
143
  parsed.store(:stream, true)
143
144
 
144
- tool_models = get_structured_output_models(parsed)
145
+ tools, models = Anthropic::Helpers::Messages.distill_input_schema_models!(parsed, strict: nil)
145
146
 
146
147
  raw_stream = @client.request(
147
148
  method: :post,
@@ -152,10 +153,7 @@ module Anthropic
152
153
  model: Anthropic::Models::RawMessageStreamEvent,
153
154
  options: options
154
155
  )
155
- Anthropic::Streaming::MessageStream.new(
156
- raw_stream: raw_stream,
157
- tool_models: tool_models
158
- )
156
+ Anthropic::Streaming::MessageStream.new(raw_stream:, tools:, models:)
159
157
  end
160
158
 
161
159
  # See {Anthropic::Resources::Messages#create} for non-streaming counterpart.
@@ -169,7 +167,8 @@ module Anthropic
169
167
  # The Messages API can be used for either single queries or stateless multi-turn
170
168
  # conversations.
171
169
  #
172
- # Learn more about the Messages API in our [user guide](/en/docs/initial-setup)
170
+ # Learn more about the Messages API in our
171
+ # [user guide](https://docs.claude.com/en/docs/initial-setup)
173
172
  #
174
173
  # @overload stream_raw(max_tokens:, messages:, model:, metadata: nil, service_tier: nil, stop_sequences: nil, system_: nil, temperature: nil, thinking: nil, tool_choice: nil, tools: nil, top_k: nil, top_p: nil, request_options: {})
175
174
  #
@@ -231,7 +230,7 @@ module Anthropic
231
230
  # including tools, images, and documents, without creating it.
232
231
  #
233
232
  # Learn more about token counting in our
234
- # [user guide](/en/docs/build-with-claude/token-counting)
233
+ # [user guide](https://docs.claude.com/en/docs/build-with-claude/token-counting)
235
234
  #
236
235
  # @overload count_tokens(messages:, model:, system_: nil, thinking: nil, tool_choice: nil, tools: nil, request_options: {})
237
236
  #
@@ -270,77 +269,6 @@ module Anthropic
270
269
  @client = client
271
270
  @batches = Anthropic::Resources::Messages::Batches.new(client: client)
272
271
  end
273
-
274
- private
275
-
276
- # Extract tool models from the request and convert them to JSON Schema
277
- # Returns a hash mapping tool name to Ruby model.
278
- def get_structured_output_models(parsed)
279
- tool_models = {}
280
-
281
- case parsed
282
- in {tools: Array => tools}
283
- mapped = tools.map do |tool|
284
- case tool
285
- # Direct tool class:
286
- in Anthropic::Helpers::InputSchema::JsonSchemaConverter
287
- name = tool.name.split("::").last
288
- description = extract_class_description(tool)
289
- tool_models.store(name, tool)
290
- {
291
- name: name,
292
- description: description,
293
- input_schema: tool.to_json_schema
294
- }
295
- # Tool with explicit name/description and BaseModel as input_schema:
296
- in {name: String => name,
297
- input_schema: Anthropic::Helpers::InputSchema::JsonSchemaConverter => model,
298
- **rest}
299
- tool_models.store(name, model)
300
- rest.merge(
301
- name: name,
302
- input_schema: model.to_json_schema
303
- )
304
- else
305
- # Any other format (pass through unchanged)
306
- # This includes raw JSON schemas and any other tool definitions.
307
- tool
308
- end
309
- end
310
- tools.replace(mapped)
311
- else
312
- end
313
-
314
- tool_models
315
- end
316
-
317
- # Extract class description from a BaseModel class
318
- def extract_class_description(klass)
319
- klass.respond_to?(:doc_string) ? klass.doc_string : nil
320
- end
321
-
322
- def parse_structured_outputs!(raw, tool_models)
323
- return raw if tool_models.empty?
324
-
325
- raw[:content]&.each do |content|
326
- next unless content[:type] == "tool_use"
327
-
328
- model = tool_models[content[:name]]
329
- next unless model
330
-
331
- begin
332
- parsed_input = content[:input]
333
-
334
- coerced = Anthropic::Internal::Type::Converter.coerce(model, parsed_input)
335
-
336
- content.store(:parsed, coerced)
337
- rescue StandardError => e
338
- content.store(:parsed, {error: e.message})
339
- end
340
- end
341
-
342
- raw
343
- end
344
272
  end
345
273
  end
346
274
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Anthropic
4
- VERSION = "1.13.0"
4
+ VERSION = "1.15.0"
5
5
  end
data/lib/anthropic.rb CHANGED
@@ -9,6 +9,7 @@ require "erb"
9
9
  require "etc"
10
10
  require "json"
11
11
  require "net/http"
12
+ require "openssl"
12
13
  require "pathname"
13
14
  require "rbconfig"
14
15
  require "securerandom"
@@ -55,6 +56,17 @@ require_relative "anthropic/helpers/bedrock/client"
55
56
  require_relative "anthropic/helpers/vertex/client"
56
57
  require_relative "anthropic/bedrock"
57
58
  require_relative "anthropic/vertex"
59
+ require_relative "anthropic/helpers/input_schema"
60
+ require_relative "anthropic/helpers/input_schema/supported_schemas"
61
+ require_relative "anthropic/helpers/input_schema/json_schema_converter"
62
+ require_relative "anthropic/helpers/input_schema/base_model"
63
+ require_relative "anthropic/helpers/input_schema/array_of"
64
+ require_relative "anthropic/helpers/input_schema/boolean"
65
+ require_relative "anthropic/helpers/input_schema/enum_of"
66
+ require_relative "anthropic/helpers/input_schema/union_of"
67
+ require_relative "anthropic/helpers/input_schema/parsed_json"
68
+ require_relative "anthropic/input_schema"
69
+ require_relative "anthropic/helpers/messages"
58
70
  require_relative "anthropic/internal/stream"
59
71
  require_relative "anthropic/internal/jsonl_stream"
60
72
  require_relative "anthropic/internal/page"
@@ -128,6 +140,7 @@ require_relative "anthropic/models/beta/beta_image_block_param"
128
140
  require_relative "anthropic/models/beta/beta_input_json_delta"
129
141
  require_relative "anthropic/models/beta/beta_input_tokens_clear_at_least"
130
142
  require_relative "anthropic/models/beta/beta_input_tokens_trigger"
143
+ require_relative "anthropic/models/beta/beta_json_output_format"
131
144
  require_relative "anthropic/models/beta/beta_mcp_tool_result_block"
132
145
  require_relative "anthropic/models/beta/beta_mcp_tool_use_block"
133
146
  require_relative "anthropic/models/beta/beta_mcp_tool_use_block_param"
@@ -412,13 +425,6 @@ require_relative "anthropic/resources/completions"
412
425
  require_relative "anthropic/resources/messages"
413
426
  require_relative "anthropic/resources/messages/batches"
414
427
  require_relative "anthropic/resources/models"
415
- require_relative "anthropic/helpers/input_schema"
416
- require_relative "anthropic/helpers/input_schema/property_mapping"
417
- require_relative "anthropic/helpers/input_schema/json_schema_converter"
418
- require_relative "anthropic/helpers/input_schema/base_model"
419
- require_relative "anthropic/helpers/input_schema/array_of"
420
- require_relative "anthropic/helpers/input_schema/boolean"
421
- require_relative "anthropic/helpers/input_schema/enum_of"
422
- require_relative "anthropic/helpers/input_schema/union_of"
423
- require_relative "anthropic/helpers/input_schema/parsed_json"
424
- require_relative "anthropic/input_schema"
428
+ require_relative "anthropic/helpers/tools/runner"
429
+ require_relative "anthropic/helpers/tools/base_tool"
430
+ require_relative "anthropic/helpers/tools"
data/manifest.yaml CHANGED
@@ -6,6 +6,7 @@ dependencies:
6
6
  - etc
7
7
  - json
8
8
  - net/http
9
+ - openssl
9
10
  - pathname
10
11
  - rbconfig
11
12
  - securerandom
@@ -12,8 +12,13 @@ module Anthropic
12
12
  extend Anthropic::Helpers::InputSchema::JsonSchemaConverter
13
13
 
14
14
  class << self
15
- sig { params(doc: T.nilable(String)).void }
16
- attr_writer :doc
15
+ # @api public
16
+ #
17
+ sig { params(description: String).returns(T.class_of(String)) }
18
+ def description(description)
19
+ end
20
+
21
+ alias_method :doc, :description
17
22
  end
18
23
  end
19
24
  end
@@ -0,0 +1,51 @@
1
+ # typed: strong
2
+
3
+ module Anthropic
4
+ module Helpers
5
+ module Tools
6
+ # @api private
7
+ #
8
+ class BaseTool
9
+ include Anthropic::Internal::Type::Converter
10
+ include Anthropic::Helpers::InputSchema::JsonSchemaConverter
11
+
12
+ class << self
13
+ # @api public
14
+ #
15
+ sig { returns(T.class_of(Anthropic::Helpers::InputSchema::BaseModel)) }
16
+ attr_reader :model
17
+
18
+ sig { returns(T.class_of(String)) }
19
+ attr_reader :doc_string
20
+
21
+ # @api public
22
+ #
23
+ sig { params(description: String).returns(T.class_of(String)) }
24
+ def description(description)
25
+ end
26
+
27
+ alias_method :doc, :description
28
+
29
+ # @api public
30
+ #
31
+ sig { params(model: T.class_of(Anthropic::Helpers::InputSchema::BaseModel)).returns(T.class_of(Anthropic::Helpers::InputSchema::BaseModel)) }
32
+ def input_schema(model) = (@model = model)
33
+ end
34
+
35
+ # @api public
36
+ #
37
+ # Override the `#parse` method to customize the pre-processing of the tool call argument
38
+ #
39
+ sig { params(value: T.anything).returns(T.anything) }
40
+ def parse(value)
41
+ end
42
+
43
+ # @api public
44
+ #
45
+ sig { params(parsed: Anthropic::Helpers::InputSchema::BaseModel).returns(T.anything) }
46
+ def call(parsed)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,40 @@
1
+ # typed: strong
2
+
3
+ module Anthropic
4
+ module Helpers
5
+ module Tools
6
+ # @api private
7
+ #
8
+ class Runner
9
+ sig { returns(Anthropic::Models::Beta::MessageCreateParams) }
10
+ attr_accessor :params
11
+
12
+ sig { returns(T::Boolean) }
13
+ def finished?; end
14
+
15
+ sig { params(messages: Anthropic::Models::Beta::BetaMessageParam).void }
16
+ def feed_messages(*messages); end
17
+
18
+ sig { returns(Anthropic::Models::BetaMessage) }
19
+ def next_message; end
20
+
21
+ sig { returns(T::Array[Anthropic::Models::BetaMessage]) }
22
+ def run_until_finished; end
23
+
24
+ sig do
25
+ params(
26
+ blk: T.proc.params(arg0: Anthropic::Models::BetaMessage).void
27
+ ).void
28
+ end
29
+ def each_message(&blk); end
30
+
31
+ sig do
32
+ params(
33
+ blk: T.proc.params(arg0: Anthropic::Streaming::MessageStream).void
34
+ ).void
35
+ end
36
+ def each_streaming(&blk); end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,5 @@
1
+ # typed: strong
2
+
3
+ module Anthropic
4
+ BaseTool = Anthropic::Helpers::Tools::BaseTool
5
+ end
@@ -184,6 +184,11 @@ module Anthropic
184
184
  private def auth_headers
185
185
  end
186
186
 
187
+ # @api private
188
+ sig { returns(String) }
189
+ private def user_agent
190
+ end
191
+
187
192
  # @api private
188
193
  sig { returns(String) }
189
194
  private def generate_idempotency_key
@@ -26,8 +26,12 @@ module Anthropic
26
26
 
27
27
  class << self
28
28
  # @api private
29
- sig { params(url: URI::Generic).returns(Net::HTTP) }
30
- def connect(url)
29
+ sig do
30
+ params(cert_store: OpenSSL::X509::Store, url: URI::Generic).returns(
31
+ Net::HTTP
32
+ )
33
+ end
34
+ def connect(cert_store:, url:)
31
35
  end
32
36
 
33
37
  # @api private