Skip to content

Commit

Permalink
Add function to convert to a dictionary
Browse files Browse the repository at this point in the history
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
  • Loading branch information
userlerueda committed Jun 22, 2024
1 parent 4998d0a commit 11bc0e0
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion libs/aws/langchain_aws/chat_models/bedrock_converse.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ def _anthropic_to_bedrock(
{
"toolUse": {
"toolUseId": block["id"],
"input": block["input"],
"input": _try_to_convert_to_dict(block["input"]),
"name": block["name"],
}
}
Expand Down Expand Up @@ -860,3 +860,13 @@ def _format_openai_image_url(image_url: str) -> Dict:
"format": match.group("media_type"),
"source": {"bytes": _b64str_to_bytes(match.group("data"))},
}


def _try_to_convert_to_dict(tool_use_input: Any) -> Any:
"""Attempt to convert the toolUse.input to a dictionary."""
if isinstance(tool_use_input, str):
try:
return json.loads(tool_use_input)
except json.JSONDecodeError:
return tool_use_input
return tool_use_input

0 comments on commit 11bc0e0

Please sign in to comment.