Skip to content

Commit c1eef52

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 6398230 commit c1eef52

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

libs/aws/langchain_aws/chat_models/bedrock_converse.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ def _anthropic_to_bedrock(
650650
{
651651
"toolUse": {
652652
"toolUseId": block["id"],
653-
"input": block["input"],
653+
"input": _try_to_convert_to_dict(block["input"]),
654654
"name": block["name"],
655655
}
656656
}
@@ -833,3 +833,13 @@ def _upsert_tool_calls_to_bedrock_content(
833833
}
834834
)
835835
return content
836+
837+
838+
def _try_to_convert_to_dict(tool_use_input: Any) -> Any:
839+
"""Attempt to convert the toolUse.input to a dictionary."""
840+
if isinstance(tool_use_input, str):
841+
try:
842+
return json.loads(tool_use_input)
843+
except json.JSONDecodeError:
844+
return tool_use_input
845+
return tool_use_input

0 commit comments

Comments
 (0)