diff --git a/libs/partners/anthropic/langchain_anthropic/chat_models.py b/libs/partners/anthropic/langchain_anthropic/chat_models.py index e5deebd6b7..5b2c715029 100644 --- a/libs/partners/anthropic/langchain_anthropic/chat_models.py +++ b/libs/partners/anthropic/langchain_anthropic/chat_models.py @@ -95,11 +95,12 @@ def _format_image(image_url: str) -> Dict: def _merge_messages( - messages: List[BaseMessage], + messages: Sequence[BaseMessage], ) -> List[Union[SystemMessage, AIMessage, HumanMessage]]: """Merge runs of human/tool messages into single human messages with content blocks.""" # noqa: E501 merged: list = [] for curr in messages: + curr = curr.copy(deep=True) if isinstance(curr, ToolMessage): if isinstance(curr.content, str): curr = HumanMessage( diff --git a/libs/partners/anthropic/pyproject.toml b/libs/partners/anthropic/pyproject.toml index ba8a44ad54..6f15ec65cf 100644 --- a/libs/partners/anthropic/pyproject.toml +++ b/libs/partners/anthropic/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "langchain-anthropic" -version = "0.1.10" +version = "0.1.11" description = "An integration package connecting AnthropicMessages and LangChain" authors = [] readme = "README.md" diff --git a/libs/partners/anthropic/tests/unit_tests/test_chat_models.py b/libs/partners/anthropic/tests/unit_tests/test_chat_models.py index 9373b0a1ea..7b5c1624df 100644 --- a/libs/partners/anthropic/tests/unit_tests/test_chat_models.py +++ b/libs/partners/anthropic/tests/unit_tests/test_chat_models.py @@ -165,6 +165,25 @@ def test__merge_messages() -> None: assert expected == actual +def test__merge_messages_mutation() -> None: + original_messages = [ + HumanMessage([{"type": "text", "text": "bar"}]), + HumanMessage("next thing"), + ] + messages = [ + HumanMessage([{"type": "text", "text": "bar"}]), + HumanMessage("next thing"), + ] + expected = [ + HumanMessage( + [{"type": "text", "text": "bar"}, {"type": "text", "text": "next thing"}] + ), + ] + actual = _merge_messages(messages) + assert expected == actual + assert messages == original_messages + + @pytest.fixture() def pydantic() -> Type[BaseModel]: class dummy_function(BaseModel):