Skip to content

Commit 03ec23b

Browse files
author
stlc-bot
committed
feat(scrape): add json output format to POST /web/scrape (#1213)
Stainless-Generated-From: 0fbc1cc
1 parent ea1a9e2 commit 03ec23b

10 files changed

Lines changed: 302 additions & 12 deletions

File tree

‎lib/context_dev/models/web_scrape_params.rb‎

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ class WebScrapeParams < ContextDev::Internal::Type::BaseModel
2525
# @return [ContextDev::Models::WebScrapeParams::ImageParams, nil]
2626
optional :image_params, -> { ContextDev::WebScrapeParams::ImageParams }, api_name: :imageParams
2727

28+
# @!attribute json_params
29+
# Required when formats.json is true.
30+
#
31+
# @return [ContextDev::Models::WebScrapeParams::JsonParams, nil]
32+
optional :json_params, -> { ContextDev::WebScrapeParams::JsonParams }, api_name: :jsonParams
33+
2834
# @!attribute markdown_params
2935
# Markdown options. Requires formats.markdown: true.
3036
#
@@ -86,7 +92,7 @@ class WebScrapeParams < ContextDev::Internal::Type::BaseModel
8692
# @return [Symbol, ContextDev::Models::WebScrapeParams::Zdr, nil]
8793
optional :zdr, enum: -> { ContextDev::WebScrapeParams::Zdr }
8894

89-
# @!method initialize(formats:, url:, image_params: nil, markdown_params: nil, max_age_ms: nil, parse_params: nil, screenshot_params: nil, shared_params: nil, tags: nil, timeout_opts: nil, zdr: nil, request_options: {})
95+
# @!method initialize(formats:, url:, image_params: nil, json_params: nil, markdown_params: nil, max_age_ms: nil, parse_params: nil, screenshot_params: nil, shared_params: nil, tags: nil, timeout_opts: nil, zdr: nil, request_options: {})
9096
# Some parameter documentations has been truncated, see
9197
# {ContextDev::Models::WebScrapeParams} for more details.
9298
#
@@ -96,6 +102,8 @@ class WebScrapeParams < ContextDev::Internal::Type::BaseModel
96102
#
97103
# @param image_params [ContextDev::Models::WebScrapeParams::ImageParams] Image options. Requires formats.images: true.
98104
#
105+
# @param json_params [ContextDev::Models::WebScrapeParams::JsonParams] Required when formats.json is true.
106+
#
99107
# @param markdown_params [ContextDev::Models::WebScrapeParams::MarkdownParams] Markdown options. Requires formats.markdown: true.
100108
#
101109
# @param max_age_ms [Integer] Maximum age of each cached output. Defaults to 1 day; 0 fetches fresh and update
@@ -133,6 +141,15 @@ class Formats < ContextDev::Internal::Type::BaseModel
133141
# @return [Boolean, nil]
134142
optional :images, ContextDev::Internal::Type::Boolean
135143

144+
# @!attribute json
145+
# Page data extracted by an LLM from the page Markdown into jsonParams.schema;
146+
# values carried only in attributes or CSS classes need formats.parse instead.
147+
# Adds four credits when the page has text to extract; when shared content filters
148+
# leave no text the result is an empty object and only the base price applies.
149+
#
150+
# @return [Boolean, nil]
151+
optional :json, ContextDev::Internal::Type::Boolean
152+
136153
# @!attribute markdown
137154
# Page content as Markdown.
138155
#
@@ -151,7 +168,10 @@ class Formats < ContextDev::Internal::Type::BaseModel
151168
# @return [Boolean, nil]
152169
optional :screenshot, ContextDev::Internal::Type::Boolean
153170

154-
# @!method initialize(bytes: nil, html: nil, images: nil, markdown: nil, parse: nil, screenshot: nil)
171+
# @!method initialize(bytes: nil, html: nil, images: nil, json: nil, markdown: nil, parse: nil, screenshot: nil)
172+
# Some parameter documentations has been truncated, see
173+
# {ContextDev::Models::WebScrapeParams::Formats} for more details.
174+
#
155175
# Outputs to return. Enable at least one; omitted formats are false.
156176
#
157177
# @param bytes [Boolean] The original HTTP response body.
@@ -160,6 +180,8 @@ class Formats < ContextDev::Internal::Type::BaseModel
160180
#
161181
# @param images [Boolean] Images found on the page.
162182
#
183+
# @param json [Boolean] Page data extracted by an LLM from the page Markdown into jsonParams.schema; val
184+
#
163185
# @param markdown [Boolean] Page content as Markdown.
164186
#
165187
# @param parse [Boolean] Fields selected by parseParams.rules.
@@ -218,6 +240,35 @@ module Enrich
218240
end
219241
end
220242

243+
class JsonParams < ContextDev::Internal::Type::BaseModel
244+
# @!attribute schema
245+
# JSON Schema for the returned object. Must describe a top-level object; at most
246+
# 50 KB serialized. Optional fields the page does not state are omitted, or null
247+
# when their type allows null, while required non-nullable fields always receive a
248+
# best-effort value, so prefer nullable or optional fields for data a page may
249+
# omit. Zod users can pass the output of z.toJSONSchema().
250+
#
251+
# @return [Hash{Symbol=>Object}]
252+
required :schema, ContextDev::Internal::Type::HashOf[ContextDev::Internal::Type::Unknown]
253+
254+
# @!attribute instructions
255+
# Optional guidance on which facts to prioritize or how to interpret schema
256+
# fields.
257+
#
258+
# @return [String, nil]
259+
optional :instructions, String
260+
261+
# @!method initialize(schema:, instructions: nil)
262+
# Some parameter documentations has been truncated, see
263+
# {ContextDev::Models::WebScrapeParams::JsonParams} for more details.
264+
#
265+
# Required when formats.json is true.
266+
#
267+
# @param schema [Hash{Symbol=>Object}] JSON Schema for the returned object. Must describe a top-level object; at most 5
268+
#
269+
# @param instructions [String] Optional guidance on which facts to prioritize or how to interpret schema fields
270+
end
271+
221272
class MarkdownParams < ContextDev::Internal::Type::BaseModel
222273
# @!attribute include_images
223274
#

‎lib/context_dev/models/web_scrape_response.rb‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ class WebScrapeResponse < ContextDev::Internal::Type::BaseModel
3131
# @return [ContextDev::Models::WebScrapeResponse::Images]
3232
required :images, -> { ContextDev::Models::WebScrapeResponse::Images }
3333

34+
# @!attribute json
35+
# Page data extracted into jsonParams.schema, after shared content filters. Values
36+
# are grounded in the page; optional fields the page does not state are omitted,
37+
# or null when their type allows null. An empty object when the filters leave no
38+
# text.
39+
#
40+
# @return [ContextDev::Models::WebScrapeResponse::Json]
41+
required :json, -> { ContextDev::Models::WebScrapeResponse::Json }
42+
3443
# @!attribute markdown
3544
# Markdown after content filters.
3645
#
@@ -81,7 +90,7 @@ class WebScrapeResponse < ContextDev::Internal::Type::BaseModel
8190
# @return [ContextDev::Models::WebScrapeResponse::KeyMetadata, nil]
8291
optional :key_metadata, -> { ContextDev::Models::WebScrapeResponse::KeyMetadata }
8392

84-
# @!method initialize(bytes:, cache_metadata:, html:, images:, markdown:, metadata:, parsed:, request_id:, screenshot:, url:, is_partial: nil, key_metadata: nil)
93+
# @!method initialize(bytes:, cache_metadata:, html:, images:, json:, markdown:, metadata:, parsed:, request_id:, screenshot:, url:, is_partial: nil, key_metadata: nil)
8594
# Some parameter documentations has been truncated, see
8695
# {ContextDev::Models::WebScrapeResponse} for more details.
8796
#
@@ -93,6 +102,8 @@ class WebScrapeResponse < ContextDev::Internal::Type::BaseModel
93102
#
94103
# @param images [ContextDev::Models::WebScrapeResponse::Images] Images after content filters. Empty when none are found.
95104
#
105+
# @param json [ContextDev::Models::WebScrapeResponse::Json] Page data extracted into jsonParams.schema, after shared content filters. Values
106+
#
96107
# @param markdown [ContextDev::Models::WebScrapeResponse::Markdown] Markdown after content filters.
97108
#
98109
# @param metadata [ContextDev::Models::WebScrapeResponse::Metadata] Page details, when available.
@@ -304,6 +315,28 @@ module Classification
304315
end
305316
end
306317

318+
# @see ContextDev::Models::WebScrapeResponse#json
319+
class Json < ContextDev::Internal::Type::BaseModel
320+
# @!attribute data
321+
#
322+
# @return [Hash{Symbol=>Object}, nil]
323+
required :data, ContextDev::Internal::Type::HashOf[ContextDev::Internal::Type::Unknown], nil?: true
324+
325+
# @!attribute requested
326+
#
327+
# @return [Boolean]
328+
required :requested, ContextDev::Internal::Type::Boolean
329+
330+
# @!method initialize(data:, requested:)
331+
# Page data extracted into jsonParams.schema, after shared content filters. Values
332+
# are grounded in the page; optional fields the page does not state are omitted,
333+
# or null when their type allows null. An empty object when the filters leave no
334+
# text.
335+
#
336+
# @param data [Hash{Symbol=>Object}, nil]
337+
# @param requested [Boolean]
338+
end
339+
307340
# @see ContextDev::Models::WebScrapeResponse#markdown
308341
class Markdown < ContextDev::Internal::Type::BaseModel
309342
# @!attribute data

‎lib/context_dev/resources/web.rb‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,21 +183,25 @@ def map_urls(params)
183183
#
184184
# Reuse cached outputs independently and capture missing formats in one page
185185
# visit. Each cache key includes only the settings that affect that output. HTML
186-
# is shared with Markdown and parsed fields. Cached outputs can come from
187-
# different visits within maxAgeMs; use 0 for a fresh capture. HTML-only requests
188-
# use the existing fast acquisition path. One credit per request, including cache
189-
# hits, or two with browser actions; PDF OCR adds one credit per recovered page on
186+
# is shared with Markdown, parsed fields, and JSON extraction. Cached outputs can
187+
# come from different visits within maxAgeMs; use 0 for a fresh capture. HTML-only
188+
# requests use the existing fast acquisition path. One credit per request,
189+
# including cache hits and missing pages, or two with browser actions; JSON
190+
# extraction adds four credits and runs an LLM over the page Markdown on every
191+
# request that has text to extract; PDF OCR adds one credit per recovered page on
190192
# fresh extraction. Original response bytes and screenshots are limited to 20 MiB
191193
# each, screenshots to 40 megapixels, and the combined browser capture to 60 MiB.
192194
#
193-
# @overload scrape(formats:, url:, image_params: nil, markdown_params: nil, max_age_ms: nil, parse_params: nil, screenshot_params: nil, shared_params: nil, tags: nil, timeout_opts: nil, zdr: nil, request_options: {})
195+
# @overload scrape(formats:, url:, image_params: nil, json_params: nil, markdown_params: nil, max_age_ms: nil, parse_params: nil, screenshot_params: nil, shared_params: nil, tags: nil, timeout_opts: nil, zdr: nil, request_options: {})
194196
#
195197
# @param formats [ContextDev::Models::WebScrapeParams::Formats] Outputs to return. Enable at least one; omitted formats are false.
196198
#
197199
# @param url [String] The URL to scrape.
198200
#
199201
# @param image_params [ContextDev::Models::WebScrapeParams::ImageParams] Image options. Requires formats.images: true.
200202
#
203+
# @param json_params [ContextDev::Models::WebScrapeParams::JsonParams] Required when formats.json is true.
204+
#
201205
# @param markdown_params [ContextDev::Models::WebScrapeParams::MarkdownParams] Markdown options. Requires formats.markdown: true.
202206
#
203207
# @param max_age_ms [Integer] Maximum age of each cached output. Defaults to 1 day; 0 fetches fresh and update

‎rbi/context_dev/models/web_scrape_params.rbi‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ module ContextDev
3333
end
3434
attr_writer :image_params
3535

36+
# Required when formats.json is true.
37+
sig { returns(T.nilable(ContextDev::WebScrapeParams::JsonParams)) }
38+
attr_reader :json_params
39+
40+
sig do
41+
params(
42+
json_params: ContextDev::WebScrapeParams::JsonParams::OrHash
43+
).void
44+
end
45+
attr_writer :json_params
46+
3647
# Markdown options. Requires formats.markdown: true.
3748
sig { returns(T.nilable(ContextDev::WebScrapeParams::MarkdownParams)) }
3849
attr_reader :markdown_params
@@ -126,6 +137,7 @@ module ContextDev
126137
formats: ContextDev::WebScrapeParams::Formats::OrHash,
127138
url: String,
128139
image_params: ContextDev::WebScrapeParams::ImageParams::OrHash,
140+
json_params: ContextDev::WebScrapeParams::JsonParams::OrHash,
129141
markdown_params: ContextDev::WebScrapeParams::MarkdownParams::OrHash,
130142
max_age_ms: Integer,
131143
parse_params: ContextDev::WebScrapeParams::ParseParams::OrHash,
@@ -145,6 +157,8 @@ module ContextDev
145157
url:,
146158
# Image options. Requires formats.images: true.
147159
image_params: nil,
160+
# Required when formats.json is true.
161+
json_params: nil,
148162
# Markdown options. Requires formats.markdown: true.
149163
markdown_params: nil,
150164
# Maximum age of each cached output. Defaults to 1 day; 0 fetches fresh and
@@ -182,6 +196,7 @@ module ContextDev
182196
formats: ContextDev::WebScrapeParams::Formats,
183197
url: String,
184198
image_params: ContextDev::WebScrapeParams::ImageParams,
199+
json_params: ContextDev::WebScrapeParams::JsonParams,
185200
markdown_params: ContextDev::WebScrapeParams::MarkdownParams,
186201
max_age_ms: Integer,
187202
parse_params: ContextDev::WebScrapeParams::ParseParams,
@@ -227,6 +242,16 @@ module ContextDev
227242
sig { params(images: T::Boolean).void }
228243
attr_writer :images
229244

245+
# Page data extracted by an LLM from the page Markdown into jsonParams.schema;
246+
# values carried only in attributes or CSS classes need formats.parse instead.
247+
# Adds four credits when the page has text to extract; when shared content filters
248+
# leave no text the result is an empty object and only the base price applies.
249+
sig { returns(T.nilable(T::Boolean)) }
250+
attr_reader :json
251+
252+
sig { params(json: T::Boolean).void }
253+
attr_writer :json
254+
230255
# Page content as Markdown.
231256
sig { returns(T.nilable(T::Boolean)) }
232257
attr_reader :markdown
@@ -254,6 +279,7 @@ module ContextDev
254279
bytes: T::Boolean,
255280
html: T::Boolean,
256281
images: T::Boolean,
282+
json: T::Boolean,
257283
markdown: T::Boolean,
258284
parse: T::Boolean,
259285
screenshot: T::Boolean
@@ -266,6 +292,11 @@ module ContextDev
266292
html: nil,
267293
# Images found on the page.
268294
images: nil,
295+
# Page data extracted by an LLM from the page Markdown into jsonParams.schema;
296+
# values carried only in attributes or CSS classes need formats.parse instead.
297+
# Adds four credits when the page has text to extract; when shared content filters
298+
# leave no text the result is an empty object and only the base price applies.
299+
json: nil,
269300
# Page content as Markdown.
270301
markdown: nil,
271302
# Fields selected by parseParams.rules.
@@ -281,6 +312,7 @@ module ContextDev
281312
bytes: T::Boolean,
282313
html: T::Boolean,
283314
images: T::Boolean,
315+
json: T::Boolean,
284316
markdown: T::Boolean,
285317
parse: T::Boolean,
286318
screenshot: T::Boolean
@@ -445,6 +477,60 @@ module ContextDev
445477
end
446478
end
447479

480+
class JsonParams < ContextDev::Internal::Type::BaseModel
481+
OrHash =
482+
T.type_alias do
483+
T.any(
484+
ContextDev::WebScrapeParams::JsonParams,
485+
ContextDev::Internal::AnyHash
486+
)
487+
end
488+
489+
# JSON Schema for the returned object. Must describe a top-level object; at most
490+
# 50 KB serialized. Optional fields the page does not state are omitted, or null
491+
# when their type allows null, while required non-nullable fields always receive a
492+
# best-effort value, so prefer nullable or optional fields for data a page may
493+
# omit. Zod users can pass the output of z.toJSONSchema().
494+
sig { returns(T::Hash[Symbol, T.anything]) }
495+
attr_accessor :schema
496+
497+
# Optional guidance on which facts to prioritize or how to interpret schema
498+
# fields.
499+
sig { returns(T.nilable(String)) }
500+
attr_reader :instructions
501+
502+
sig { params(instructions: String).void }
503+
attr_writer :instructions
504+
505+
# Required when formats.json is true.
506+
sig do
507+
params(
508+
schema: T::Hash[Symbol, T.anything],
509+
instructions: String
510+
).returns(T.attached_class)
511+
end
512+
def self.new(
513+
# JSON Schema for the returned object. Must describe a top-level object; at most
514+
# 50 KB serialized. Optional fields the page does not state are omitted, or null
515+
# when their type allows null, while required non-nullable fields always receive a
516+
# best-effort value, so prefer nullable or optional fields for data a page may
517+
# omit. Zod users can pass the output of z.toJSONSchema().
518+
schema:,
519+
# Optional guidance on which facts to prioritize or how to interpret schema
520+
# fields.
521+
instructions: nil
522+
)
523+
end
524+
525+
sig do
526+
override.returns(
527+
{ schema: T::Hash[Symbol, T.anything], instructions: String }
528+
)
529+
end
530+
def to_hash
531+
end
532+
end
533+
448534
class MarkdownParams < ContextDev::Internal::Type::BaseModel
449535
OrHash =
450536
T.type_alias do

0 commit comments

Comments
 (0)