anthropic 1.1.1 → 1.9.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 (331) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +170 -0
  3. data/README.md +32 -16
  4. data/lib/anthropic/client.rb +5 -2
  5. data/lib/anthropic/errors.rb +22 -0
  6. data/lib/anthropic/helpers/bedrock/client.rb +34 -4
  7. data/lib/anthropic/helpers/input_schema/array_of.rb +41 -0
  8. data/lib/anthropic/helpers/input_schema/base_model.rb +86 -0
  9. data/lib/anthropic/helpers/input_schema/boolean.rb +29 -0
  10. data/lib/anthropic/helpers/input_schema/enum_of.rb +63 -0
  11. data/lib/anthropic/helpers/input_schema/json_schema_converter.rb +199 -0
  12. data/lib/anthropic/helpers/input_schema/parsed_json.rb +39 -0
  13. data/lib/anthropic/helpers/input_schema/property_mapping.rb +47 -0
  14. data/lib/anthropic/helpers/input_schema/union_of.rb +56 -0
  15. data/lib/anthropic/helpers/input_schema.rb +16 -0
  16. data/lib/anthropic/helpers/streaming/events.rb +141 -0
  17. data/lib/anthropic/helpers/streaming/message_stream.rb +278 -0
  18. data/lib/anthropic/helpers/streaming.rb +37 -0
  19. data/lib/anthropic/helpers/vertex/client.rb +4 -1
  20. data/lib/anthropic/input_schema.rb +10 -0
  21. data/lib/anthropic/internal/stream.rb +6 -3
  22. data/lib/anthropic/internal/transport/base_client.rb +30 -5
  23. data/lib/anthropic/internal/transport/pooled_net_requester.rb +1 -9
  24. data/lib/anthropic/internal/type/array_of.rb +7 -1
  25. data/lib/anthropic/internal/type/base_model.rb +81 -32
  26. data/lib/anthropic/internal/type/base_stream.rb +3 -1
  27. data/lib/anthropic/internal/type/boolean.rb +7 -1
  28. data/lib/anthropic/internal/type/converter.rb +69 -34
  29. data/lib/anthropic/internal/type/enum.rb +16 -5
  30. data/lib/anthropic/internal/type/file_input.rb +6 -1
  31. data/lib/anthropic/internal/type/hash_of.rb +7 -1
  32. data/lib/anthropic/internal/type/union.rb +26 -16
  33. data/lib/anthropic/internal/type/unknown.rb +7 -1
  34. data/lib/anthropic/internal/util.rb +10 -11
  35. data/lib/anthropic/models/anthropic_beta.rb +3 -0
  36. data/lib/anthropic/models/beta/beta_base64_pdf_block.rb +1 -65
  37. data/lib/anthropic/models/beta/beta_bash_code_execution_output_block.rb +25 -0
  38. data/lib/anthropic/models/beta/beta_bash_code_execution_output_block_param.rb +25 -0
  39. data/lib/anthropic/models/beta/beta_bash_code_execution_result_block.rb +44 -0
  40. data/lib/anthropic/models/beta/beta_bash_code_execution_result_block_param.rb +44 -0
  41. data/lib/anthropic/models/beta/beta_bash_code_execution_tool_result_block.rb +43 -0
  42. data/lib/anthropic/models/beta/beta_bash_code_execution_tool_result_block_param.rb +53 -0
  43. data/lib/anthropic/models/beta/beta_bash_code_execution_tool_result_error.rb +39 -0
  44. data/lib/anthropic/models/beta/beta_bash_code_execution_tool_result_error_param.rb +39 -0
  45. data/lib/anthropic/models/beta/beta_citation_char_location.rb +7 -1
  46. data/lib/anthropic/models/beta/beta_citation_config.rb +19 -0
  47. data/lib/anthropic/models/beta/beta_citation_content_block_location.rb +7 -1
  48. data/lib/anthropic/models/beta/beta_citation_page_location.rb +7 -1
  49. data/lib/anthropic/models/beta/beta_citation_search_result_location.rb +55 -0
  50. data/lib/anthropic/models/beta/beta_citation_search_result_location_param.rb +55 -0
  51. data/lib/anthropic/models/beta/beta_citations_delta.rb +5 -3
  52. data/lib/anthropic/models/beta/beta_code_execution_tool_20250825.rb +40 -0
  53. data/lib/anthropic/models/beta/beta_content_block.rb +12 -5
  54. data/lib/anthropic/models/beta/beta_content_block_param.rb +28 -17
  55. data/lib/anthropic/models/beta/beta_document_block.rb +56 -0
  56. data/lib/anthropic/models/beta/beta_message.rb +6 -2
  57. data/lib/anthropic/models/beta/beta_message_param.rb +3 -3
  58. data/lib/anthropic/models/beta/beta_raw_content_block_start_event.rb +14 -7
  59. data/lib/anthropic/models/beta/beta_request_document_block.rb +75 -0
  60. data/lib/anthropic/models/beta/beta_search_result_block_param.rb +55 -0
  61. data/lib/anthropic/models/beta/beta_server_tool_usage.rb +9 -1
  62. data/lib/anthropic/models/beta/beta_server_tool_use_block.rb +3 -0
  63. data/lib/anthropic/models/beta/beta_server_tool_use_block_param.rb +3 -0
  64. data/lib/anthropic/models/beta/beta_text_block.rb +2 -2
  65. data/lib/anthropic/models/beta/beta_text_block_param.rb +2 -2
  66. data/lib/anthropic/models/beta/beta_text_citation.rb +3 -1
  67. data/lib/anthropic/models/beta/beta_text_citation_param.rb +3 -1
  68. data/lib/anthropic/models/beta/beta_text_editor_code_execution_create_result_block.rb +25 -0
  69. data/lib/anthropic/models/beta/beta_text_editor_code_execution_create_result_block_param.rb +26 -0
  70. data/lib/anthropic/models/beta/beta_text_editor_code_execution_str_replace_result_block.rb +49 -0
  71. data/lib/anthropic/models/beta/beta_text_editor_code_execution_str_replace_result_block_param.rb +50 -0
  72. data/lib/anthropic/models/beta/beta_text_editor_code_execution_tool_result_block.rb +47 -0
  73. data/lib/anthropic/models/beta/beta_text_editor_code_execution_tool_result_block_param.rb +57 -0
  74. data/lib/anthropic/models/beta/beta_text_editor_code_execution_tool_result_error.rb +45 -0
  75. data/lib/anthropic/models/beta/beta_text_editor_code_execution_tool_result_error_param.rb +46 -0
  76. data/lib/anthropic/models/beta/beta_text_editor_code_execution_view_result_block.rb +61 -0
  77. data/lib/anthropic/models/beta/beta_text_editor_code_execution_view_result_block_param.rb +62 -0
  78. data/lib/anthropic/models/beta/beta_tool.rb +7 -1
  79. data/lib/anthropic/models/beta/beta_tool_result_block_param.rb +8 -4
  80. data/lib/anthropic/models/beta/beta_tool_text_editor_20250728.rb +49 -0
  81. data/lib/anthropic/models/beta/beta_tool_union.rb +12 -6
  82. data/lib/anthropic/models/beta/beta_web_fetch_block.rb +42 -0
  83. data/lib/anthropic/models/beta/beta_web_fetch_block_param.rb +42 -0
  84. data/lib/anthropic/models/beta/beta_web_fetch_tool_20250910.rb +82 -0
  85. data/lib/anthropic/models/beta/beta_web_fetch_tool_result_block.rb +43 -0
  86. data/lib/anthropic/models/beta/beta_web_fetch_tool_result_block_param.rb +53 -0
  87. data/lib/anthropic/models/beta/beta_web_fetch_tool_result_error_block.rb +25 -0
  88. data/lib/anthropic/models/beta/beta_web_fetch_tool_result_error_block_param.rb +25 -0
  89. data/lib/anthropic/models/beta/beta_web_fetch_tool_result_error_code.rb +25 -0
  90. data/lib/anthropic/models/beta/message_count_tokens_params.rb +23 -36
  91. data/lib/anthropic/models/beta/message_create_params.rb +10 -27
  92. data/lib/anthropic/models/beta/messages/batch_create_params.rb +13 -37
  93. data/lib/anthropic/models/beta/messages/beta_message_batch.rb +1 -4
  94. data/lib/anthropic/models/beta_error_response.rb +7 -1
  95. data/lib/anthropic/models/cache_control_ephemeral.rb +39 -1
  96. data/lib/anthropic/models/cache_creation.rb +24 -0
  97. data/lib/anthropic/models/citation_char_location.rb +7 -1
  98. data/lib/anthropic/models/citation_content_block_location.rb +7 -1
  99. data/lib/anthropic/models/citation_page_location.rb +7 -1
  100. data/lib/anthropic/models/citation_search_result_location_param.rb +51 -0
  101. data/lib/anthropic/models/citations_delta.rb +5 -3
  102. data/lib/anthropic/models/citations_search_result_location.rb +51 -0
  103. data/lib/anthropic/models/content_block.rb +5 -5
  104. data/lib/anthropic/models/content_block_param.rb +14 -11
  105. data/lib/anthropic/models/document_block_param.rb +2 -2
  106. data/lib/anthropic/models/error_response.rb +7 -1
  107. data/lib/anthropic/models/message.rb +6 -2
  108. data/lib/anthropic/models/message_count_tokens_params.rb +10 -27
  109. data/lib/anthropic/models/message_count_tokens_tool.rb +5 -1
  110. data/lib/anthropic/models/message_create_params.rb +10 -27
  111. data/lib/anthropic/models/message_param.rb +3 -3
  112. data/lib/anthropic/models/messages/batch_create_params.rb +11 -31
  113. data/lib/anthropic/models/model.rb +16 -21
  114. data/lib/anthropic/models/raw_content_block_start_event.rb +7 -7
  115. data/lib/anthropic/models/search_result_block_param.rb +51 -0
  116. data/lib/anthropic/models/text_block.rb +2 -2
  117. data/lib/anthropic/models/text_block_param.rb +2 -2
  118. data/lib/anthropic/models/text_citation.rb +3 -1
  119. data/lib/anthropic/models/text_citation_param.rb +3 -1
  120. data/lib/anthropic/models/tool.rb +7 -1
  121. data/lib/anthropic/models/tool_result_block_param.rb +8 -4
  122. data/lib/anthropic/models/tool_text_editor_20250429.rb +36 -0
  123. data/lib/anthropic/models/tool_text_editor_20250728.rb +45 -0
  124. data/lib/anthropic/models/tool_union.rb +5 -1
  125. data/lib/anthropic/models/tool_use_block.rb +14 -0
  126. data/lib/anthropic/models/usage.rb +9 -1
  127. data/lib/anthropic/models.rb +16 -4
  128. data/lib/anthropic/resources/beta/messages.rb +73 -6
  129. data/lib/anthropic/resources/messages.rb +152 -7
  130. data/lib/anthropic/streaming.rb +5 -0
  131. data/lib/anthropic/version.rb +1 -1
  132. data/lib/anthropic.rb +54 -0
  133. data/rbi/anthropic/errors.rbi +18 -2
  134. data/rbi/anthropic/helpers/bedrock/client.rbi +17 -6
  135. data/rbi/anthropic/helpers/input_schema/array_of.rbi +16 -0
  136. data/rbi/anthropic/helpers/input_schema/base_model.rbi +21 -0
  137. data/rbi/anthropic/helpers/input_schema/boolean.rbi +11 -0
  138. data/rbi/anthropic/helpers/input_schema/enum_of.rbi +30 -0
  139. data/rbi/anthropic/helpers/input_schema/json_schema_converter.rbi +89 -0
  140. data/rbi/anthropic/helpers/input_schema/union_of.rbi +23 -0
  141. data/rbi/anthropic/helpers/streaming/events.rbi +95 -0
  142. data/rbi/anthropic/helpers/streaming/message_stream.rbi +73 -0
  143. data/rbi/anthropic/helpers/structured_output.rbi +16 -0
  144. data/rbi/anthropic/helpers/vertex/client.rbi +17 -6
  145. data/rbi/anthropic/input_schema.rbi +12 -0
  146. data/rbi/anthropic/internal/transport/base_client.rbi +1 -1
  147. data/rbi/anthropic/internal/type/base_stream.rbi +8 -1
  148. data/rbi/anthropic/internal/type/boolean.rbi +2 -0
  149. data/rbi/anthropic/internal/type/converter.rbi +69 -15
  150. data/rbi/anthropic/internal/type/union.rbi +14 -2
  151. data/rbi/anthropic/internal/type/unknown.rbi +2 -0
  152. data/rbi/anthropic/internal/util.rbi +2 -0
  153. data/rbi/anthropic/models/anthropic_beta.rbi +2 -0
  154. data/rbi/anthropic/models/beta/beta_base64_pdf_block.rbi +1 -128
  155. data/rbi/anthropic/models/beta/beta_bash_code_execution_output_block.rbi +33 -0
  156. data/rbi/anthropic/models/beta/beta_bash_code_execution_output_block_param.rbi +34 -0
  157. data/rbi/anthropic/models/beta/beta_bash_code_execution_result_block.rbi +72 -0
  158. data/rbi/anthropic/models/beta/beta_bash_code_execution_result_block_param.rbi +77 -0
  159. data/rbi/anthropic/models/beta/beta_bash_code_execution_tool_result_block.rbi +86 -0
  160. data/rbi/anthropic/models/beta/beta_bash_code_execution_tool_result_block_param.rbi +110 -0
  161. data/rbi/anthropic/models/beta/beta_bash_code_execution_tool_result_error.rbi +101 -0
  162. data/rbi/anthropic/models/beta/beta_bash_code_execution_tool_result_error_param.rbi +101 -0
  163. data/rbi/anthropic/models/beta/beta_citation_char_location.rbi +6 -0
  164. data/rbi/anthropic/models/beta/beta_citation_config.rbi +30 -0
  165. data/rbi/anthropic/models/beta/beta_citation_content_block_location.rbi +6 -0
  166. data/rbi/anthropic/models/beta/beta_citation_page_location.rbi +6 -0
  167. data/rbi/anthropic/models/beta/beta_citation_search_result_location.rbi +78 -0
  168. data/rbi/anthropic/models/beta/beta_citation_search_result_location_param.rbi +79 -0
  169. data/rbi/anthropic/models/beta/beta_citations_delta.rbi +4 -2
  170. data/rbi/anthropic/models/beta/beta_code_execution_tool_20250825.rbi +72 -0
  171. data/rbi/anthropic/models/beta/beta_content_block.rbi +6 -3
  172. data/rbi/anthropic/models/beta/beta_content_block_param.rbi +11 -7
  173. data/rbi/anthropic/models/beta/beta_document_block.rbi +95 -0
  174. data/rbi/anthropic/models/beta/beta_message.rbi +14 -3
  175. data/rbi/anthropic/models/beta/beta_raw_content_block_start_event.rbi +12 -6
  176. data/rbi/anthropic/models/beta/beta_request_document_block.rbi +142 -0
  177. data/rbi/anthropic/models/beta/beta_search_result_block_param.rbi +91 -0
  178. data/rbi/anthropic/models/beta/beta_server_tool_usage.rbi +17 -2
  179. data/rbi/anthropic/models/beta/beta_server_tool_use_block.rbi +15 -0
  180. data/rbi/anthropic/models/beta/beta_server_tool_use_block_param.rbi +15 -0
  181. data/rbi/anthropic/models/beta/beta_text_block.rbi +2 -1
  182. data/rbi/anthropic/models/beta/beta_text_block_param.rbi +6 -3
  183. data/rbi/anthropic/models/beta/beta_text_citation.rbi +2 -1
  184. data/rbi/anthropic/models/beta/beta_text_citation_param.rbi +2 -1
  185. data/rbi/anthropic/models/beta/beta_text_editor_code_execution_create_result_block.rbi +41 -0
  186. data/rbi/anthropic/models/beta/beta_text_editor_code_execution_create_result_block_param.rbi +41 -0
  187. data/rbi/anthropic/models/beta/beta_text_editor_code_execution_str_replace_result_block.rbi +73 -0
  188. data/rbi/anthropic/models/beta/beta_text_editor_code_execution_str_replace_result_block_param.rbi +73 -0
  189. data/rbi/anthropic/models/beta/beta_text_editor_code_execution_tool_result_block.rbi +90 -0
  190. data/rbi/anthropic/models/beta/beta_text_editor_code_execution_tool_result_block_param.rbi +118 -0
  191. data/rbi/anthropic/models/beta/beta_text_editor_code_execution_tool_result_error.rbi +110 -0
  192. data/rbi/anthropic/models/beta/beta_text_editor_code_execution_tool_result_error_param.rbi +110 -0
  193. data/rbi/anthropic/models/beta/beta_text_editor_code_execution_view_result_block.rbi +118 -0
  194. data/rbi/anthropic/models/beta/beta_text_editor_code_execution_view_result_block_param.rbi +118 -0
  195. data/rbi/anthropic/models/beta/beta_tool.rbi +14 -5
  196. data/rbi/anthropic/models/beta/beta_tool_result_block_param.rbi +3 -1
  197. data/rbi/anthropic/models/beta/beta_tool_text_editor_20250728.rbi +82 -0
  198. data/rbi/anthropic/models/beta/beta_tool_union.rbi +7 -4
  199. data/rbi/anthropic/models/beta/beta_web_fetch_block.rbi +67 -0
  200. data/rbi/anthropic/models/beta/beta_web_fetch_block_param.rbi +71 -0
  201. data/rbi/anthropic/models/beta/beta_web_fetch_tool_20250910.rbi +125 -0
  202. data/rbi/anthropic/models/beta/beta_web_fetch_tool_result_block.rbi +81 -0
  203. data/rbi/anthropic/models/beta/beta_web_fetch_tool_result_block_param.rbi +109 -0
  204. data/rbi/anthropic/models/beta/beta_web_fetch_tool_result_error_block.rbi +51 -0
  205. data/rbi/anthropic/models/beta/beta_web_fetch_tool_result_error_block_param.rbi +50 -0
  206. data/rbi/anthropic/models/beta/beta_web_fetch_tool_result_error_code.rbi +70 -0
  207. data/rbi/anthropic/models/beta/message_count_tokens_params.rbi +51 -70
  208. data/rbi/anthropic/models/beta/message_create_params.rbi +44 -66
  209. data/rbi/anthropic/models/beta/messages/batch_create_params.rbi +44 -66
  210. data/rbi/anthropic/models/beta_error_response.rbi +10 -2
  211. data/rbi/anthropic/models/cache_control_ephemeral.rbi +68 -3
  212. data/rbi/anthropic/models/cache_creation.rbi +45 -0
  213. data/rbi/anthropic/models/citation_char_location.rbi +6 -0
  214. data/rbi/anthropic/models/citation_content_block_location.rbi +6 -0
  215. data/rbi/anthropic/models/citation_page_location.rbi +6 -0
  216. data/rbi/anthropic/models/citation_search_result_location_param.rbi +74 -0
  217. data/rbi/anthropic/models/citations_delta.rbi +4 -2
  218. data/rbi/anthropic/models/citations_search_result_location.rbi +74 -0
  219. data/rbi/anthropic/models/content_block.rbi +3 -3
  220. data/rbi/anthropic/models/content_block_param.rbi +6 -5
  221. data/rbi/anthropic/models/document_block_param.rbi +7 -3
  222. data/rbi/anthropic/models/error_response.rbi +10 -2
  223. data/rbi/anthropic/models/message.rbi +11 -3
  224. data/rbi/anthropic/models/message_count_tokens_params.rbi +24 -50
  225. data/rbi/anthropic/models/message_count_tokens_tool.rbi +2 -0
  226. data/rbi/anthropic/models/message_create_params.rbi +24 -50
  227. data/rbi/anthropic/models/messages/batch_create_params.rbi +24 -50
  228. data/rbi/anthropic/models/model.rbi +4 -8
  229. data/rbi/anthropic/models/raw_content_block_start_event.rbi +6 -6
  230. data/rbi/anthropic/models/search_result_block_param.rbi +77 -0
  231. data/rbi/anthropic/models/text_block.rbi +2 -1
  232. data/rbi/anthropic/models/text_block_param.rbi +6 -3
  233. data/rbi/anthropic/models/text_citation.rbi +2 -1
  234. data/rbi/anthropic/models/text_citation_param.rbi +2 -1
  235. data/rbi/anthropic/models/tool.rbi +16 -5
  236. data/rbi/anthropic/models/tool_result_block_param.rbi +6 -1
  237. data/rbi/anthropic/models/tool_text_editor_20250429.rbi +62 -0
  238. data/rbi/anthropic/models/tool_text_editor_20250728.rbi +72 -0
  239. data/rbi/anthropic/models/tool_union.rbi +2 -0
  240. data/rbi/anthropic/models/usage.rbi +13 -0
  241. data/rbi/anthropic/models.rbi +14 -0
  242. data/rbi/anthropic/resources/beta/messages.rbi +45 -87
  243. data/rbi/anthropic/resources/messages.rbi +315 -69
  244. data/rbi/anthropic/streaming.rbi +5 -0
  245. data/sig/anthropic/errors.rbs +9 -0
  246. data/sig/anthropic/helpers/streaming/events.rbs +117 -0
  247. data/sig/anthropic/helpers/streaming/message_stream.rbs +57 -0
  248. data/sig/anthropic/internal/transport/base_client.rbs +1 -1
  249. data/sig/anthropic/internal/type/base_stream.rbs +4 -0
  250. data/sig/anthropic/internal/type/converter.rbs +24 -1
  251. data/sig/anthropic/internal/type/union.rbs +2 -2
  252. data/sig/anthropic/models/anthropic_beta.rbs +2 -0
  253. data/sig/anthropic/models/beta/beta_base64_pdf_block.rbs +1 -58
  254. data/sig/anthropic/models/beta/beta_bash_code_execution_output_block.rbs +23 -0
  255. data/sig/anthropic/models/beta/beta_bash_code_execution_output_block_param.rbs +23 -0
  256. data/sig/anthropic/models/beta/beta_bash_code_execution_result_block.rbs +44 -0
  257. data/sig/anthropic/models/beta/beta_bash_code_execution_result_block_param.rbs +44 -0
  258. data/sig/anthropic/models/beta/beta_bash_code_execution_tool_result_block.rbs +44 -0
  259. data/sig/anthropic/models/beta/beta_bash_code_execution_tool_result_block_param.rbs +49 -0
  260. data/sig/anthropic/models/beta/beta_bash_code_execution_tool_result_error.rbs +48 -0
  261. data/sig/anthropic/models/beta/beta_bash_code_execution_tool_result_error_param.rbs +48 -0
  262. data/sig/anthropic/models/beta/beta_citation_char_location.rbs +5 -0
  263. data/sig/anthropic/models/beta/beta_citation_config.rbs +17 -0
  264. data/sig/anthropic/models/beta/beta_citation_content_block_location.rbs +5 -0
  265. data/sig/anthropic/models/beta/beta_citation_page_location.rbs +5 -0
  266. data/sig/anthropic/models/beta/beta_citation_search_result_location.rbs +54 -0
  267. data/sig/anthropic/models/beta/beta_citation_search_result_location_param.rbs +54 -0
  268. data/sig/anthropic/models/beta/beta_citations_delta.rbs +1 -0
  269. data/sig/anthropic/models/beta/beta_code_execution_tool_20250825.rbs +34 -0
  270. data/sig/anthropic/models/beta/beta_content_block.rbs +5 -2
  271. data/sig/anthropic/models/beta/beta_content_block_param.rbs +12 -8
  272. data/sig/anthropic/models/beta/beta_document_block.rbs +49 -0
  273. data/sig/anthropic/models/beta/beta_raw_content_block_start_event.rbs +5 -2
  274. data/sig/anthropic/models/beta/beta_request_document_block.rbs +62 -0
  275. data/sig/anthropic/models/beta/beta_search_result_block_param.rbs +53 -0
  276. data/sig/anthropic/models/beta/beta_server_tool_usage.rbs +12 -3
  277. data/sig/anthropic/models/beta/beta_server_tool_use_block.rbs +9 -1
  278. data/sig/anthropic/models/beta/beta_server_tool_use_block_param.rbs +9 -1
  279. data/sig/anthropic/models/beta/beta_text_citation.rbs +1 -0
  280. data/sig/anthropic/models/beta/beta_text_citation_param.rbs +1 -0
  281. data/sig/anthropic/models/beta/beta_text_editor_code_execution_create_result_block.rbs +29 -0
  282. data/sig/anthropic/models/beta/beta_text_editor_code_execution_create_result_block_param.rbs +29 -0
  283. data/sig/anthropic/models/beta/beta_text_editor_code_execution_str_replace_result_block.rbs +49 -0
  284. data/sig/anthropic/models/beta/beta_text_editor_code_execution_str_replace_result_block_param.rbs +49 -0
  285. data/sig/anthropic/models/beta/beta_text_editor_code_execution_tool_result_block.rbs +46 -0
  286. data/sig/anthropic/models/beta/beta_text_editor_code_execution_tool_result_block_param.rbs +51 -0
  287. data/sig/anthropic/models/beta/beta_text_editor_code_execution_tool_result_error.rbs +53 -0
  288. data/sig/anthropic/models/beta/beta_text_editor_code_execution_tool_result_error_param.rbs +53 -0
  289. data/sig/anthropic/models/beta/beta_text_editor_code_execution_view_result_block.rbs +61 -0
  290. data/sig/anthropic/models/beta/beta_text_editor_code_execution_view_result_block_param.rbs +61 -0
  291. data/sig/anthropic/models/beta/beta_tool.rbs +14 -3
  292. data/sig/anthropic/models/beta/beta_tool_result_block_param.rbs +2 -0
  293. data/sig/anthropic/models/beta/beta_tool_text_editor_20250728.rbs +39 -0
  294. data/sig/anthropic/models/beta/beta_tool_union.rbs +7 -4
  295. data/sig/anthropic/models/beta/beta_web_fetch_block.rbs +39 -0
  296. data/sig/anthropic/models/beta/beta_web_fetch_block_param.rbs +39 -0
  297. data/sig/anthropic/models/beta/beta_web_fetch_tool_20250910.rbs +59 -0
  298. data/sig/anthropic/models/beta/beta_web_fetch_tool_result_block.rbs +44 -0
  299. data/sig/anthropic/models/beta/beta_web_fetch_tool_result_block_param.rbs +49 -0
  300. data/sig/anthropic/models/beta/beta_web_fetch_tool_result_error_block.rbs +29 -0
  301. data/sig/anthropic/models/beta/beta_web_fetch_tool_result_error_block_param.rbs +29 -0
  302. data/sig/anthropic/models/beta/beta_web_fetch_tool_result_error_code.rbs +32 -0
  303. data/sig/anthropic/models/beta/message_count_tokens_params.rbs +7 -4
  304. data/sig/anthropic/models/beta_error_response.rbs +13 -2
  305. data/sig/anthropic/models/cache_control_ephemeral.rbs +27 -3
  306. data/sig/anthropic/models/cache_creation.rbs +25 -0
  307. data/sig/anthropic/models/citation_char_location.rbs +5 -0
  308. data/sig/anthropic/models/citation_content_block_location.rbs +5 -0
  309. data/sig/anthropic/models/citation_page_location.rbs +5 -0
  310. data/sig/anthropic/models/citation_search_result_location_param.rbs +50 -0
  311. data/sig/anthropic/models/citations_delta.rbs +1 -0
  312. data/sig/anthropic/models/citations_search_result_location.rbs +50 -0
  313. data/sig/anthropic/models/content_block.rbs +2 -2
  314. data/sig/anthropic/models/content_block_param.rbs +6 -5
  315. data/sig/anthropic/models/document_block_param.rbs +4 -8
  316. data/sig/anthropic/models/error_response.rbs +13 -2
  317. data/sig/anthropic/models/message_count_tokens_tool.rbs +2 -0
  318. data/sig/anthropic/models/model.rbs +4 -10
  319. data/sig/anthropic/models/raw_content_block_start_event.rbs +2 -2
  320. data/sig/anthropic/models/search_result_block_param.rbs +49 -0
  321. data/sig/anthropic/models/text_citation.rbs +1 -0
  322. data/sig/anthropic/models/text_citation_param.rbs +1 -0
  323. data/sig/anthropic/models/tool.rbs +14 -3
  324. data/sig/anthropic/models/tool_result_block_param.rbs +5 -1
  325. data/sig/anthropic/models/tool_text_editor_20250429.rbs +30 -0
  326. data/sig/anthropic/models/tool_text_editor_20250728.rbs +35 -0
  327. data/sig/anthropic/models/tool_union.rbs +2 -0
  328. data/sig/anthropic/models/usage.rbs +5 -0
  329. data/sig/anthropic/models.rbs +12 -0
  330. data/sig/anthropic/streaming.rbs +3 -0
  331. metadata +152 -3
@@ -0,0 +1,278 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Anthropic
4
+ module Helpers
5
+ module Streaming
6
+ # @api private
7
+ #
8
+ # MessageStream provides a Ruby Enumerable interface over Server-Sent Events from
9
+ # the Anthropic API, yielding a mix of raw streaming events and higher-level typed
10
+ # events while maintaining accumulated message state throughout the stream lifecycle.
11
+ #
12
+ #
13
+ # @generic Elem
14
+ class MessageStream
15
+ include Anthropic::Internal::Type::BaseStream
16
+
17
+ # @api private
18
+ #
19
+ # Consumes raw stream events and yields a mix of raw and higher-level typed events while
20
+ # maintaining accumulated message state. This is what's called when you run `each` on the
21
+ # stream.
22
+ #
23
+ # @return [Enumerable<generic<Elem>>]
24
+ private def iterator
25
+ @iterator ||= Anthropic::Internal::Util.chain_fused(@stream) do |y|
26
+ @raw_stream.each do |raw_event|
27
+ @accumated_message_snapshot = accumulate_event(
28
+ event: raw_event,
29
+ current_snapshot: @accumated_message_snapshot
30
+ )
31
+ events_to_yield = build_events(event: raw_event, message_snapshot: @accumated_message_snapshot)
32
+ events_to_yield.each(&y)
33
+ end
34
+ end
35
+ end
36
+
37
+ # @api public
38
+ #
39
+ # Blocks until the stream has been consumed
40
+ #
41
+ # @return [void]
42
+ def until_done = each {} # rubocop:disable Lint/EmptyBlock
43
+
44
+ # @api public
45
+ #
46
+ # Returns an enumerable of text deltas from the streaming response.
47
+ #
48
+ # @return [Enumerable<String>]
49
+ def text
50
+ Anthropic::Internal::Util.chain_fused(@iterator) do |y|
51
+ @iterator.each do |event|
52
+ if event.type == :content_block_delta && event.delta.type == :text_delta
53
+ y << event.delta.text
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ # @api public
60
+ #
61
+ # Returns the complete accumulated Message object after stream completion.
62
+ #
63
+ # @return [Anthropic::Models::Message]
64
+ def accumulated_message
65
+ until_done
66
+ parse_tool_uses!(@accumated_message_snapshot) if @tool_models.any?
67
+ @accumated_message_snapshot
68
+ end
69
+
70
+ # @api public
71
+ #
72
+ # Returns all text content blocks concatenated into a single string.
73
+ # NOTE: Currently the API will only respond with a single content block.
74
+ #
75
+ # Will raise an error if no `text` content blocks were returned.
76
+ # @return [String]
77
+ def accumulated_text
78
+ message = accumulated_message
79
+ text_blocks = []
80
+ message.content.each do |block|
81
+ if block.type == :text
82
+ text_blocks << block.text
83
+ end
84
+ end
85
+
86
+ if text_blocks.empty?
87
+ raise RuntimeError.new("Expected to have received at least 1 text block")
88
+ end
89
+
90
+ text_blocks.join
91
+ end
92
+
93
+ # @api private
94
+ #
95
+ # Builds up a complete Message object as streaming events arrive.
96
+ #
97
+ # @param event [Anthropic::Models::RawMessageStreamEvent] the raw streaming event to process
98
+ # @param current_snapshot [Anthropic::Models::Message, nil] current accumulated message state
99
+ #
100
+ # @return [Anthropic::Models::Message] updated message snapshot with event applied
101
+ private def accumulate_event(event:, current_snapshot:)
102
+ unless event in Anthropic::Models::RawMessageStreamEvent
103
+ message = "Expected event to be a variant of RawMessageStreamEvent, got #{event.class}"
104
+ raise ArgumentError.new(message)
105
+ end
106
+
107
+ if current_snapshot.nil?
108
+ return event.message if event.type == :message_start
109
+
110
+ message = "Unexpected event order, got \"#{event.type}\" before \":message_start\""
111
+ raise RuntimeError.new(message)
112
+ end
113
+
114
+ case event
115
+ in Anthropic::Models::RawMessageStartEvent
116
+ # Use the converter to create a new, isolated copy of the message object.
117
+ # This ensures proper type validation and prevents shared object references
118
+ # that could lead to unintended mutations during streaming accumulation.
119
+ # Matches the Python SDK's approach of explicitly constructing Message objects.
120
+ return Anthropic::Internal::Type::Converter.coerce(Anthropic::Models::Message, event.message)
121
+ in Anthropic::Models::RawContentBlockStartEvent
122
+ current_snapshot.content = (current_snapshot.content || []) + [event.content_block]
123
+ in Anthropic::Models::RawContentBlockDeltaEvent
124
+ content = current_snapshot.content[event.index]
125
+
126
+ case (delta = event.delta)
127
+ in Anthropic::Models::TextDelta if content.type == :text
128
+ content.text += delta.text
129
+ in Anthropic::Models::InputJSONDelta if content.type == :tool_use
130
+ json_buf = content.json_buf.to_s
131
+ json_buf += delta.partial_json
132
+
133
+ content.input = json_buf
134
+ content.json_buf = json_buf
135
+ in Anthropic::Models::CitationsDelta if content.type == :text
136
+ content.citations ||= []
137
+ content.citations << delta.citation
138
+ in Anthropic::Models::ThinkingDelta if content.type == :thinking
139
+ content.thinking += delta.thinking
140
+ in Anthropic::Models::SignatureDelta if content.type == :thinking
141
+ content.signature = delta.signature
142
+ else
143
+ end
144
+ in Anthropic::Models::RawMessageDeltaEvent
145
+ current_snapshot.stop_reason = event.delta.stop_reason
146
+ current_snapshot.stop_sequence = event.delta.stop_sequence
147
+ current_snapshot.usage.output_tokens = event.usage.output_tokens
148
+ else
149
+ end
150
+
151
+ current_snapshot
152
+ end
153
+
154
+ # @api private
155
+ #
156
+ # Determines which events to yield for a given raw streaming event.
157
+ #
158
+ # May transform events into higher-level types (TextEvent, InputJsonEvent),
159
+ # pass through raw events unchanged, or produce multiple events.
160
+ #
161
+ # @param event [Anthropic::Models::RawMessageStreamEvent] the raw event to process
162
+ # @param message_snapshot [Anthropic::Models::Message] current accumulated message state
163
+ #
164
+ # @return [Array<Object>] events to yield (mix of raw and typed events)
165
+ private def build_events(event:, message_snapshot:)
166
+ events_to_yield = []
167
+
168
+ case event
169
+ in Anthropic::Models::RawMessageStopEvent
170
+ events_to_yield << MessageStopEvent.new(
171
+ type: :message_stop,
172
+ message: message_snapshot
173
+ )
174
+ in Anthropic::Models::RawContentBlockDeltaEvent
175
+ events_to_yield << event
176
+ content_block = message_snapshot.content[event.index]
177
+
178
+ case (delta = event.delta)
179
+ in Anthropic::Models::TextDelta if content_block.type == :text
180
+ events_to_yield << Anthropic::Streaming::TextEvent.new(
181
+ type: :text,
182
+ text: delta.text,
183
+ snapshot: content_block.text
184
+ )
185
+ in Anthropic::Models::InputJSONDelta if content_block.type == :tool_use
186
+ events_to_yield << Anthropic::Streaming::InputJsonEvent.new(
187
+ type: :input_json,
188
+ partial_json: delta.partial_json,
189
+ snapshot: content_block.input
190
+ )
191
+ in Anthropic::Models::CitationsDelta if content_block.type == :text
192
+ events_to_yield << Anthropic::Streaming::CitationEvent.new(
193
+ type: :citation,
194
+ citation: delta.citation,
195
+ snapshot: content_block.citations || []
196
+ )
197
+ in Anthropic::Models::ThinkingDelta if content_block.type == :thinking
198
+ events_to_yield << Anthropic::Streaming::ThinkingEvent.new(
199
+ type: :thinking,
200
+ thinking: delta.thinking,
201
+ snapshot: content_block.thinking
202
+ )
203
+ in Anthropic::Models::SignatureDelta if content_block.type == :thinking
204
+ events_to_yield << Anthropic::Streaming::SignatureEvent.new(
205
+ type: :signature,
206
+ signature: content_block.signature
207
+ )
208
+ else
209
+ end
210
+ in Anthropic::Models::RawContentBlockStopEvent
211
+ content_block = message_snapshot.content[event.index]
212
+
213
+ events_to_yield << ContentBlockStopEvent.new(
214
+ type: :content_block_stop,
215
+ index: event.index,
216
+ content_block: content_block
217
+ )
218
+ else
219
+ events_to_yield << event
220
+ end
221
+
222
+ events_to_yield
223
+ end
224
+
225
+ # @api private
226
+ #
227
+ # Parse tool use blocks in the message using the provided tool models.
228
+ #
229
+ # @param message [Anthropic::Models::Message] The message to parse
230
+ #
231
+ # @return [Anthropic::Models::Message] The message with parsed tool uses
232
+ private def parse_tool_uses!(message)
233
+ return message unless message&.content
234
+
235
+ message.content.each_with_index do |content, index|
236
+ next unless content.type == :tool_use
237
+
238
+ next unless (model = @tool_models[content.name])
239
+
240
+ parsed =
241
+ begin
242
+ parsed_input = content.input.is_a?(String) ? JSON.parse(content.input) : content.input
243
+
244
+ Anthropic::Internal::Type::Converter.coerce(model, parsed_input)
245
+ rescue StandardError => e
246
+ e
247
+ end
248
+
249
+ message.content[index] = Anthropic::Models::ToolUseBlock.new(
250
+ id: content.id,
251
+ input: content.input,
252
+ name: content.name,
253
+ type: content.type,
254
+ parsed: parsed
255
+ )
256
+ end
257
+
258
+ message
259
+ end
260
+
261
+ # @api private
262
+ #
263
+ # @param raw_stream [Anthropic::Internal::Type::BaseStream]
264
+ # @param tool_models [Hash{String=>Class}] Mapping of tool names to model classes
265
+ def initialize(raw_stream:, tool_models: {})
266
+ # The underlying Server-Sent Event stream from the Anthropic API.
267
+ @raw_stream = raw_stream
268
+ # Accumulated message state that builds up as events are processed.
269
+ @accumated_message_snapshot = nil
270
+ # Mapping of tool names to model classes for parsing.
271
+ @tool_models = tool_models
272
+ # Lazy enumerable that transforms raw events into consumable events.
273
+ @iterator = iterator
274
+ end
275
+ end
276
+ end
277
+ end
278
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Anthropic
4
+ module Helpers
5
+ module Streaming
6
+ extend Anthropic::Internal::Util::SorbetRuntimeSupport
7
+
8
+ define_sorbet_constant!(:RawMessageEvent) do
9
+ T.type_alias do
10
+ T.any(
11
+ Anthropic::Models::RawMessageStartEvent,
12
+ Anthropic::Models::RawMessageDeltaEvent,
13
+ Anthropic::Models::RawMessageStopEvent,
14
+ Anthropic::Models::RawContentBlockStartEvent,
15
+ Anthropic::Models::RawContentBlockDeltaEvent,
16
+ Anthropic::Models::RawContentBlockStopEvent
17
+ )
18
+ end
19
+ end
20
+
21
+ define_sorbet_constant!(:StreamEvent) do
22
+ T.type_alias do
23
+ T.any(
24
+ Anthropic::Streaming::RawMessageEvent,
25
+ Anthropic::Streaming::TextEvent,
26
+ Anthropic::Streaming::CitationEvent,
27
+ Anthropic::Streaming::ThinkingEvent,
28
+ Anthropic::Streaming::SignatureEvent,
29
+ Anthropic::Streaming::InputJsonEvent,
30
+ Anthropic::Streaming::MessageStopEvent,
31
+ Anthropic::Streaming::ContentBlockStopEvent
32
+ )
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -82,7 +82,10 @@ module Anthropic
82
82
  end
83
83
  @project_id = project_id
84
84
 
85
- base_url ||= ENV.fetch("ANTHROPIC_VERTEX_BASE_URL", "https://#{@region}-aiplatform.googleapis.com/v1")
85
+ base_url ||= ENV.fetch(
86
+ "ANTHROPIC_VERTEX_BASE_URL",
87
+ @region.to_s == "global" ? "https://aiplatform.googleapis.com/v1" : "https://#{@region}-aiplatform.googleapis.com/v1"
88
+ )
86
89
 
87
90
  super(
88
91
  base_url: base_url,
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Anthropic
4
+ InputSchema = Anthropic::Helpers::InputSchema
5
+ ArrayOf = Anthropic::Helpers::InputSchema::ArrayOf
6
+ BaseModel = Anthropic::Helpers::InputSchema::BaseModel
7
+ Boolean = Anthropic::Helpers::InputSchema::Boolean
8
+ EnumOf = Anthropic::Helpers::InputSchema::EnumOf
9
+ UnionOf = Anthropic::Helpers::InputSchema::UnionOf
10
+ end
@@ -23,13 +23,15 @@ module Anthropic
23
23
  case msg
24
24
  in {event: "completion", data: String => data}
25
25
  decoded = JSON.parse(data, symbolize_names: true)
26
- y << Anthropic::Internal::Type::Converter.coerce(@model, decoded)
26
+ unwrapped = Anthropic::Internal::Util.dig(decoded, @unwrap)
27
+ y << Anthropic::Internal::Type::Converter.coerce(@model, unwrapped)
27
28
  in {
28
29
  event: "message_start" | "message_delta" | "message_stop" | "content_block_start" | "content_block_delta" | "content_block_stop",
29
30
  data: String => data
30
31
  }
31
32
  decoded = JSON.parse(data, symbolize_names: true)
32
- y << Anthropic::Internal::Type::Converter.coerce(@model, decoded)
33
+ unwrapped = Anthropic::Internal::Util.dig(decoded, @unwrap)
34
+ y << Anthropic::Internal::Type::Converter.coerce(@model, unwrapped)
33
35
  in {event: "ping"}
34
36
  next
35
37
  in {event: "error", data: String => data}
@@ -38,13 +40,14 @@ module Anthropic
38
40
  rescue JSON::ParserError
39
41
  data
40
42
  end
41
- Anthropic::Errors::APIStatusError.for(
43
+ err = Anthropic::Errors::APIStatusError.for(
42
44
  url: @url,
43
45
  status: @status,
44
46
  body: decoded,
45
47
  request: nil,
46
48
  response: @response
47
49
  )
50
+ raise err
48
51
  else
49
52
  end
50
53
  end
@@ -341,6 +341,23 @@ module Anthropic
341
341
  (@initial_retry_delay * scale * jitter).clamp(0, @max_retry_delay)
342
342
  end
343
343
 
344
+ # @api private
345
+ #
346
+ # Very private API, do not use
347
+ #
348
+ # @param request [Hash{Symbol=>Object}] .
349
+ #
350
+ # @option request [Symbol] :method
351
+ #
352
+ # @option request [URI::Generic] :url
353
+ #
354
+ # @option request [Hash{String=>String}] :headers
355
+ #
356
+ # @option request [Object] :body
357
+ #
358
+ # @return [Hash{Symbol, Object}]
359
+ private def transform_request(request) = request
360
+
344
361
  # @api private
345
362
  #
346
363
  # @param request [Hash{Symbol=>Object}] .
@@ -365,7 +382,8 @@ module Anthropic
365
382
  #
366
383
  # @raise [Anthropic::Errors::APIError]
367
384
  # @return [Array(Integer, Net::HTTPResponse, Enumerable<String>)]
368
- private def send_request(request, redirect_count:, retry_count:, send_retry_header:)
385
+ def send_request(request, redirect_count:, retry_count:, send_retry_header:)
386
+ request = transform_request(request)
369
387
  url, headers, max_retries, timeout = request.fetch_values(:url, :headers, :max_retries, :timeout)
370
388
  input = {**request.except(:timeout), deadline: Anthropic::Internal::Util.monotonic_secs + timeout}
371
389
 
@@ -471,6 +489,7 @@ module Anthropic
471
489
  self.class.validate!(req)
472
490
  model = req.fetch(:model) { Anthropic::Internal::Type::Unknown }
473
491
  opts = req[:options].to_h
492
+ unwrap = req[:unwrap]
474
493
  Anthropic::RequestOptions.validate!(opts)
475
494
  request = build_request(req.except(:options), opts)
476
495
  url = request.fetch(:url)
@@ -487,11 +506,18 @@ module Anthropic
487
506
  decoded = Anthropic::Internal::Util.decode_content(response, stream: stream)
488
507
  case req
489
508
  in {stream: Class => st}
490
- st.new(model: model, url: url, status: status, response: response, stream: decoded)
509
+ st.new(
510
+ model: model,
511
+ url: url,
512
+ status: status,
513
+ response: response,
514
+ unwrap: unwrap,
515
+ stream: decoded
516
+ )
491
517
  in {page: Class => page}
492
518
  page.new(client: self, req: req, headers: response, page_data: decoded)
493
519
  else
494
- unwrapped = Anthropic::Internal::Util.dig(decoded, req[:unwrap])
520
+ unwrapped = Anthropic::Internal::Util.dig(decoded, unwrap)
495
521
  Anthropic::Internal::Type::Converter.coerce(model, unwrapped)
496
522
  end
497
523
  end
@@ -532,8 +558,7 @@ module Anthropic
532
558
  ),
533
559
  page: T.nilable(T::Class[Anthropic::Internal::Type::BasePage[Anthropic::Internal::Type::BaseModel]]),
534
560
  stream: T.nilable(
535
- T::Class[Anthropic::Internal::Type::BaseStream[T.anything,
536
- Anthropic::Internal::Type::BaseModel]]
561
+ T::Class[Anthropic::Internal::Type::BaseStream[T.anything, Anthropic::Internal::Type::BaseModel]]
537
562
  ),
538
563
  model: T.nilable(Anthropic::Internal::Type::Converter::Input),
539
564
  options: T.nilable(Anthropic::RequestOptions::OrHash)
@@ -193,15 +193,7 @@ module Anthropic
193
193
  end
194
194
 
195
195
  define_sorbet_constant!(:Request) do
196
- T.type_alias do
197
- {
198
- method: Symbol,
199
- url: URI::Generic,
200
- headers: T::Hash[String, String],
201
- body: T.anything,
202
- deadline: Float
203
- }
204
- end
196
+ T.type_alias { {method: Symbol, url: URI::Generic, headers: T::Hash[String, String], body: T.anything, deadline: Float} }
205
197
  end
206
198
  end
207
199
  end
@@ -62,10 +62,14 @@ module Anthropic
62
62
  #
63
63
  # @param state [Hash{Symbol=>Object}] .
64
64
  #
65
- # @option state [Boolean, :strong] :strictness
65
+ # @option state [Boolean] :translate_names
66
+ #
67
+ # @option state [Boolean] :strictness
66
68
  #
67
69
  # @option state [Hash{Symbol=>Object}] :exactness
68
70
  #
71
+ # @option state [Class<StandardError>] :error
72
+ #
69
73
  # @option state [Integer] :branched
70
74
  #
71
75
  # @return [Array<Object>, Object]
@@ -74,6 +78,7 @@ module Anthropic
74
78
 
75
79
  unless value.is_a?(Array)
76
80
  exactness[:no] += 1
81
+ state[:error] = TypeError.new("#{value.class} can't be coerced into #{Array}")
77
82
  return value
78
83
  end
79
84
 
@@ -143,6 +148,7 @@ module Anthropic
143
148
  # @option spec [Boolean] :"nil?"
144
149
  def initialize(type_info, spec = {})
145
150
  @item_type_fn = Anthropic::Internal::Type::Converter.type_info(type_info || spec)
151
+ @meta = Anthropic::Internal::Type::Converter.meta_info(type_info, spec)
146
152
  @nilable = spec.fetch(:nil?, false)
147
153
  end
148
154
 
@@ -59,18 +59,12 @@ module Anthropic
59
59
  in Hash
60
60
  [Anthropic::Internal::Type::Converter.type_info(type_info), type_info]
61
61
  end
62
+ meta = Anthropic::Internal::Type::Converter.meta_info(type_info, spec)
62
63
 
63
- setter = "#{name_sym}="
64
+ setter = :"#{name_sym}="
64
65
  api_name = info.fetch(:api_name, name_sym)
65
66
  nilable = info.fetch(:nil?, false)
66
- const = if required && !nilable
67
- info.fetch(
68
- :const,
69
- Anthropic::Internal::OMIT
70
- )
71
- else
72
- Anthropic::Internal::OMIT
73
- end
67
+ const = required && !nilable ? info.fetch(:const, Anthropic::Internal::OMIT) : Anthropic::Internal::OMIT
74
68
 
75
69
  [name_sym, setter].each { undef_method(_1) } if known_fields.key?(name_sym)
76
70
 
@@ -81,31 +75,65 @@ module Anthropic
81
75
  required: required,
82
76
  nilable: nilable,
83
77
  const: const,
84
- type_fn: type_fn
78
+ type_fn: type_fn,
79
+ meta: meta
85
80
  }
86
81
 
87
- define_method(setter) { @data.store(name_sym, _1) }
82
+ define_method(setter) do |value|
83
+ target = type_fn.call
84
+ state = Anthropic::Internal::Type::Converter.new_coerce_state(translate_names: false)
85
+ coerced = Anthropic::Internal::Type::Converter.coerce(target, value, state: state)
86
+ status = @coerced.store(name_sym, state.fetch(:error) || true)
87
+ stored =
88
+ case [target, status]
89
+ in [Anthropic::Internal::Type::Converter | Symbol, true]
90
+ coerced
91
+ else
92
+ value
93
+ end
94
+ @data.store(name_sym, stored)
95
+ end
88
96
 
97
+ # rubocop:disable Style/CaseEquality
98
+ # rubocop:disable Metrics/BlockLength
89
99
  define_method(name_sym) do
90
100
  target = type_fn.call
91
- value = @data.fetch(name_sym) { const == Anthropic::Internal::OMIT ? nil : const }
92
- state = {strictness: :strong, exactness: {yes: 0, no: 0, maybe: 0}, branched: 0}
93
- if (nilable || !required) && value.nil?
94
- nil
95
- else
96
- Anthropic::Internal::Type::Converter.coerce(
97
- target, value, state: state
101
+
102
+ case @coerced[name_sym]
103
+ in true | false if Anthropic::Internal::Type::Converter === target
104
+ @data.fetch(name_sym)
105
+ in ::StandardError => e
106
+ raise Anthropic::Errors::ConversionError.new(
107
+ on: self.class,
108
+ method: __method__,
109
+ target: target,
110
+ value: @data.fetch(name_sym),
111
+ cause: e
98
112
  )
113
+ else
114
+ Kernel.then do
115
+ value = @data.fetch(name_sym) { const == Anthropic::Internal::OMIT ? nil : const }
116
+ state = Anthropic::Internal::Type::Converter.new_coerce_state(translate_names: false)
117
+ if (nilable || !required) && value.nil?
118
+ nil
119
+ else
120
+ Anthropic::Internal::Type::Converter.coerce(
121
+ target, value, state: state
122
+ )
123
+ end
124
+ rescue StandardError => e
125
+ raise Anthropic::Errors::ConversionError.new(
126
+ on: self.class,
127
+ method: __method__,
128
+ target: target,
129
+ value: value,
130
+ cause: e
131
+ )
132
+ end
99
133
  end
100
- rescue StandardError => e
101
- cls = self.class.name.split("::").last
102
- message = [
103
- "Failed to parse #{cls}.#{__method__} from #{value.class} to #{target.inspect}.",
104
- "To get the unparsed API response, use #{cls}[#{__method__.inspect}].",
105
- "Cause: #{e.message}"
106
- ].join(" ")
107
- raise Anthropic::Errors::ConversionError.new(message)
108
134
  end
135
+ # rubocop:enable Metrics/BlockLength
136
+ # rubocop:enable Style/CaseEquality
109
137
  end
110
138
 
111
139
  # @api private
@@ -205,23 +233,28 @@ module Anthropic
205
233
  #
206
234
  # @param state [Hash{Symbol=>Object}] .
207
235
  #
208
- # @option state [Boolean, :strong] :strictness
236
+ # @option state [Boolean] :translate_names
237
+ #
238
+ # @option state [Boolean] :strictness
209
239
  #
210
240
  # @option state [Hash{Symbol=>Object}] :exactness
211
241
  #
242
+ # @option state [Class<StandardError>] :error
243
+ #
212
244
  # @option state [Integer] :branched
213
245
  #
214
246
  # @return [self, Object]
215
247
  def coerce(value, state:)
216
248
  exactness = state.fetch(:exactness)
217
249
 
218
- if value.is_a?(self.class)
250
+ if value.is_a?(self)
219
251
  exactness[:yes] += 1
220
252
  return value
221
253
  end
222
254
 
223
255
  unless (val = Anthropic::Internal::Util.coerce_hash(value)).is_a?(Hash)
224
256
  exactness[:no] += 1
257
+ state[:error] = TypeError.new("#{value.class} can't be coerced into #{Hash}")
225
258
  return value
226
259
  end
227
260
  exactness[:yes] += 1
@@ -229,13 +262,15 @@ module Anthropic
229
262
  keys = val.keys.to_set
230
263
  instance = new
231
264
  data = instance.to_h
265
+ status = instance.instance_variable_get(:@coerced)
232
266
 
233
267
  # rubocop:disable Metrics/BlockLength
234
268
  fields.each do |name, field|
235
269
  mode, required, target = field.fetch_values(:mode, :required, :type)
236
270
  api_name, nilable, const = field.fetch_values(:api_name, :nilable, :const)
271
+ src_name = state.fetch(:translate_names) ? api_name : name
237
272
 
238
- unless val.key?(api_name)
273
+ unless val.key?(src_name)
239
274
  if required && mode != :dump && const == Anthropic::Internal::OMIT
240
275
  exactness[nilable ? :maybe : :no] += 1
241
276
  else
@@ -244,9 +279,10 @@ module Anthropic
244
279
  next
245
280
  end
246
281
 
247
- item = val.fetch(api_name)
248
- keys.delete(api_name)
282
+ item = val.fetch(src_name)
283
+ keys.delete(src_name)
249
284
 
285
+ state[:error] = nil
250
286
  converted =
251
287
  if item.nil? && (nilable || !required)
252
288
  exactness[nilable ? :yes : :maybe] += 1
@@ -260,6 +296,8 @@ module Anthropic
260
296
  item
261
297
  end
262
298
  end
299
+
300
+ status.store(name, state.fetch(:error) || true)
263
301
  data.store(name, converted)
264
302
  end
265
303
  # rubocop:enable Metrics/BlockLength
@@ -434,7 +472,18 @@ module Anthropic
434
472
  # Create a new instance of a model.
435
473
  #
436
474
  # @param data [Hash{Symbol=>Object}, self]
437
- def initialize(data = {}) = (@data = Anthropic::Internal::Util.coerce_hash!(data).to_h)
475
+ def initialize(data = {})
476
+ @data = {}
477
+ @coerced = {}
478
+ Anthropic::Internal::Util.coerce_hash!(data).each do
479
+ if self.class.known_fields.key?(_1)
480
+ public_send(:"#{_1}=", _2)
481
+ else
482
+ @data.store(_1, _2)
483
+ @coerced.store(_1, false)
484
+ end
485
+ end
486
+ end
438
487
 
439
488
  class << self
440
489
  # @api private