anthropic.md
1 --- 2 title: "Anthropic" 3 id: integrations-anthropic 4 description: "Anthropic integration for Haystack" 5 slug: "/integrations-anthropic" 6 --- 7 8 9 ## haystack_integrations.components.generators.anthropic.chat.chat_generator 10 11 ### AnthropicChatGenerator 12 13 Completes chats using Anthropic's large language models (LLMs). 14 15 It uses [ChatMessage](https://docs.haystack.deepset.ai/docs/data-classes#chatmessage) 16 format in input and output. Supports multimodal inputs including text and images. 17 18 You can customize how the text is generated by passing parameters to the 19 Anthropic API. Use the `**generation_kwargs` argument when you initialize 20 the component or when you run it. Any parameter that works with 21 `anthropic.Message.create` will work here too. 22 23 For details on Anthropic API parameters, see 24 [Anthropic documentation](https://docs.anthropic.com/en/api/messages). 25 26 Usage example: 27 28 ```python 29 from haystack_integrations.components.generators.anthropic import ( 30 AnthropicChatGenerator, 31 ) 32 from haystack.dataclasses import ChatMessage 33 34 generator = AnthropicChatGenerator( 35 generation_kwargs={ 36 "max_tokens": 1000, 37 "temperature": 0.7, 38 }, 39 ) 40 41 messages = [ 42 ChatMessage.from_system( 43 "You are a helpful, respectful and honest assistant" 44 ), 45 ChatMessage.from_user("What's Natural Language Processing?"), 46 ] 47 print(generator.run(messages=messages)) 48 ``` 49 50 Usage example with images: 51 52 ```python 53 from haystack.dataclasses import ChatMessage, ImageContent 54 55 image_content = ImageContent.from_file_path("path/to/image.jpg") 56 messages = [ 57 ChatMessage.from_user( 58 content_parts=["What's in this image?", image_content] 59 ) 60 ] 61 generator = AnthropicChatGenerator() 62 result = generator.run(messages) 63 ``` 64 65 #### SUPPORTED_MODELS 66 67 ```python 68 SUPPORTED_MODELS: list[str] = [ 69 "claude-opus-4-6", 70 "claude-sonnet-4-6", 71 "claude-haiku-4-5-20251001", 72 "claude-sonnet-4-5-20250929", 73 "claude-opus-4-5-20251101", 74 "claude-opus-4-1-20250805", 75 "claude-sonnet-4-20250514", 76 "claude-opus-4-20250514", 77 "claude-3-haiku-20240307", 78 ] 79 80 ``` 81 82 A non-exhaustive list of chat models supported by this component. See 83 https://platform.claude.com/docs/en/about-claude/models/overview for the full list. 84 85 #### __init__ 86 87 ```python 88 __init__( 89 api_key: Secret = Secret.from_env_var("ANTHROPIC_API_KEY"), 90 model: str = "claude-sonnet-4-5", 91 streaming_callback: StreamingCallbackT | None = None, 92 generation_kwargs: dict[str, Any] | None = None, 93 ignore_tools_thinking_messages: bool = True, 94 tools: ToolsType | None = None, 95 *, 96 timeout: float | None = None, 97 max_retries: int | None = None 98 ) 99 ``` 100 101 Creates an instance of AnthropicChatGenerator. 102 103 **Parameters:** 104 105 - **api_key** (<code>Secret</code>) – The Anthropic API key 106 - **model** (<code>str</code>) – The name of the model to use. 107 - **streaming_callback** (<code>StreamingCallbackT | None</code>) – A callback function that is called when a new token is received from the stream. 108 The callback function accepts StreamingChunk as an argument. 109 - **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Other parameters to use for the model. These parameters are all sent directly to 110 the Anthropic endpoint. See Anthropic [documentation](https://docs.anthropic.com/claude/reference/messages_post) 111 for more details. 112 113 Supported generation_kwargs parameters are: 114 115 - `system`: The system message to be passed to the model. 116 - `max_tokens`: The maximum number of tokens to generate. 117 - `metadata`: A dictionary of metadata to be passed to the model. 118 - `stop_sequences`: A list of strings that the model should stop generating at. 119 - `temperature`: The temperature to use for sampling. 120 - `top_p`: The top_p value to use for nucleus sampling. 121 - `top_k`: The top_k value to use for top-k sampling. 122 - `extra_headers`: A dictionary of extra headers to be passed to the model (i.e. for beta features). 123 - `thinking`: A dictionary of thinking parameters to be passed to the model. 124 The `budget_tokens` passed for thinking should be less than `max_tokens`. 125 For more details and supported models, see: [Anthropic Extended Thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking) 126 - `output_config`: A dictionary of output configuration options to be passed to the model. 127 - **ignore_tools_thinking_messages** (<code>bool</code>) – Anthropic's approach to tools (function calling) resolution involves a 128 "chain of thought" messages before returning the actual function names and parameters in a message. If 129 `ignore_tools_thinking_messages` is `True`, the generator will drop so-called thinking messages when tool 130 use is detected. See the Anthropic [tools](https://docs.anthropic.com/en/docs/tool-use#chain-of-thought-tool-use) 131 for more details. 132 - **tools** (<code>ToolsType | None</code>) – A list of Tool and/or Toolset objects, or a single Toolset, that the model can use. 133 Each tool should have a unique name. 134 - **timeout** (<code>float | None</code>) – Timeout for Anthropic client calls. If not set, it defaults to the default set by the Anthropic client. 135 - **max_retries** (<code>int | None</code>) – Maximum number of retries to attempt for failed requests. If not set, it defaults to the default set by 136 the Anthropic client. 137 138 #### to_dict 139 140 ```python 141 to_dict() -> dict[str, Any] 142 ``` 143 144 Serialize this component to a dictionary. 145 146 **Returns:** 147 148 - <code>dict\[str, Any\]</code> – The serialized component as a dictionary. 149 150 #### from_dict 151 152 ```python 153 from_dict(data: dict[str, Any]) -> AnthropicChatGenerator 154 ``` 155 156 Deserialize this component from a dictionary. 157 158 **Parameters:** 159 160 - **data** (<code>dict\[str, Any\]</code>) – The dictionary representation of this component. 161 162 **Returns:** 163 164 - <code>AnthropicChatGenerator</code> – The deserialized component instance. 165 166 #### run 167 168 ```python 169 run( 170 messages: list[ChatMessage], 171 streaming_callback: StreamingCallbackT | None = None, 172 generation_kwargs: dict[str, Any] | None = None, 173 tools: ToolsType | None = None, 174 ) -> dict[str, list[ChatMessage]] 175 ``` 176 177 Invokes the Anthropic API with the given messages and generation kwargs. 178 179 **Parameters:** 180 181 - **messages** (<code>list\[ChatMessage\]</code>) – A list of ChatMessage instances representing the input messages. 182 - **streaming_callback** (<code>StreamingCallbackT | None</code>) – A callback function that is called when a new token is received from the stream. 183 - **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Optional arguments to pass to the Anthropic generation endpoint. 184 - **tools** (<code>ToolsType | None</code>) – A list of Tool and/or Toolset objects, or a single Toolset, that the model can use. 185 Each tool should have a unique name. If set, it will override the `tools` parameter set during component 186 initialization. 187 188 **Returns:** 189 190 - <code>dict\[str, list\[ChatMessage\]\]</code> – A dictionary with the following keys: 191 - `replies`: The responses from the model 192 193 #### run_async 194 195 ```python 196 run_async( 197 messages: list[ChatMessage], 198 streaming_callback: StreamingCallbackT | None = None, 199 generation_kwargs: dict[str, Any] | None = None, 200 tools: ToolsType | None = None, 201 ) -> dict[str, list[ChatMessage]] 202 ``` 203 204 Async version of the run method. Invokes the Anthropic API with the given messages and generation kwargs. 205 206 **Parameters:** 207 208 - **messages** (<code>list\[ChatMessage\]</code>) – A list of ChatMessage instances representing the input messages. 209 - **streaming_callback** (<code>StreamingCallbackT | None</code>) – A callback function that is called when a new token is received from the stream. 210 - **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Optional arguments to pass to the Anthropic generation endpoint. 211 - **tools** (<code>ToolsType | None</code>) – A list of Tool and/or Toolset objects, or a single Toolset, that the model can use. 212 Each tool should have a unique name. If set, it will override the `tools` parameter set during component 213 initialization. 214 215 **Returns:** 216 217 - <code>dict\[str, list\[ChatMessage\]\]</code> – A dictionary with the following keys: 218 - `replies`: The responses from the model 219 220 ## haystack_integrations.components.generators.anthropic.chat.vertex_chat_generator 221 222 ### AnthropicVertexChatGenerator 223 224 Bases: <code>AnthropicChatGenerator</code> 225 226 Enables text generation using Anthropic's Claude models via the Anthropic Vertex AI API. 227 A variety of Claude models (Opus, Sonnet, Haiku, and others) are available through the Vertex AI API endpoint. 228 229 To use AnthropicVertexChatGenerator, you must have a GCP project with Vertex AI enabled. 230 Additionally, ensure that the desired Anthropic model is activated in the Vertex AI Model Garden. 231 Before making requests, you may need to authenticate with GCP using `gcloud auth login`. 232 For more details, refer to the [guide] (https://docs.anthropic.com/en/api/claude-on-vertex-ai). 233 234 Any valid text generation parameters for the Anthropic messaging API can be passed to 235 the AnthropicVertex API. Users can provide these parameters directly to the component via 236 the `generation_kwargs` parameter in `__init__` or the `run` method. 237 238 For more details on the parameters supported by the Anthropic API, refer to the 239 Anthropic Message API [documentation](https://docs.anthropic.com/en/api/messages). 240 241 ```python 242 from haystack_integrations.components.generators.anthropic import AnthropicVertexChatGenerator 243 from haystack.dataclasses import ChatMessage 244 245 messages = [ChatMessage.from_user("What's Natural Language Processing?")] 246 client = AnthropicVertexChatGenerator( 247 model="claude-sonnet-4@20250514", 248 project_id="your-project-id", region="your-region" 249 ) 250 response = client.run(messages) 251 print(response) 252 253 >> {'replies': [ChatMessage(_role=<ChatRole.ASSISTANT: 'assistant'>, _content=[TextContent(text= 254 >> "Natural Language Processing (NLP) is a field of artificial intelligence that 255 >> focuses on enabling computers to understand, interpret, and generate human language. It involves developing 256 >> techniques and algorithms to analyze and process text or speech data, allowing machines to comprehend and 257 >> communicate in natural languages like English, Spanish, or Chinese.")], 258 >> _name=None, _meta={'model': 'claude-sonnet-4@20250514', 'index': 0, 'finish_reason': 'end_turn', 259 >> 'usage': {'input_tokens': 15, 'output_tokens': 64}})]} 260 ``` 261 262 For more details on supported models and their capabilities, refer to the Anthropic 263 [documentation](https://docs.anthropic.com/claude/docs/intro-to-claude). 264 265 For a list of available model IDs when using Claude on Vertex AI, see 266 [Claude on Vertex AI - model availability](https://platform.claude.com/docs/en/build-with-claude/claude-on-vertex-ai#model-availability). 267 268 #### SUPPORTED_MODELS 269 270 ```python 271 SUPPORTED_MODELS: list[str] = [ 272 "claude-opus-4-6", 273 "claude-sonnet-4-6", 274 "claude-sonnet-4-5@20250929", 275 "claude-sonnet-4@20250514", 276 "claude-opus-4-5@20251101", 277 "claude-opus-4-1@20250805", 278 "claude-opus-4@20250514", 279 "claude-haiku-4-5@20251001", 280 ] 281 282 ``` 283 284 A non-exhaustive list of chat models supported by this component. See 285 https://platform.claude.com/docs/en/build-with-claude/claude-on-vertex-ai#model-availability for the full list. 286 287 #### __init__ 288 289 ```python 290 __init__( 291 region: str, 292 project_id: str, 293 model: str = "claude-sonnet-4@20250514", 294 streaming_callback: Callable[[StreamingChunk], None] | None = None, 295 generation_kwargs: dict[str, Any] | None = None, 296 ignore_tools_thinking_messages: bool = True, 297 tools: ToolsType | None = None, 298 *, 299 timeout: float | None = None, 300 max_retries: int | None = None 301 ) 302 ``` 303 304 Creates an instance of AnthropicVertexChatGenerator. 305 306 **Parameters:** 307 308 - **region** (<code>str</code>) – The region where the Anthropic model is deployed. Defaults to "us-central1". 309 - **project_id** (<code>str</code>) – The GCP project ID where the Anthropic model is deployed. 310 - **model** (<code>str</code>) – The name of the model to use. 311 - **streaming_callback** (<code>Callable\\[[StreamingChunk\], None\] | None</code>) – A callback function that is called when a new token is received from the stream. 312 The callback function accepts StreamingChunk as an argument. 313 - **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Other parameters to use for the model. These parameters are all sent directly to 314 the AnthropicVertex endpoint. See Anthropic [documentation](https://docs.anthropic.com/claude/reference/messages_post) 315 for more details. 316 317 Supported generation_kwargs parameters are: 318 319 - `system`: The system message to be passed to the model. 320 - `max_tokens`: The maximum number of tokens to generate. 321 - `metadata`: A dictionary of metadata to be passed to the model. 322 - `stop_sequences`: A list of strings that the model should stop generating at. 323 - `temperature`: The temperature to use for sampling. 324 - `top_p`: The top_p value to use for nucleus sampling. 325 - `top_k`: The top_k value to use for top-k sampling. 326 - `extra_headers`: A dictionary of extra headers to be passed to the model (i.e. for beta features). 327 - **ignore_tools_thinking_messages** (<code>bool</code>) – Anthropic's approach to tools (function calling) resolution involves a 328 "chain of thought" messages before returning the actual function names and parameters in a message. If 329 `ignore_tools_thinking_messages` is `True`, the generator will drop so-called thinking messages when tool 330 use is detected. See the Anthropic [tools](https://docs.anthropic.com/en/docs/tool-use#chain-of-thought-tool-use) 331 for more details. 332 - **tools** (<code>ToolsType | None</code>) – A list of Tool and/or Toolset objects, or a single Toolset, that the model can use. 333 Each tool should have a unique name. 334 - **timeout** (<code>float | None</code>) – Timeout for Anthropic client calls. If not set, it defaults to the default set by the Anthropic client. 335 - **max_retries** (<code>int | None</code>) – Maximum number of retries to attempt for failed requests. If not set, it defaults to the default set by 336 the Anthropic client. 337 338 #### to_dict 339 340 ```python 341 to_dict() -> dict[str, Any] 342 ``` 343 344 Serialize this component to a dictionary. 345 346 **Returns:** 347 348 - <code>dict\[str, Any\]</code> – The serialized component as a dictionary. 349 350 #### from_dict 351 352 ```python 353 from_dict(data: dict[str, Any]) -> AnthropicVertexChatGenerator 354 ``` 355 356 Deserialize this component from a dictionary. 357 358 **Parameters:** 359 360 - **data** (<code>dict\[str, Any\]</code>) – The dictionary representation of this component. 361 362 **Returns:** 363 364 - <code>AnthropicVertexChatGenerator</code> – The deserialized component instance. 365 366 ## haystack_integrations.components.generators.anthropic.generator 367 368 ### AnthropicGenerator 369 370 Enables text generation using Anthropic large language models (LLMs). It supports the Claude family of models. 371 372 Although Anthropic natively supports a much richer messaging API, we have intentionally simplified it in this 373 component so that the main input/output interface is string-based. 374 For more complete support, consider using the AnthropicChatGenerator. 375 376 ```python 377 from haystack_integrations.components.generators.anthropic import AnthropicGenerator 378 379 client = AnthropicGenerator(model="claude-sonnet-4-20250514") 380 response = client.run("What's Natural Language Processing? Be brief.") 381 print(response) 382 >>{'replies': ['Natural language processing (NLP) is a branch of artificial intelligence focused on enabling 383 >>computers to understand, interpret, and manipulate human language. The goal of NLP is to read, decipher, 384 >> understand, and make sense of the human languages in a manner that is valuable.'], 'meta': {'model': 385 >> 'claude-2.1', 'index': 0, 'finish_reason': 'end_turn', 'usage': {'input_tokens': 18, 'output_tokens': 58}}} 386 ``` 387 388 #### __init__ 389 390 ```python 391 __init__( 392 api_key: Secret = Secret.from_env_var("ANTHROPIC_API_KEY"), 393 model: str = "claude-sonnet-4-20250514", 394 streaming_callback: Callable[[StreamingChunk], None] | None = None, 395 system_prompt: str | None = None, 396 generation_kwargs: dict[str, Any] | None = None, 397 *, 398 timeout: float | None = None, 399 max_retries: int | None = None 400 ) 401 ``` 402 403 Initialize the AnthropicGenerator. 404 405 **Parameters:** 406 407 - **api_key** (<code>Secret</code>) – The Anthropic API key. 408 - **model** (<code>str</code>) – The name of the Anthropic model to use. 409 - **streaming_callback** (<code>Callable\\[[StreamingChunk\], None\] | None</code>) – An optional callback function to handle streaming chunks. 410 - **system_prompt** (<code>str | None</code>) – An optional system prompt to use for generation. 411 - **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Additional keyword arguments for generation. 412 413 #### to_dict 414 415 ```python 416 to_dict() -> dict[str, Any] 417 ``` 418 419 Serialize this component to a dictionary. 420 421 **Returns:** 422 423 - <code>dict\[str, Any\]</code> – The serialized component as a dictionary. 424 425 #### from_dict 426 427 ```python 428 from_dict(data: dict[str, Any]) -> AnthropicGenerator 429 ``` 430 431 Deserialize this component from a dictionary. 432 433 **Parameters:** 434 435 - **data** (<code>dict\[str, Any\]</code>) – The dictionary representation of this component. 436 437 **Returns:** 438 439 - <code>AnthropicGenerator</code> – The deserialized component instance. 440 441 #### run 442 443 ```python 444 run( 445 prompt: str, 446 generation_kwargs: dict[str, Any] | None = None, 447 streaming_callback: Callable[[StreamingChunk], None] | None = None, 448 ) -> dict[str, list[str] | list[dict[str, Any]]] 449 ``` 450 451 Generate replies using the Anthropic API. 452 453 **Parameters:** 454 455 - **prompt** (<code>str</code>) – The input prompt for generation. 456 - **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Additional keyword arguments for generation. 457 - **streaming_callback** (<code>Callable\\[[StreamingChunk\], None\] | None</code>) – An optional callback function to handle streaming chunks. 458 459 **Returns:** 460 461 - <code>dict\[str, list\[str\] | list\[dict\[str, Any\]\]\]</code> – A dictionary containing: 462 - `replies`: A list of generated replies. 463 - `meta`: A list of metadata dictionaries for each reply.