Getting continuously internal server error 500 while calling the sonar api

Prakhyat_Bhartiya
Mon, 13 Oct 2025 08:28:56 +0000
General

Steps to Reproduce

class AnswerFormat(BaseModel):
title: str
url: str
date: str
summary: str

class AnswerList(BaseModel):
search_query: str
results: List[AnswerFormat]

dt = datetime.now()
if user_prompt and system_prompt:
system_prompt = system_prompt
user_query = user_prompt
else:
system_prompt = SYSTEM_PROMPT_FOR_WEB_AGENT
user_query = f"user_query: {question}\n" + “client_categories:\n” + “\n”.join(
f"{k}: {v}" for k, v in bucket_company_map.items()
)

try :

headers = {
    "Authorization": f'Bearer {PERPLEXITY_CONF\["PERPLEXITY_API_KEY"\]}',
    "Content-Type": "application/json"
}

payload = {
    "search_mode": "web",
    "reasoning_effort": "medium",
    "temperature": 0,
    "top_p": 0.95,
    "return_images": False,
    "return_related_questions": False,
    "top_k": 0,
    "stream": False,
    "presence_penalty": 0,
    "frequency_penalty": 0,
    "web_search_options": {
        "search_context_size": "high"
    },
    "model": PERPLEXITY_CONF\["PERPLEXITY_MODEL"\],
    "messages": \[
        {
            "content": system_prompt,
            "role": "system"
        },
        {
            "content": user_query,
            "role": "user"
        }
    \],
    "response_format": {
        "type": "json_schema",
        "json_schema": {
            "schema": AnswerList.model_json_schema()
        }
    }
}

response = requests.post(PERPLEXITY_CONF\["PERPLEXITY_API_URL"\], headers=headers, json=payload).json()

Some of the times I am getting correct results but mostly i am getting server error as 500

3 posts - 2 participants