How to beginner · 3 min read

How to use DeepSeek structured outputs

Quick answer
Use the DeepSeek API by sending a prompt with instructions to output structured data (e.g., JSON). Parse the response.choices[0].message.content as JSON in your Python code to handle structured outputs effectively.

PREREQUISITES

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

Setup

Install the openai Python package (compatible with DeepSeek API) and set your DeepSeek API key as an environment variable.

  • Run pip install openai
  • Set environment variable DEEPSEEK_API_KEY with your API key
bash
pip install openai

Step by step

Use the OpenAI-compatible openai SDK to call DeepSeek's chat completions endpoint with a prompt requesting structured JSON output. Parse the response content as JSON for further processing.

python
import os
import json
from openai import OpenAI

client = OpenAI(api_key=os.environ["DEEPSEEK_API_KEY"], base_url="https://api.deepseek.com")

prompt = (
    "Generate a JSON object with keys 'name', 'age', and 'city' based on the following input:\n"
    "Input: John is 30 years old and lives in New York."
)

response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[{"role": "user", "content": prompt}]
)

content = response.choices[0].message.content

try:
    structured_output = json.loads(content)
    print("Parsed structured output:", structured_output)
except json.JSONDecodeError:
    print("Failed to parse JSON. Raw output:", content)
output
Parsed structured output: {'name': 'John', 'age': 30, 'city': 'New York'}

Common variations

You can customize the model, increase max_tokens, or use async calls with httpx or asyncio. Also, instruct the model to output other structured formats like XML or CSV by changing the prompt.

python
import os
import json
import asyncio
from openai import OpenAI

async def async_deepseek_call():
    client = OpenAI(api_key=os.environ["DEEPSEEK_API_KEY"], base_url="https://api.deepseek.com")
    prompt = "Generate a JSON list of fruits with their colors."
    response = await client.chat.completions.acreate(
        model="deepseek-chat",
        messages=[{"role": "user", "content": prompt}],
        max_tokens=200
    )
    content = response.choices[0].message.content
    try:
        data = json.loads(content)
        print("Async parsed output:", data)
    except json.JSONDecodeError:
        print("Async JSON parse failed. Raw:", content)

asyncio.run(async_deepseek_call())
output
Async parsed output: [{'fruit': 'apple', 'color': 'red'}, {'fruit': 'banana', 'color': 'yellow'}]

Troubleshooting

  • If JSON parsing fails, verify the prompt explicitly requests JSON output.
  • Check for trailing text or explanations in the response that break JSON parsing.
  • Use print(content) to debug raw output.
  • Ensure your API key and base_url are correctly set for DeepSeek.

Key Takeaways

  • Use the OpenAI-compatible openai SDK with base_url set to DeepSeek's API endpoint.
  • Request structured JSON output explicitly in your prompt for reliable parsing.
  • Parse response.choices[0].message.content as JSON to handle structured data.
  • Async calls are supported via acreate for scalable applications.
  • Debug parsing issues by inspecting raw response content and refining prompts.
Verified 2026-04 · deepseek-chat
Verify ↗