When using the Perplexity API with the Sonar model, you may find that your carefully crafted system prompt is completely ignored. The model returns answers as if no instructions were given, defaulting to its base behavior. This happens because the Sonar model has a specific requirement for how system prompts are structured in the API call. This article explains the exact cause of this problem and provides a step-by-step fix to ensure your system prompt is applied correctly.
Key Takeaways: Fixing the Ignored System Prompt on Sonar
- API messages array role order: The
systemrole message must be the first item in themessagesarray, before anyuserorassistantmessages. - Explicit model parameter: Set the model parameter to
sonarorsonar-proin the request body to ensure the correct endpoint behavior. - No extra whitespace or formatting: Keep the system prompt content plain without markdown or extra line breaks to avoid parsing errors.
Why the Sonar Model Ignores the System Prompt
The Perplexity API supports multiple models, each with its own message handling logic. The Sonar model was designed to prioritize the user role message for context, especially when the system prompt is not placed at the very beginning of the messages array. If your API request sends the system prompt after a user message, or if the system prompt contains special characters or excessive formatting, the Sonar model may silently discard it. This behavior is not a bug but a design choice to optimize response speed and relevance for conversational queries. The fix requires precise ordering and formatting of the messages array.
How the API Messages Array Works
The API expects a JSON array named messages that contains one or more message objects. Each object has a role field (system, user, or assistant) and a content field. The order of these objects matters. For the Sonar model, the system message must be the first element. If any user or assistant message comes before the system message, the system prompt is ignored.
Common Mistake: Reversing the Order
Many developers build the messages array by appending the system prompt after the user query. This is a natural pattern when using code libraries that process user input first. The resulting array looks like [{role: "user", content: "..."}, {role: "system", content: "..."}]. The Sonar model treats the first message as the primary instruction and ignores subsequent system messages.
Steps to Fix the Ignored System Prompt
Follow these steps to correct your API request so the Sonar model respects your system prompt.
- Open your API client or code editor
Access the script, Postman collection, or curl command that sends requests to the Perplexity API endpointhttps://api.perplexity.ai/chat/completions. - Locate the messages array
Find the JSON body of your POST request. Look for themessagesarray. It should currently contain one or more objects withroleandcontentfields. - Move the system message to the first position
Ensure the object with"role": "system"is the very first element in the array. The corrected array should look like this:[{"role": "system", "content": "You are a helpful assistant that answers in French."}, {"role": "user", "content": "What is the capital of France?"}] - Verify the model parameter
Check that themodelfield in the request body is set to"sonar"or"sonar-pro". For example:"model": "sonar". This ensures the request is routed to the correct model handler. - Simplify the system prompt content
Remove any markdown formatting, HTML tags, or excessive line breaks from the system prompt. Keep the content plain and concise. For example, use"You are a helpful assistant."instead of"You are a helpful assistant." - Send the corrected request
Run the API call again. The response should now reflect your system prompt. Test with a simple instruction like"Answer in one sentence."to confirm the change.
If the System Prompt Is Still Ignored After the Fix
If the problem persists, check these additional causes and solutions.
API Key Has Limited Permissions
Your API key may be restricted to a specific model or usage tier. Verify that your key has access to the Sonar model by checking the Perplexity API dashboard under API Keys > Permissions. If the key is restricted to a different model, generate a new key with full model access.
System Prompt Exceeds Token Limit
The Sonar model has a maximum context length of 4096 tokens. If your system prompt plus user message exceed this limit, the model may truncate or ignore the prompt. Reduce the system prompt to under 500 tokens. Use the tokenizer tool in the Perplexity API documentation to count tokens.
Multiple System Messages in the Array
The Sonar model only recognizes the first system message. If you include more than one system message in the array, only the first is used. Remove any duplicate system messages and merge their content into a single system message at the start of the array.
Caching of Previous Requests
Some API clients cache responses to avoid redundant calls. Clear your client cache or add a unique user field to the request body to bypass caching. The user field can be a random string like "user": "test-1234".
Perplexity API Models: System Prompt Support Comparison
| Item | Sonar / Sonar-Pro | Other Models (e.g., Mixtral, Llama) |
|---|---|---|
| System prompt position requirement | Must be first in messages array | Any position is accepted |
| Maximum system prompt tokens | 500 tokens recommended | Up to 1024 tokens |
| Formatting sensitivity | Plain text only | Basic markdown allowed |
| Multiple system messages | Only first is used | All are merged |
The Sonar model requires stricter system prompt formatting than other Perplexity API models. Always place the system message first and keep it plain. For complex instructions, consider using a different model like mixtral-8x7b-instruct which accepts system prompts in any order.
You can now reliably set system prompts for the Sonar model by placing the system message first in the messages array and keeping its content plain. Test your fix with a simple instruction to confirm the model follows it. For advanced use, explore the Perplexity API documentation for the temperature and top_p parameters to further control the model’s behavior.