How to install DeepSeek Python SDK
Quick answer
Install the DeepSeek Python SDK by using
pip install openai since DeepSeek is OpenAI-compatible and uses the openai package. Then, instantiate the client with OpenAI(api_key=os.environ["DEEPSEEK_API_KEY"]) to start making API calls.PREREQUISITES
Python 3.8+DeepSeek API keypip install openai>=1.0
Setup
DeepSeek does not have a separate Python SDK package. Instead, it is OpenAI-compatible and uses the openai Python package. Install it via pip and set your API key as an environment variable.
pip install openai Step by step
Use the openai package to create a client with your DeepSeek API key and call the chat completion endpoint with the deepseek-chat model.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["DEEPSEEK_API_KEY"])
response = client.chat.completions.create(
model="deepseek-chat",
messages=[{"role": "user", "content": "Hello from DeepSeek!"}]
)
print(response.choices[0].message.content) output
Hello from DeepSeek! How can I assist you today?
Common variations
You can switch to the reasoning model deepseek-reasoner for complex queries or adjust parameters like max_tokens and temperature. Async usage is not officially supported via the SDK but can be implemented with async HTTP clients.
response = client.chat.completions.create(
model="deepseek-reasoner",
messages=[{"role": "user", "content": "Explain quantum computing in simple terms."}],
max_tokens=500,
temperature=0.7
)
print(response.choices[0].message.content) output
Quantum computing uses quantum bits that can be in multiple states simultaneously, enabling faster problem solving for certain tasks.
Troubleshooting
- If you get authentication errors, verify your
DEEPSEEK_API_KEYenvironment variable is set correctly. - For network errors, check your internet connection and proxy settings.
- If the model name is unrecognized, confirm you are using the latest
openaipackage and correct model IDs.
Key Takeaways
- DeepSeek uses the OpenAI-compatible
openaiPython package for integration. - Set your API key in the environment variable
DEEPSEEK_API_KEYbefore running code. - Use
deepseek-chatfor general chat anddeepseek-reasonerfor reasoning tasks. - Adjust parameters like
max_tokensandtemperatureto customize responses. - Check environment variables and model names if you encounter errors.