{"record":{"id":"84134f6fa9354a21","repo":"Fosowl/agenticSeek","slug":"openrouter-api-error-str-e","errorCode":null,"errorMessage":"OpenRouter API error: {str(e)}","messagePattern":"OpenRouter API error: (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"sources/llm_provider.py","lineNumber":439,"sourceCode":"        \"\"\"\n        client = OpenAI(api_key=self.api_key, base_url=\"https://openrouter.ai/api/v1\")\n        if self.is_local:\n            # This case should ideally not be reached if unsafe_providers is set correctly\n            # and is_local is False in config for openrouter\n            raise Exception(\"OpenRouter is not available for local use. Change config.ini\")\n        try:\n            response = client.chat.completions.create(\n                model=self.model,\n                messages=history,\n            )\n            if response is None:\n                raise Exception(\"OpenRouter response is empty.\")\n            thought = response.choices[0].message.content\n            if verbose:\n                print(thought)\n            return thought\n        except Exception as e:\n            raise Exception(f\"OpenRouter API error: {str(e)}\") from e\n\n    def minimax_fn(self, history, verbose=False):\n        \"\"\"\n        Use MiniMax API to generate text via OpenAI-compatible interface.\n\n        Supported models:\n        - MiniMax-M3: Latest flagship model with enhanced reasoning and coding (default)\n        - MiniMax-M2.7: Previous flagship, kept for compatibility\n        - MiniMax-M2.7-highspeed: High-speed version of M2.7 for low-latency scenarios\n\n        Note: temperature must be in range (0.0, 1.0], default is 1.0\n        \"\"\"\n        load_dotenv()\n        base_url = os.getenv(\"MINIMAX_BASE_URL\", \"https://api.minimax.io/v1\")\n\n        client = OpenAI(api_key=self.api_key, base_url=base_url)\n        if self.is_local:\n            raise Exception(\"MiniMax is not available for local use. Change config.ini\")","sourceCodeStart":421,"sourceCodeEnd":457,"githubUrl":"https://github.com/Fosowl/agenticSeek/blob/ae57a2357745a9706cb12d0fd76d954c84d166fa/sources/llm_provider.py#L421-L457","documentation":"The catch-all in openrouter_fn: any exception raised inside the try block (network error from the OpenAI SDK, the empty-response error, HTTP 4xx/5xx) is re-wrapped as 'OpenRouter API error: <details>' with the original exception chained via 'from e'. This is the standard wrapper you'll see for all OpenRouter call failures.","triggerScenarios":"Any failure in openrouter_fn's try block: invalid API key (401), rate limits (429), unknown model, server 5xx, connection failures to openrouter.ai, or the internal 'OpenRouter response is empty.' raise.","commonSituations":"OPENROUTER/OPENAI_API_KEY not set or revoked; no internet access to openrouter.ai; invalid model slug; exceeding rate limits on free models; key lacking credit balance.","solutions":["Read the chained cause (__cause__) for the precise HTTP status and message","Verify the API key is set and valid for openrouter.ai","Confirm the model slug in config.ini is valid and you have credits","Check network connectivity/proxy settings for reaching openrouter.ai","Back off and retry on 429 rate-limit errors"],"exampleFix":"// before\nclient = OpenAI(api_key=None, base_url=\"https://openrouter.ai/api/v1\")\n// after\nclient = OpenAI(api_key=os.getenv(\"OPENROUTER_API_KEY\"), base_url=\"https://openrouter.ai/api/v1\")","handlingStrategy":"try-catch","validationCode":"import os, requests\ndef openrouter_ok():\n    key = os.getenv('OPENROUTER_API_KEY')\n    if not key:\n        return False\n    r = requests.get('https://openrouter.ai/api/v1/auth/key', headers={'Authorization': f'Bearer {key}'}, timeout=10)\n    return r.ok","typeGuard":null,"tryCatchPattern":"try:\n    out = provider.openrouter_fn(history)\nexcept Exception as e:\n    cause = e.__cause__\n    status = getattr(cause, 'status_code', None)\n    if status == 401:\n        log.error('OpenRouter auth failed: check API key')\n    elif status == 429:\n        time.sleep(30); retry()\n    else:\n        raise","preventionTips":["Keep OPENROUTER_API_KEY in .env and validate it at startup","Validate model slugs against the /models endpoint","Implement backoff retry for 429/5xx","Check __cause__ status codes to branch recovery logic"],"tags":["openrouter","api","error-wrapping"],"backgroundTag":"api-request-error","analyzedSha":"ae57a2357745a9706cb12d0fd76d954c84d166fa","analyzedAt":"2026-08-30T02:49:05.834Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}