Skip to content

Commit fbe10f0

Browse files
Validate extract tool schema value types
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 5b5b6fa commit fbe10f0

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

hyperbrowser/tools/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ def _format_tool_param_key_for_error(key: str) -> str:
5555
def _prepare_extract_tool_params(params: Mapping[str, Any]) -> Dict[str, Any]:
5656
normalized_params = _to_param_dict(params)
5757
schema_value = normalized_params.get("schema")
58+
if schema_value is not None and not isinstance(schema_value, (str, MappingABC)):
59+
raise HyperbrowserError(
60+
"Extract tool `schema` must be an object or JSON string"
61+
)
5862
if isinstance(schema_value, str):
5963
try:
6064
parsed_schema = json.loads(schema_value)

tests/test_tools_extract.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,35 @@ async def run():
140140
asyncio.run(run())
141141

142142

143+
def test_extract_tool_runnable_rejects_non_mapping_non_string_schema():
144+
client = _SyncClient()
145+
params = {
146+
"urls": ["https://example.com"],
147+
"schema": 123,
148+
}
149+
150+
with pytest.raises(
151+
HyperbrowserError, match="Extract tool `schema` must be an object or JSON string"
152+
):
153+
WebsiteExtractTool.runnable(client, params)
154+
155+
156+
def test_extract_tool_async_runnable_rejects_non_mapping_non_string_schema():
157+
client = _AsyncClient()
158+
params = {
159+
"urls": ["https://example.com"],
160+
"schema": 123,
161+
}
162+
163+
async def run():
164+
await WebsiteExtractTool.async_runnable(client, params)
165+
166+
with pytest.raises(
167+
HyperbrowserError, match="Extract tool `schema` must be an object or JSON string"
168+
):
169+
asyncio.run(run())
170+
171+
143172
def test_extract_tool_runnable_serializes_empty_object_data():
144173
client = _SyncClient(response_data={})
145174

0 commit comments

Comments
 (0)