Replies: 8 comments 5 replies
-
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"
} |
Beta Was this translation helpful? Give feedback.
-
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
Output:
For more articles, you might need to search through news databases or specific news websites.
|
Beta Was this translation helpful? Give feedback.
-
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 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) |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
Wow. Insane. Thanks @AidanShipperley.
100%, this would have saved me a day of writing parsing code to handle edge cases because I just wouldn't have bothered. |
Beta Was this translation helpful? Give feedback.
-
Apologies for this - feedback taken and will make sure to improve the documentation for this. |
Beta Was this translation helpful? Give feedback.
-
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 :/ |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
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:
Beta Was this translation helpful? Give feedback.
All reactions