DEV Community

Daniel Dong
Daniel Dong

Posted on

Make AI Return Valid JSON Every Time

Tired of parsing broken JSON from AI responses? Here's how to make AI output perfectly structured JSON — guaranteed, every time.

AI generating broken JSON is the #1 frustration I see developers complain about. Missing braces, trailing commas, random explanations mixed in.

Here's how to fix it.

The Problem

# AI response: "Sure! Here's the data: {"name": "Alice", "age": 30}"
# ← Parse error: "Sure! Here's the data:" is not valid JSON

json.loads(response)  # 💥 json.JSONDecodeError
Enter fullscreen mode Exit fullscreen mode

The Fix: System Prompt + Function Calling

response = client.chat.completions.create(
    model="deepseek-v4-pro",
    messages=[
        {"role": "system", "content": "Always respond with valid JSON only. No markdown. No explanations."},
        {"role": "user", "content": "Extract name and age from: Alice is 30 years old"}
    ],
    response_format={"type": "json_object"}  # ← Force JSON output
)

data = json.loads(response.choices[0].message.content)
print(data)  # {"name": "Alice", "age": 30}  ← Always valid
Enter fullscreen mode Exit fullscreen mode

Real Example: Text Extraction

def extract_entities(text):
    response = client.chat.completions.create(
        model="deepseek-v4-pro",
        messages=[
            {"role": "system", "content": "Extract entities as JSON. Format: {people: [], dates: [], companies: []}"},
            {"role": "user", "content": text}
        ],
        response_format={"type": "json_object"}
    )
    return json.loads(response.choices[0].message.content)

# Works every time — no regex, no parsing hacks
entities = extract_entities("Mark met Sarah at Google on July 3rd")
# {"people": ["Mark", "Sarah"], "companies": ["Google"], "dates": ["July 3rd"]}
Enter fullscreen mode Exit fullscreen mode

Why This Matters

Before After
Parse errors 30% of time Parse errors 0% of time
Try/except everywhere One clean call
Regex hacks to fix JSON Pure structured data
Can't build on top Reliable API pipeline

Try It

  1. Copy the code above (one parameter fixes everything)
  2. Get a free API key → aibridge-api.com
  3. Use response_format={"type": "json_object"} on all 14 models
  4. Never json.loads() fail again

11

22

33

44

Top comments (0)