Skip to content

Commit da317fd

Browse files
authored
Merge pull request #8 from superagents-lab/extract-required-fields
Require prompt and response_format in extract(), release 0.7.0
2 parents e13530b + ac385a6 commit da317fd

8 files changed

Lines changed: 231 additions & 25 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.7.0 - 2026-11-09
4+
5+
Breaking: `extract()` on `Search1API` and `AsyncSearch1API` requires the keyword arguments `prompt` and `response_format`, matching the API. `response_format` is typed as `ExtractResponseFormat` (in `search1api.types`): `{"type": "json_schema", "json_schema": <your JSON Schema>}`. The API never accepted a request without them; it now rejects one with `422` and an `errors` array instead of `400`, which the client raises as `UnprocessableEntityError`. The bundled OpenAPI snapshot records the required fields, the request example and `TARGET_BAD_REQUEST` in `failureCategory`.
6+
37
## 0.6.1 - 2026-11-05
48

59
The `X-Search1API-Client` request header now reports the installed version (`python/0.6.1`). Since 0.3.0 it had always said `python/0.3.0`. The version lives in `search1api/_version.py` and a test keeps it equal to `pyproject.toml`. No API changes.

‎README.md‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,34 @@ Path("screenshot.png").write_bytes(screenshot["data"])
147147
print(screenshot["content_type"], screenshot.get("request_id"))
148148
```
149149

150-
The clients also support news, crawl, sitemap, trending, extract, usage, and
150+
## Extract
151+
152+
`extract` pulls structured fields out of one page. Both `prompt` and
153+
`response_format` are required; wrap your JSON Schema as
154+
`{"type": "json_schema", "json_schema": <schema>}`:
155+
156+
```python
157+
response = client.extract(
158+
"https://example.com/product",
159+
prompt="Extract the product name and price.",
160+
response_format={
161+
"type": "json_schema",
162+
"json_schema": {
163+
"type": "object",
164+
"properties": {
165+
"title": {"type": "string"},
166+
"price": {"type": "number"},
167+
},
168+
},
169+
},
170+
)
171+
172+
print(response["results"])
173+
```
174+
175+
A request without either field raises `UnprocessableEntityError` (`422`).
176+
177+
The clients also support news, crawl, sitemap, trending, usage, and
151178
batch operations exposed by the Search1API HTTP API. Requests time out after
152179
30 seconds and retry `429` and transient `5xx` responses twice by default.
153180
Authentication, payment, and validation errors are never retried. Deepcrawl

‎openapi/search1api.openapi.json‎

Lines changed: 85 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
},
7272
"failureCategory": {
7373
"type": "string",
74-
"description": "`/crawl` and `/extract`: why the page could not be read, e.g. `NOT_FOUND`, `GONE`, `FORBIDDEN`, `AUTH_REQUIRED`, `BLOCKED`, `RATE_LIMITED`, `DNS_NOT_FOUND`, `TARGET_UNREACHABLE`, `TIMEOUT`, `QUALITY_CHECK`."
74+
"description": "`/crawl` and `/extract`: why the page could not be read, e.g. `NOT_FOUND`, `GONE`, `FORBIDDEN`, `AUTH_REQUIRED`, `BLOCKED`, `RATE_LIMITED`, `DNS_NOT_FOUND`, `TARGET_UNREACHABLE`, `TARGET_BAD_REQUEST`, `TIMEOUT`, `QUALITY_CHECK`."
7575
},
7676
"upstreamStatus": {
7777
"type": "integer",
@@ -501,18 +501,41 @@
501501
"properties": {
502502
"url": {
503503
"type": "string",
504-
"format": "uri"
504+
"format": "uri",
505+
"description": "The webpage to extract data from."
505506
},
506507
"prompt": {
507-
"type": "string"
508+
"type": "string",
509+
"minLength": 1,
510+
"description": "Natural-language instructions: what to extract from the page, e.g. \"Extract the product name and price.\""
508511
},
509512
"response_format": {
510513
"type": "object",
511-
"additionalProperties": {}
514+
"properties": {
515+
"type": {
516+
"type": "string",
517+
"enum": [
518+
"json_schema"
519+
]
520+
},
521+
"json_schema": {
522+
"type": "object",
523+
"additionalProperties": {},
524+
"description": "The JSON Schema the result must follow, e.g. {\"type\": \"object\", \"properties\": {\"title\": {\"type\": \"string\"}}}. Pass the schema itself, not an object wrapping it under name/schema."
525+
}
526+
},
527+
"required": [
528+
"type",
529+
"json_schema"
530+
],
531+
"additionalProperties": false,
532+
"description": "Shape of the result. Wrap your JSON Schema as {\"type\": \"json_schema\", \"json_schema\": <schema>}."
512533
}
513534
},
514535
"required": [
515-
"url"
536+
"url",
537+
"prompt",
538+
"response_format"
516539
],
517540
"additionalProperties": false
518541
},
@@ -3270,7 +3293,7 @@
32703293
"post": {
32713294
"operationId": "extract",
32723295
"summary": "Extract structured content from a URL",
3273-
"description": "Pull structured data out of a single webpage using a natural-language prompt and a JSON schema you supply. Use it when you need specific fields — prices, specifications, contact details — rather than the whole document; when you want the full text, use POST /crawl. Costs 10 credits per request.",
3296+
"description": "Pull structured data out of a single webpage using a natural-language prompt and a JSON schema you supply; both are required, and a request missing either returns 422 with the fields to fix. Use it when you need specific fields — prices, specifications, contact details — rather than the whole document; when you want the full text, use POST /crawl. Costs 10 credits per request.",
32743297
"tags": [
32753298
"Crawl"
32763299
],
@@ -3279,13 +3302,13 @@
32793302
"id": "js",
32803303
"lang": "ts",
32813304
"label": "TypeScript SDK",
3282-
"source": "import { Search1API } from '@search1api/client';\n\nconst client = new Search1API();\nconst response = await client.extract('https://example.com', {\n prompt: 'Extract the page title and description.',\n});\n\nconsole.log(response);"
3305+
"source": "import { Search1API } from '@search1api/client';\n\nconst client = new Search1API();\nconst response = await client.extract('https://example.com', {\n prompt: 'Extract the page title and description.',\n responseFormat: {\n type: 'json_schema',\n json_schema: {\n type: 'object',\n properties: {\n title: { type: 'string' },\n description: { type: 'string' },\n },\n },\n },\n});\n\nconsole.log(response.results);"
32833306
},
32843307
{
32853308
"id": "python",
32863309
"lang": "python",
32873310
"label": "Python SDK",
3288-
"source": "from search1api import Search1API\n\nclient = Search1API()\nresponse = client.extract(\n \"https://example.com\",\n prompt=\"Extract the page title and description.\",\n)\n\nprint(response)"
3311+
"source": "from search1api import Search1API\n\nclient = Search1API()\nresponse = client.extract(\n \"https://example.com\",\n prompt=\"Extract the page title and description.\",\n response_format={\n \"type\": \"json_schema\",\n \"json_schema\": {\n \"type\": \"object\",\n \"properties\": {\n \"title\": {\"type\": \"string\"},\n \"description\": {\"type\": \"string\"},\n },\n },\n },\n)\n\nprint(response[\"results\"])"
32893312
}
32903313
],
32913314
"responses": {
@@ -3411,20 +3434,70 @@
34113434
"properties": {
34123435
"url": {
34133436
"type": "string",
3414-
"format": "uri"
3437+
"format": "uri",
3438+
"description": "The webpage to extract data from."
34153439
},
34163440
"prompt": {
3417-
"type": "string"
3441+
"type": "string",
3442+
"minLength": 1,
3443+
"description": "Natural-language instructions: what to extract from the page, e.g. \"Extract the product name and price.\""
34183444
},
34193445
"response_format": {
34203446
"type": "object",
3421-
"additionalProperties": {}
3447+
"properties": {
3448+
"type": {
3449+
"type": "string",
3450+
"enum": [
3451+
"json_schema"
3452+
]
3453+
},
3454+
"json_schema": {
3455+
"type": "object",
3456+
"additionalProperties": {},
3457+
"description": "The JSON Schema the result must follow, e.g. {\"type\": \"object\", \"properties\": {\"title\": {\"type\": \"string\"}}}. Pass the schema itself, not an object wrapping it under name/schema."
3458+
}
3459+
},
3460+
"required": [
3461+
"type",
3462+
"json_schema"
3463+
],
3464+
"additionalProperties": false,
3465+
"description": "Shape of the result. Wrap your JSON Schema as {\"type\": \"json_schema\", \"json_schema\": <schema>}."
34223466
}
34233467
},
34243468
"required": [
3425-
"url"
3469+
"url",
3470+
"prompt",
3471+
"response_format"
34263472
],
34273473
"additionalProperties": false
3474+
},
3475+
"examples": {
3476+
"pageSummary": {
3477+
"summary": "Title and description",
3478+
"description": "Wrap the JSON Schema of the result as {\"type\": \"json_schema\", \"json_schema\": <schema>}.",
3479+
"value": {
3480+
"url": "https://example.com",
3481+
"prompt": "Extract the page title and description.",
3482+
"response_format": {
3483+
"type": "json_schema",
3484+
"json_schema": {
3485+
"type": "object",
3486+
"properties": {
3487+
"title": {
3488+
"type": "string"
3489+
},
3490+
"description": {
3491+
"type": "string"
3492+
}
3493+
},
3494+
"required": [
3495+
"title"
3496+
]
3497+
}
3498+
}
3499+
}
3500+
}
34283501
}
34293502
}
34303503
}

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "search1api"
7-
version = "0.6.1"
7+
version = "0.7.0"
88
description = "Official Python client for Search1API"
99
readme = "README.md"
1010
license = { file = "LICENSE" }

‎src/search1api/_version.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Sent as ``X-Search1API-Client: python/<version>``. Kept equal to the version
22
# in pyproject.toml by tests/test_client.py; bump both together.
3-
__version__ = "0.6.1"
3+
__version__ = "0.7.0"

‎src/search1api/client.py‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
DeepcrawlFormat,
3232
DeepcrawlStatusResponse,
3333
ExtractResponse,
34+
ExtractResponseFormat,
3435
FeedbackAgent,
3536
FeedbackCategory,
3637
FeedbackResponse,
@@ -476,17 +477,15 @@ def extract(
476477
self,
477478
url: str,
478479
*,
479-
prompt: Optional[str] = None,
480-
response_format: Optional[Mapping[str, Any]] = None,
480+
prompt: str,
481+
response_format: ExtractResponseFormat,
481482
) -> ExtractResponse:
482483
return cast(
483484
ExtractResponse,
484485
self._request_json(
485486
"POST",
486487
"/extract",
487-
json=_compact(
488-
{"url": url, "prompt": prompt, "response_format": response_format}
489-
),
488+
json={"url": url, "prompt": prompt, "response_format": response_format},
490489
),
491490
)
492491

@@ -908,17 +907,15 @@ async def extract(
908907
self,
909908
url: str,
910909
*,
911-
prompt: Optional[str] = None,
912-
response_format: Optional[Mapping[str, Any]] = None,
910+
prompt: str,
911+
response_format: ExtractResponseFormat,
913912
) -> ExtractResponse:
914913
return cast(
915914
ExtractResponse,
916915
await self._request_json(
917916
"POST",
918917
"/extract",
919-
json=_compact(
920-
{"url": url, "prompt": prompt, "response_format": response_format}
921-
),
918+
json={"url": url, "prompt": prompt, "response_format": response_format},
922919
),
923920
)
924921

‎src/search1api/types.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,14 @@ class TrendingResponse(TypedDict):
279279
results: List[TrendingResult]
280280

281281

282+
class ExtractResponseFormat(TypedDict):
283+
"""Shape of the extract result: your JSON Schema, wrapped."""
284+
285+
type: Literal["json_schema"]
286+
json_schema: Dict[str, Any]
287+
"""The JSON Schema of the result itself, not wrapped in ``{name, schema}``."""
288+
289+
282290
class ExtractResponse(TypedDict):
283291
success: bool
284292
extractParameters: Dict[str, str]

‎tests/test_client.py‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
InternalServerError,
1313
Search1API,
1414
Search1APIConfigurationError,
15+
UnprocessableEntityError,
1516
__version__,
1617
)
18+
from search1api.types import ExtractResponseFormat
1719

1820

1921
def json_response(status_code, body, request):
@@ -201,6 +203,101 @@ def handler(request):
201203
http_client.close()
202204

203205

206+
EXTRACT_FORMAT: ExtractResponseFormat = {
207+
"type": "json_schema",
208+
"json_schema": {"type": "object", "properties": {"title": {"type": "string"}}},
209+
}
210+
211+
212+
def test_extract_sends_prompt_and_response_format():
213+
seen = []
214+
215+
def handler(request):
216+
seen.append(request)
217+
return json_response(
218+
200,
219+
{
220+
"success": True,
221+
"extractParameters": {"url": "https://example.com"},
222+
"results": {"title": "Example Domain"},
223+
},
224+
request,
225+
)
226+
227+
http_client = httpx.Client(transport=httpx.MockTransport(handler))
228+
client = Search1API("test-key", client=http_client)
229+
230+
result = client.extract(
231+
"https://example.com",
232+
prompt="Extract the page title.",
233+
response_format=EXTRACT_FORMAT,
234+
)
235+
236+
assert result["results"] == {"title": "Example Domain"}
237+
assert seen[0].url.path == "/extract"
238+
assert json.loads(seen[0].content) == {
239+
"url": "https://example.com",
240+
"prompt": "Extract the page title.",
241+
"response_format": EXTRACT_FORMAT,
242+
}
243+
http_client.close()
244+
245+
246+
def test_extract_validation_error_raises_unprocessable_entity():
247+
def handler(request):
248+
return json_response(
249+
422,
250+
{
251+
"ok": False,
252+
"error": "Unprocessable Entity",
253+
"message": "prompt is required and cannot be empty",
254+
"errors": [{"field": "prompt", "code": "too_small"}],
255+
},
256+
request,
257+
)
258+
259+
http_client = httpx.Client(transport=httpx.MockTransport(handler))
260+
client = Search1API("test-key", client=http_client)
261+
262+
with pytest.raises(UnprocessableEntityError):
263+
client.extract(
264+
"https://example.com", prompt=" ", response_format=EXTRACT_FORMAT
265+
)
266+
http_client.close()
267+
268+
269+
def test_async_extract_sends_prompt_and_response_format():
270+
async def run():
271+
seen = []
272+
273+
async def handler(request):
274+
seen.append(request)
275+
return json_response(
276+
200,
277+
{
278+
"success": True,
279+
"extractParameters": {"url": "https://example.com"},
280+
"results": {"title": "Example Domain"},
281+
},
282+
request,
283+
)
284+
285+
http_client = httpx.AsyncClient(transport=httpx.MockTransport(handler))
286+
client = AsyncSearch1API("test-key", client=http_client)
287+
try:
288+
await client.extract(
289+
"https://example.com",
290+
prompt="Extract the page title.",
291+
response_format=EXTRACT_FORMAT,
292+
)
293+
finally:
294+
await http_client.aclose()
295+
return seen
296+
297+
seen = asyncio.run(run())
298+
assert json.loads(seen[0].content)["response_format"] == EXTRACT_FORMAT
299+
300+
204301
def test_deepcrawl_starts_and_polls():
205302
responses = [
206303
(202, {"taskId": "task_1", "status": "queued"}),

0 commit comments

Comments
 (0)