How to make AI return JSON output
Quick answer
To make AI return JSON output, explicitly instruct the model in your
prompt to respond only with JSON formatted data. Use clear delimiters and specify the JSON schema or keys you expect to receive to ensure consistent, parseable output.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the openai Python package and set your API key as an environment variable for secure access.
pip install openai>=1.0 Step by step
Use the OpenAI SDK to send a prompt that instructs the model to return JSON only. The example below requests a JSON object with specific keys.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
prompt = (
"Return a JSON object with keys 'name', 'age', and 'city'. "
"Respond ONLY with JSON, no extra text."
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}]
)
print(response.choices[0].message.content) output
{
"name": "Alice",
"age": 30,
"city": "New York"
} Common variations
You can adapt this pattern for different models like claude-3-5-sonnet-20241022 or use streaming for large JSON outputs. Also, specify JSON schema details in the prompt for complex structures.
import os
from anthropic import Anthropic
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
system_prompt = "You are a helpful assistant that only outputs JSON."
user_prompt = (
"Provide a JSON array of user objects with 'id' and 'email' fields. "
"Respond ONLY with JSON, no explanations."
)
message = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=500,
system=system_prompt,
messages=[{"role": "user", "content": user_prompt}]
)
print(message.content[0].text) output
[
{"id": 1, "email": "user1@example.com"},
{"id": 2, "email": "user2@example.com"}
] Troubleshooting
If the AI returns extra text or malformed JSON, refine your prompt to emphasize ONLY JSON output and use delimiters like triple backticks or specify JSON schema explicitly. Validate output with a JSON parser before use.
Key Takeaways
- Always instruct the AI explicitly to respond with JSON only to avoid extra text.
- Use clear JSON schema or example structures in your prompt for consistent output.
- Validate AI responses with a JSON parser to catch formatting errors early.