Skip to content

Commit 73edd60

Browse files
committed
Add function to convert to a dictionary
Anthropic returns the input dictionary for the tool as a string. Adding a function to attempt to convert it to a dict if it is a string, if conversion fails, returns the string
1 parent 34bf74a commit 73edd60

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

libs/aws/langchain_aws/chat_models/bedrock_converse.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ def _anthropic_to_bedrock(
411411
{
412412
"toolUse": {
413413
"toolUseId": block["id"],
414-
"input": block["input"],
414+
"input": _try_to_convert_to_dict(block["input"]),
415415
"name": block["name"],
416416
}
417417
}
@@ -550,3 +550,12 @@ def _drop_none(obj: Any) -> Any:
550550
return new or None
551551
else:
552552
return obj
553+
554+
def _try_to_convert_to_dict(tool_use_input: Any) -> Any:
555+
"""Attempt to convert the toolUse.input to a dictionary."""
556+
if isinstance(tool_use_input, str):
557+
try:
558+
return json.loads(tool_use_input)
559+
except json.JSONDecodeError:
560+
return tool_use_input
561+
return tool_use_input

0 commit comments

Comments
 (0)