How to beginner · 3 min read

How to use DeepSeek R1 for coding

Quick answer
Use the deepseek-reasoner model via the OpenAI-compatible Python SDK by setting your DEEPSEEK_API_KEY in os.environ. Call client.chat.completions.create() with model="deepseek-reasoner" and provide your coding prompt in messages to get AI-generated code completions.

PREREQUISITES

  • Python 3.8+
  • DeepSeek API key
  • pip install openai>=1.0

Setup

Install the OpenAI Python SDK, which is compatible with DeepSeek's API. Set your DeepSeek API key as an environment variable DEEPSEEK_API_KEY before running your code.

bash
pip install openai>=1.0

Step by step

This example shows how to call DeepSeek's deepseek-reasoner model to generate code completions for a Python function prompt.

python
import os
from openai import OpenAI

# Initialize client with DeepSeek API key and base URL
client = OpenAI(api_key=os.environ["DEEPSEEK_API_KEY"], base_url="https://api.deepseek.com")

response = client.chat.completions.create(
    model="deepseek-reasoner",
    messages=[{"role": "user", "content": "Write a Python function to check if a number is prime."}]
)

print(response.choices[0].message.content)
output
def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            return False
    return True

Common variations

  • Use different prompts to generate code snippets or explanations.
  • Change max_tokens or temperature parameters for output length and creativity.
  • Use async calls with asyncio and await for non-blocking requests.
python
import asyncio
from openai import OpenAI

async def async_code_generation():
    client = OpenAI(api_key=os.environ["DEEPSEEK_API_KEY"], base_url="https://api.deepseek.com")
    response = await client.chat.completions.acreate(
        model="deepseek-reasoner",
        messages=[{"role": "user", "content": "Generate a Python class for a linked list."}]
    )
    print(response.choices[0].message.content)

asyncio.run(async_code_generation())
output
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            return
        last = self.head
        while last.next:
            last = last.next
        last.next = new_node

Troubleshooting

  • If you get authentication errors, verify your DEEPSEEK_API_KEY is set correctly in your environment.
  • For network errors, check your internet connection and DeepSeek service status.
  • If responses are incomplete, increase max_tokens in the request.

Key Takeaways

  • Use the OpenAI Python SDK with base_url="https://api.deepseek.com" to access DeepSeek models.
  • The deepseek-reasoner model is optimized for coding and reasoning tasks.
  • Always set your API key securely via environment variables to avoid credential leaks.
Verified 2026-04 · deepseek-reasoner
Verify ↗