Describe the Bug
Structured output doesnt work stable - with one code and one environment it builds sometimes structured output and sometimes not. It works so even with structured code example from documentation.
Expected Behavior
Structured output works always.
Actual Behavior
Structured output works unstable.
Steps to Reproduce
Run code from example multiple times (https://docs.perplexity.ai/guides/structured-outputs):
import requests
from pydantic import BaseModel
class AnswerFormat(BaseModel):
first_name: str
last_name: str
year_of_birth: int
num_seasons_in_nba: int
url = "https://api.perplexity.ai/chat/completions"
headers = {"Authorization": "Bearer YOUR_API_KEY"}
payload = {
"model": "sonar",
"messages": [
{"role": "system", "content": "Be precise and concise."},
{"role": "user", "content": (
"Tell me about Michael Jordan. "
"Please output a JSON object containing the following fields: "
"first_name, last_name, year_of_birth, num_seasons_in_nba. "
)},
],
"response_format": {
"type": "json_schema",
"json_schema": {"schema": AnswerFormat.model_json_schema()},
},
}
response = requests.post(url, headers=headers, json=payload).json()
print(response["choices"][0]["message"]["content"])
API Request & Response (if applicable)
Environment
- API Version: Current (model - sonar)
- Pydantic version (used for BaseModel): 2.11.7
- Operating System: Linux, Windows
Logs or Screenshots (if applicable)
Several runs of the code from the example:
PS C:\> & ... test.py
{ "first_name": "Michael", "last_name": "Jordan", "year_of_birth": 1963, "num_seasons_in_nba": 15 }
PS C:\> & ... test.py
{"first_name":"Michael","last_name":"Jordan","year_of_birth":1963,"num_seasons_in_nba":15}
PS C:\> & ... test.py
```json
{
"first_name": "Michael",
"last_name": "Jordan",
"year_of_birth": 1963,
"num_seasons_in_nba": 15
}
\```
Michael Jordan was born in 1963 and played 15 seasons in the NBA between 1984 and 2003[1][3][5].
PS C:\>
Additional Context
I’ve been using Structured Output in Perplexity for a long time, it stopped working recently