11from datetime import datetime , timezone
2- from typing import Any , ClassVar , Optional , Union
2+ from typing import Any , ClassVar
33
44from haystack import component , default_from_dict , default_to_dict , logging
55from haystack .components .generators .utils import _convert_streaming_chunks_to_chat_message
@@ -113,13 +113,13 @@ def __init__(
113113 self ,
114114 api_key : Secret = Secret .from_env_var ("ANTHROPIC_API_KEY" ), # noqa: B008
115115 model : str = "claude-sonnet-4-5" ,
116- streaming_callback : Optional [ StreamingCallbackT ] = None ,
117- generation_kwargs : Optional [ dict [str , Any ]] = None ,
116+ streaming_callback : StreamingCallbackT | None = None ,
117+ generation_kwargs : dict [str , Any ] | None = None ,
118118 ignore_tools_thinking_messages : bool = True ,
119- tools : Optional [ ToolsType ] = None ,
119+ tools : ToolsType | None = None ,
120120 * ,
121- timeout : Optional [ float ] = None ,
122- max_retries : Optional [ int ] = None ,
121+ timeout : float | None = None ,
122+ max_retries : int | None = None ,
123123 ):
124124 """
125125 Creates an instance of AnthropicChatGenerator.
@@ -229,8 +229,8 @@ def from_dict(cls, data: dict[str, Any]) -> "AnthropicChatGenerator":
229229 def _prepare_request_params (
230230 self ,
231231 messages : list [ChatMessage ],
232- generation_kwargs : Optional [ dict [str , Any ]] = None ,
233- tools : Optional [ ToolsType ] = None ,
232+ generation_kwargs : dict [str , Any ] | None = None ,
233+ tools : ToolsType | None = None ,
234234 ) -> tuple [list [TextBlockParam ], list [MessageParam ], dict [str , Any ], list [ToolParam ]]:
235235 """
236236 Prepare the parameters for the Anthropic API request.
@@ -277,8 +277,8 @@ def _prepare_request_params(
277277
278278 def _process_response (
279279 self ,
280- response : Union [ Message , Stream [RawMessageStreamEvent ] ],
281- streaming_callback : Optional [ SyncStreamingCallbackT ] = None ,
280+ response : Message | Stream [RawMessageStreamEvent ],
281+ streaming_callback : SyncStreamingCallbackT | None = None ,
282282 ) -> dict [str , list [ChatMessage ]]:
283283 """
284284 Process the response from the Anthropic API.
@@ -291,7 +291,7 @@ def _process_response(
291291 # we cannot use isinstance(Stream)
292292 if not isinstance (response , Message ):
293293 chunks : list [StreamingChunk ] = []
294- model : Optional [ str ] = None
294+ model : str | None = None
295295 tool_call_index = - 1
296296 input_tokens = None
297297 component_info = ComponentInfo .from_component (self )
@@ -345,7 +345,7 @@ def _process_response(
345345 async def _process_response_async (
346346 self ,
347347 response : Any ,
348- streaming_callback : Optional [ AsyncStreamingCallbackT ] = None ,
348+ streaming_callback : AsyncStreamingCallbackT | None = None ,
349349 ) -> dict [str , list [ChatMessage ]]:
350350 """
351351 Process the response from the Anthropic API asynchronously.
@@ -359,7 +359,7 @@ async def _process_response_async(
359359 # workaround for https://github.com/DataDog/dd-trace-py/issues/12562
360360 if not isinstance (response , Message ):
361361 chunks : list [StreamingChunk ] = []
362- model : Optional [ str ] = None
362+ model : str | None = None
363363 tool_call_index = - 1
364364 input_tokens = None
365365 component_info = ComponentInfo .from_component (self )
@@ -418,9 +418,9 @@ async def _process_response_async(
418418 def run (
419419 self ,
420420 messages : list [ChatMessage ],
421- streaming_callback : Optional [ StreamingCallbackT ] = None ,
422- generation_kwargs : Optional [ dict [str , Any ]] = None ,
423- tools : Optional [ ToolsType ] = None ,
421+ streaming_callback : StreamingCallbackT | None = None ,
422+ generation_kwargs : dict [str , Any ] | None = None ,
423+ tools : ToolsType | None = None ,
424424 ) -> dict [str , list [ChatMessage ]]:
425425 """
426426 Invokes the Anthropic API with the given messages and generation kwargs.
@@ -460,9 +460,9 @@ def run(
460460 async def run_async (
461461 self ,
462462 messages : list [ChatMessage ],
463- streaming_callback : Optional [ StreamingCallbackT ] = None ,
464- generation_kwargs : Optional [ dict [str , Any ]] = None ,
465- tools : Optional [ ToolsType ] = None ,
463+ streaming_callback : StreamingCallbackT | None = None ,
464+ generation_kwargs : dict [str , Any ] | None = None ,
465+ tools : ToolsType | None = None ,
466466 ) -> dict [str , list [ChatMessage ]]:
467467 """
468468 Async version of the run method. Invokes the Anthropic API with the given messages and generation kwargs.
0 commit comments