Skip to content

Commit abbd379

Browse files
fix: reject non-finite numbers in agent tool arguments (#4326)
## Changes being requested Check decoded beta agent session tool arguments for non-finite floats before invoking a local handler. Both JSON strings and objects use the existing argument error path, including values nested in arrays or objects and numbers that overflow to infinity. ## Additional context & links The sync and async regression tests verify that the handler is skipped and the tool result contains only the standard error. The agent streaming suite had 391 passes and 14 skips. Ruff and ty passed for both files.
1 parent 6805f05 commit abbd379

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

‎src/openai/lib/streaming/agents/_tools.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
import json
44
from copy import deepcopy
5+
from math import isfinite
56
from typing import Any, Dict, Mapping, cast
67

78
from ._types import ToolOutput, ToolHandler, AsyncToolHandler
8-
from ...._utils import maybe_transform
9+
from ...._utils import is_dict, is_list, maybe_transform
910
from ...._exceptions import BadRequestError
1011
from ...._utils._json import openapi_dumps
1112
from ...beta.agents._tool_error import AgentToolError, ToolErrorStage
@@ -14,10 +15,22 @@
1415
from ....types.beta.agent_function_call_output_param import AgentFunctionCallOutputParam
1516

1617

18+
def _check_finite_numbers(value: object) -> None:
19+
if isinstance(value, float) and not isfinite(value):
20+
raise ValueError("Numbers in function arguments must be finite")
21+
if is_dict(value):
22+
for item in value.values():
23+
_check_finite_numbers(item)
24+
elif is_list(value):
25+
for item in value:
26+
_check_finite_numbers(item)
27+
28+
1729
def arguments(call: AgentFunctionCallItem) -> Dict[str, Any]:
1830
value = json.loads(call.arguments) if isinstance(call.arguments, str) else deepcopy(call.arguments)
1931
if not isinstance(value, dict):
2032
raise ValueError("Function arguments must be a JSON object")
33+
_check_finite_numbers(value)
2134
return cast(Dict[str, Any], value)
2235

2336

‎tests/lib/streaming/agents/test_streams.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,20 @@ async def test_missing_handler_does_not_submit_result(sdk: OpenAI | AsyncOpenAI,
289289
assert len(server.inputs()) == 1
290290

291291

292+
@pytest.mark.parametrize(
293+
"arguments",
294+
['{"values":[NaN]}', '{"values":[-Infinity]}', '{"values":[1e999]}', {"values": [float("inf")]}],
295+
)
296+
async def test_non_finite_arguments_skip_handler(sdk: OpenAI | AsyncOpenAI, server: Server, arguments: object) -> None:
297+
called: list[object] = []
298+
server.body = EventBody([turn_event("created"), call(arguments), turn_event("completed"), idle()])
299+
await consume(sdk, input="Hello", tool_handlers={"search": called.append})
300+
assert not called
301+
result = server.inputs()[1]
302+
assert result["success"] is False
303+
assert result["error"] == "Tool handler failed."
304+
305+
292306
async def test_handler_error_is_redacted(sdk: OpenAI | AsyncOpenAI, server: Server) -> None:
293307
def handler(_arguments: dict[str, object]) -> object:
294308
raise ValueError("synthetic-sensitive-value")

0 commit comments

Comments
 (0)