Mastering JSON Prompts for LLMs: Practical Python Examples
'Learn how JSON prompting turns vague instructions into precise, machine-readable requests for LLMs, with Python examples comparing free-form and JSON outputs to show the gains in consistency and integration.'
What is JSON prompting
JSON Prompting is a technique for structuring instructions to AI models using the JavaScript Object Notation (JSON) format, making prompts clear, explicit, and machine-readable. Unlike free-form text prompts that can be ambiguous, JSON prompts organize requirements as key-value pairs, arrays, and nested objects. That transforms vague requests into precise blueprints the model can follow, improving consistency and accuracy for complex or repetitive tasks.
Why structured prompts matter
By specifying fields like task, topic, audience, and output format, you reduce ambiguity and make model outputs easier to parse and integrate into downstream systems. Many LLMs, including GPT-4, Claude, and Gemini, perform better when given predictable, well-structured input. JSON prompting is particularly valuable when you need consistent results for workflows such as summaries, data extraction, project trackers, dashboards, or automated handlers.
Setup and dependencies
Install the OpenAI client library and set your API key before running the examples.
pip install openaiimport os
from getpass import getpass
os.environ["OPENAI_API_KEY"] = getpass('Enter OpenAI API Key: ')To get an OpenAI API key, visit https://platform.openai.com/settings/organization/api-keys and generate a new key. New users may need to add billing details and make a minimum payment to activate access.
from openai import OpenAI
client = OpenAI()Structured prompts ensure consistency
Thinking in terms of fields and values forces a predictable output format. Defining a fixed response structure eliminates guesswork and helps guarantee that every response follows the same pattern. Below are practical examples comparing free-form prompts to JSON-based prompts so you can see the difference.
Example 1 — Summarize an email (free-form)
Free-form prompt sent to the model:
prompt_text = """
Summarize the following email and list the action items clearly.
Email:
Hi team, let's finalize the marketing plan by Tuesday. Alice, prepare the draft; Bob, handle the design.
"""
response_text = client.chat.completions.create(
model="gpt-5",
messages=[{"role": "user", "content": prompt_text}]
)
text_output = response_text.choices[0].message.content
print(text_output)Typical free-form output:
Summary:
The team needs to finalize the marketing plan by Tuesday. Alice will prepare the draft, and Bob will handle the design.
Action items:
- Alice: Prepare the draft of the marketing plan by Tuesday.
- Bob: Handle the design by Tuesday.
- Team: Finalize the marketing plan by Tuesday.Example 1 — Summarize an email (JSON prompt)
Now structure the same request so the model returns a strict JSON schema.
prompt_json = """
Summarize the following email and return the output strictly in JSON format:
{
"summary": "short summary of the email",
"action_items": ["task 1", "task 2", "task 3"],
"priority": "low | medium | high"
}
Email:
Hi team, let's finalize the marketing plan by Tuesday. Alice, prepare the draft; Bob, handle the design.
"""
response_json = client.chat.completions.create(
model="gpt-5",
messages=[
{"role": "system", "content": "You are a precise assistant that always replies in valid JSON."},
{"role": "user", "content": prompt_json}
]
)
json_output = response_json.choices[0].message.content
print(json_output)Example JSON output from the model:
{
"summary": "Finalize the marketing plan by Tuesday; Alice to draft and Bob to handle design.",
"action_items": [
"Alice: prepare the draft",
"Bob: handle the design",
"Team: finalize the marketing plan by Tuesday"
],
"priority": "medium"
}This JSON result is easy to parse programmatically and integrates cleanly into tools or pipelines.
Example 2 — Market update (free-form)
Free-form prompt asking for a short market summary, sentiments, opportunities, risks, and a confidence score:
plain_text_prompt = """
Analyze the following market update:
Market Text:
Tesla's Q2 earnings beat expectations due to higher Model Y sales, but rising competition from BYD is a risk.
Apple reported steady revenue growth driven by iPhone sales, but services revenue slightly declined.
Amazon's AWS division continues to dominate cloud computing, though regulatory scrutiny in Europe is increasing.
Generate:
- A 2-line market summary
- Sentiment for each company (positive, negative, neutral)
- Key growth opportunities and risks
- A confidence score from 0 to 10
"""
response_plain = client.chat.completions.create(
model="gpt-5",
messages=[{"role": "user", "content": plain_text_prompt}]
)
plain_output = response_plain.choices[0].message.content
print(plain_output)Typical free-form output:
Market summary:
- Earnings updates skew constructive: Tesla beat on Q2 with strong Model Y, Apple grew on iPhone, and AWS remains the cloud leader.
- Offsetting risks include BYD pressure on Tesla, Apple's services dip, and rising European scrutiny on Amazon.
Sentiment:
- Tesla: Positive
- Apple: Neutral
- Amazon: Positive
Key growth opportunities and risks:
- Tesla
- Opportunities: Sustain Model Y momentum; scale production and pricing to drive volume.
- Risks: Intensifying competition from BYD could pressure share and margins.
- Apple
- Opportunities: Monetize large iPhone base; re-accelerate services via bundles and ecosystem engagement.
- Risks: Services softness; dependence on iPhone for top-line growth.
- Amazon (AWS)
- Opportunities: Leverage leadership to win more enterprise/AI workloads and multi-year commitments.
- Risks: European regulatory scrutiny may lead to fines, compliance costs, or contract/pricing constraints.
Confidence score: 7/10Example 2 — Market update (JSON prompt)
Request the same analysis but force the model to output a defined JSON structure.
json_prompt = """
Analyze the following market update and return the response in this JSON format:
{
"summary": "2-line market overview",
"companies": [
{
"name": "string",
"sentiment": "positive | negative | neutral",
"opportunities": ["list of opportunities"],
"risks": ["list of risks"]
}
],
"confidence_score": "integer (0-10)"
}
Market Text:
Tesla's Q2 earnings beat expectations due to higher Model Y sales, but rising competition from BYD is a risk.
Apple reported steady revenue growth driven by iPhone sales, but services revenue slightly declined.
Amazon's AWS division continues to dominate cloud computing, though regulatory scrutiny in Europe is increasing.
"""
response_json = client.chat.completions.create(
model="gpt-5",
messages=[
{"role": "system", "content": "You are a precise assistant that always outputs valid JSON."},
{"role": "user", "content": json_prompt}
]
)
json_output = response_json.choices[0].message.content
print(json_output)Example JSON response:
{
"summary": "Markets saw mixed corporate updates: Tesla beat expectations on strong Model Y sales and AWS maintained cloud leadership.\nHowever, Apple's growth was tempered by softer services revenue while Tesla and AWS face competition and regulatory risks.",
"companies": [
{
"name": "Tesla",
"sentiment": "positive",
"opportunities": [
"Leverage strong Model Y demand to drive revenue and scale production",
"Sustain earnings momentum from better-than-expected Q2 results"
],
"risks": [
"Intensifying competition from BYD",
"Potential price pressure impacting margins"
]
},
{
"name": "Apple",
"sentiment": "neutral",
"opportunities": [
"Build on steady iPhone-driven revenue growth",
"Revitalize Services to reaccelerate growth"
],
"risks": [
"Slight decline in services revenue",
"Reliance on iPhone as the primary growth driver"
]
},
{
"name": "Amazon (AWS)",
"sentiment": "positive",
"opportunities": [
"Capitalize on cloud leadership to win new enterprise workloads",
"Expand higher-margin managed services and deepen customer spend"
],
"risks": [
"Increasing regulatory scrutiny in Europe",
"Potential compliance costs or operational restrictions"
]
}
],
"confidence_score": 8
}This JSON output is straightforward to consume in automated systems and ensures predictable fields across responses.
Best practices for JSON prompting
- Define a clear schema up front with the exact fields you need.
- Use system messages or top-level instructions to enforce strict formatting rules (for example, "always return valid JSON").
- Validate the model output and implement fallback parsing or re-prompting if the structure is invalid.
- Keep schemas compact and explicit to reduce room for interpretation.
- Use these templates as building blocks for pipelines that require consistent, machine-readable outputs.
Reusable JSON prompt templates unlock scalability, speed, and clean handoffs across teams and automation flows. By defining structured fields upfront, you get reliable, repeatable results that plug directly into APIs, databases, or apps without manual formatting.
Сменить язык
Читать эту статью на русском