High severity beginner · Fix: 2-5 min

APIConnectionError

openai.APIConnectionError (when routing to Qwen DashScope via OpenAI SDK)

What this error means
Qwen DashScope OpenAI-compatible endpoint base_url is misconfigured, causing the OpenAI SDK to route requests to the wrong URL, resulting in connection failures, 404 errors, or authentication failures.

Stack trace

traceback
openai.APIConnectionError: Connection error: Unable to establish a connection to https://api.openai.com/v1/chat/completions (Failed to resolve 'api.openai.com' for url https://api.openai.com/v1/chat/completions)

Or:

HTTPError: 404 Not Found — The requested URL was not found on this server. Expected: https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation or similar Qwen endpoint
QUICK FIX
Set `base_url='https://dashscope.aliyuncs.com/compatible-mode/v1'` when initializing the OpenAI client and verify your DASHSCOPE_API_KEY environment variable is set and exported.

Why it happens

Qwen's DashScope service provides an OpenAI-compatible API endpoint, but the base_url must point to Qwen's servers (dashscope.aliyuncs.com or regional variant), not OpenAI's. When you initialize the OpenAI SDK without specifying base_url, it defaults to api.openai.com. Additionally, the DashScope endpoint structure differs from OpenAI's: Qwen uses `/api/v1/services/aigc/text-generation/generation` while OpenAI uses `/v1/chat/completions`. The SDK's path-rewriting logic may not correctly map between these incompatible structures.

Detection

Test your endpoint connectivity before deploying by making a simple HEAD or GET request to your base_url. Enable debug logging in the OpenAI SDK with `import logging; logging.basicConfig(level=logging.DEBUG)` to see the actual URL being requested and compare it against Qwen DashScope's documented endpoint.

Causes & fixes

1

base_url parameter is missing or defaults to OpenAI's endpoint

✓ Fix

Explicitly set base_url to Qwen DashScope: `client = OpenAI(api_key=os.environ['DASHSCOPE_API_KEY'], base_url='https://dashscope.aliyuncs.com/compatible-mode/v1')`

2

Using incorrect DashScope region or deprecated endpoint URL

✓ Fix

Use the correct region-specific endpoint. For mainland China use `https://dashscope.aliyuncs.com/compatible-mode/v1`, for international use `https://dashscope-intl.aliyuncs.com/compatible-mode/v1`, and verify the endpoint hasn't changed in Qwen's docs.

3

API key is for wrong service (OpenAI key instead of DashScope key) or missing altogether

✓ Fix

Ensure api_key is set to your DashScope API token from https://dashscope.console.aliyun.com, not an OpenAI key. Set it via: `api_key=os.environ['DASHSCOPE_API_KEY']` and verify the env var is exported.

4

OpenAI SDK version is too old and doesn't support the base_url parameter correctly with Qwen's path structure

✓ Fix

Upgrade to OpenAI SDK v1.3.0+ which includes improved base_url handling: `pip install --upgrade openai>=1.3.0`

Code: broken vs fixed

Broken - triggers the error
python
import os
from openai import OpenAI

# BROKEN: Missing base_url defaults to OpenAI's endpoint, causing routing failure
client = OpenAI(
    api_key=os.environ.get('DASHSCOPE_API_KEY')
    # base_url is not set — defaults to https://api.openai.com/v1
)

response = client.chat.completions.create(
    model='qwen2.5-7b-instruct',  # Qwen model on DashScope
    messages=[{'role': 'user', 'content': 'Hello'}]
)
print(response.choices[0].message.content)
Fixed - works correctly
python
import os
from openai import OpenAI

# FIXED: Explicitly set base_url to Qwen DashScope's OpenAI-compatible endpoint
client = OpenAI(
    api_key=os.environ['DASHSCOPE_API_KEY'],  # Set env var: export DASHSCOPE_API_KEY='sk-...'
    base_url='https://dashscope.aliyuncs.com/compatible-mode/v1'  # Correct Qwen endpoint
)

response = client.chat.completions.create(
    model='qwen2.5-7b-instruct',  # Qwen model on DashScope
    messages=[{'role': 'user', 'content': 'Hello'}]
)
print(response.choices[0].message.content)
Added explicit base_url parameter pointing to Qwen DashScope's OpenAI-compatible API endpoint, preventing the SDK from defaulting to OpenAI's servers and ensuring requests route to the correct service.

Workaround

If you cannot immediately update your environment variables, hardcode the base_url as a fallback in your client initialization: `base_url=os.environ.get('QWEN_BASE_URL', 'https://dashscope.aliyuncs.com/compatible-mode/v1')`. This allows override via env var but doesn't fail silently. For multiple regions, create a helper function: `def get_dashscope_endpoint(region='cn'): endpoints = {'cn': 'https://dashscope.aliyuncs.com/compatible-mode/v1', 'intl': 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1'}; return endpoints.get(region, endpoints['cn'])`

Prevention

Create a centralized configuration module (e.g., `config.py`) that validates all LLM endpoints at startup. Load endpoint URLs from environment variables with validation: `DASHSCOPE_BASE_URL = os.environ.get('DASHSCOPE_BASE_URL', '').strip(); assert DASHSCOPE_BASE_URL.startswith('https://'), 'Invalid base_url'`. Test endpoint connectivity in your CI/CD pipeline with a simple health check before deploying. Use type-hinted factory functions that return configured clients to prevent re-initialization mistakes: `def create_qwen_client() -> OpenAI: return OpenAI(api_key=..., base_url=...)`

Python 3.8+ · openai >=1.3.0 · tested on 1.40.0+
Verified 2026-04 · qwen2.5-7b-instruct, qwen2.5-14b-instruct, qwen2.5-32b-instruct, qwen-plus, qwen-turbo
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.