DEV Community

Daniel Dong
Daniel Dong

Posted on

How to Get Reliable JSON from Any AI Model (Every Time)

AI output unpredictable? Need strict JSON for your app?

The fix: response_format: { "type": "json_object" }

Works with OpenAI, DeepSeek, Qwen, GLM — same parameter.

from openai import OpenAI

client = OpenAI(
    api_key="mb_your_key",
    base_url="https://aibridge-api.com/v1"
)

response = client.chat.completions.create(
    model="deepseek-v4-pro",  # or qwen3-235b-a22b, glm-4-plus...
    messages=[{"role": "user", "content": "Return a JSON with name, age, city"}],
    response_format={ "type": "json_object" }  # ← Force JSON
)

print(response.choices[0].message.content)
# {"name": "Alice", "age": 30, "city": "Shanghai"}
Enter fullscreen mode Exit fullscreen mode

Why this matters:
✅ No more regex parsing
✅ No more "sorry, I can't" non-JSON replies
✅ Works across 14+ models (test which is most reliable!)

Pro tip: Test JSON mode reliability across models:

for model in ["deepseek-v4-pro", "qwen3-235b-a22b", "glm-4-plus"]:
    # Same prompt, compare JSON reliability
    ...
Enter fullscreen mode Exit fullscreen mode

Try it: https://aibridge-api.com

Reliable AI output starts here. 🔒

mainpage

models

playground

pricing

Top comments (0)