requestStructuredData PRO
requestStructuredData requests structured JSON output from the assistant that conforms to a provided JSON schema.
This API is designed for workflows where you want a predictable, programmatically usable result rather than free-form text.
Common use cases include:
- Extracting structured fields from natural language
- Parsing invoices, receipts, and tickets
- Generating configuration objects
- Normalizing data across different AI providers/models
Supported JSON Schema Types
Scripting defines a lightweight schema structure with three building blocks.
Primitive
Object
Array
API Signatures
Without images
With images
Parameters
prompt
-
Type:
string -
Required
-
The instruction to the model describing what to extract or generate.
-
For best reliability, explicitly specify:
- expected formats (e.g., ISO date)
- currency rules
- how to handle missing fields
images (optional)
- Type:
string[] - Each item must be a data URI, e.g.
data:image/png;base64,... - Not all providers/models support images.
- Avoid passing too many images to reduce failure risk.
schema
- Type:
JSONSchemaArray | JSONSchemaObject - Required
- Defines the only acceptable JSON structure for the response.
- Every field should have a clear
descriptionto guide the model.
options.provider
-
Type:
Provider -
Optional (uses the default configured provider if omitted)
-
Supported:
"openai" | "gemini" | "anthropic" | "deepseek" | "openrouter" | { custom: string }
options.modelId (optional)
- Type:
string - Must match a model actually supported by the chosen provider.
- If omitted, Scripting uses the provider’s default model.
Return Value
Ris the generic type you provide.- The resolved value is expected to match your schema.
- The promise rejects if the assistant cannot return a valid structured result.
Examples
Example 1: Parse a receipt/bill into line items (time + amount)
This example asks the assistant to analyze a textual receipt and extract:
-
receipt time (
purchasedAt) -
line items (
items[])- item name
- item time (if present; otherwise null)
- amount
-
total amount
Example 2: Generate an array
Example 3: Use images + schema
Best Practices
- Make the schema explicit and descriptive; ambiguous schemas lead to unstable results.
- Prefer
requestStructuredDataover parsing free-form text when your output is used by program logic. - For business-critical extraction (e.g., finance/receipts), add strict formatting rules in
prompt.
