Perplexity API Returns Empty Answer With Valid Key: Fix
🔍 WiseChecker

Perplexity API Returns Empty Answer With Valid Key: Fix

You have a valid Perplexity API key, but your requests return empty or null answers. The API call succeeds without an error, yet the response contains no content. This problem often occurs due to missing or incorrect parameters in the request body or headers. This article explains the root causes and provides step-by-step fixes to get your API working correctly.

Key Takeaways: Fixing Empty API Responses from Perplexity

  • Set the “model” parameter: Perplexity requires a specific model name like “sonar-pro” or “sonar-small-online” in the request body. Omitting it returns an empty answer.
  • Include “stream: false”: Non-streaming requests must explicitly set stream to false. Without this, the API may return incomplete data.
  • Check the request endpoint: Use https://api.perplexity.ai/chat/completions — not the legacy or incorrect endpoint URL.

ADVERTISEMENT

Why Perplexity API Returns Empty Answers With a Valid Key

The Perplexity API follows the OpenAI-compatible chat completions format. When you send a request, the API expects a JSON body with specific required fields. If any required field is missing or malformed, the API may return a 200 status with an empty choices array instead of a proper error. This behavior differs from many other APIs that return a clear 400 error.

The two most common missing fields are model and stream. Without a model name, the API does not know which engine to use and defaults to no output. Without setting stream to false, the API expects a streaming connection and may close the connection early for non-streaming clients, resulting in an empty response.

Another cause is an incorrect endpoint URL. The correct endpoint is https://api.perplexity.ai/chat/completions. Some older documentation points to https://api.perplexity.ai/v1/chat/completions, which no longer works and returns empty results.

Finally, the API key must be passed in the Authorization header as Bearer YOUR_API_KEY. If the key is passed in the body or as a query parameter, the API ignores it and treats the request as unauthenticated, returning an empty response.

Steps to Fix Empty API Responses From Perplexity

  1. Verify the request endpoint
    Ensure your POST request targets https://api.perplexity.ai/chat/completions. Do not use /v1/ in the path. Test with curl: curl -X POST https://api.perplexity.ai/chat/completions -H "Authorization: Bearer YOUR_KEY" -H "Content-Type: application/json" -d '{"model": "sonar-pro", "messages": [{"role": "user", "content": "Hello"}], "stream": false}'
  2. Include the model parameter in the request body
    Add "model": "sonar-pro" or "model": "sonar-small-online" to your JSON body. Perplexity does not have a default model. Without this field, the API returns an empty choices array.
  3. Set stream to false for non-streaming requests
    Add "stream": false to the request body. If you omit this, the API treats the request as streaming and may return no data to a non-streaming client.
  4. Check the Authorization header format
    Use Authorization: Bearer YOUR_API_KEY. Do not include the key in the request body or as a query parameter. Verify the key is active in the Perplexity dashboard under API Keys.
  5. Validate the messages array structure
    The messages array must contain at least one object with role and content fields. Supported roles are system, user, and assistant. Example: "messages": [{"role": "system", "content": "Be precise"}, {"role": "user", "content": "What is the capital of France?"}]
  6. Test with a minimal request using curl
    Run this command in your terminal, replacing YOUR_KEY with your actual API key: curl -s -X POST https://api.perplexity.ai/chat/completions -H "Authorization: Bearer YOUR_KEY" -H "Content-Type: application/json" -d '{"model": "sonar-small-online", "messages": [{"role": "user", "content": "Say hi"}], "stream": false}'. You should see a JSON response with a choices array containing a message object.

ADVERTISEMENT

If the API Still Returns Empty Answers After the Main Fix

Response Contains Only Usage Data but No Choices

If the response includes usage fields but choices is empty, your request likely has a content filter trigger. Perplexity may block certain prompts without returning an error. Review your messages for prohibited content. Try a neutral prompt like “Hello” to isolate the issue.

Error Code 401 or 403 Instead of Empty Response

If you see HTTP 401 or 403, the API key is invalid or expired. Generate a new key from the Perplexity dashboard under API Keys. Ensure the key has the correct permissions and is not revoked.

Response Contains Only an ID and Object Field

This indicates the request reached the API but the model field was missing or misspelled. Double-check the model name. Valid models include sonar-pro, sonar-small-online, and sonar-medium-online. The model name is case-sensitive.

Rate Limiting Causes Empty Responses

If you send too many requests per minute, Perplexity may rate-limit your key and return empty responses. Check the x-ratelimit-remaining header in the API response. Wait 60 seconds before retrying.

Perplexity API Request Variations: Correct vs Incorrect Parameters

Item Correct Request Incorrect Request
Endpoint https://api.perplexity.ai/chat/completions https://api.perplexity.ai/v1/chat/completions
Model field “model”: “sonar-pro” Missing or “model”: “”
Stream field “stream”: false Missing or “stream”: true (for non-streaming clients)
Authorization Header: Authorization: Bearer KEY Key in body or query
Messages format Array with role and content Array with missing role or content

After applying the fixes above, your API should return the expected text in the choices[0].message.content field. If the issue persists, check the Perplexity status page for service outages. You can also enable verbose logging in your code to see the exact request being sent.

ADVERTISEMENT