Structured output stopped working for sonar-reasoning-pro

:bug: Describe the Bug

Until yesterday, structured JSON output worked when using sonar-reasoning-pro.

Here is a minimal query example:

import json

import requests
import pydantic


class MyBaseModel(pydantic.BaseModel):
    model_config = pydantic.ConfigDict(extra="forbid")


class GenericAnswerFormat(pydantic.BaseModel):
    result: str


url = "https://api.perplexity.ai/chat/completions"

headers = {
    "Authorization": f"Bearer {yourkeyhere}",
    "Content-Type": "application/json",
}

data = {
    "model": "sonar-reasoning-pro",
    "messages": [
        {
            "role": "system",
            "content": "You are an AI assistant that provides structured JSON output.",
        },
        {
            "role": "user",
            "content": "Give me information about the Eiffel Tower in Paris.",
        },
    ],
    "response_format": {
        "type": "json_schema",
        "json_schema": {"schema": GenericAnswerFormat.model_json_schema()},
    },
}

print(json.dumps(data, indent=2))

response = requests.post(url, headers=headers, json=data)

if response.status_code == 200:
    print(response.json())
else:
    print(f"Error: {response.status_code}, {response.text}")

:white_check_mark: Expected Behavior

Get a result.

:cross_mark: Actual Behavior

Error: 400, {“error”:{“message”:“Structured output schema error. Please refer to Overview - Perplexity for more information.”,“type”:“invalid_parameter”,“code”:400}}`

:index_pointing_up:t2: If I change the model to “sonar-pro” above, the query works as expected.

:counterclockwise_arrows_button: Steps to Reproduce

See above.

2 Likes

Hey! Just reproduced on my end and it seems to work for both sonar-pro and sonar-reasoning-pro. Can you please try again?

I got the same error.

sonar, sonar-pro, sonar-reasoning works.

sonar-reasoning-pro, sonar-deep-research throws the following error. They worked two days ago, and nothing has changed on our end.

{
  error: {
    message: 'Structured output schema error. Please refer to https://docs.perplexity.ai/guides/structured-outputs for more information.',
    type: 'invalid_parameter',
    code: 400
  }
}
fetch('https://api.perplexity.ai/chat/completions', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    Authorization: `Bearer pplx-token`,
  },
  body: JSON.stringify({
    model: 'sonar-deep-research',
    response_format: {
      type: 'json_schema',
      json_schema: {
        schema: {
          type: 'object',
          properties: {
            products: {
              type: 'array',
              items: {
                type: 'object',
                properties: {
                  name: {
                    type: 'string',
                  },
                  description: {
                    type: 'string',
                  },
                },
                required: ['name', 'description'],
              },
            },
          },
          required: ['products'],
        },
      },
    },
    reasoning_effort: 'low',
    messages: [
      {
        role: 'user',
        content:
          'List up to 10 highly relevant commercial products that use AI to enhance image editing. For each product, include its name and a concise description.',
      },
    ],
  }),
})
  .then((res) => res.json())
  .then((data) => {
    console.log(data);
  })
  .catch((err) => {
    console.log(err);
  });

Im also seeing the same issue with reasoning pro, have some code I run every 2 weeks with structured output and today is failing - error 400

I tried my minimal example from above again about 8 hours ago, at that time I still got the error as described. Now it seems to work, also for sonar-reasoning-pro! Fingers crossed that this is a permanent fix…

Woohoo! Glad it’s working. Please make another note if something breaks or doesn’t work as intended.