Skip to content

Commit

Permalink
[Anthropic] Shallow Copy (#27105)
Browse files Browse the repository at this point in the history
Co-authored-by: Erick Friis <erick@langchain.dev>
  • Loading branch information
hinthornw and efriis authored Oct 15, 2024
1 parent c653361 commit 0a3e089
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions libs/partners/anthropic/langchain_anthropic/chat_models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import re
import warnings
from operator import itemgetter
Expand Down Expand Up @@ -116,7 +117,6 @@ def _merge_messages(
"""Merge runs of human/tool messages into single human messages with content blocks.""" # noqa: E501
merged: list = []
for curr in messages:
curr = curr.model_copy(deep=True)
if isinstance(curr, ToolMessage):
if (
isinstance(curr.content, list)
Expand All @@ -143,12 +143,12 @@ def _merge_messages(
if isinstance(last.content, str):
new_content: List = [{"type": "text", "text": last.content}]
else:
new_content = last.content
new_content = copy.copy(last.content)
if isinstance(curr.content, str):
new_content.append({"type": "text", "text": curr.content})
else:
new_content.extend(curr.content)
last.content = new_content
merged[-1] = curr.model_copy(update={"content": new_content}, deep=False)
else:
merged.append(curr)
return merged
Expand Down Expand Up @@ -178,9 +178,11 @@ def _format_messages(
raise ValueError("System message must be at beginning of message list.")
if isinstance(message.content, list):
system = [
block
if isinstance(block, dict)
else {"type": "text", "text": "block"}
(
block
if isinstance(block, dict)
else {"type": "text", "text": "block"}
)
for block in message.content
]
else:
Expand Down

0 comments on commit 0a3e089

Please sign in to comment.