How to extract reasoning steps from DeepSeek R1
Quick answer
Use the
deepseek-reasoner model via the OpenAI-compatible OpenAI Python SDK by sending a prompt that requests step-by-step reasoning. Parse the response's choices[0].message.content to extract the reasoning steps explicitly provided by the model.PREREQUISITES
Python 3.8+DeepSeek API keypip install openai>=1.0
Setup
Install the openai Python package and set your DeepSeek API key as an environment variable.
- Run
pip install openaito install the SDK. - Export your API key in your shell:
export DEEPSEEK_API_KEY='your_api_key_here'.
pip install openai Step by step
Use the deepseek-reasoner model to request reasoning steps by prompting it explicitly. The example below sends a question and asks the model to provide detailed reasoning steps in the response.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["DEEPSEEK_API_KEY"], base_url="https://api.deepseek.com")
prompt = (
"Explain the reasoning steps for the following problem:\n"
"If a train travels 60 miles in 1.5 hours, what is its average speed?"
" Please provide step-by-step reasoning."
)
response = client.chat.completions.create(
model="deepseek-reasoner",
messages=[{"role": "user", "content": prompt}]
)
reasoning_steps = response.choices[0].message.content
print("Reasoning steps from DeepSeek R1:\n", reasoning_steps) output
Reasoning steps from DeepSeek R1: 1. Identify the total distance traveled: 60 miles. 2. Identify the total time taken: 1.5 hours. 3. Use the formula for average speed: speed = distance / time. 4. Calculate speed = 60 miles / 1.5 hours = 40 miles per hour. 5. The average speed of the train is 40 mph.
Common variations
You can customize the request by adjusting max_tokens to control response length or use async calls for concurrency. You may also switch to other DeepSeek models for different tasks.
import asyncio
import os
from openai import OpenAI
async def async_reasoning():
client = OpenAI(api_key=os.environ["DEEPSEEK_API_KEY"], base_url="https://api.deepseek.com")
prompt = (
"Explain the reasoning steps for this math problem:\n"
"What is the sum of the first 10 natural numbers?"
" Provide detailed step-by-step reasoning."
)
response = await client.chat.completions.acreate(
model="deepseek-reasoner",
messages=[{"role": "user", "content": prompt}],
max_tokens=512
)
print("Async reasoning steps:\n", response.choices[0].message.content)
asyncio.run(async_reasoning()) output
Async reasoning steps: 1. The first 10 natural numbers are 1, 2, 3, ..., 10. 2. Use the formula for the sum of the first n natural numbers: n(n+1)/2. 3. Substitute n=10: 10 * 11 / 2 = 55. 4. The sum is 55.
Troubleshooting
- If you receive incomplete reasoning, increase
max_tokensto allow longer responses. - If the API returns authentication errors, verify your
DEEPSEEK_API_KEYenvironment variable is set correctly. - For network issues, check your internet connection and DeepSeek service status.
Key Takeaways
- Use the
deepseek-reasonermodel with explicit prompts requesting step-by-step reasoning. - Parse
response.choices[0].message.contentto extract detailed reasoning steps. - Adjust
max_tokensto control the length and completeness of reasoning outputs.