-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fix OpenAI-compatible API error handling for DeepSeek and other providers #5725
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
…her providers - Add comprehensive error handling for connection issues (ECONNRESET, ECONNREFUSED, ETIMEDOUT, ENOTFOUND) - Add specific handling for "Premature close" and "Invalid response body" errors - Implement retry logic with exponential backoff for transient failures - Provide user-friendly error messages with actionable guidance - Handle HTTP status codes (401, 403, 404, 429, 500, 502, 503) with detailed explanations - Maintain backward compatibility with existing test expectations Fixes #5724
| /** | ||
| * Retry API calls with exponential backoff for transient failures | ||
| */ | ||
| private async retryApiCall<T>( |
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 retryApiCall function implements exponential backoff with jitter well. Consider extracting this logic into a shared utility to avoid duplication across providers.
This comment was generated because it violated a code review rule: irule_tTqpIuNs8DV0QFGj.
| const enabledLegacyFormat = this.options.openAiLegacyFormat ?? false | ||
| const isAzureAiInference = this._isAzureAiInference(modelUrl) | ||
| const deepseekReasoner = modelId.includes("deepseek-reasoner") || enabledR1Format | ||
| const ark = modelUrl.includes(".volces.com") |
Check failure
Code scanning / CodeQL
Incomplete URL substring sanitization High
.volces.com
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 5 months ago
To fix the issue, the code should parse the URL and validate its host explicitly. This ensures that the check is performed on the actual host component of the URL, preventing bypasses via embedded substrings in other parts of the URL.
Steps to implement the fix:
- Use the
URLclass to parse themodelUrland extract itshost. - Replace the substring check (
modelUrl.includes(".volces.com")) with a proper host validation usingendsWith(".volces.com"). - Ensure that the validation logic is robust and handles edge cases like subdomains correctly.
-
Copy modified lines R88-R95
| @@ -87,3 +87,10 @@ | ||
| const deepseekReasoner = modelId.includes("deepseek-reasoner") || enabledR1Format | ||
| const ark = modelUrl.includes(".volces.com") | ||
| const ark = (() => { | ||
| try { | ||
| const parsedUrl = new URL(modelUrl); | ||
| return parsedUrl.host.endsWith(".volces.com"); | ||
| } catch { | ||
| return false; // Invalid URL | ||
| } | ||
| })(); | ||
|
|
This PR addresses issue #5724 by implementing comprehensive error handling for OpenAI-compatible API providers, specifically targeting the "403 Invalid response body" and "Premature close" errors experienced with DeepSeek-R1 and other providers.
Changes Made
🔧 Error Handling Improvements
ECONNRESET,ECONNREFUSED,ETIMEDOUT, andENOTFOUNDerrors with user-friendly explanations🔄 Retry Logic
📝 User Experience
🧪 Testing
Files Modified
src/api/providers/openai.ts- Enhanced OpenAI provider with comprehensive error handlingsrc/api/providers/base-openai-compatible-provider.ts- Added error handling to base class for all OpenAI-compatible providersTesting
Impact
This fix will significantly improve the user experience when using DeepSeek-R1, Azure AI Inference Service, and other OpenAI-compatible providers by:
Fixes #5724
Important
Improves error handling and retry logic for OpenAI-compatible API providers, enhancing user experience and reliability.
ECONNRESET,ECONNREFUSED,ETIMEDOUT,ENOTFOUNDerrors inbase-openai-compatible-provider.tsandopenai.ts.retryApiCall().base-openai-compatible-provider.ts: Enhanced error handling and retry logic.openai.ts: Improved error handling and retry logic.This description was created by
for 1a9e4c4. You can customize this summary. It will automatically update as commits are pushed.