Structured output / JSON schema support

It would be great if the API support passing a JSON schema that specifies the format the response should conform to, similar to OpenAI. This would make it much easier to programmatically handle messages in API responses as we could have a stronger guarantee that the message is in a well-structured and specific format.

Right now the only way to achieve this is by crafting a very specific prompt, and even so the results are inconsistent. The responses are often wrapped in markdown code blocks and can include invalid JSON.

For example, sending this prompt to the API:

Write a two paragraph summary about the company Perplexity AI

Return only a valid JSON object, without any line breaks or additional text.

Do not include a code block with markdown formatting.

Do not include citations in the output.

Output only valid JSON according to RFC 8259 outlined here: https://datatracker.ietf.org/doc/html/rfc8259.

Start your response with "{"

Do not include any backtick (`) characters

Ensure the JSON object conforms to this JSON schema:

{
  "$schema": "http://json-schema.org/draft/2020-12/schema#",
  "title": "CustomResponse",
  "type": "object",
  "properties": {
    "text": { "type": "string" }
  }
}

Results in a response with line breaks in the JSON, which are invalid:

{
  "text": "Perplexity AI is an AI-powered conversational search engine founded in 2022 by former employees of Google and OpenAI. It aims to democratize access to knowledge by providing concise, relevant, and up-to-date answers to user-generated queries. The platform utilizes large language models (LLMs) such as GPT-4, Claude, and Mistral, along with its own custom models, to search the web in real-time and offer citations and images alongside text responses. This approach distinguishes it from traditional search engines and chatbots, making it a valuable tool for research, fact-checking, and various professional tasks.

Perplexity AI has gained significant traction, with over 10 million active users and a valuation of over $1 billion. The company is backed by prominent investors including Amazon, Nvidia, and Databricks. It offers a range of features, including the ability to summarize PDFs, translate files, generate code snippets, and solve math problems. The platform is designed to be user-friendly, providing personalized and interactive search experiences, and it continues to evolve with new capabilities and integrations."
}

Structured outputs are still in closed beta for us. If you want in, please email api@perplexity.ai with a long-form description of your use case and anticipated usage volume.

We have used ChatPerplexity from LangChain and using with_structured_output fuctionality but somehow its not working but provided in the documentation ChatPerplexity — 🦜🔗 LangChain documentation

This is available for all users!