How to make AI follow a specific format
Quick answer
To make AI follow a specific format, explicitly define the desired structure in your prompt using clear instructions and examples. Use delimiters, bullet points, or JSON templates within the prompt to guide models like
gpt-4o to produce output in the exact format you need.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the OpenAI Python SDK and set your API key as an environment variable to authenticate requests.
pip install openai>=1.0 Step by step
Use explicit instructions and a clear example format in your prompt. This example shows how to instruct gpt-4o to output a JSON object with specific fields.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
prompt = '''
You are a helpful assistant. Please respond ONLY in the following JSON format without extra text:
{
"name": "string",
"age": integer,
"email": "string"
}
Example:
{
"name": "Alice",
"age": 30,
"email": "alice@example.com"
}
Now provide the JSON for a person named Bob, age 25, email bob@example.com.
'''
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}]
)
print(response.choices[0].message.content) output
{
"name": "Bob",
"age": 25,
"email": "bob@example.com"
} Common variations
You can adapt the format instructions for other output types like XML, CSV, or markdown tables. Also, use different models or async calls as needed.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
async def get_formatted_response():
prompt = '''
Respond ONLY in CSV format with columns: name,age,email
Example:
name,age,email
Alice,30,alice@example.com
Now provide data for Bob, 25, bob@example.com.
'''
response = await client.chat.completions.acreate(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}]
)
print(response.choices[0].message.content)
# To run async function:
# import asyncio
# asyncio.run(get_formatted_response()) output
name,age,email Bob,25,bob@example.com
Troubleshooting
If the AI output includes extra text or deviates from the format, reinforce instructions by adding phrases like "Respond ONLY with the format" or provide multiple examples. Also, consider using temperature=0 for deterministic output.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
prompt = '''
Respond ONLY with this JSON format, no extra text:
{
"title": "string",
"year": integer
}
Example:
{
"title": "Inception",
"year": 2010
}
Now provide the JSON for "The Matrix", 1999.
'''
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}],
temperature=0
)
print(response.choices[0].message.content) output
{
"title": "The Matrix",
"year": 1999
} Key Takeaways
- Always specify the exact output format with clear instructions and examples in your prompt.
- Use delimiters or structured templates like JSON or CSV to guide AI output.
- Set temperature=0 to reduce randomness and improve format adherence.
- Reinforce instructions by telling the AI to respond ONLY in the desired format.
- Test with multiple examples to ensure consistent formatting.