Skip to content

Commit

Permalink
anthropic[patch]: fix msg mutation (#20572)
Browse files Browse the repository at this point in the history
  • Loading branch information
baskaryan authored Apr 17, 2024
1 parent 719da87 commit 54e9271
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
3 changes: 2 additions & 1 deletion libs/partners/anthropic/langchain_anthropic/chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion libs/partners/anthropic/pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
19 changes: 19 additions & 0 deletions libs/partners/anthropic/tests/unit_tests/test_chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 54e9271

Please sign in to comment.