anthropic 1.1.1 → 1.3.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 (135) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +69 -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 +5 -21
  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 +73 -6
  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/bedrock/client.rbi +17 -6
  64. data/rbi/anthropic/helpers/streaming/events.rbi +95 -0
  65. data/rbi/anthropic/helpers/streaming/message_stream.rbi +73 -0
  66. data/rbi/anthropic/helpers/vertex/client.rbi +17 -6
  67. data/rbi/anthropic/internal/type/base_stream.rbi +8 -1
  68. data/rbi/anthropic/internal/type/boolean.rbi +2 -0
  69. data/rbi/anthropic/internal/type/converter.rbi +15 -15
  70. data/rbi/anthropic/internal/type/union.rbi +5 -0
  71. data/rbi/anthropic/internal/type/unknown.rbi +2 -0
  72. data/rbi/anthropic/internal/util.rbi +2 -0
  73. data/rbi/anthropic/models/beta/beta_base64_pdf_block.rbi +1 -128
  74. data/rbi/anthropic/models/beta/beta_citation_search_result_location.rbi +78 -0
  75. data/rbi/anthropic/models/beta/beta_citation_search_result_location_param.rbi +79 -0
  76. data/rbi/anthropic/models/beta/beta_citations_delta.rbi +4 -2
  77. data/rbi/anthropic/models/beta/beta_content_block.rbi +3 -3
  78. data/rbi/anthropic/models/beta/beta_content_block_param.rbi +8 -7
  79. data/rbi/anthropic/models/beta/beta_message.rbi +11 -3
  80. data/rbi/anthropic/models/beta/beta_raw_content_block_start_event.rbi +6 -6
  81. data/rbi/anthropic/models/beta/beta_request_document_block.rbi +140 -0
  82. data/rbi/anthropic/models/beta/beta_search_result_block_param.rbi +91 -0
  83. data/rbi/anthropic/models/beta/beta_text_block.rbi +2 -1
  84. data/rbi/anthropic/models/beta/beta_text_block_param.rbi +6 -3
  85. data/rbi/anthropic/models/beta/beta_text_citation.rbi +2 -1
  86. data/rbi/anthropic/models/beta/beta_text_citation_param.rbi +2 -1
  87. data/rbi/anthropic/models/beta/beta_tool.rbi +14 -5
  88. data/rbi/anthropic/models/beta/beta_tool_result_block_param.rbi +2 -1
  89. data/rbi/anthropic/models/beta/beta_tool_union.rbi +5 -5
  90. data/rbi/anthropic/models/beta/message_count_tokens_params.rbi +39 -27
  91. data/rbi/anthropic/models/beta/message_create_params.rbi +34 -22
  92. data/rbi/anthropic/models/beta/messages/batch_create_params.rbi +34 -22
  93. data/rbi/anthropic/models/content_block.rbi +3 -3
  94. data/rbi/anthropic/models/content_block_param.rbi +5 -5
  95. data/rbi/anthropic/models/message.rbi +11 -3
  96. data/rbi/anthropic/models/message_count_tokens_params.rbi +18 -2
  97. data/rbi/anthropic/models/message_count_tokens_tool.rbi +61 -0
  98. data/rbi/anthropic/models/message_create_params.rbi +18 -2
  99. data/rbi/anthropic/models/messages/batch_create_params.rbi +18 -2
  100. data/rbi/anthropic/models/model.rbi +0 -8
  101. data/rbi/anthropic/models/raw_content_block_start_event.rbi +6 -6
  102. data/rbi/anthropic/models/tool.rbi +16 -5
  103. data/rbi/anthropic/models/tool_union.rbi +61 -0
  104. data/rbi/anthropic/resources/beta/messages.rbi +36 -18
  105. data/rbi/anthropic/resources/messages.rbi +315 -3
  106. data/rbi/anthropic/streaming.rbi +5 -0
  107. data/sig/anthropic/errors.rbs +9 -0
  108. data/sig/anthropic/helpers/streaming/events.rbs +117 -0
  109. data/sig/anthropic/helpers/streaming/message_stream.rbs +57 -0
  110. data/sig/anthropic/internal/type/base_stream.rbs +4 -0
  111. data/sig/anthropic/internal/type/converter.rbs +7 -1
  112. data/sig/anthropic/models/beta/beta_base64_pdf_block.rbs +1 -58
  113. data/sig/anthropic/models/beta/beta_citation_search_result_location.rbs +54 -0
  114. data/sig/anthropic/models/beta/beta_citation_search_result_location_param.rbs +54 -0
  115. data/sig/anthropic/models/beta/beta_citations_delta.rbs +1 -0
  116. data/sig/anthropic/models/beta/beta_content_block.rbs +2 -2
  117. data/sig/anthropic/models/beta/beta_content_block_param.rbs +9 -8
  118. data/sig/anthropic/models/beta/beta_raw_content_block_start_event.rbs +2 -2
  119. data/sig/anthropic/models/beta/beta_request_document_block.rbs +66 -0
  120. data/sig/anthropic/models/beta/beta_search_result_block_param.rbs +53 -0
  121. data/sig/anthropic/models/beta/beta_text_citation.rbs +1 -0
  122. data/sig/anthropic/models/beta/beta_text_citation_param.rbs +1 -0
  123. data/sig/anthropic/models/beta/beta_tool.rbs +14 -3
  124. data/sig/anthropic/models/beta/beta_tool_result_block_param.rbs +1 -0
  125. data/sig/anthropic/models/beta/beta_tool_union.rbs +4 -4
  126. data/sig/anthropic/models/beta/message_count_tokens_params.rbs +4 -4
  127. data/sig/anthropic/models/content_block.rbs +2 -2
  128. data/sig/anthropic/models/content_block_param.rbs +5 -5
  129. data/sig/anthropic/models/message_count_tokens_tool.rbs +28 -0
  130. data/sig/anthropic/models/model.rbs +0 -10
  131. data/sig/anthropic/models/raw_content_block_start_event.rbs +2 -2
  132. data/sig/anthropic/models/tool.rbs +14 -3
  133. data/sig/anthropic/models/tool_union.rbs +28 -0
  134. data/sig/anthropic/streaming.rbs +3 -0
  135. 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
 
@@ -89,8 +89,75 @@ module Anthropic
89
89
  )
90
90
  end
91
91
 
92
- def stream
93
- raise NotImplementedError.new("higher level helpers are coming soon!")
92
+ # See {Anthropic::Resources::Beta::Messages#create} for non-streaming counterpart.
93
+ #
94
+ # Some parameter documentations has been truncated, see
95
+ # {Anthropic::Models::Beta::MessageCreateParams} for more details.
96
+ #
97
+ # Send a structured list of input messages with text and/or image content, and the
98
+ # model will generate the next message in the conversation.
99
+ #
100
+ # The Messages API can be used for either single queries or stateless multi-turn
101
+ # conversations.
102
+ #
103
+ # Learn more about the Messages API in our [user guide](/en/docs/initial-setup)
104
+ #
105
+ # @overload stream_raw(max_tokens:, messages:, model:, container: 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: {})
106
+ #
107
+ # @param max_tokens [Integer] Body param: The maximum number of tokens to generate before stopping.
108
+ #
109
+ # @param messages [Array<Anthropic::Models::Beta::BetaMessageParam>] Body param: Input messages.
110
+ #
111
+ # @param model [Symbol, String, Anthropic::Models::Model] Body param: The model that will complete your prompt.\n\nSee [models](https://do
112
+ #
113
+ # @param container [String, nil] Body param: Container identifier for reuse across requests.
114
+ #
115
+ # @param mcp_servers [Array<Anthropic::Models::Beta::BetaRequestMCPServerURLDefinition>] Body param: MCP servers to be utilized in this request
116
+ #
117
+ # @param metadata [Anthropic::Models::Beta::BetaMetadata] Body param: An object describing metadata about the request.
118
+ #
119
+ # @param service_tier [Symbol, Anthropic::Models::Beta::MessageCreateParams::ServiceTier] Body param: Determines whether to use priority capacity (if available) or standa
120
+ #
121
+ # @param stop_sequences [Array<String>] Body param: Custom text sequences that will cause the model to stop generating.
122
+ #
123
+ # @param system_ [String, Array<Anthropic::Models::Beta::BetaTextBlockParam>] Body param: System prompt.
124
+ #
125
+ # @param temperature [Float] Body param: Amount of randomness injected into the response.
126
+ #
127
+ # @param thinking [Anthropic::Models::Beta::BetaThinkingConfigEnabled, Anthropic::Models::Beta::BetaThinkingConfigDisabled] Body param: Configuration for enabling Claude's extended thinking.
128
+ #
129
+ # @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
130
+ #
131
+ # @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.
132
+ #
133
+ # @param top_k [Integer] Body param: Only sample from the top K options for each subsequent token.
134
+ #
135
+ # @param top_p [Float] Body param: Use nucleus sampling.
136
+ #
137
+ # @param betas [Array<String, Symbol, Anthropic::Models::AnthropicBeta>] Header param: Optional header to specify the beta version(s) you want to use.
138
+ #
139
+ # @param request_options [Anthropic::RequestOptions, Hash{Symbol=>Object}, nil]
140
+ #
141
+ # @return [Anthropic::Internal::Stream<Anthropic::Models::Beta::BetaRawMessageStartEvent, Anthropic::Models::Beta::BetaRawMessageDeltaEvent, Anthropic::Models::Beta::BetaRawMessageStopEvent, Anthropic::Models::Beta::BetaRawContentBlockStartEvent, Anthropic::Models::Beta::BetaRawContentBlockDeltaEvent, Anthropic::Models::Beta::BetaRawContentBlockStopEvent>]
142
+ #
143
+ # @see Anthropic::Models::Beta::MessageCreateParams
144
+ def stream(params)
145
+ parsed, options = Anthropic::Models::Beta::MessageCreateParams.dump_request(params)
146
+ unless parsed.fetch(:stream, true)
147
+ message = "Please use `#create` for the non-streaming use case."
148
+ raise ArgumentError.new(message)
149
+ end
150
+ parsed.store(:stream, true)
151
+ raw_stream = @client.request(
152
+ method: :post,
153
+ path: "v1/messages?beta=true",
154
+ headers: {"accept" => "text/event-stream"},
155
+ body: parsed,
156
+ stream: Anthropic::Internal::Stream,
157
+ model: Anthropic::Beta::BetaRawMessageStreamEvent,
158
+ options: options
159
+ )
160
+ Anthropic::Streaming::MessageStream.new(raw_stream: raw_stream)
94
161
  end
95
162
 
96
163
  # See {Anthropic::Resources::Beta::Messages#create} for non-streaming counterpart.
@@ -132,7 +199,7 @@ module Anthropic
132
199
  #
133
200
  # @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
201
  #
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.
202
+ # @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
203
  #
137
204
  # @param top_k [Integer] Body param: Only sample from the top K options for each subsequent token.
138
205
  #
@@ -192,7 +259,7 @@ module Anthropic
192
259
  #
193
260
  # @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
261
  #
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.
262
+ # @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
263
  #
197
264
  # @param betas [Array<String, Symbol, Anthropic::Models::AnthropicBeta>] Header param: Optional header to specify the beta version(s) you want to use.
198
265
  #
@@ -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.3.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
@@ -21,10 +21,15 @@ module Anthropic
21
21
  # @api private
22
22
  sig do
23
23
  override
24
- .params(req: Anthropic::Internal::Transport::BaseClient::RequestComponents, opts: Anthropic::Internal::AnyHash)
24
+ .params(
25
+ req:
26
+ Anthropic::Internal::Transport::BaseClient::RequestComponents,
27
+ opts: Anthropic::Internal::AnyHash
28
+ )
25
29
  .returns(Anthropic::Internal::Transport::BaseClient::RequestInput)
26
30
  end
27
- private def build_request(req, opts); end
31
+ private def build_request(req, opts)
32
+ end
28
33
 
29
34
  sig do
30
35
  params(
@@ -45,10 +50,15 @@ module Anthropic
45
50
  end
46
51
 
47
52
  sig do
48
- params(request_components: Anthropic::Internal::Transport::BaseClient::RequestComponents)
49
- .returns(Anthropic::Internal::Transport::BaseClient::RequestComponents)
53
+ params(
54
+ request_components:
55
+ Anthropic::Internal::Transport::BaseClient::RequestComponents
56
+ ).returns(
57
+ Anthropic::Internal::Transport::BaseClient::RequestComponents
58
+ )
59
+ end
60
+ private def fit_req_to_bedrock_specs!(request_components)
50
61
  end
51
- private def fit_req_to_bedrock_specs!(request_components) end
52
62
 
53
63
  sig do
54
64
  params(
@@ -75,7 +85,8 @@ module Anthropic
75
85
  aws_secret_key: nil,
76
86
  aws_session_token: nil,
77
87
  aws_profile: nil
78
- ) end
88
+ )
89
+ end
79
90
  end
80
91
  end
81
92
  end
@@ -0,0 +1,95 @@
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 do
65
+ params(message: Anthropic::Models::Message, type: Symbol).returns(
66
+ T.attached_class
67
+ )
68
+ end
69
+ def self.new(message:, type: :message_stop)
70
+ end
71
+ end
72
+
73
+ class ContentBlockStopEvent < Anthropic::Models::RawContentBlockStopEvent
74
+ sig { returns(Integer) }
75
+ attr_accessor :index
76
+
77
+ sig { returns(Symbol) }
78
+ attr_accessor :type
79
+
80
+ sig { returns(Anthropic::Models::ContentBlock) }
81
+ attr_accessor :content_block
82
+
83
+ sig do
84
+ params(
85
+ index: Integer,
86
+ content_block: Anthropic::Models::ContentBlock,
87
+ type: Symbol
88
+ ).returns(T.attached_class)
89
+ end
90
+ def self.new(index:, content_block:, type: :content_block_stop)
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end