Skip to main content
This guide shows how to extract structured, typed JSON from the Agent API using the response_format parameter with JSON schemas. You will learn practical patterns for product data extraction, research findings, comparison tables, and building reliable data pipelines — all with guaranteed schema conformance.
The Agent API enforces your JSON schema at generation time, so responses always conform to the specified structure. For the full parameter reference, see Output Control.

Prerequisites

Install the Perplexity SDK:
If you don’t have an API key yet:

Get your Perplexity API Key

Navigate to the API Keys tab in the API Portal and generate a new key.
Then export your API key as an environment variable:

How Structured Outputs Work

When you pass response_format with type: "json_schema", the Agent API constrains the model’s output to match your schema exactly. The response in output_text is a valid JSON string you can parse directly. The schema format follows JSON Schema with a few constraints specific to the Perplexity API:
  • No recursive schemas. The schema cannot reference itself.
  • No unconstrained objects. Avoid additionalProperties: true or bare object types without defined properties.
  • Named schemas required. Each schema needs a name field for identification.

Basic: Extracting a Single Entity

Extract structured data about a single topic with web search grounding.

Extracting Lists: Product Comparisons

Extract a structured comparison of multiple items from a single query.

Research Findings Extraction

Parse search-grounded research into a structured format suitable for reports or databases.

Building a Data Pipeline

Chain structured output extraction into a pipeline that queries, extracts, and stores structured data.

Schema Design Constraints

The Agent API enforces these constraints on JSON schemas:
  • additionalProperties must be false. Every "type": "object" in the schema must include "additionalProperties": false. This applies to the top-level schema and all nested objects.
  • No recursive schemas. A schema cannot reference itself with $ref pointing to its own definition.
  • No unconstrained dicts. Avoid "type": "object" without properties. Every object type must have explicitly defined properties.
  • All properties should be required. While optional properties are allowed, making all properties required ensures consistent output structure.
  • No $ref to external schemas. All definitions must be inline.

Patterns That Work

Patterns to Avoid

Combining Structured Output with Function Calling

You can use response_format alongside custom tools. The model calls your functions first, then formats the final response according to your schema.
When combining structured outputs with function calling, pass the same response_format in every turn of the multi-turn loop. The schema is only enforced on the final text output, not on function call arguments.

Next Steps

Output Control

Full reference for response_format, streaming, and output shaping.

Function Calling

Combine structured outputs with multi-turn function calling.

Agent API Quickstart

Get started with the Agent API in minutes.

Agent API Models

Choose the right model for structured extraction tasks.