Skip to content

Commit f829790

Browse files
committed
fixes key error and errors on not claude-3
1 parent 63a7338 commit f829790

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

libs/aws/langchain_aws/chat_models/bedrock.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import json
22
import re
3-
import warnings
43
from collections import defaultdict
54
from operator import itemgetter
65
from typing import (
@@ -215,7 +214,12 @@ def _merge_messages(
215214
for curr in messages:
216215
curr = curr.copy(deep=True)
217216
if isinstance(curr, ToolMessage):
218-
if isinstance(curr.content, str):
217+
if isinstance(curr.content, list) and all(
218+
isinstance(block, dict) and block.get("type") == "tool_result"
219+
for block in curr.content
220+
):
221+
curr = HumanMessage(curr.content) # type: ignore[misc]
222+
else:
219223
curr = HumanMessage( # type: ignore[misc]
220224
[
221225
{
@@ -225,8 +229,6 @@ def _merge_messages(
225229
}
226230
]
227231
)
228-
else:
229-
curr = HumanMessage(curr.content) # type: ignore[misc]
230232
last = merged[-1] if merged else None
231233
if isinstance(last, HumanMessage) and isinstance(curr, HumanMessage):
232234
if isinstance(last.content, str):
@@ -373,7 +375,12 @@ def format_messages(
373375
)
374376

375377

376-
_message_type_lookups = {"human": "user", "ai": "assistant"}
378+
_message_type_lookups = {
379+
"human": "user",
380+
"ai": "assistant",
381+
"AIMessageChunk": "assistant",
382+
"HumanMessageChunk": "user",
383+
}
377384

378385

379386
class ChatBedrock(BaseChatModel, BedrockBase):
@@ -739,8 +746,8 @@ class AnswerWithJustification(BaseModel):
739746
740747
""" # noqa: E501
741748
if "claude-3" not in self._get_model():
742-
warnings.warn(
743-
"Structured output is only supported for claude-3 models on Bedrock."
749+
ValueError(
750+
f"Structured output is not supported for model {self._get_model()}"
744751
)
745752
llm = self.bind_tools([schema], tool_choice="any")
746753
if isinstance(schema, type) and issubclass(schema, BaseModel):

0 commit comments

Comments
 (0)