send error finish reason on failing to parse a tool call (#1785)
a simplification of #1757 which is now stale
This commit is contained in:
@@ -740,12 +740,9 @@ def _parse_kimi_tool_calls(text: str):
|
||||
if func_args_match is None:
|
||||
raise ValueError("No tool call arguments found.")
|
||||
func_args = func_args_match.group(1)
|
||||
try:
|
||||
arg_dct = json.loads(func_args) # pyright: ignore[reportAny]
|
||||
except Exception:
|
||||
arg_dct = None
|
||||
arg_dct = json.loads(func_args) # pyright: ignore[reportAny]
|
||||
|
||||
return dict(id=tool_call_id, name=func_name, arguments=arg_dct)
|
||||
return dict(id=tool_call_id, name=func_name, arguments=arg_dct) # pyright: ignore[reportAny]
|
||||
|
||||
tool_matches = _tool_call_split_regex.findall(text)
|
||||
if tool_matches:
|
||||
|
||||
@@ -358,8 +358,10 @@ def parse_tool_calls(
|
||||
|
||||
if parsed is None:
|
||||
logger.warning(f"tool call parsing failed for text {combined}")
|
||||
yield response.model_copy(update={"text": combined})
|
||||
continue
|
||||
yield response.model_copy(
|
||||
update={"text": combined, "token": 0, "finish_reason": "error"}
|
||||
)
|
||||
break
|
||||
|
||||
yield ToolCallResponse(
|
||||
tool_calls=parsed, usage=response.usage, stats=response.stats
|
||||
@@ -374,6 +376,7 @@ def parse_tool_calls(
|
||||
update={
|
||||
"text": "".join(tool_call_text_parts),
|
||||
"token": 0,
|
||||
"finish_reason": "error",
|
||||
}
|
||||
)
|
||||
yield response
|
||||
|
||||
@@ -87,6 +87,7 @@ class TestParseToolCalls:
|
||||
assert len(results) == 1
|
||||
assert isinstance(results[0], GenerationResponse)
|
||||
assert results[0].text == "<tool_call>bad content</tool_call>"
|
||||
assert results[0].finish_reason == "error"
|
||||
|
||||
def test_tool_schema_coerces_string_arguments_to_expected_types(self):
|
||||
"""Tool argument values should be coerced using provided JSON schema."""
|
||||
|
||||
Reference in New Issue
Block a user