Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class AnthropicChatGenerator:
from haystack_integrations.components.generators.anthropic import AnthropicChatGenerator
from haystack.dataclasses import ChatMessage

generator = AnthropicChatGenerator(model="claude-3-5-sonnet-20240620",
generator = AnthropicChatGenerator(model="claude-sonnet-4-20250514",
generation_kwargs={
"max_tokens": 1000,
"temperature": 0.7,
Expand Down Expand Up @@ -190,7 +190,7 @@ class AnthropicChatGenerator:
def __init__(
self,
api_key: Secret = Secret.from_env_var("ANTHROPIC_API_KEY"), # noqa: B008
model: str = "claude-3-5-sonnet-20240620",
model: str = "claude-sonnet-4-20250514",
streaming_callback: Optional[StreamingCallbackT] = None,
generation_kwargs: Optional[Dict[str, Any]] = None,
ignore_tools_thinking_messages: bool = True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class AnthropicVertexChatGenerator(AnthropicChatGenerator):

messages = [ChatMessage.from_user("What's Natural Language Processing?")]
client = AnthropicVertexChatGenerator(
model="claude-3-sonnet@20240229",
model="claude-sonnet-4@20250514",
project_id="your-project-id", region="your-region"
)
response = client.run(messages)
Expand All @@ -50,7 +50,7 @@ class AnthropicVertexChatGenerator(AnthropicChatGenerator):
>> focuses on enabling computers to understand, interpret, and generate human language. It involves developing
>> techniques and algorithms to analyze and process text or speech data, allowing machines to comprehend and
>> communicate in natural languages like English, Spanish, or Chinese.")],
>> _name=None, _meta={'model': 'claude-3-sonnet@20240229', 'index': 0, 'finish_reason': 'end_turn',
>> _name=None, _meta={'model': 'claude-sonnet-4@20250514', 'index': 0, 'finish_reason': 'end_turn',
>> 'usage': {'input_tokens': 15, 'output_tokens': 64}})]}
```

Expand All @@ -63,7 +63,7 @@ def __init__(
self,
region: str,
project_id: str,
model: str = "claude-3-5-sonnet@20240620",
model: str = "claude-sonnet-4@20250514",
streaming_callback: Optional[Callable[[StreamingChunk], None]] = None,
generation_kwargs: Optional[Dict[str, Any]] = None,
ignore_tools_thinking_messages: bool = True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class AnthropicGenerator:
```python
from haystack_integrations.components.generators.anthropic import AnthropicGenerator

client = AnthropicGenerator(model="claude-3-sonnet-20240229")
client = AnthropicGenerator(model="claude-sonnet-4-20250514")
response = client.run("What's Natural Language Processing? Be brief.")
print(response)
>>{'replies': ['Natural language processing (NLP) is a branch of artificial intelligence focused on enabling
Expand All @@ -60,7 +60,7 @@ class AnthropicGenerator:
def __init__(
self,
api_key: Secret = Secret.from_env_var("ANTHROPIC_API_KEY"), # noqa: B008
model: str = "claude-3-sonnet-20240229",
model: str = "claude-sonnet-4-20250514",
streaming_callback: Optional[Callable[[StreamingChunk], None]] = None,
system_prompt: Optional[str] = None,
generation_kwargs: Optional[Dict[str, Any]] = None,
Expand Down
4 changes: 2 additions & 2 deletions integrations/anthropic/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def mock_chat_completion():
completion = Message(
id="foo",
content=[{"type": "text", "text": "Hello, world!"}],
model="claude-3-sonnet-20240229",
model="claude-sonnet-4-20250514",
role="assistant",
type="message",
usage={"input_tokens": 57, "output_tokens": 40},
Expand All @@ -35,7 +35,7 @@ def mock_chat_completion_extended_thinking():
{"type": "thinking", "thinking": "This is a thinking part!", "signature": ""},
{"type": "text", "text": "Hello, world!"},
],
model="claude-3-sonnet-20240229",
model="claude-sonnet-4-20250514",
role="assistant",
type="message",
usage={"input_tokens": 57, "output_tokens": 40},
Expand Down
64 changes: 32 additions & 32 deletions integrations/anthropic/tests/test_chat_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def mock_anthropic_completion():
completion = Message(
id="foo",
type="message",
model="claude-3-5-sonnet-20240620",
model="claude-sonnet-4-20250514",
role="assistant",
content=[TextBlockParam(type="text", text="Hello! I'm Claude.")],
stop_reason="end_turn",
Expand All @@ -88,7 +88,7 @@ def test_init_default(self, monkeypatch):
monkeypatch.setenv("ANTHROPIC_API_KEY", "test-api-key")
component = AnthropicChatGenerator()
assert component.client.api_key == "test-api-key"
assert component.model == "claude-3-5-sonnet-20240620"
assert component.model == "claude-sonnet-4-20250514"
assert component.streaming_callback is None
assert not component.generation_kwargs
assert component.tools is None
Expand Down Expand Up @@ -121,13 +121,13 @@ def test_init_with_parameters(self, monkeypatch):
monkeypatch.setenv("OPENAI_MAX_RETRIES", "10")
component = AnthropicChatGenerator(
api_key=Secret.from_token("test-api-key"),
model="claude-3-5-sonnet-20240620",
model="claude-sonnet-4-20250514",
streaming_callback=print_streaming_chunk,
generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"},
tools=[tool],
)
assert component.client.api_key == "test-api-key"
assert component.model == "claude-3-5-sonnet-20240620"
assert component.model == "claude-sonnet-4-20250514"
assert component.streaming_callback is print_streaming_chunk
assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"}
assert component.tools == [tool]
Expand All @@ -139,13 +139,13 @@ def test_init_with_parameters_and_env_vars(self, monkeypatch):
monkeypatch.setenv("OPENAI_TIMEOUT", "100")
monkeypatch.setenv("OPENAI_MAX_RETRIES", "10")
component = AnthropicChatGenerator(
model="claude-3-5-sonnet-20240620",
model="claude-sonnet-4-20250514",
api_key=Secret.from_token("test-api-key"),
streaming_callback=print_streaming_chunk,
generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"},
)
assert component.client.api_key == "test-api-key"
assert component.model == "claude-3-5-sonnet-20240620"
assert component.model == "claude-sonnet-4-20250514"
assert component.streaming_callback is print_streaming_chunk
assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"}

Expand All @@ -160,7 +160,7 @@ def test_to_dict_default(self, monkeypatch):
"type": "haystack_integrations.components.generators.anthropic.chat.chat_generator.AnthropicChatGenerator",
"init_parameters": {
"api_key": {"env_vars": ["ANTHROPIC_API_KEY"], "type": "env_var", "strict": True},
"model": "claude-3-5-sonnet-20240620",
"model": "claude-sonnet-4-20250514",
"streaming_callback": None,
"ignore_tools_thinking_messages": True,
"generation_kwargs": {},
Expand All @@ -179,7 +179,7 @@ def test_to_dict_with_parameters(self, monkeypatch):
monkeypatch.setenv("ENV_VAR", "test-api-key")
component = AnthropicChatGenerator(
api_key=Secret.from_env_var("ENV_VAR"),
model="claude-3-5-sonnet-20240620",
model="claude-sonnet-4-20250514",
streaming_callback=print_streaming_chunk,
generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"},
tools=[tool],
Expand All @@ -192,7 +192,7 @@ def test_to_dict_with_parameters(self, monkeypatch):
"type": "haystack_integrations.components.generators.anthropic.chat.chat_generator.AnthropicChatGenerator",
"init_parameters": {
"api_key": {"env_vars": ["ENV_VAR"], "type": "env_var", "strict": True},
"model": "claude-3-5-sonnet-20240620",
"model": "claude-sonnet-4-20250514",
"streaming_callback": "haystack.components.generators.utils.print_streaming_chunk",
"ignore_tools_thinking_messages": True,
"generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"},
Expand Down Expand Up @@ -236,7 +236,7 @@ def test_from_dict(self, monkeypatch):
"type": "haystack_integrations.components.generators.anthropic.chat.chat_generator.AnthropicChatGenerator",
"init_parameters": {
"api_key": {"env_vars": ["ANTHROPIC_API_KEY"], "type": "env_var", "strict": True},
"model": "claude-3-5-sonnet-20240620",
"model": "claude-sonnet-4-20250514",
"streaming_callback": "haystack.components.generators.utils.print_streaming_chunk",
"generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"},
"tools": [
Expand All @@ -259,7 +259,7 @@ def test_from_dict(self, monkeypatch):
component = AnthropicChatGenerator.from_dict(data)

assert isinstance(component, AnthropicChatGenerator)
assert component.model == "claude-3-5-sonnet-20240620"
assert component.model == "claude-sonnet-4-20250514"
assert component.streaming_callback is print_streaming_chunk
assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"}
assert component.api_key == Secret.from_env_var("ANTHROPIC_API_KEY")
Expand All @@ -276,7 +276,7 @@ def test_from_dict_fail_wo_env_var(self, monkeypatch):
"type": "haystack_integrations.components.generators.anthropic.chat.chat_generator.AnthropicChatGenerator",
"init_parameters": {
"api_key": {"env_vars": ["ANTHROPIC_API_KEY"], "type": "env_var", "strict": True},
"model": "claude-3-5-sonnet-20240620",
"model": "claude-sonnet-4-20250514",
"streaming_callback": "haystack.components.generators.utils.print_streaming_chunk",
"generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"},
},
Expand Down Expand Up @@ -316,7 +316,7 @@ def test_run_with_params(self, chat_messages, mock_anthropic_completion):
assert len(response["replies"]) == 1
assert isinstance(response["replies"][0], ChatMessage)
assert "Hello! I'm Claude." in response["replies"][0].text
assert response["replies"][0].meta["model"] == "claude-3-5-sonnet-20240620"
assert response["replies"][0].meta["model"] == "claude-sonnet-4-20250514"
assert response["replies"][0].meta["finish_reason"] == "end_turn"

def test_check_duplicate_tool_names(self, tools):
Expand Down Expand Up @@ -350,7 +350,7 @@ def test_convert_anthropic_chunk_to_streaming_chunk(self):
"type": "message",
"role": "assistant",
"content": [],
"model": "claude-3-5-sonnet-20240620",
"model": "claude-sonnet-4-20250514",
"stop_reason": None,
"stop_sequence": None,
"usage": {"input_tokens": 25, "output_tokens": 1},
Expand All @@ -371,7 +371,7 @@ def test_convert_anthropic_chunk_to_streaming_chunk(self):
"type": "message",
"role": "assistant",
"content": [],
"model": "claude-3-5-sonnet-20240620",
"model": "claude-sonnet-4-20250514",
"stop_reason": None,
"stop_sequence": None,
"usage": {
Expand Down Expand Up @@ -413,7 +413,7 @@ def test_convert_streaming_chunks_to_chat_message(self):
"type": "message",
"role": "assistant",
"content": [],
"model": "claude-3-sonnet",
"model": "claude-sonnet-4-20250514",
"stop_reason": None,
"stop_sequence": None,
"usage": {"input_tokens": 25, "output_tokens": 0},
Expand Down Expand Up @@ -480,7 +480,7 @@ def test_convert_streaming_chunks_to_chat_message(self):
]

component = AnthropicChatGenerator(api_key=Secret.from_token("test-api-key"))
message = component._convert_streaming_chunks_to_chat_message(chunks, model="claude-3-sonnet")
message = component._convert_streaming_chunks_to_chat_message(chunks, model="claude-sonnet-4-20250514")

# Verify the message content
assert message.text == "Let me check the weather"
Expand All @@ -493,7 +493,7 @@ def test_convert_streaming_chunks_to_chat_message(self):
assert tool_call.arguments == {"city": "Paris"}

# Verify meta information
assert message._meta["model"] == "claude-3-sonnet"
assert message._meta["model"] == "claude-sonnet-4-20250514"
assert message._meta["index"] == 0
assert message._meta["finish_reason"] == "tool_use"
assert message._meta["usage"] == {"prompt_tokens": 25, "completion_tokens": 40}
Expand Down Expand Up @@ -558,7 +558,7 @@ def test_convert_streaming_chunks_to_chat_message_malformed_json(self, caplog):
]

component = AnthropicChatGenerator(api_key=Secret.from_token("test-api-key"))
message = component._convert_streaming_chunks_to_chat_message(chunks, model="claude-3-sonnet")
message = component._convert_streaming_chunks_to_chat_message(chunks, model="claude-sonnet-4-20250514")

# Verify the message content is preserve
assert message.text == "Let me check the weather"
Expand All @@ -585,7 +585,7 @@ def test_convert_streaming_chunks_to_chat_message_tool_call_with_empty_arguments
"type": "message",
"role": "assistant",
"content": [],
"model": "claude-3-sonnet",
"model": "claude-sonnet-4-20250514",
"stop_reason": None,
"stop_sequence": None,
"usage": {"input_tokens": 50, "output_tokens": 0},
Expand Down Expand Up @@ -670,7 +670,7 @@ def test_convert_streaming_chunks_to_chat_message_tool_call_with_empty_arguments
]

component = AnthropicChatGenerator(api_key=Secret.from_token("test-api-key"))
message = component._convert_streaming_chunks_to_chat_message(chunks, model="claude-3-sonnet")
message = component._convert_streaming_chunks_to_chat_message(chunks, model="claude-sonnet-4-20250514")

# Verify the message content
assert message.text == (
Expand All @@ -686,7 +686,7 @@ def test_convert_streaming_chunks_to_chat_message_tool_call_with_empty_arguments
assert tool_call.arguments == {}

# Verify meta information
assert message._meta["model"] == "claude-3-sonnet"
assert message._meta["model"] == "claude-sonnet-4-20250514"
assert message._meta["index"] == 0
assert message._meta["finish_reason"] == "tool_use"
assert message._meta["usage"] == {
Expand All @@ -702,7 +702,7 @@ def test_serde_in_pipeline(self):

generator = AnthropicChatGenerator(
api_key=Secret.from_env_var("ANTHROPIC_API_KEY", strict=False),
model="claude-3-5-sonnet-20240620",
model="claude-sonnet-4-20250514",
generation_kwargs={"temperature": 0.6},
tools=[tool],
)
Expand All @@ -722,7 +722,7 @@ def test_serde_in_pipeline(self):
"type": type_,
"init_parameters": {
"api_key": {"type": "env_var", "env_vars": ["ANTHROPIC_API_KEY"], "strict": False},
"model": "claude-3-5-sonnet-20240620",
"model": "claude-sonnet-4-20250514",
"generation_kwargs": {"temperature": 0.6},
"ignore_tools_thinking_messages": True,
"streaming_callback": None,
Expand Down Expand Up @@ -784,7 +784,7 @@ def test_live_run(self):
assert len(results["replies"]) == 1
message: ChatMessage = results["replies"][0]
assert "Paris" in message.text
assert "claude-3-5-sonnet-20240620" in message.meta["model"]
assert "claude-sonnet-4-20250514" in message.meta["model"]
assert message.meta["finish_reason"] == "end_turn"

@pytest.mark.skipif(
Expand Down Expand Up @@ -824,7 +824,7 @@ def __call__(self, chunk: StreamingChunk) -> None:
message: ChatMessage = results["replies"][0]
assert "Paris" in message.text

assert "claude-3-5-sonnet-20240620" in message.meta["model"]
assert "claude-sonnet-4-20250514" in message.meta["model"]
assert message.meta["finish_reason"] == "end_turn"

assert callback.counter > 1
Expand Down Expand Up @@ -1317,7 +1317,7 @@ def test_from_dict_with_prompt_caching(self, monkeypatch):
"type": "haystack_integrations.components.generators.anthropic.chat.chat_generator.AnthropicChatGenerator",
"init_parameters": {
"api_key": {"env_vars": ["ANTHROPIC_API_KEY"], "strict": True, "type": "env_var"},
"model": "claude-3-5-sonnet-20240620",
"model": "claude-sonnet-4-20250514",
"generation_kwargs": {"extra_headers": {"anthropic-beta": "prompt-caching-2024-07-31"}},
},
}
Expand Down Expand Up @@ -1364,7 +1364,7 @@ async def mock_anthropic_completion_async(self):
completion = Message(
id="foo",
type="message",
model="claude-3-5-sonnet-20240620",
model="claude-sonnet-4-20250514",
role="assistant",
content=[TextBlockParam(type="text", text="Hello! I'm Claude.")],
stop_reason="end_turn",
Expand All @@ -1380,7 +1380,7 @@ async def mock_anthropic_completion_async_with_tool(self):
completion = Message(
id="foo",
type="message",
model="claude-3-5-sonnet-20240620",
model="claude-sonnet-4-20250514",
role="assistant",
content=[
TextBlockParam(type="text", text="Let me check the weather for you."),
Expand Down Expand Up @@ -1431,7 +1431,7 @@ async def test_run_async_with_params(self, chat_messages, mock_anthropic_complet
assert len(response["replies"]) == 1
assert isinstance(response["replies"][0], ChatMessage)
assert "Hello! I'm Claude." in response["replies"][0].text
assert response["replies"][0].meta["model"] == "claude-3-5-sonnet-20240620"
assert response["replies"][0].meta["model"] == "claude-sonnet-4-20250514"
assert response["replies"][0].meta["finish_reason"] == "end_turn"

@pytest.mark.asyncio
Expand All @@ -1449,7 +1449,7 @@ async def test_live_run_async(self):
assert len(results["replies"]) == 1
message: ChatMessage = results["replies"][0]
assert "Paris" in message.text
assert "claude-3-5-sonnet-20240620" in message.meta["model"]
assert "claude-sonnet-4-20250514" in message.meta["model"]
assert message.meta["finish_reason"] == "end_turn"

@pytest.mark.asyncio
Expand Down Expand Up @@ -1482,7 +1482,7 @@ async def callback(chunk: StreamingChunk) -> None:
assert len(results["replies"]) == 1
message = results["replies"][0]
assert "paris" in message.text.lower()
assert "claude-3-5-sonnet-20240620" in message.meta["model"]
assert "claude-sonnet-4-20250514" in message.meta["model"]
assert message.meta["finish_reason"] == "end_turn"

# Verify streaming behavior
Expand Down
Loading
Loading