fix: add error transform to cryptic openAI SDK errors when API key is invalid#7586
fix: add error transform to cryptic openAI SDK errors when API key is invalid#7586
Conversation
| this.options = options | ||
|
|
||
| // LM Studio uses "noop" as a placeholder API key, but we should still validate if a real key is provided | ||
| const apiKey = "noop" |
There was a problem hiding this comment.
Is this intentional? The comment on line 28 suggests we should validate a real key if provided, but we're always validating the hardcoded "noop" string. Should we check for a real API key from options.lmStudioApiKey first before defaulting to "noop"?
| const charCode = apiKey.charCodeAt(i) | ||
| if (charCode > 255) { | ||
| throw new Error( | ||
| `Invalid ${providerName} API key: contains non-ASCII character at position ${i + 1}. ` + |
There was a problem hiding this comment.
The error message says "non-ASCII character" but we actually allow extended ASCII (128-255). Would it be clearer to say something like "character above 255" or "non-ByteString compatible character" to be more precise about what's actually being validated?
| for (let i = 0; i < apiKey.length; i++) { | ||
| const charCode = apiKey.charCodeAt(i) | ||
| if (charCode > 255) { | ||
| throw new Error( |
There was a problem hiding this comment.
Could we enhance the error message to also show the actual character that caused the issue? This might help with debugging. For example, including the character itself and its character code in the error message.
| ) | ||
| }) | ||
|
|
||
| it("should handle undefined and empty keys", () => { |
There was a problem hiding this comment.
Consider adding test cases for null values (in addition to undefined), very long API keys to ensure performance, and keys with only extended ASCII characters. This would make the test coverage even more comprehensive.
f90906a to
3c43bc0
Compare
…ible providers - Created shared validation utility to check for non-ASCII characters in API keys - Added validation to all OpenAI-compatible providers to prevent ByteString conversion errors - Provides clear error messages when invalid characters are detected - Fixes issue #7483 where non-ASCII characters in API keys caused cryptic errors
…-time; centralize in openai-error-handler; remove provider/index specifics; i18n: add invalidKeyInvalidChars, remove invalidKeyNonAscii
…truct clients directly and keep request-time error mapping via handleOpenAIError
…atch and use handleOpenAIError directly
3c43bc0 to
fc6e9c6
Compare
- Wrap all non-ByteString errors with provider-specific prefix - Handle potential undefined error messages safely - Maintain consistent error format expected by unit tests
- Add providerName constant to all provider classes that use handleOpenAIError - Replace hardcoded provider name strings with this.providerName - Improves maintainability and consistency across providers - Affected providers: OpenAI, HuggingFace, LM Studio, Ollama, OpenRouter, Requesty, xAI
- Replace hardcoded 'OpenAI' strings with this.providerName property - Follows same pattern as other provider classes for consistency
Summary
Display a localized, provider-agnostic error when an API key contains invalid characters, triggered at request time for OpenAI-compatible providers.
Problem
If the API key contains invalid characters (for example emojis or other non-ASCII symbols), the OpenAI SDK surfaces a low-level ByteString error that is unclear to users.
Implementation
i18n
User impact
Users see a clear, localized message instead of a cryptic low-level error when their API key contains invalid characters.
Fixes #7483