Skip to content

Commit 11bc0e0

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 4998d0a commit 11bc0e0

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
@@ -657,7 +657,7 @@ def _anthropic_to_bedrock(
657657
{
658658
"toolUse": {
659659
"toolUseId": block["id"],
660-
"input": block["input"],
660+
"input": _try_to_convert_to_dict(block["input"]),
661661
"name": block["name"],
662662
}
663663
}
@@ -860,3 +860,13 @@ def _format_openai_image_url(image_url: str) -> Dict:
860860
"format": match.group("media_type"),
861861
"source": {"bytes": _b64str_to_bytes(match.group("data"))},
862862
}
863+
864+
865+
def _try_to_convert_to_dict(tool_use_input: Any) -> Any:
866+
"""Attempt to convert the toolUse.input to a dictionary."""
867+
if isinstance(tool_use_input, str):
868+
try:
869+
return json.loads(tool_use_input)
870+
except json.JSONDecodeError:
871+
return tool_use_input
872+
return tool_use_input

0 commit comments

Comments
 (0)