fix(bedrock): parse tool_call text tags

This commit is contained in:
milo 2026-05-22 21:28:51 +08:00
parent fcfa5f636e
commit 8b771ebe4e
2 changed files with 39 additions and 0 deletions

View file

@ -1815,6 +1815,24 @@ class AmazonConverseConfig(BaseConfig):
return None
def _parse_tool_call_tag_text_tool_call(
self, content: str
) -> Tuple[Optional[str], dict[str, Any]]:
tool_call_match = re.search(
r"<tool_call>(.*?)</tool_call>", content, re.DOTALL | re.IGNORECASE
)
if tool_call_match is None:
return None, {}
tool_call = self._parse_tool_call_json_arguments(tool_call_match.group(1).strip())
tool_name = tool_call.get("name")
arguments = tool_call.get("arguments", {})
if not isinstance(tool_name, str):
return None, {}
if not isinstance(arguments, dict):
return None, {}
return tool_name, arguments
def _parse_function_text_tool_call(
self, content: str
) -> Tuple[Optional[str], dict[str, Any]]:
@ -1895,6 +1913,7 @@ class AmazonConverseConfig(BaseConfig):
return None
parsers = (
self._parse_tool_call_tag_text_tool_call,
self._parse_function_text_tool_call,
self._parse_tool_use_text_tool_call,
self._parse_bare_text_tool_call,

View file

@ -305,6 +305,26 @@ def test_apply_tool_call_transformation_parses_bare_tool_name_json_text():
_assert_read_file_tool_call(transformed_message, finish_reason)
def test_apply_tool_call_transformation_parses_tool_call_json_tag_text():
from litellm.types.utils import Message
config = AmazonConverseConfig()
message = Message(
role="assistant",
content=(
"I'll call the read_file tool immediately as instructed.\n\n"
'<tool_call>\n{"name": "read_file", "arguments": {"path": "C:\\\\Projects\\\\redaigo\\\\scripts\\\\run_etf_v13.py", "offset": 0, "length": 3000}}\n</tool_call>\n\n'
"<tool_response>"
),
)
transformed_message, finish_reason = config.apply_tool_call_transformation_if_needed(
message, _read_file_tool(), initial_finish_reason="stop"
)
_assert_read_file_tool_call(transformed_message, finish_reason)
def test_apply_tool_call_transformation_ignores_text_for_unknown_tool_name():
from litellm.types.utils import Message