anthropic 1.1.1 → 1.2.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 (131) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +45 -0
  3. data/README.md +32 -16
  4. data/lib/anthropic/errors.rb +22 -0
  5. data/lib/anthropic/helpers/streaming/events.rb +141 -0
  6. data/lib/anthropic/helpers/streaming/message_stream.rb +238 -0
  7. data/lib/anthropic/helpers/streaming.rb +37 -0
  8. data/lib/anthropic/helpers/vertex/client.rb +4 -1
  9. data/lib/anthropic/internal/stream.rb +4 -2
  10. data/lib/anthropic/internal/transport/base_client.rb +10 -2
  11. data/lib/anthropic/internal/type/array_of.rb +6 -1
  12. data/lib/anthropic/internal/type/base_model.rb +77 -23
  13. data/lib/anthropic/internal/type/base_stream.rb +3 -1
  14. data/lib/anthropic/internal/type/boolean.rb +7 -1
  15. data/lib/anthropic/internal/type/converter.rb +42 -34
  16. data/lib/anthropic/internal/type/enum.rb +16 -5
  17. data/lib/anthropic/internal/type/file_input.rb +6 -1
  18. data/lib/anthropic/internal/type/hash_of.rb +6 -1
  19. data/lib/anthropic/internal/type/union.rb +17 -9
  20. data/lib/anthropic/internal/type/unknown.rb +7 -1
  21. data/lib/anthropic/internal/util.rb +8 -9
  22. data/lib/anthropic/models/beta/beta_base64_pdf_block.rb +1 -65
  23. data/lib/anthropic/models/beta/beta_citation_search_result_location.rb +55 -0
  24. data/lib/anthropic/models/beta/beta_citation_search_result_location_param.rb +55 -0
  25. data/lib/anthropic/models/beta/beta_citations_delta.rb +5 -3
  26. data/lib/anthropic/models/beta/beta_content_block.rb +5 -5
  27. data/lib/anthropic/models/beta/beta_content_block_param.rb +20 -17
  28. data/lib/anthropic/models/beta/beta_message.rb +6 -2
  29. data/lib/anthropic/models/beta/beta_message_param.rb +3 -3
  30. data/lib/anthropic/models/beta/beta_raw_content_block_start_event.rb +7 -7
  31. data/lib/anthropic/models/beta/beta_request_document_block.rb +75 -0
  32. data/lib/anthropic/models/beta/beta_search_result_block_param.rb +55 -0
  33. data/lib/anthropic/models/beta/beta_text_block.rb +2 -2
  34. data/lib/anthropic/models/beta/beta_text_block_param.rb +2 -2
  35. data/lib/anthropic/models/beta/beta_text_citation.rb +3 -1
  36. data/lib/anthropic/models/beta/beta_text_citation_param.rb +3 -1
  37. data/lib/anthropic/models/beta/beta_tool.rb +7 -1
  38. data/lib/anthropic/models/beta/beta_tool_result_block_param.rb +6 -4
  39. data/lib/anthropic/models/beta/beta_tool_union.rb +7 -7
  40. data/lib/anthropic/models/beta/message_count_tokens_params.rb +16 -10
  41. data/lib/anthropic/models/beta/message_create_params.rb +9 -3
  42. data/lib/anthropic/models/beta/messages/batch_create_params.rb +9 -3
  43. data/lib/anthropic/models/content_block.rb +5 -5
  44. data/lib/anthropic/models/content_block_param.rb +11 -11
  45. data/lib/anthropic/models/message.rb +6 -2
  46. data/lib/anthropic/models/message_count_tokens_params.rb +9 -3
  47. data/lib/anthropic/models/message_count_tokens_tool.rb +35 -1
  48. data/lib/anthropic/models/message_create_params.rb +9 -3
  49. data/lib/anthropic/models/message_param.rb +3 -3
  50. data/lib/anthropic/models/messages/batch_create_params.rb +9 -3
  51. data/lib/anthropic/models/model.rb +6 -0
  52. data/lib/anthropic/models/raw_content_block_start_event.rb +7 -7
  53. data/lib/anthropic/models/tool.rb +7 -1
  54. data/lib/anthropic/models/tool_union.rb +34 -1
  55. data/lib/anthropic/models/tool_use_block.rb +6 -0
  56. data/lib/anthropic/models.rb +4 -4
  57. data/lib/anthropic/resources/beta/messages.rb +5 -5
  58. data/lib/anthropic/resources/messages.rb +68 -7
  59. data/lib/anthropic/streaming.rb +5 -0
  60. data/lib/anthropic/version.rb +1 -1
  61. data/lib/anthropic.rb +8 -0
  62. data/rbi/anthropic/errors.rbi +16 -0
  63. data/rbi/anthropic/helpers/streaming/events.rbi +85 -0
  64. data/rbi/anthropic/helpers/streaming/message_stream.rbi +59 -0
  65. data/rbi/anthropic/internal/type/base_stream.rbi +8 -1
  66. data/rbi/anthropic/internal/type/boolean.rbi +2 -0
  67. data/rbi/anthropic/internal/type/converter.rbi +15 -15
  68. data/rbi/anthropic/internal/type/union.rbi +5 -0
  69. data/rbi/anthropic/internal/type/unknown.rbi +2 -0
  70. data/rbi/anthropic/internal/util.rbi +2 -0
  71. data/rbi/anthropic/models/beta/beta_base64_pdf_block.rbi +1 -128
  72. data/rbi/anthropic/models/beta/beta_citation_search_result_location.rbi +78 -0
  73. data/rbi/anthropic/models/beta/beta_citation_search_result_location_param.rbi +79 -0
  74. data/rbi/anthropic/models/beta/beta_citations_delta.rbi +4 -2
  75. data/rbi/anthropic/models/beta/beta_content_block.rbi +3 -3
  76. data/rbi/anthropic/models/beta/beta_content_block_param.rbi +8 -7
  77. data/rbi/anthropic/models/beta/beta_message.rbi +11 -3
  78. data/rbi/anthropic/models/beta/beta_raw_content_block_start_event.rbi +6 -6
  79. data/rbi/anthropic/models/beta/beta_request_document_block.rbi +140 -0
  80. data/rbi/anthropic/models/beta/beta_search_result_block_param.rbi +91 -0
  81. data/rbi/anthropic/models/beta/beta_text_block.rbi +2 -1
  82. data/rbi/anthropic/models/beta/beta_text_block_param.rbi +6 -3
  83. data/rbi/anthropic/models/beta/beta_text_citation.rbi +2 -1
  84. data/rbi/anthropic/models/beta/beta_text_citation_param.rbi +2 -1
  85. data/rbi/anthropic/models/beta/beta_tool.rbi +14 -5
  86. data/rbi/anthropic/models/beta/beta_tool_result_block_param.rbi +2 -1
  87. data/rbi/anthropic/models/beta/beta_tool_union.rbi +5 -5
  88. data/rbi/anthropic/models/beta/message_count_tokens_params.rbi +39 -27
  89. data/rbi/anthropic/models/beta/message_create_params.rbi +34 -22
  90. data/rbi/anthropic/models/beta/messages/batch_create_params.rbi +34 -22
  91. data/rbi/anthropic/models/content_block.rbi +3 -3
  92. data/rbi/anthropic/models/content_block_param.rbi +5 -5
  93. data/rbi/anthropic/models/message.rbi +11 -3
  94. data/rbi/anthropic/models/message_count_tokens_params.rbi +18 -2
  95. data/rbi/anthropic/models/message_count_tokens_tool.rbi +61 -0
  96. data/rbi/anthropic/models/message_create_params.rbi +18 -2
  97. data/rbi/anthropic/models/messages/batch_create_params.rbi +18 -2
  98. data/rbi/anthropic/models/raw_content_block_start_event.rbi +6 -6
  99. data/rbi/anthropic/models/tool.rbi +16 -5
  100. data/rbi/anthropic/models/tool_union.rbi +61 -0
  101. data/rbi/anthropic/resources/beta/messages.rbi +36 -18
  102. data/rbi/anthropic/resources/messages.rbi +293 -3
  103. data/rbi/anthropic/streaming.rbi +5 -0
  104. data/sig/anthropic/errors.rbs +9 -0
  105. data/sig/anthropic/helpers/streaming/events.rbs +119 -0
  106. data/sig/anthropic/helpers/streaming/message_stream.rbs +55 -0
  107. data/sig/anthropic/internal/type/base_stream.rbs +4 -0
  108. data/sig/anthropic/internal/type/converter.rbs +7 -1
  109. data/sig/anthropic/models/beta/beta_base64_pdf_block.rbs +1 -58
  110. data/sig/anthropic/models/beta/beta_citation_search_result_location.rbs +54 -0
  111. data/sig/anthropic/models/beta/beta_citation_search_result_location_param.rbs +54 -0
  112. data/sig/anthropic/models/beta/beta_citations_delta.rbs +1 -0
  113. data/sig/anthropic/models/beta/beta_content_block.rbs +2 -2
  114. data/sig/anthropic/models/beta/beta_content_block_param.rbs +9 -8
  115. data/sig/anthropic/models/beta/beta_raw_content_block_start_event.rbs +2 -2
  116. data/sig/anthropic/models/beta/beta_request_document_block.rbs +66 -0
  117. data/sig/anthropic/models/beta/beta_search_result_block_param.rbs +53 -0
  118. data/sig/anthropic/models/beta/beta_text_citation.rbs +1 -0
  119. data/sig/anthropic/models/beta/beta_text_citation_param.rbs +1 -0
  120. data/sig/anthropic/models/beta/beta_tool.rbs +14 -3
  121. data/sig/anthropic/models/beta/beta_tool_result_block_param.rbs +1 -0
  122. data/sig/anthropic/models/beta/beta_tool_union.rbs +4 -4
  123. data/sig/anthropic/models/beta/message_count_tokens_params.rbs +4 -4
  124. data/sig/anthropic/models/content_block.rbs +2 -2
  125. data/sig/anthropic/models/content_block_param.rbs +5 -5
  126. data/sig/anthropic/models/message_count_tokens_tool.rbs +28 -0
  127. data/sig/anthropic/models/raw_content_block_start_event.rbs +2 -2
  128. data/sig/anthropic/models/tool.rbs +14 -3
  129. data/sig/anthropic/models/tool_union.rbs +28 -0
  130. data/sig/anthropic/streaming.rbs +3 -0
  131. metadata +26 -3
@@ -5,7 +5,7 @@ module Anthropic
5
5
  class RawContentBlockStartEvent < Anthropic::Internal::Type::BaseModel
6
6
  # @!attribute content_block
7
7
  #
8
- # @return [Anthropic::Models::TextBlock, Anthropic::Models::ToolUseBlock, Anthropic::Models::ServerToolUseBlock, Anthropic::Models::WebSearchToolResultBlock, Anthropic::Models::ThinkingBlock, Anthropic::Models::RedactedThinkingBlock]
8
+ # @return [Anthropic::Models::TextBlock, Anthropic::Models::ThinkingBlock, Anthropic::Models::RedactedThinkingBlock, Anthropic::Models::ToolUseBlock, Anthropic::Models::ServerToolUseBlock, Anthropic::Models::WebSearchToolResultBlock]
9
9
  required :content_block, union: -> { Anthropic::RawContentBlockStartEvent::ContentBlock }
10
10
 
11
11
  # @!attribute index
@@ -19,7 +19,7 @@ module Anthropic
19
19
  required :type, const: :content_block_start
20
20
 
21
21
  # @!method initialize(content_block:, index:, type: :content_block_start)
22
- # @param content_block [Anthropic::Models::TextBlock, Anthropic::Models::ToolUseBlock, Anthropic::Models::ServerToolUseBlock, Anthropic::Models::WebSearchToolResultBlock, Anthropic::Models::ThinkingBlock, Anthropic::Models::RedactedThinkingBlock]
22
+ # @param content_block [Anthropic::Models::TextBlock, Anthropic::Models::ThinkingBlock, Anthropic::Models::RedactedThinkingBlock, Anthropic::Models::ToolUseBlock, Anthropic::Models::ServerToolUseBlock, Anthropic::Models::WebSearchToolResultBlock]
23
23
  # @param index [Integer]
24
24
  # @param type [Symbol, :content_block_start]
25
25
 
@@ -31,18 +31,18 @@ module Anthropic
31
31
 
32
32
  variant :text, -> { Anthropic::TextBlock }
33
33
 
34
+ variant :thinking, -> { Anthropic::ThinkingBlock }
35
+
36
+ variant :redacted_thinking, -> { Anthropic::RedactedThinkingBlock }
37
+
34
38
  variant :tool_use, -> { Anthropic::ToolUseBlock }
35
39
 
36
40
  variant :server_tool_use, -> { Anthropic::ServerToolUseBlock }
37
41
 
38
42
  variant :web_search_tool_result, -> { Anthropic::WebSearchToolResultBlock }
39
43
 
40
- variant :thinking, -> { Anthropic::ThinkingBlock }
41
-
42
- variant :redacted_thinking, -> { Anthropic::RedactedThinkingBlock }
43
-
44
44
  # @!method self.variants
45
- # @return [Array(Anthropic::Models::TextBlock, Anthropic::Models::ToolUseBlock, Anthropic::Models::ServerToolUseBlock, Anthropic::Models::WebSearchToolResultBlock, Anthropic::Models::ThinkingBlock, Anthropic::Models::RedactedThinkingBlock)]
45
+ # @return [Array(Anthropic::Models::TextBlock, Anthropic::Models::ThinkingBlock, Anthropic::Models::RedactedThinkingBlock, Anthropic::Models::ToolUseBlock, Anthropic::Models::ServerToolUseBlock, Anthropic::Models::WebSearchToolResultBlock)]
46
46
  end
47
47
  end
48
48
  end
@@ -68,13 +68,19 @@ module Anthropic
68
68
  # @return [Object, nil]
69
69
  optional :properties, Anthropic::Internal::Type::Unknown, nil?: true
70
70
 
71
- # @!method initialize(properties: nil, type: :object)
71
+ # @!attribute required
72
+ #
73
+ # @return [Array<String>, nil]
74
+ optional :required, Anthropic::Internal::Type::ArrayOf[String], nil?: true
75
+
76
+ # @!method initialize(properties: nil, required: nil, type: :object)
72
77
  # [JSON schema](https://json-schema.org/draft/2020-12) for this tool's input.
73
78
  #
74
79
  # This defines the shape of the `input` that your tool accepts and that the model
75
80
  # will produce.
76
81
  #
77
82
  # @param properties [Object, nil]
83
+ # @param required [Array<String>, nil]
78
84
  # @param type [Symbol, :object]
79
85
  end
80
86
 
@@ -11,10 +11,43 @@ module Anthropic
11
11
 
12
12
  variant -> { Anthropic::ToolTextEditor20250124 }
13
13
 
14
+ variant -> { Anthropic::ToolUnion::TextEditor20250429 }
15
+
14
16
  variant -> { Anthropic::WebSearchTool20250305 }
15
17
 
18
+ class TextEditor20250429 < Anthropic::Internal::Type::BaseModel
19
+ # @!attribute name
20
+ # Name of the tool.
21
+ #
22
+ # This is how the tool will be called by the model and in `tool_use` blocks.
23
+ #
24
+ # @return [Symbol, :str_replace_based_edit_tool]
25
+ required :name, const: :str_replace_based_edit_tool
26
+
27
+ # @!attribute type
28
+ #
29
+ # @return [Symbol, :text_editor_20250429]
30
+ required :type, const: :text_editor_20250429
31
+
32
+ # @!attribute cache_control
33
+ # Create a cache control breakpoint at this content block.
34
+ #
35
+ # @return [Anthropic::Models::CacheControlEphemeral, nil]
36
+ optional :cache_control, -> { Anthropic::CacheControlEphemeral }, nil?: true
37
+
38
+ # @!method initialize(cache_control: nil, name: :str_replace_based_edit_tool, type: :text_editor_20250429)
39
+ # Some parameter documentations has been truncated, see
40
+ # {Anthropic::Models::ToolUnion::TextEditor20250429} for more details.
41
+ #
42
+ # @param cache_control [Anthropic::Models::CacheControlEphemeral, nil] Create a cache control breakpoint at this content block.
43
+ #
44
+ # @param name [Symbol, :str_replace_based_edit_tool] Name of the tool.
45
+ #
46
+ # @param type [Symbol, :text_editor_20250429]
47
+ end
48
+
16
49
  # @!method self.variants
17
- # @return [Array(Anthropic::Models::Tool, Anthropic::Models::ToolBash20250124, Anthropic::Models::ToolTextEditor20250124, Anthropic::Models::WebSearchTool20250305)]
50
+ # @return [Array(Anthropic::Models::Tool, Anthropic::Models::ToolBash20250124, Anthropic::Models::ToolTextEditor20250124, Anthropic::Models::ToolUnion::TextEditor20250429, Anthropic::Models::WebSearchTool20250305)]
18
51
  end
19
52
  end
20
53
  end
@@ -23,6 +23,12 @@ 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
+
26
32
  # @!method initialize(id:, input:, name:, type: :tool_use)
27
33
  # @param id [String]
28
34
  # @param input [Object]
@@ -11,16 +11,16 @@ module Anthropic
11
11
  mod.constants.each do |name|
12
12
  case mod.const_get(name)
13
13
  in true | false
14
- mod.define_sorbet_constant!(:TaggedBoolean) { T.type_alias { T.all(T::Boolean, mod) } }
14
+ mod.define_sorbet_constant!(:TaggedBoolean) { T.type_alias { T::Boolean } }
15
15
  mod.define_sorbet_constant!(:OrBoolean) { T.type_alias { T::Boolean } }
16
16
  in Integer
17
- mod.define_sorbet_constant!(:TaggedInteger) { T.type_alias { T.all(Integer, mod) } }
17
+ mod.define_sorbet_constant!(:TaggedInteger) { T.type_alias { Integer } }
18
18
  mod.define_sorbet_constant!(:OrInteger) { T.type_alias { Integer } }
19
19
  in Float
20
- mod.define_sorbet_constant!(:TaggedFloat) { T.type_alias { T.all(Float, mod) } }
20
+ mod.define_sorbet_constant!(:TaggedFloat) { T.type_alias { Float } }
21
21
  mod.define_sorbet_constant!(:OrFloat) { T.type_alias { Float } }
22
22
  in Symbol
23
- mod.define_sorbet_constant!(:TaggedSymbol) { T.type_alias { T.all(Symbol, mod) } }
23
+ mod.define_sorbet_constant!(:TaggedSymbol) { T.type_alias { Symbol } }
24
24
  mod.define_sorbet_constant!(:OrSymbol) { T.type_alias { T.any(Symbol, String) } }
25
25
  else
26
26
  end
@@ -46,7 +46,7 @@ module Anthropic
46
46
  #
47
47
  # @param tool_choice [Anthropic::Models::Beta::BetaToolChoiceAuto, Anthropic::Models::Beta::BetaToolChoiceAny, Anthropic::Models::Beta::BetaToolChoiceTool, Anthropic::Models::Beta::BetaToolChoiceNone] Body param: How the model should use the provided tools. The model can use a spe
48
48
  #
49
- # @param tools [Array<Anthropic::Models::Beta::BetaTool, Anthropic::Models::Beta::BetaToolComputerUse20241022, Anthropic::Models::Beta::BetaToolBash20241022, Anthropic::Models::Beta::BetaToolTextEditor20241022, Anthropic::Models::Beta::BetaToolComputerUse20250124, Anthropic::Models::Beta::BetaToolBash20250124, Anthropic::Models::Beta::BetaToolTextEditor20250124, Anthropic::Models::Beta::BetaToolTextEditor20250429, Anthropic::Models::Beta::BetaWebSearchTool20250305, Anthropic::Models::Beta::BetaCodeExecutionTool20250522>] Body param: Definitions of tools that the model may use.
49
+ # @param tools [Array<Anthropic::Models::Beta::BetaTool, Anthropic::Models::Beta::BetaToolBash20241022, Anthropic::Models::Beta::BetaToolBash20250124, Anthropic::Models::Beta::BetaCodeExecutionTool20250522, Anthropic::Models::Beta::BetaToolComputerUse20241022, Anthropic::Models::Beta::BetaToolComputerUse20250124, Anthropic::Models::Beta::BetaToolTextEditor20241022, Anthropic::Models::Beta::BetaToolTextEditor20250124, Anthropic::Models::Beta::BetaToolTextEditor20250429, Anthropic::Models::Beta::BetaWebSearchTool20250305>] Body param: Definitions of tools that the model may use.
50
50
  #
51
51
  # @param top_k [Integer] Body param: Only sample from the top K options for each subsequent token.
52
52
  #
@@ -62,7 +62,7 @@ module Anthropic
62
62
  def create(params)
63
63
  parsed, options = Anthropic::Beta::MessageCreateParams.dump_request(params)
64
64
  if parsed[:stream]
65
- message = "Please use `#stream_raw` for the streaming use case."
65
+ message = "Please use `#stream` for the streaming use case."
66
66
  raise ArgumentError.new(message)
67
67
  end
68
68
 
@@ -90,7 +90,7 @@ module Anthropic
90
90
  end
91
91
 
92
92
  def stream
93
- raise NotImplementedError.new("higher level helpers are coming soon!")
93
+ Anthropic::Streaming::MessageStream.new(raw_stream: raw_stream)
94
94
  end
95
95
 
96
96
  # See {Anthropic::Resources::Beta::Messages#create} for non-streaming counterpart.
@@ -132,7 +132,7 @@ module Anthropic
132
132
  #
133
133
  # @param tool_choice [Anthropic::Models::Beta::BetaToolChoiceAuto, Anthropic::Models::Beta::BetaToolChoiceAny, Anthropic::Models::Beta::BetaToolChoiceTool, Anthropic::Models::Beta::BetaToolChoiceNone] Body param: How the model should use the provided tools. The model can use a spe
134
134
  #
135
- # @param tools [Array<Anthropic::Models::Beta::BetaTool, Anthropic::Models::Beta::BetaToolComputerUse20241022, Anthropic::Models::Beta::BetaToolBash20241022, Anthropic::Models::Beta::BetaToolTextEditor20241022, Anthropic::Models::Beta::BetaToolComputerUse20250124, Anthropic::Models::Beta::BetaToolBash20250124, Anthropic::Models::Beta::BetaToolTextEditor20250124, Anthropic::Models::Beta::BetaToolTextEditor20250429, Anthropic::Models::Beta::BetaWebSearchTool20250305, Anthropic::Models::Beta::BetaCodeExecutionTool20250522>] Body param: Definitions of tools that the model may use.
135
+ # @param tools [Array<Anthropic::Models::Beta::BetaTool, Anthropic::Models::Beta::BetaToolBash20241022, Anthropic::Models::Beta::BetaToolBash20250124, Anthropic::Models::Beta::BetaCodeExecutionTool20250522, Anthropic::Models::Beta::BetaToolComputerUse20241022, Anthropic::Models::Beta::BetaToolComputerUse20250124, Anthropic::Models::Beta::BetaToolTextEditor20241022, Anthropic::Models::Beta::BetaToolTextEditor20250124, Anthropic::Models::Beta::BetaToolTextEditor20250429, Anthropic::Models::Beta::BetaWebSearchTool20250305>] Body param: Definitions of tools that the model may use.
136
136
  #
137
137
  # @param top_k [Integer] Body param: Only sample from the top K options for each subsequent token.
138
138
  #
@@ -192,7 +192,7 @@ module Anthropic
192
192
  #
193
193
  # @param tool_choice [Anthropic::Models::Beta::BetaToolChoiceAuto, Anthropic::Models::Beta::BetaToolChoiceAny, Anthropic::Models::Beta::BetaToolChoiceTool, Anthropic::Models::Beta::BetaToolChoiceNone] Body param: How the model should use the provided tools. The model can use a spe
194
194
  #
195
- # @param tools [Array<Anthropic::Models::Beta::BetaTool, Anthropic::Models::Beta::BetaToolComputerUse20241022, Anthropic::Models::Beta::BetaToolBash20241022, Anthropic::Models::Beta::BetaToolTextEditor20241022, Anthropic::Models::Beta::BetaToolComputerUse20250124, Anthropic::Models::Beta::BetaToolBash20250124, Anthropic::Models::Beta::BetaToolTextEditor20250124, Anthropic::Models::Beta::BetaToolTextEditor20250429, Anthropic::Models::Beta::BetaWebSearchTool20250305, Anthropic::Models::Beta::BetaCodeExecutionTool20250522>] Body param: Definitions of tools that the model may use.
195
+ # @param tools [Array<Anthropic::Models::Beta::BetaTool, Anthropic::Models::Beta::BetaToolBash20241022, Anthropic::Models::Beta::BetaToolBash20250124, Anthropic::Models::Beta::BetaCodeExecutionTool20250522, Anthropic::Models::Beta::BetaToolComputerUse20241022, Anthropic::Models::Beta::BetaToolComputerUse20250124, Anthropic::Models::Beta::BetaToolTextEditor20241022, Anthropic::Models::Beta::BetaToolTextEditor20250124, Anthropic::Models::Beta::BetaToolTextEditor20250429, Anthropic::Models::Beta::BetaWebSearchTool20250305>] Body param: Definitions of tools that the model may use.
196
196
  #
197
197
  # @param betas [Array<String, Symbol, Anthropic::Models::AnthropicBeta>] Header param: Optional header to specify the beta version(s) you want to use.
198
198
  #
@@ -6,7 +6,7 @@ module Anthropic
6
6
  # @return [Anthropic::Resources::Messages::Batches]
7
7
  attr_reader :batches
8
8
 
9
- # See {Anthropic::Resources::Messages#stream_raw} for streaming counterpart.
9
+ # See {Anthropic::Resources::Messages#stream} for streaming counterpart.
10
10
  #
11
11
  # Some parameter documentations has been truncated, see
12
12
  # {Anthropic::Models::MessageCreateParams} for more details.
@@ -41,7 +41,7 @@ module Anthropic
41
41
  #
42
42
  # @param tool_choice [Anthropic::Models::ToolChoiceAuto, Anthropic::Models::ToolChoiceAny, Anthropic::Models::ToolChoiceTool, Anthropic::Models::ToolChoiceNone] How the model should use the provided tools. The model can use a specific tool,
43
43
  #
44
- # @param tools [Array<Anthropic::Models::Tool, Anthropic::Models::ToolBash20250124, Anthropic::Models::ToolTextEditor20250124, Anthropic::Models::WebSearchTool20250305>] Definitions of tools that the model may use.
44
+ # @param tools [Array<Anthropic::Models::Tool, Anthropic::Models::ToolBash20250124, Anthropic::Models::ToolTextEditor20250124, Anthropic::Models::ToolUnion::TextEditor20250429, Anthropic::Models::WebSearchTool20250305>] Definitions of tools that the model may use.
45
45
  #
46
46
  # @param top_k [Integer] Only sample from the top K options for each subsequent token.
47
47
  #
@@ -55,7 +55,7 @@ module Anthropic
55
55
  def create(params)
56
56
  parsed, options = Anthropic::MessageCreateParams.dump_request(params)
57
57
  if parsed[:stream]
58
- message = "Please use `#stream_raw` for the streaming use case."
58
+ message = "Please use `#stream` for the streaming use case."
59
59
  raise ArgumentError.new(message)
60
60
  end
61
61
 
@@ -80,8 +80,69 @@ module Anthropic
80
80
  )
81
81
  end
82
82
 
83
- def stream
84
- raise NotImplementedError.new("higher level helpers are coming soon!")
83
+ # See {Anthropic::Resources::Messages#create} for non-streaming counterpart.
84
+ #
85
+ # Some parameter documentations has been truncated, see
86
+ # {Anthropic::Models::MessageCreateParams} for more details.
87
+ #
88
+ # Send a structured list of input messages with text and/or image content, and the
89
+ # model will generate the next message in the conversation with streaming.
90
+ #
91
+ # The Messages API can be used for either single queries or stateless multi-turn
92
+ # conversations.
93
+ #
94
+ # Learn more about the Messages API in our [user guide](/en/docs/initial-setup)
95
+ #
96
+ # @overload stream(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: {})
97
+ #
98
+ # @param max_tokens [Integer] The maximum number of tokens to generate before stopping.
99
+ #
100
+ # @param messages [Array<Anthropic::Models::MessageParam>] Input messages.
101
+ #
102
+ # @param model [Symbol, String, Anthropic::Models::Model] The model that will complete your prompt.\n\nSee [models](https://docs.anthropic
103
+ #
104
+ # @param metadata [Anthropic::Models::Metadata] An object describing metadata about the request.
105
+ #
106
+ # @param service_tier [Symbol, Anthropic::Models::MessageCreateParams::ServiceTier] Determines whether to use priority capacity (if available) or standard capacity
107
+ #
108
+ # @param stop_sequences [Array<String>] Custom text sequences that will cause the model to stop generating.
109
+ #
110
+ # @param system_ [String, Array<Anthropic::Models::TextBlockParam>] System prompt.
111
+ #
112
+ # @param temperature [Float] Amount of randomness injected into the response.
113
+ #
114
+ # @param thinking [Anthropic::Models::ThinkingConfigEnabled, Anthropic::Models::ThinkingConfigDisabled] Configuration for enabling Claude's extended thinking.
115
+ #
116
+ # @param tool_choice [Anthropic::Models::ToolChoiceAuto, Anthropic::Models::ToolChoiceAny, Anthropic::Models::ToolChoiceTool, Anthropic::Models::ToolChoiceNone] How the model should use the provided tools. The model can use a specific tool,
117
+ #
118
+ # @param tools [Array<Anthropic::Models::Tool, Anthropic::Models::ToolBash20250124, Anthropic::Models::ToolTextEditor20250124, Anthropic::Models::WebSearchTool20250305>] Definitions of tools that the model may use.
119
+ #
120
+ # @param top_k [Integer] Only sample from the top K options for each subsequent token.
121
+ #
122
+ # @param top_p [Float] Use nucleus sampling.
123
+ #
124
+ # @param request_options [Anthropic::RequestOptions, Hash{Symbol=>Object}, nil]
125
+ #
126
+ # @return [Anthropic::Streaming::MessageStream]
127
+ #
128
+ # @see Anthropic::Models::MessageCreateParams
129
+ def stream(params)
130
+ parsed, options = Anthropic::Models::MessageCreateParams.dump_request(params)
131
+ unless parsed.fetch(:stream, true)
132
+ message = "Please use `#create` for the non-streaming use case."
133
+ raise ArgumentError.new(message)
134
+ end
135
+ parsed.store(:stream, true)
136
+ raw_stream = @client.request(
137
+ method: :post,
138
+ path: "v1/messages",
139
+ headers: {"accept" => "text/event-stream"},
140
+ body: parsed,
141
+ stream: Anthropic::Internal::Stream,
142
+ model: Anthropic::Models::RawMessageStreamEvent,
143
+ options: options
144
+ )
145
+ Anthropic::Streaming::MessageStream.new(raw_stream: raw_stream)
85
146
  end
86
147
 
87
148
  # See {Anthropic::Resources::Messages#create} for non-streaming counterpart.
@@ -119,7 +180,7 @@ module Anthropic
119
180
  #
120
181
  # @param tool_choice [Anthropic::Models::ToolChoiceAuto, Anthropic::Models::ToolChoiceAny, Anthropic::Models::ToolChoiceTool, Anthropic::Models::ToolChoiceNone] How the model should use the provided tools. The model can use a specific tool,
121
182
  #
122
- # @param tools [Array<Anthropic::Models::Tool, Anthropic::Models::ToolBash20250124, Anthropic::Models::ToolTextEditor20250124, Anthropic::Models::WebSearchTool20250305>] Definitions of tools that the model may use.
183
+ # @param tools [Array<Anthropic::Models::Tool, Anthropic::Models::ToolBash20250124, Anthropic::Models::ToolTextEditor20250124, Anthropic::Models::ToolUnion::TextEditor20250429, Anthropic::Models::WebSearchTool20250305>] Definitions of tools that the model may use.
123
184
  #
124
185
  # @param top_k [Integer] Only sample from the top K options for each subsequent token.
125
186
  #
@@ -171,7 +232,7 @@ module Anthropic
171
232
  #
172
233
  # @param tool_choice [Anthropic::Models::ToolChoiceAuto, Anthropic::Models::ToolChoiceAny, Anthropic::Models::ToolChoiceTool, Anthropic::Models::ToolChoiceNone] How the model should use the provided tools. The model can use a specific tool,
173
234
  #
174
- # @param tools [Array<Anthropic::Models::Tool, Anthropic::Models::ToolBash20250124, Anthropic::Models::ToolTextEditor20250124, Anthropic::Models::WebSearchTool20250305>] Definitions of tools that the model may use.
235
+ # @param tools [Array<Anthropic::Models::Tool, Anthropic::Models::ToolBash20250124, Anthropic::Models::ToolTextEditor20250124, Anthropic::Models::MessageCountTokensTool::TextEditor20250429, Anthropic::Models::WebSearchTool20250305>] Definitions of tools that the model may use.
175
236
  #
176
237
  # @param request_options [Anthropic::RequestOptions, Hash{Symbol=>Object}, nil]
177
238
  #
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Anthropic
4
+ Streaming = Anthropic::Helpers::Streaming
5
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Anthropic
4
- VERSION = "1.1.1"
4
+ VERSION = "1.2.0"
5
5
  end
data/lib/anthropic.rb CHANGED
@@ -58,6 +58,7 @@ require_relative "anthropic/vertex"
58
58
  require_relative "anthropic/internal/stream"
59
59
  require_relative "anthropic/internal/jsonl_stream"
60
60
  require_relative "anthropic/internal/page"
61
+ require_relative "anthropic/models/beta/beta_request_document_block"
61
62
  require_relative "anthropic/models/anthropic_beta"
62
63
  require_relative "anthropic/models/api_error_object"
63
64
  require_relative "anthropic/models/authentication_error"
@@ -76,6 +77,8 @@ require_relative "anthropic/models/beta/beta_citation_page_location"
76
77
  require_relative "anthropic/models/beta/beta_citation_page_location_param"
77
78
  require_relative "anthropic/models/beta/beta_citations_config_param"
78
79
  require_relative "anthropic/models/beta/beta_citations_delta"
80
+ require_relative "anthropic/models/beta/beta_citation_search_result_location"
81
+ require_relative "anthropic/models/beta/beta_citation_search_result_location_param"
79
82
  require_relative "anthropic/models/beta/beta_citations_web_search_result_location"
80
83
  require_relative "anthropic/models/beta/beta_citation_web_search_result_location_param"
81
84
  require_relative "anthropic/models/beta/beta_code_execution_output_block"
@@ -124,6 +127,7 @@ require_relative "anthropic/models/beta/beta_redacted_thinking_block_param"
124
127
  require_relative "anthropic/models/beta/beta_request_mcp_server_tool_configuration"
125
128
  require_relative "anthropic/models/beta/beta_request_mcp_server_url_definition"
126
129
  require_relative "anthropic/models/beta/beta_request_mcp_tool_result_block_param"
130
+ require_relative "anthropic/models/beta/beta_search_result_block_param"
127
131
  require_relative "anthropic/models/beta/beta_server_tool_usage"
128
132
  require_relative "anthropic/models/beta/beta_server_tool_use_block"
129
133
  require_relative "anthropic/models/beta/beta_server_tool_use_block_param"
@@ -314,6 +318,10 @@ require_relative "anthropic/models/web_search_tool_result_block_content"
314
318
  require_relative "anthropic/models/web_search_tool_result_block_param"
315
319
  require_relative "anthropic/models/web_search_tool_result_block_param_content"
316
320
  require_relative "anthropic/models/web_search_tool_result_error"
321
+ require_relative "anthropic/helpers/streaming/events"
322
+ require_relative "anthropic/helpers/streaming/message_stream"
323
+ require_relative "anthropic/helpers/streaming"
324
+ require_relative "anthropic/streaming"
317
325
  require_relative "anthropic/models"
318
326
  require_relative "anthropic/resources/beta"
319
327
  require_relative "anthropic/resources/beta/files"
@@ -8,6 +8,22 @@ module Anthropic
8
8
  end
9
9
 
10
10
  class ConversionError < Anthropic::Errors::Error
11
+ sig { returns(T.nilable(StandardError)) }
12
+ def cause
13
+ end
14
+
15
+ # @api private
16
+ sig do
17
+ params(
18
+ on: T::Class[StandardError],
19
+ method: Symbol,
20
+ target: T.anything,
21
+ value: T.anything,
22
+ cause: T.nilable(StandardError)
23
+ ).returns(T.attached_class)
24
+ end
25
+ def self.new(on:, method:, target:, value:, cause: nil)
26
+ end
11
27
  end
12
28
 
13
29
  class APIError < Anthropic::Errors::Error
@@ -0,0 +1,85 @@
1
+ # typed: strong
2
+ # frozen_string_literal: true
3
+ # typed: true
4
+
5
+ module Anthropic
6
+ module Helpers
7
+ module Streaming
8
+ class TextEvent < Anthropic::Internal::Type::BaseModel
9
+ sig { returns(Symbol) }
10
+ attr_accessor :type
11
+
12
+ sig { returns(String) }
13
+ attr_accessor :text
14
+
15
+ sig { returns(String) }
16
+ attr_accessor :snapshot
17
+ end
18
+
19
+ class CitationEvent < Anthropic::Internal::Type::BaseModel
20
+ sig { returns(Symbol) }
21
+ attr_accessor :type
22
+
23
+ sig { returns(Anthropic::CitationsDelta::Citation::Variants) }
24
+ attr_accessor :citation
25
+
26
+ sig { returns(T::Array[Anthropic::CitationsDelta::Citation::Variants]) }
27
+ attr_accessor :snapshot
28
+ end
29
+
30
+ class ThinkingEvent < Anthropic::Internal::Type::BaseModel
31
+ sig { returns(Symbol) }
32
+ attr_accessor :type
33
+
34
+ sig { returns(String) }
35
+ attr_accessor :thinking
36
+
37
+ sig { returns(String) }
38
+ attr_accessor :snapshot
39
+ end
40
+
41
+ class SignatureEvent < Anthropic::Internal::Type::BaseModel
42
+ sig { returns(Symbol) }
43
+ attr_accessor :type
44
+
45
+ sig { returns(String) }
46
+ attr_accessor :signature
47
+ end
48
+
49
+ class InputJsonEvent < Anthropic::Internal::Type::BaseModel
50
+ sig { returns(Symbol) }
51
+ attr_accessor :type
52
+
53
+ sig { returns(String) }
54
+ attr_accessor :partial_json
55
+
56
+ sig { returns(T.untyped) }
57
+ attr_accessor :snapshot
58
+ end
59
+
60
+ class MessageStopEvent < Anthropic::Models::RawMessageStopEvent
61
+ sig { returns(Anthropic::Models::Message) }
62
+ attr_accessor :message
63
+
64
+ sig { params(message: Anthropic::Models::Message, type: Symbol).returns(T.attached_class) }
65
+ def self.new(message:, type: :message_stop)
66
+ end
67
+ end
68
+
69
+ class ContentBlockStopEvent < Anthropic::Models::RawContentBlockStopEvent
70
+ sig { returns(Integer) }
71
+ attr_accessor :index
72
+
73
+ sig { returns(Symbol) }
74
+ attr_accessor :type
75
+
76
+ sig { returns(Anthropic::Models::ContentBlock) }
77
+ attr_accessor :content_block
78
+
79
+ sig { params(index: Integer, content_block: Anthropic::Models::ContentBlock, type: Symbol).returns(T.attached_class) }
80
+ def self.new(index:, content_block:, type: :content_block_stop)
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,59 @@
1
+ # typed: strong
2
+
3
+ module Anthropic
4
+ module Helpers
5
+ module Streaming
6
+ RawMessageEvent = T.type_alias do
7
+ T.any(
8
+ Anthropic::Models::RawMessageStartEvent,
9
+ Anthropic::Models::RawMessageDeltaEvent,
10
+ Anthropic::Models::RawMessageStopEvent,
11
+ Anthropic::Models::RawContentBlockStartEvent,
12
+ Anthropic::Models::RawContentBlockDeltaEvent,
13
+ Anthropic::Models::RawContentBlockStopEvent
14
+ )
15
+ end
16
+
17
+ StreamEvent = T.type_alias do
18
+ T.any(
19
+ Anthropic::Streaming::RawMessageEvent,
20
+ Anthropic::Streaming::TextEvent,
21
+ Anthropic::Streaming::CitationEvent,
22
+ Anthropic::Streaming::ThinkingEvent,
23
+ Anthropic::Streaming::SignatureEvent,
24
+ Anthropic::Streaming::InputJsonEvent,
25
+ Anthropic::Streaming::MessageStopEvent,
26
+ Anthropic::Streaming::ContentBlockStopEvent
27
+ )
28
+ end
29
+
30
+ class MessageStream
31
+ include Anthropic::Internal::Type::BaseStream
32
+
33
+ Message =
34
+ type_member(:in) do
35
+ {fixed: Anthropic::Streaming::RawMessageEvent}
36
+ end
37
+ Elem = type_member(:out) { {fixed: Anthropic::Streaming::StreamEvent} }
38
+
39
+ sig { params(raw_stream: Anthropic::Internal::Stream[RawMessageEvent]).void }
40
+ def initialize(raw_stream:); end
41
+
42
+ sig { void }
43
+ def until_done; end
44
+
45
+ sig { returns(T::Enumerator[String]) }
46
+ def text(); end
47
+
48
+ sig { returns(Anthropic::Models::Message) }
49
+ def accumulated_message; end
50
+
51
+ sig { returns(String) }
52
+ def accumulated_text; end
53
+
54
+ sig { params(event: Anthropic::Streaming::RawMessageEvent, current_snapshot: T.nilable(Anthropic::Models::Message)).returns(Anthropic::Models::Message) }
55
+ private def accumulate_event(event:, current_snapshot:); end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -52,10 +52,17 @@ module Anthropic
52
52
  url: URI::Generic,
53
53
  status: Integer,
54
54
  response: Net::HTTPResponse,
55
+ unwrap:
56
+ T.any(
57
+ Symbol,
58
+ Integer,
59
+ T::Array[T.any(Symbol, Integer)],
60
+ T.proc.params(arg0: T.anything).returns(T.anything)
61
+ ),
55
62
  stream: T::Enumerable[Message]
56
63
  ).void
57
64
  end
58
- def initialize(model:, url:, status:, response:, stream:)
65
+ def initialize(model:, url:, status:, response:, unwrap:, stream:)
59
66
  end
60
67
 
61
68
  # @api private
@@ -22,6 +22,8 @@ module Anthropic
22
22
 
23
23
  class << self
24
24
  # @api private
25
+ #
26
+ # Coerce value to Boolean if possible, otherwise return the original value.
25
27
  sig do
26
28
  override
27
29
  .params(
@@ -15,12 +15,14 @@ module Anthropic
15
15
  CoerceState =
16
16
  T.type_alias do
17
17
  {
18
- strictness: T.any(T::Boolean, Symbol),
18
+ translate_names: T::Boolean,
19
+ strictness: T::Boolean,
19
20
  exactness: {
20
21
  yes: Integer,
21
22
  no: Integer,
22
23
  maybe: Integer
23
24
  },
25
+ error: T::Class[StandardError],
24
26
  branched: Integer
25
27
  }
26
28
  end
@@ -88,6 +90,15 @@ module Anthropic
88
90
  def self.type_info(spec)
89
91
  end
90
92
 
93
+ # @api private
94
+ sig do
95
+ params(translate_names: T::Boolean).returns(
96
+ Anthropic::Internal::Type::Converter::CoerceState
97
+ )
98
+ end
99
+ def self.new_coerce_state(translate_names: true)
100
+ end
101
+
91
102
  # @api private
92
103
  #
93
104
  # Based on `target`, transform `value` into `target`, to the extent possible:
@@ -109,14 +120,11 @@ module Anthropic
109
120
  def self.coerce(
110
121
  target,
111
122
  value,
112
- # The `strictness` is one of `true`, `false`, or `:strong`. This informs the
113
- # coercion strategy when we have to decide between multiple possible conversion
114
- # targets:
123
+ # The `strictness` is one of `true`, `false`. This informs the coercion strategy
124
+ # when we have to decide between multiple possible conversion targets:
115
125
  #
116
126
  # - `true`: the conversion must be exact, with minimum coercion.
117
127
  # - `false`: the conversion can be approximate, with some coercion.
118
- # - `:strong`: the conversion must be exact, with no coercion, and raise an error
119
- # if not possible.
120
128
  #
121
129
  # The `exactness` is `Hash` with keys being one of `yes`, `no`, or `maybe`. For
122
130
  # any given conversion attempt, the exactness will be updated based on how closely
@@ -128,15 +136,7 @@ module Anthropic
128
136
  # - `no`: the value cannot be converted to the target type.
129
137
  #
130
138
  # See implementation below for more details.
131
- state: {
132
- strictness: true,
133
- exactness: {
134
- yes: 0,
135
- no: 0,
136
- maybe: 0
137
- },
138
- branched: 0
139
- }
139
+ state: Anthropic::Internal::Type::Converter.new_coerce_state
140
140
  )
141
141
  end
142
142
 
@@ -78,6 +78,11 @@ module Anthropic
78
78
  end
79
79
 
80
80
  # @api private
81
+ #
82
+ # Tries to efficiently coerce the given value to one of the known variants.
83
+ #
84
+ # If the value cannot match any of the known variants, the coercion is considered
85
+ # non-viable and returns the original value.
81
86
  sig do
82
87
  override
83
88
  .params(
@@ -22,6 +22,8 @@ module Anthropic
22
22
 
23
23
  class << self
24
24
  # @api private
25
+ #
26
+ # No coercion needed for Unknown type.
25
27
  sig do
26
28
  override
27
29
  .params(
@@ -332,6 +332,8 @@ module Anthropic
332
332
  end
333
333
 
334
334
  # @api private
335
+ #
336
+ # https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.1.md#special-considerations-for-multipart-content
335
337
  sig do
336
338
  params(body: T.anything).returns([String, T::Enumerable[String]])
337
339
  end