Skip to main content
Perplexity
current

Search documentation

Type to search this documentation.

On this pageOverview

Structure the output

Force the model to return JSON your code can parse instead of prose you have to scrape.

By default a run returns one block of text when it finishes. When a downstream system has to consume the result, free-form prose is hard to work with — you end up writing brittle parsers. Structured output makes the model return JSON that conforms to a schema you define, so you can deserialize it directly. For the exhaustive reference — streaming, background runs, error handling, and full schema examples — see Output Control.

Set response_format to a json_schema with a name and a schema. The response text conforms to the schema unless generation is cut short. The example below builds the schema from a Pydantic model and validates the response against it:

Python
from typing import List, Optional
from pydantic import BaseModel
from perplexity import Perplexity

class CompanySummary(BaseModel):
    name: str
    sector: str
    headquarters: str
    key_products: Optional[List[str]] = None

client = Perplexity()

response = client.responses.create(
    model="openai/gpt-5.6-sol",
    input="Summarize NVIDIA: sector, headquarters, and key products.",
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "company_summary",
            "schema": {
                **CompanySummary.model_json_schema(),
                "required": list(CompanySummary.model_fields.keys()),
                "additionalProperties": False,
            },
        },
    },
)

summary = CompanySummary.model_validate_json(response.output_text)
print(summary.name, summary.sector)
cURL
curl https://api.perplexity.ai/v1/agent \
  -H "Authorization: Bearer $PERPLEXITY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/gpt-5.6-sol",
    "input": "Summarize NVIDIA: sector, headquarters, and key products.",
    "response_format": {
      "type": "json_schema",
      "json_schema": {
        "name": "company_summary",
        "schema": {
          "type": "object",
          "properties": {
            "name": { "type": "string" },
            "sector": { "type": "string" },
            "headquarters": { "type": "string" },
            "key_products": { "type": "array", "items": { "type": "string" } }
          },
          "required": ["name", "sector", "headquarters"]
        }
      }
    }
  }' | jq
Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu