diff --git a/llm_providers.py b/llm_providers.py index c1397d7..00a2d12 100644 --- a/llm_providers.py +++ b/llm_providers.py @@ -222,8 +222,17 @@ async def _call_openrouter_async(model: str, prompt: str, system_prompt: str = N async with httpx.AsyncClient() as client: res = await client.post(url, json=payload, headers=headers, timeout=120) if res.status_code == 200: - return res.json()["choices"][0]["message"]["content"] - return f"Erro OpenRouter: {res.status_code} - {res.text}" + data = res.json() + if "choices" in data and len(data["choices"]) > 0: + return data["choices"][0]["message"]["content"] + return f"Erro OpenRouter (Resposta sem 'choices'): {json.dumps(data)}" + + # Se não for 200, tenta extrair erro detalhado + try: + error_data = res.json() + return f"Erro OpenRouter {res.status_code}: {json.dumps(error_data)}" + except: + return f"Erro OpenRouter: {res.status_code} - {res.text}" except Exception as e: return f"Erro OpenRouter: {str(e)}"