-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: resolve OpenRouter 401 authentication errors #6460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- Add Authorization header with Bearer token to OpenRouter API requests - Include API key in getOpenRouterModels and getOpenRouterModelEndpoints functions - Add proper 401 error handling with descriptive error messages - Update GetModelsOptions type to include apiKey and baseUrl for OpenRouter - Add API key validation in OpenRouter handler constructor - Add comprehensive tests for authentication scenarios Fixes #6459
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your contribution! I've reviewed the changes and found that this PR effectively addresses the OpenRouter 401 authentication issue. The implementation adds proper authorization headers and error handling as described. I've identified one critical issue and several suggestions for improvement.
| } from "@roo-code/types" | ||
|
|
||
| import { getOpenRouterModelEndpoints, getOpenRouterModels } from "../openrouter" | ||
| import axios from "axios" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The axios import was added here but wasn't present in the original file structure. Could you verify this import is actually needed and properly available in the test environment?
| } | ||
| } catch (error) { | ||
| if (axios.isAxiosError(error) && error.response?.status === 401) { | ||
| console.error("OpenRouter API authentication failed. Please check your API key.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the duplication of the same error message in both console.error and throw new Error intentional? Could we streamline this to avoid redundant logging?
|
|
||
| // Validate API key format | ||
| if (apiKey === "not-provided" || !apiKey || apiKey.trim() === "") { | ||
| console.warn("OpenRouter API key is missing or invalid. This may cause authentication errors.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The API key validation happens in the constructor but API calls occur later. Could we consider more strict validation (e.g., checking expected prefixes or length) to catch format issues earlier?
|
|
||
| export type GetModelsOptions = | ||
| | { provider: "openrouter" } | ||
| | { provider: "openrouter"; apiKey?: string; baseUrl?: string } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The GetModelsOptions type makes apiKey optional, but the error handling suggests it's expected. Could we make this more explicit in the type definition for better type safety?
|
Issue seems solved |
This PR fixes the OpenRouter 401 authentication issue reported in #6459.
Problem
Users were experiencing "401 No auth credentials found" errors when using OpenRouter API keys. The issue occurred because:
getOpenRouterModelsandgetOpenRouterModelEndpoints) were not including the API key in their requestsSolution
GetModelsOptionstype to includeapiKeyandbaseUrlfor OpenRouterTesting
Related Issue
Fixes #6459
Type of Change
Important
Fix OpenRouter 401 authentication errors by adding authorization headers and error handling.
Authorizationheader with Bearer token to OpenRouter API requests ingetOpenRouterModelsandgetOpenRouterModelEndpoints.getOpenRouterModelsandgetOpenRouterModelEndpointswith descriptive error messages.OpenRouterHandlerconstructor, logging warnings for missing/invalid keys.GetModelsOptionsto includeapiKeyandbaseUrlfor OpenRouter.openrouter.spec.tsfor API key validation, authorization header inclusion, and 401 error handling.openrouter.spec.tsforgetOpenRouterModelsandgetOpenRouterModelEndpointsto ensure correct header usage and error handling.This description was created by
for d186eb0. You can customize this summary. It will automatically update as commits are pushed.