The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
fix(anthropic): handle empty AIMessage (#33390)
ccurme11a2efe49b3f
diff --git a/libs/partners/anthropic/langchain_anthropic/chat_models.py b/libs/partners/anthropic/langchain_anthropic/chat_models.pyindex 31bf2ae9f9..8335b39d24 100644--- a/libs/partners/anthropic/langchain_anthropic/chat_models.py+++ b/libs/partners/anthropic/langchain_anthropic/chat_models.py@@ -549,6 +549,10 @@ def _format_messages(_lc_tool_calls_to_anthropic_tool_use_blocks(missing_tool_calls),)+ if not content and role == "assistant" and _i < len(merged_messages) - 1:+ # anthropic.BadRequestError: Error code: 400: all messages must have+ # non-empty content except for the optional final assistant message+ continueformatted_messages.append({"role": role, "content": content})return system, formatted_messagesdiff --git a/libs/partners/anthropic/tests/integration_tests/test_chat_models.py b/libs/partners/anthropic/tests/integration_tests/test_chat_models.pyindex a8eb60cb40..fc2f9f3cf8 100644--- a/libs/partners/anthropic/tests/integration_tests/test_chat_models.py+++ b/libs/partners/anthropic/tests/integration_tests/test_chat_models.py@@ -278,6 +278,53 @@ def test_system_invoke() -> None:assert isinstance(result.content, str)+def test_handle_empty_aimessage() -> None:+ # Anthropic can generate empty AIMessages, which are not valid unless in the last+ # message in a sequence.+ llm = ChatAnthropic(model=MODEL_NAME)+ messages = [+ HumanMessage("Hello"),+ AIMessage([]),+ HumanMessage("My name is Bob."),+ ]+ _ = llm.invoke(messages)++ # Test tool call sequence+ llm_with_tools = llm.bind_tools(+ [+ {+ "name": "get_weather",+ "description": "Get weather report for a city",+ "input_schema": {+ "type": "object",+ "properties": {"location": {"type": "string"}},+ },+ },+ ],+ )+ _ = llm_with_tools.invoke(+ [+ HumanMessage("What's the weather in Boston?"),+ AIMessage(+ content=[],+ tool_calls=[+ {+ "name": "get_weather",+ "args": {"location": "Boston"},+ "id": "toolu_01V6d6W32QGGSmQm4BT98EKk",+ "type": "tool_call",+ },+ ],+ ),+ ToolMessage(+ content="It's sunny.", tool_call_id="toolu_01V6d6W32QGGSmQm4BT98EKk"+ ),+ AIMessage([]),+ HumanMessage("Thanks!"),+ ]+ )++def test_anthropic_call() -> None:"""Test valid call to anthropic."""chat = ChatAnthropic(model=MODEL_NAME) # type: ignore[call-arg]diff --git a/libs/partners/anthropic/tests/unit_tests/test_chat_models.py b/libs/partners/anthropic/tests/unit_tests/test_chat_models.pyindex df55402ed5..7aabfe860b 100644--- a/libs/partners/anthropic/tests/unit_tests/test_chat_models.py+++ b/libs/partners/anthropic/tests/unit_tests/test_chat_models.py@@ -642,6 +642,38 @@ def test__format_messages_with_tool_calls() -> None:actual = _format_messages(messages)assert expected == actual+ # Check handling of empty AIMessage+ empty_contents: list[str | list[str | dict]] = ["", []]+ for empty_content in empty_contents:+ ## Permit message in final position+ _, anthropic_messages = _format_messages([human, AIMessage(empty_content)])+ expected_messages = [+ {"role": "user", "content": "foo"},+ {"role": "assistant", "content": empty_content},+ ]+ assert expected_messages == anthropic_messages++ ## Remove message otherwise+ _, anthropic_messages = _format_messages(+ [human, AIMessage(empty_content), human]+ )+ expected_messages = [+ {"role": "user", "content": "foo"},+ {"role": "user", "content": "foo"},+ ]+ assert expected_messages == anthropic_messages++ actual = _format_messages(+ [system, human, ai, tool, AIMessage(empty_content), human]+ )+ assert actual[0] == "fuzz"+ assert [message["role"] for message in actual[1]] == [+ "user",+ "assistant",+ "user",+ "user",+ ]+def test__format_tool_use_block() -> None:# Test we correctly format tool_use blocks when there is no corresponding tool_call.