Skip to content

Commit 1f0f7af

Browse files
author
stlc-bot
committed
feat(scrape): add highlights format to POST /web/scrape (#1210)
Stainless-Generated-From: 76b2be5
1 parent cdb570f commit 1f0f7af

4 files changed

Lines changed: 83 additions & 19 deletions

File tree

‎src/context/dev/resources/web.py‎

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ def scrape(
391391
*,
392392
formats: web_scrape_params.Formats,
393393
url: str,
394+
highlights_params: web_scrape_params.HighlightsParams | Omit = omit,
394395
image_params: web_scrape_params.ImageParams | Omit = omit,
395396
json_params: web_scrape_params.JsonParams | Omit = omit,
396397
markdown_params: web_scrape_params.MarkdownParams | Omit = omit,
@@ -412,20 +413,24 @@ def scrape(
412413
visit.
413414
414415
Each cache key includes only the settings that affect that output. HTML
415-
is shared with Markdown, parsed fields, and JSON extraction. Cached outputs can
416-
come from different visits within maxAgeMs; use 0 for a fresh capture. HTML-only
417-
requests use the existing fast acquisition path. One credit per request,
418-
including cache hits and missing pages, or two with browser actions; JSON
419-
extraction adds four credits and runs an LLM over the page Markdown on every
420-
request that has text to extract; PDF OCR adds one credit per recovered page on
421-
fresh extraction. Original response bytes and screenshots are limited to 20 MiB
422-
each, screenshots to 40 megapixels, and the combined browser capture to 60 MiB.
416+
is shared with Markdown, parsed fields, highlights, and JSON extraction. Cached
417+
outputs can come from different visits within maxAgeMs; use 0 for a fresh
418+
capture. HTML-only requests use the existing fast acquisition path. Highlights
419+
return the plain-text passages most relevant to highlightsParams.query. One
420+
credit per request, including cache hits and missing pages, or two with browser
421+
actions; highlights add 3 credits when passages are returned; JSON extraction
422+
adds four credits and runs an LLM over the page Markdown on every request that
423+
has text to extract; PDF OCR adds one credit per recovered page on fresh
424+
extraction. Original response bytes and screenshots are limited to 20 MiB each,
425+
screenshots to 40 megapixels, and the combined browser capture to 60 MiB.
423426
424427
Args:
425428
formats: Outputs to return. Enable at least one; omitted formats are false.
426429
427430
url: The URL to scrape.
428431
432+
highlights_params: Highlight options. Requires formats.highlights: true.
433+
429434
image_params: Image options. Requires formats.images: true.
430435
431436
json_params: Required when formats.json is true.
@@ -455,7 +460,8 @@ def scrape(
455460
when using return-partial.
456461
457462
zdr: Zero data retention. Bypasses caches and uploads; excludes request/response
458-
content and tags from logs. Must be enabled for your organization.
463+
content and tags from logs. Must be enabled for your organization. Not available
464+
with the highlights output.
459465
460466
extra_headers: Send extra headers
461467
@@ -471,6 +477,7 @@ def scrape(
471477
{
472478
"formats": formats,
473479
"url": url,
480+
"highlights_params": highlights_params,
474481
"image_params": image_params,
475482
"json_params": json_params,
476483
"markdown_params": markdown_params,
@@ -1874,6 +1881,7 @@ async def scrape(
18741881
*,
18751882
formats: web_scrape_params.Formats,
18761883
url: str,
1884+
highlights_params: web_scrape_params.HighlightsParams | Omit = omit,
18771885
image_params: web_scrape_params.ImageParams | Omit = omit,
18781886
json_params: web_scrape_params.JsonParams | Omit = omit,
18791887
markdown_params: web_scrape_params.MarkdownParams | Omit = omit,
@@ -1895,20 +1903,24 @@ async def scrape(
18951903
visit.
18961904
18971905
Each cache key includes only the settings that affect that output. HTML
1898-
is shared with Markdown, parsed fields, and JSON extraction. Cached outputs can
1899-
come from different visits within maxAgeMs; use 0 for a fresh capture. HTML-only
1900-
requests use the existing fast acquisition path. One credit per request,
1901-
including cache hits and missing pages, or two with browser actions; JSON
1902-
extraction adds four credits and runs an LLM over the page Markdown on every
1903-
request that has text to extract; PDF OCR adds one credit per recovered page on
1904-
fresh extraction. Original response bytes and screenshots are limited to 20 MiB
1905-
each, screenshots to 40 megapixels, and the combined browser capture to 60 MiB.
1906+
is shared with Markdown, parsed fields, highlights, and JSON extraction. Cached
1907+
outputs can come from different visits within maxAgeMs; use 0 for a fresh
1908+
capture. HTML-only requests use the existing fast acquisition path. Highlights
1909+
return the plain-text passages most relevant to highlightsParams.query. One
1910+
credit per request, including cache hits and missing pages, or two with browser
1911+
actions; highlights add 3 credits when passages are returned; JSON extraction
1912+
adds four credits and runs an LLM over the page Markdown on every request that
1913+
has text to extract; PDF OCR adds one credit per recovered page on fresh
1914+
extraction. Original response bytes and screenshots are limited to 20 MiB each,
1915+
screenshots to 40 megapixels, and the combined browser capture to 60 MiB.
19061916
19071917
Args:
19081918
formats: Outputs to return. Enable at least one; omitted formats are false.
19091919
19101920
url: The URL to scrape.
19111921
1922+
highlights_params: Highlight options. Requires formats.highlights: true.
1923+
19121924
image_params: Image options. Requires formats.images: true.
19131925
19141926
json_params: Required when formats.json is true.
@@ -1938,7 +1950,8 @@ async def scrape(
19381950
when using return-partial.
19391951
19401952
zdr: Zero data retention. Bypasses caches and uploads; excludes request/response
1941-
content and tags from logs. Must be enabled for your organization.
1953+
content and tags from logs. Must be enabled for your organization. Not available
1954+
with the highlights output.
19421955
19431956
extra_headers: Send extra headers
19441957
@@ -1954,6 +1967,7 @@ async def scrape(
19541967
{
19551968
"formats": formats,
19561969
"url": url,
1970+
"highlights_params": highlights_params,
19571971
"image_params": image_params,
19581972
"json_params": json_params,
19591973
"markdown_params": markdown_params,

‎src/context/dev/types/web_scrape_params.py‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
__all__ = [
1212
"WebScrapeParams",
1313
"Formats",
14+
"HighlightsParams",
1415
"ImageParams",
1516
"JsonParams",
1617
"MarkdownParams",
@@ -41,6 +42,9 @@ class WebScrapeParams(TypedDict, total=False):
4142
url: Required[str]
4243
"""The URL to scrape."""
4344

45+
highlights_params: Annotated[HighlightsParams, PropertyInfo(alias="highlightsParams")]
46+
"""Highlight options. Requires formats.highlights: true."""
47+
4448
image_params: Annotated[ImageParams, PropertyInfo(alias="imageParams")]
4549
"""Image options. Requires formats.images: true."""
4650

@@ -88,7 +92,8 @@ class WebScrapeParams(TypedDict, total=False):
8892
"""Zero data retention.
8993
9094
Bypasses caches and uploads; excludes request/response content and tags from
91-
logs. Must be enabled for your organization.
95+
logs. Must be enabled for your organization. Not available with the highlights
96+
output.
9297
"""
9398

9499

@@ -98,6 +103,13 @@ class Formats(TypedDict, total=False):
98103
bytes: bool
99104
"""The original HTTP response body."""
100105

106+
highlights: bool
107+
"""
108+
Plain-text passages from the page that are most relevant to
109+
highlightsParams.query, each prefixed with its section heading. Adds 3 credits.
110+
Not available with zdr enabled.
111+
"""
112+
101113
html: bool
102114
"""Rendered HTML."""
103115

@@ -122,6 +134,16 @@ class Formats(TypedDict, total=False):
122134
"""An inline image of the page."""
123135

124136

137+
class HighlightsParams(TypedDict, total=False):
138+
"""Highlight options. Requires formats.highlights: true."""
139+
140+
query: Required[str]
141+
"""The question or topic to find passages for."""
142+
143+
max_characters: Annotated[int, PropertyInfo(alias="maxCharacters")]
144+
"""Maximum combined length of the returned passages, in characters."""
145+
146+
125147
class ImageParams(TypedDict, total=False):
126148
"""Image options. Requires formats.images: true."""
127149

‎src/context/dev/types/web_scrape_response.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"Bytes",
1313
"BytesData",
1414
"CacheMetadata",
15+
"Highlights",
1516
"HTML",
1617
"Images",
1718
"ImagesData",
@@ -63,6 +64,16 @@ class CacheMetadata(BaseModel):
6364
"""
6465

6566

67+
class Highlights(BaseModel):
68+
"""
69+
Plain-text passages relevant to highlightsParams.query, in page order, each prefixed with its section heading in square brackets. Empty when the page has no text.
70+
"""
71+
72+
data: Optional[List[str]] = None
73+
74+
requested: bool
75+
76+
6677
class HTML(BaseModel):
6778
"""Rendered HTML after content filters."""
6879

@@ -243,6 +254,13 @@ class WebScrapeResponse(BaseModel):
243254
to the output was a hit; age_ms is the oldest contributing hit.
244255
"""
245256

257+
highlights: Highlights
258+
"""
259+
Plain-text passages relevant to highlightsParams.query, in page order, each
260+
prefixed with its section heading in square brackets. Empty when the page has no
261+
text.
262+
"""
263+
246264
html: HTML
247265
"""Rendered HTML after content filters."""
248266

‎tests/api_resources/test_web.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ def test_method_scrape_with_all_params(self, client: ContextDev) -> None:
242242
web = client.web.scrape(
243243
formats={
244244
"bytes": True,
245+
"highlights": True,
245246
"html": True,
246247
"images": True,
247248
"json": True,
@@ -250,6 +251,10 @@ def test_method_scrape_with_all_params(self, client: ContextDev) -> None:
250251
"screenshot": True,
251252
},
252253
url="https://example.com",
254+
highlights_params={
255+
"query": "x",
256+
"max_characters": 100,
257+
},
253258
image_params={
254259
"dedupe": "none",
255260
"enrich": ["dimensions"],
@@ -772,6 +777,7 @@ async def test_method_scrape_with_all_params(self, async_client: AsyncContextDev
772777
web = await async_client.web.scrape(
773778
formats={
774779
"bytes": True,
780+
"highlights": True,
775781
"html": True,
776782
"images": True,
777783
"json": True,
@@ -780,6 +786,10 @@ async def test_method_scrape_with_all_params(self, async_client: AsyncContextDev
780786
"screenshot": True,
781787
},
782788
url="https://example.com",
789+
highlights_params={
790+
"query": "x",
791+
"max_characters": 100,
792+
},
783793
image_params={
784794
"dedupe": "none",
785795
"enrich": ["dimensions"],

0 commit comments

Comments
 (0)