Thank you for providing additional details.
This error typically means the OpenAI SDK received an HTTP response from OpenRouter that was not valid JSON—often this is an HTML error page (like a 429 Too Many Requests or 5xx error), or a truncated/incomplete response, which the SDK then tries to parse as JSON and fails.
Key points:
- The OpenRouter API is OpenAI-compatible, but the SDK expects all responses to be valid JSON. If the server returns a non-JSON error (like an HTML error page), you'll get a JSONDecodeError.
- This is more likely to happen during rate limits, provider timeouts, or upstream service issues.
How to debug and mitigate:
- Log the full HTTP response body (not just the error/traceback) when a JSONDecodeError occurs. This will often show the true server-side error (like 429, 502, etc.).
- Check for rate limiting and upstream errors. If you see HTML with messages like "Too many requests" or "Internal server error," those are often the root cause.
- Add error handling for non-JSON responses before passing to the OpenAI SDK for parsing.
Reference from docs:
"OpenRouter provides an OpenAI-compatible completion API... In the examples below, the OpenRouter-specific headers are optional."
See more about API usage and supported error handling in the API Reference and about provider routing and error recovery in Provider Routing.
Next steps:
- Try catching the exception and printing/logging the raw response.
- Consider implementing retry logic or handling for HTML/non-JSON error responses.
If you continue experiencing issues or find the problem only occurs with "gpt-5", please confirm the exact model identifier and check if...