core[patch]: raise error if a tool call has an empty tool call ID

This commit is contained in:
vbarda 2025-02-20 21:58:57 -05:00
parent 1fa9f6bc20
commit c1888bd9d8
2 changed files with 26 additions and 0 deletions

View File

@ -934,6 +934,13 @@ def _prep_run_args(
config = ensure_config(config)
if _is_tool_call(input):
tool_call_id: Optional[str] = cast(ToolCall, input)["id"]
if not tool_call_id:
msg = (
"Tool call ID must be a non-empty string. "
f"Got '{tool_call_id}' for tool call '{input}'."
)
raise ValueError(msg)
tool_input: Union[str, dict] = cast(ToolCall, input)["args"].copy()
else:
tool_call_id = None

View File

@ -2457,3 +2457,22 @@ def test_simple_tool_args_schema_dict() -> None:
assert tool.args == {
"a": {"title": "A", "type": "integer"},
}
def test_empty_tool_call_id() -> None:
@tool
def foo(x: int) -> str:
"""Foo."""
return "hi"
for empty_tool_call_id in (None, ""):
with pytest.raises(
ValueError,
match=(
"Tool call ID must be a non-empty string. "
f"Got '{empty_tool_call_id}' for tool call '.*'."
),
):
foo.invoke(
{"type": "tool_call", "args": {"x": 0}, "id": empty_tool_call_id}
)