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
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
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"]}
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
- Copy the code above (one parameter fixes everything)
- Get a free API key → aibridge-api.com
- Use response_format={"type": "json_object"} on all 14 models
- Never json.loads() fail again




Top comments (0)