Completions api is not giving back only json

I am requesting a json, but get it back as a text:

{ "model": "sonar", "messages": [ { "role": "system", "content": "Be precise and concise." }, { "role": "user", "content": "Tell me about coalias.com. Please output a JSON object containing the following fields: business_name, business_summary, business_types, customer_type, country, language, saas, global, insights" } ], "response_format": { "type": "json_schema", "json_schema": { "schema": { "type": "object", "properties": { "business_name": { "type": "string", "description": "Official name of the business" }, "business_summary": { "type": "string", "description": "Brief overview of the business" }, "business_types": { "type": "array", "items": { "type": "string", "enum": [ "saas", "ecommerce", "webbuilder", "marketplace", "fintech", "edtech", "healthtech", "biotech", "ai", "consulting", "logistics", "traveltech", "medtech", "proptech", "foodtech", "cleantech", "gaming", "cybersecurity", "iot", "blockchain", "hrtech", "legaltech", "retail", "nonprofit", "agency", "media", "entertainment", "telecom" ] } }, "customer_type": { "type": "string", "enum": ["b2b", "b2c", "both", "b2g", "d2c", "c2c", "p2p"] }, "country": { "type": "string", "description": "Primary operating country" }, "language": { "type": "string", "description": "Primary business language" }, "saas": { "type": "boolean", "description": "Whether the business follows SaaS model" }, "global": { "type": "boolean", "description": "Whether the business targets global markets" }, "insights": { "type": "string", "description": "Detailed analysis of target audience, business model, and pricing" } }, "required": [ "business_name", "business_summary", "business_types", "customer_type", "country", "language", "saas", "global", "insights" ], "additionalProperties": false } } } }

It returns:

Here is a JSON object summarizing the key information about CoAlias:

I tried your request and was able to get a better-formatted completion with the following prompt. It seemed consistent over 10 or so requests:

      "role": "user",
      "content": "Tell me about coalias.com. Please output a JSON object containing the following fields: business_name, business_summary, business_types, customer_type, country, language, saas, global, insights. Do not include anything but the JSON object in your response. Do not include markdown formatting. Do not include ```json."
 "message": {
                "role": "assistant",
                "content": "{\n  \"business_name\": \"CoAlias\",\n  \"business_summary\": \"CoAlias is a plugin that simplifies the white-labeling of no-code applications by adding custom domains, dynamic subdomains, SEO optimization, branded emails, and more.\",\n  \"business_types\": [\"Software\", \"SaaS Management\"],\n  \"customer_type\": \"Businesses using no-code platforms\",\n  \"country\": \"Not specified\",\n  \"language\": \"English\",\n  \"saas\": true,\n  \"global\": true,\n  \"insights\": \"Provides domain performance insights and analytics\"\n}"
            },

You can parse the rest with jq, sed, or more easily in whatever language you’re using.

cat test.txt | jq -r '. | tojson' | sed -e 's/^"//' -e 's/"$//' | jq -r '.choices[0].message.content'

{
  "business_name": "CoAlias",
  "business_summary": "CoAlias is a plugin that simplifies the white-labeling of no-code applications by adding custom domains, dynamic subdomains, SEO optimization, branded emails, and more.",
  "business_types": ["Software", "SaaS Management"],
  "customer_type": "Businesses using no-code platforms",
  "country": "Not specified",
  "language": "English",
  "saas": true,
  "global": true,
  "insights": "Provides domain performance insights and analytics"
}

I’m trying the following payload, but in the response.choices[0].[message].[conent] I also get feedback from the assistant instead of just the json object
Payload:

payload = {
                "model": "sonar",
                "messages": [
                    {
                        "role": "system",
                        "content": (
                            f"Be precise and concise."
                        )
                    },
                    {
                        "role": "user",
                        "content": f"Find {max_results} latest news articles about '{query}'. "
                            "Please output a JSON object containing the following fields: "
                            "title, source, url, summary."
                    }
                ],
                "response_format": {
                    "type": "json_schema",
                    "json_schema": {"schema": NewsArticle.model_json_schema()},
                },
            }

Output:

I couldn't find 100 specific news articles about "TrumpCoin" in the search results. However, I can provide a summary of the available information in a JSON format based on the search results:

```json
[
  {
    "title": "Trump Issues Order on Crypto, and His Own Digital Coin",
    "source": "PSCA",
    "url": "https://www.psca.org/news/psca-news/2025/1/trump-issues-order-on-crypto-and-his-own-digital-coin/",
    "summary": "President Donald Trump issued an executive order to support the growth of the crypto industry and launched his own meme coin, sparking controversy over its concentrated ownership and potential political implications."
  },
  {
    "title": "Donald Trump's $TRUMP coin dominates crypto scene with explosive $14.5 billion debut",
    "source": "Economic Times",
    "url": "https://economictimes.com/news/international/global-trends/trump-coin-dominates-crypto-scene-with-explosive-14-5-billion-debut/amp_articleshow/117357561.cms",
    "summary": "Donald Trump's $TRUMP meme coin reached a market cap of $14.5 billion shortly after its launch, causing both excitement and skepticism due to its concentrated ownership and potential political motives."
  },
  {
    "title": "Trump launches his own $TRUMP meme coin, price soars overnight",
    "source": "CBS News",
    "url": "https://www.cbsnews.com/news/trump-launches-own-meme-coin-cryptocurrency/",
    "summary": "President Trump launched his own cryptocurrency, the $TRUMP meme coin, which quickly gained significant market capitalization, sparking discussions about its legitimacy and speculative nature."
  },
  {
    "title": "Donald and Melania Trump debuted meme coins — here's what to know",
    "source": "CBS News",
    "url": "https://www.cbsnews.com/news/donald-trump-melania-meme-coin-cryptocurrency-what-to-know/",
    "summary": "Donald and Melania Trump introduced their own meme coins, which are highly volatile and not intended as investment opportunities but rather as expressions of support or digital collectibles."
  }
]

For more articles, you might need to search through news databases or specific news websites.

One thing you can do is extract the JSON object like

    # Find the content inside a markdown code block
    if "```json" in content:
        json_start = content.find("```json") + 7
        json_end = content.find("```", json_start)
        if json_end != -1:
            return content[json_start:json_end].strip()

or more explicitly for the object

    # Find the outer braces in string if they exist
    json_start = content.find("{")
    json_end = content.rfind("}") + 1
    
    if json_start != -1 and json_end > json_start:
        return content[json_start:json_end]

In your case, where you’re returning an array of objects in the structured response, replace the braces with brackets.

If you run into the API returning newlines in the content response, this will get you to a properly-parsed list of objects:

    json_start = content.find("[")
    json_end = content.find("]") + 1
    
    if json_start != -1 and json_end > json_start:
        content = content[json_start:json_end].strip()
        content = ' '.join(content.split())
    
    parsed_json = json.loads(content)

No clue, I’m just somebody who is also building on the API. I’ve just accepted that Perplexity’s API is not dependable yet for structured responses and I’ve built in extraction logic that handles multiple cases. About 60% of the time I get a response in the correct schema and the other 40% have to be extracted and normalized.

Hi @advany & @mreddimasi, have you looked at their usage tier documentation?

To use structured outputs (json schema), you need to be in tier 3, which means you have to have to have paid at least $500 to their API (all time) to get access.

Currently their API will silently ignore your response_format parameter if you are not tier 3.

Wow. Insane. Thanks @AidanShipperley.

their API should not be silently ignoring parameters if you don’t have access.

100%, this would have saved me a day of writing parsing code to handle edge cases because I just wouldn’t have bothered.

Apologies for this - feedback taken and will make sure to improve the documentation for this.

Just lost 1-2 hrs today figuring out what’s wrong lol. Thanks everyone for pointing out the API silently ignores response_format unless you’re in tier 3 :confused: