1
1
import json
2
2
import re
3
- import warnings
4
3
from collections import defaultdict
5
4
from operator import itemgetter
6
5
from typing import (
@@ -215,7 +214,12 @@ def _merge_messages(
215
214
for curr in messages :
216
215
curr = curr .copy (deep = True )
217
216
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 :
219
223
curr = HumanMessage ( # type: ignore[misc]
220
224
[
221
225
{
@@ -225,8 +229,6 @@ def _merge_messages(
225
229
}
226
230
]
227
231
)
228
- else :
229
- curr = HumanMessage (curr .content ) # type: ignore[misc]
230
232
last = merged [- 1 ] if merged else None
231
233
if isinstance (last , HumanMessage ) and isinstance (curr , HumanMessage ):
232
234
if isinstance (last .content , str ):
@@ -373,7 +375,12 @@ def format_messages(
373
375
)
374
376
375
377
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
+ }
377
384
378
385
379
386
class ChatBedrock (BaseChatModel , BedrockBase ):
@@ -739,8 +746,8 @@ class AnswerWithJustification(BaseModel):
739
746
740
747
""" # noqa: E501
741
748
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 () } "
744
751
)
745
752
llm = self .bind_tools ([schema ], tool_choice = "any" )
746
753
if isinstance (schema , type ) and issubclass (schema , BaseModel ):
0 commit comments