Skip to content

Commit 5837514

Browse files
Enforce strict JSON output in extract tool
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 76d0169 commit 5837514

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

hyperbrowser/tools/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def _serialize_extract_tool_data(data: Any) -> str:
153153
if data is None:
154154
return ""
155155
try:
156-
return json.dumps(data)
156+
return json.dumps(data, allow_nan=False)
157157
except HyperbrowserError:
158158
raise
159159
except Exception as exc:

tests/test_tools_extract.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
from collections.abc import Mapping
3+
import math
34
from types import MappingProxyType
45

56
import pytest
@@ -371,6 +372,33 @@ async def run():
371372
assert exc_info.value.original_error is not None
372373

373374

375+
def test_extract_tool_runnable_rejects_nan_json_payloads():
376+
client = _SyncClient(response_data={"value": math.nan})
377+
378+
with pytest.raises(
379+
HyperbrowserError, match="Failed to serialize extract tool response data"
380+
) as exc_info:
381+
WebsiteExtractTool.runnable(client, {"urls": ["https://example.com"]})
382+
383+
assert exc_info.value.original_error is not None
384+
385+
386+
def test_extract_tool_async_runnable_rejects_nan_json_payloads():
387+
client = _AsyncClient(response_data={"value": math.nan})
388+
389+
async def run():
390+
return await WebsiteExtractTool.async_runnable(
391+
client, {"urls": ["https://example.com"]}
392+
)
393+
394+
with pytest.raises(
395+
HyperbrowserError, match="Failed to serialize extract tool response data"
396+
) as exc_info:
397+
asyncio.run(run())
398+
399+
assert exc_info.value.original_error is not None
400+
401+
374402
def test_extract_tool_runnable_wraps_unexpected_schema_parse_failures(
375403
monkeypatch: pytest.MonkeyPatch,
376404
):

0 commit comments

Comments
 (0)