linshenkx/prompt-optimizer · error · APIError
Cross-origin connection failed: ${error.message}
Error message
Cross-origin connection failed: ${error.message} What it means
During model listing the fetch failed with 'Failed to fetch' or 'Connection error', and detectCrossOriginError determined the failure is CORS-related (browser blocked a cross-origin request). The original message is wrapped with the 'Cross-origin connection failed:' prefix.
Source
Thrown at packages/core/src/services/llm/adapters/openai-adapter.ts:175
if (models.length === 0) {
throw new APIError('API returned empty model list')
}
return models
}
throw new APIError('Unexpected API response format')
} catch (error: any) {
console.error('[OpenAIAdapter] Failed to fetch models:', error)
// 连接错误处理(包括跨域检测)
if (error.message && (error.message.includes('Failed to fetch') ||
error.message.includes('Connection error'))) {
const isCrossOriginError = this.detectCrossOriginError(error, baseURL)
if (isCrossOriginError) {
throw new APIError(`Cross-origin connection failed: ${error.message}`)
} else {
throw new APIError(`Connection failed: ${error.message}`)
}
}
// API返回的错误信息
if (error.response?.data) {
throw new APIError(`API error: ${JSON.stringify(error.response.data)}`)
}
// 其他错误,保持原始信息
throw new APIError(error.message || 'Unknown error')
}
}
// ===== 参数定义(用于buildDefaultModel) =====
/**View on GitHub (pinned to 3e677b1d9f)
Solutions
- Enable CORS on the target server (e.g. OLLAMA_ORIGINS=* for Ollama; CORS middleware on FastAPI/nginx)
- Or proxy requests through your own backend so the browser call is same-origin
- Verify scheme/port/hostname exactly match the allowed origins
- For production, never call provider APIs with secret keys directly from the browser
Example fix
# before (ollama blocks browser origin) # browser console: Cross-origin connection failed: Failed to fetch # after Ollama: OLLAMA_ORIGINS=http://localhost:5173 ollama serve # or route via backend proxy /api/llm -> target
Defensive patterns
Strategy: fallback
Validate before calling
// test CORS before first real call
fetch(endpoint, { method: 'OPTIONS', mode: 'cors' }).catch(() => flagEndpointAsCorsBlocked(endpoint)) Type guard
function isCorsError(e: unknown): boolean {
return e instanceof APIError && e.message.startsWith('Cross-origin connection failed')
} Try / catch
try { models = await adapter.getModelsAsync() }
catch (e) {
if (isCorsError(e)) { instructUserToEnableCors(); models = [] }
else throw e
} Prevention
- Enable CORS on gateways (OLLAMA_ORIGINS, nginx headers)
- Proxy cross-origin LLM calls through your backend
- Never embed provider API keys in browser code
When it happens
Trigger: Running the app in a browser and calling an OpenAI-compatible endpoint on another origin that doesn't return Access-Control-Allow-Origin — typical for local gateways (localhost:11434, :8000) accessed from a dev server on another port.
Common situations: Ollama/vLLM/llama.cpp called from a web app without CORS enabled, missing CORS middleware on a custom proxy, gateway allowing only certain origins.
Related errors
- Network error: ${error.message}
- Connection failed: ${error.message}
- formatExecutionErrorMessage(error)
- GENERATION_FAILED
- GENERATION_FAILED
AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27).
Data as JSON: /api/errors/7ef8f29f1acd8c81.
Report an issue: GitHub.