InvalidRequestError
openai.InvalidRequestError: base_url must be set to use DeepSeek API
Stack trace
Traceback (most recent call last):
File "app.py", line 42, in <module>
response = client.chat.completions.create(
File "/usr/local/lib/python3.11/site-packages/openai/resources/chat/completions.py", line 89, in create
return self._post(
File "/usr/local/lib/python3.11/site-packages/openai/client.py", line 1289, in post
return self._request(
openai.InvalidRequestError: Error code: 400 - {'error': {'message': 'Invalid API key or base_url not configured for DeepSeek. Use api.deepseek.com', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_request'}} Why it happens
The OpenAI SDK v1+ defaults to OpenAI's API endpoint (api.openai.com) when you don't explicitly provide a base_url parameter. DeepSeek-R1 uses a completely different API endpoint (api.deepseek.com) with its own authentication and request format. When you initialize the client without base_url=, the SDK attempts to route your request to OpenAI's servers, which reject it as an invalid API key or invalid request.
Detection
Before making your first chat completion call, verify that your OpenAI client is initialized with base_url='https://api.deepseek.com' by printing client.base_url or checking your environment variables. Add a unit test that confirms base_url is set before running integration tests.
Causes & fixes
Forgot to pass base_url parameter when creating the OpenAI client for DeepSeek
Add base_url='https://api.deepseek.com' to the OpenAI() initialization: client = OpenAI(api_key=os.environ['DEEPSEEK_API_KEY'], base_url='https://api.deepseek.com')
Using environment variables without setting OPENAI_BASE_URL or DEEPSEEK_BASE_URL
Set export DEEPSEEK_BASE_URL='https://api.deepseek.com' in your shell or .env file, then pass it explicitly: client = OpenAI(api_key=os.environ['DEEPSEEK_API_KEY'], base_url=os.environ.get('DEEPSEEK_BASE_URL', 'https://api.deepseek.com'))
Copy-pasted OpenAI client code and forgot to change the base_url for DeepSeek
Search your codebase for 'OpenAI(' and verify each instance has base_url set correctly: use regex: grep -n "OpenAI(" *.py and confirm DeepSeek clients include base_url='https://api.deepseek.com'
Using a wrapper or utility function that creates the OpenAI client without passing through base_url parameter
Update your wrapper function to accept and forward base_url as a parameter: def get_deepseek_client(api_key, base_url='https://api.deepseek.com'): return OpenAI(api_key=api_key, base_url=base_url)
Code: broken vs fixed
import os
from openai import OpenAI
# BROKEN: Missing base_url for DeepSeek API
client = OpenAI(
api_key=os.environ['DEEPSEEK_API_KEY']
# ↑ This defaults to api.openai.com, not api.deepseek.com
)
response = client.chat.completions.create(
model='deepseek-reasoner', # R1 model
messages=[
{'role': 'user', 'content': 'Solve 2+2'}
]
)
print(response.choices[0].message.content) import os
from openai import OpenAI
# FIXED: Explicitly set base_url to DeepSeek endpoint
client = OpenAI(
api_key=os.environ['DEEPSEEK_API_KEY'],
base_url='https://api.deepseek.com' # ← ADDED: Point to DeepSeek API
)
response = client.chat.completions.create(
model='deepseek-reasoner', # R1 model
messages=[
{'role': 'user', 'content': 'Solve 2+2'}
]
)
print(f"Response: {response.choices[0].message.content}") Workaround
If you can't modify the client initialization immediately, set the OPENAI_BASE_URL environment variable globally before importing the client: export OPENAI_BASE_URL='https://api.deepseek.com' in your shell or os.environ['OPENAI_BASE_URL'] = 'https://api.deepseek.com' at the top of your Python script before importing OpenAI. The SDK will read this environment variable automatically.
Prevention
Create a factory function or class method for DeepSeek clients that always includes the correct base_url: def create_deepseek_client(api_key=None): return OpenAI(api_key=api_key or os.environ['DEEPSEEK_API_KEY'], base_url='https://api.deepseek.com'). Use this factory everywhere instead of instantiating OpenAI directly. Add a CI test that verifies client.base_url == 'https://api.deepseek.com' for all DeepSeek instances.