mastra-ai/mastra · error
Perplexity Search request failed with status ${response.stat
Error message
Perplexity Search request failed with status ${response.status}${text ? `: ${text}` : ''} What it means
perplexitySearchRequest performs the HTTP call to the Perplexity Search API; when the response is not ok it reads the response body (truncated to 1000 chars) and throws an error embedding the HTTP status and any error text returned by the server.
Source
Thrown at integrations/perplexity/src/client.ts:74
const apiKey = resolveApiKey(options?.apiKey);
const baseUrl = options?.baseUrl ?? DEFAULT_BASE_URL;
const fetchImpl = options?.fetch ?? fetch;
const response = await fetchImpl(`${baseUrl.replace(/\/$/, '')}/search`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify(body),
});
if (!response.ok) {
const rawText = await response.text().catch(() => '');
const MAX_ERROR_BODY = 1000;
const text =
rawText.length > MAX_ERROR_BODY ? `${rawText.slice(0, MAX_ERROR_BODY)}…` : rawText;
throw new Error(
`Perplexity Search request failed with status ${response.status}${text ? `: ${text}` : ''}`,
);
}
const json = (await response.json()) as Partial<PerplexitySearchResponse>;
return {
id: json.id,
results: Array.isArray(json.results) ? json.results : [],
};
}
View on GitHub (pinned to 75dd419e61)
Solutions
- Read the status and body in the message: 401 → fix the API key; 429 → add backoff/retry; 400 → fix the request payload (query, date filters).
- Validate request parameters (query non-empty, ISO-8601 date filter formats) before sending.
- Retry on 429/5xx with exponential backoff and a request timeout.
Example fix
// before
const res = await search({ query }); // throws on transient 429
// after
try {
const res = await search({ query });
} catch (e) {
if (String(e).includes('status 429')) await sleep(backoff);
else throw e;
} Defensive patterns
Strategy: retry
Validate before calling
if (!query?.trim()) throw new Error('query must be non-empty before calling Perplexity Search');
if (search_before_date_filter && !/^\d{4}-\d{2}-\d{2}/.test(search_before_date_filter)) throw new Error('invalid date filter format'); Try / catch
try {
const res = await perplexitySearchRequest(/* ... */);
} catch (e) {
const msg = (e as Error).message;
if (/status (429|5\d\d)/.test(msg) && attempt < MAX_RETRIES) return retryWithBackoff();
if (msg.includes('status 401')) throw new Error('Check PERPLEXITY_API_KEY');
throw e;
} Prevention
- Validate query and date-filter formats before each request
- Implement exponential backoff for 429/5xx
- Log the truncated response body included in the error for diagnostics
- Watch Perplexity status for outages
When it happens
Trigger: Any Perplexity Search request receiving a non-2xx response: 401 invalid/expired API key, 400 malformed request body (bad date filters, empty query), 429 rate limit, or 5xx server-side outage.
Common situations: Wrong-tier API key lacking search access; malformed search_before_date_filter values; hitting rate limits during bursts; transient Perplexity outages.
Understand the failure class
Background: "API error: {status}" and "HTTP 401/403/404/429/5xx" errors: non-2xx HTTP responses explained — this error's family across 27 libraries.
Related errors
- Failed to fetch Copilot models: ${response.status} ${respons
- Failed to list projects (${res.status})
- Failed to create project (${res.status})
- Failed to create access token (${res.status})
- Failed to fetch templates: ${response.statusText}
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/43e7b0fa256177c8.
Report an issue: GitHub.