Skip to content

Commit 9b21b53

Browse files
Require concrete schema strings in extract tool params
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent a79d61a commit 9b21b53

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

hyperbrowser/tools/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,13 @@ def _normalize_extract_schema_mapping(
104104
def _prepare_extract_tool_params(params: Mapping[str, Any]) -> Dict[str, Any]:
105105
normalized_params = _to_param_dict(params)
106106
schema_value = normalized_params.get("schema")
107-
if schema_value is not None and not isinstance(schema_value, (str, MappingABC)):
107+
if schema_value is not None and not (
108+
type(schema_value) is str or isinstance(schema_value, MappingABC)
109+
):
108110
raise HyperbrowserError(
109111
"Extract tool `schema` must be an object or JSON string"
110112
)
111-
if isinstance(schema_value, str):
113+
if type(schema_value) is str:
112114
try:
113115
parsed_schema = json.loads(schema_value)
114116
except HyperbrowserError:

tests/test_tools_extract.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,43 @@ async def run():
174174
asyncio.run(run())
175175

176176

177+
def test_extract_tool_runnable_rejects_string_subclass_schema_strings():
178+
class _SchemaString(str):
179+
pass
180+
181+
client = _SyncClient()
182+
params = {
183+
"urls": ["https://example.com"],
184+
"schema": _SchemaString('{"type":"object"}'),
185+
}
186+
187+
with pytest.raises(
188+
HyperbrowserError,
189+
match="Extract tool `schema` must be an object or JSON string",
190+
):
191+
WebsiteExtractTool.runnable(client, params)
192+
193+
194+
def test_extract_tool_async_runnable_rejects_string_subclass_schema_strings():
195+
class _SchemaString(str):
196+
pass
197+
198+
client = _AsyncClient()
199+
params = {
200+
"urls": ["https://example.com"],
201+
"schema": _SchemaString('{"type":"object"}'),
202+
}
203+
204+
async def run():
205+
await WebsiteExtractTool.async_runnable(client, params)
206+
207+
with pytest.raises(
208+
HyperbrowserError,
209+
match="Extract tool `schema` must be an object or JSON string",
210+
):
211+
asyncio.run(run())
212+
213+
177214
def test_extract_tool_runnable_rejects_null_schema_json():
178215
client = _SyncClient()
179216
params = {

0 commit comments

Comments
 (0)