Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Jul 15, 2025

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

  • Connection Error Handling: Added specific handling for ECONNRESET, ECONNREFUSED, ETIMEDOUT, and ENOTFOUND errors with user-friendly explanations
  • Premature Close Handling: Implemented targeted error handling for "Premature close" and "Invalid response body" errors
  • HTTP Status Code Handling: Added comprehensive handling for HTTP status codes (401, 403, 404, 429, 500, 502, 503) with detailed explanations

🔄 Retry Logic

  • Exponential Backoff: Implemented retry logic with exponential backoff (1s, 2s, 4s delays) for transient failures
  • Smart Retry Logic: Only retries on transient errors (network issues, server errors), skips retries for client errors (4xx except 429)
  • Jitter: Added random jitter to prevent thundering herd problems

📝 User Experience

  • Actionable Error Messages: Replaced cryptic error messages with clear, actionable guidance
  • Provider-Specific Messages: Error messages include provider name and context-specific troubleshooting steps
  • Backward Compatibility: Maintained existing test expectations and error message formats

🧪 Testing

  • All Tests Pass: Ensured all existing tests continue to pass (354 tests passed, 1 skipped)
  • Error Message Compatibility: Preserved expected error message formats for Chutes, Groq, and OpenAI providers
  • Type Safety: All TypeScript type checks pass

Files Modified

  • src/api/providers/openai.ts - Enhanced OpenAI provider with comprehensive error handling
  • src/api/providers/base-openai-compatible-provider.ts - Added error handling to base class for all OpenAI-compatible providers

Testing

cd src && npx vitest api/providers/__tests__/ --run
# ✅ Test Files: 29 passed (29)
# ✅ Tests: 354 passed | 1 skipped (355)

Impact

This fix will significantly improve the user experience when using DeepSeek-R1, Azure AI Inference Service, and other OpenAI-compatible providers by:

  • Providing clear, actionable error messages instead of cryptic technical errors
  • Automatically retrying transient failures to improve reliability
  • Helping users understand and resolve connection issues quickly

Fixes #5724


Important

Improves error handling and retry logic for OpenAI-compatible API providers, enhancing user experience and reliability.

  • Error Handling:
    • Added handling for ECONNRESET, ECONNREFUSED, ETIMEDOUT, ENOTFOUND errors in base-openai-compatible-provider.ts and openai.ts.
    • Implemented handling for "Premature close" and "Invalid response body" errors.
    • Added HTTP status code handling for 401, 403, 404, 429, 500, 502, 503.
  • Retry Logic:
    • Introduced exponential backoff with jitter for transient errors in retryApiCall().
    • Smart retry logic to skip retries for client errors (4xx except 429).
  • User Experience:
    • Replaced cryptic error messages with actionable guidance.
    • Error messages include provider name and context-specific troubleshooting steps.
    • Maintained backward compatibility with existing tests.
  • Files Modified:
    • base-openai-compatible-provider.ts: Enhanced error handling and retry logic.
    • openai.ts: Improved error handling and retry logic.

This description was created by Ellipsis for 1a9e4c4. You can customize this summary. It will automatically update as commits are pushed.

…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
@roomote roomote bot requested review from cte, jr and mrubens as code owners July 15, 2025 08:22
@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. bug Something isn't working labels Jul 15, 2025
/**
* Retry API calls with exponential backoff for transient failures
*/
private async retryApiCall<T>(
Copy link
Contributor

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
' can be anywhere in the URL, and arbitrary hosts may come before or after it.

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:

  1. Use the URL class to parse the modelUrl and extract its host.
  2. Replace the substring check (modelUrl.includes(".volces.com")) with a proper host validation using endsWith(".volces.com").
  3. Ensure that the validation logic is robust and handles edge cases like subdomains correctly.
Suggested changeset 1
src/api/providers/openai.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/api/providers/openai.ts b/src/api/providers/openai.ts
--- a/src/api/providers/openai.ts
+++ b/src/api/providers/openai.ts
@@ -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
+				}
+			})();
 
EOF
@@ -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
}
})();

Copilot is powered by AI and may make mistakes. Always verify output.
Unable to commit as this autofix suggestion is now outdated
@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Jul 15, 2025
@daniel-lxs daniel-lxs moved this from Triage to Roomote/renovate BOT in Roo Code Roadmap Jul 15, 2025
@daniel-lxs daniel-lxs closed this Sep 16, 2025
@github-project-automation github-project-automation bot moved this from Renovate BOT to Done in Roo Code Roadmap Sep 16, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Sep 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

use OpenAI-compatible API, Request Failed 403 Invalid response body

3 participants