Enabling JSON Mode in API Calls
What you'll learn: How to configure API requests to force the LLM to return only valid JSON output.
Why Configuration Matters
In the previous lesson, you learned why JSON mode is valuable. Now it's time to actually turn it on. Think of JSON mode like switching your car into "sport mode"—it's not automatic; you need to flip the right switches in the right order.
Provider-Specific Parameters
Different LLM providers use different parameters to enable JSON mode:
OpenAI (GPT-4, GPT-3.5-turbo):
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Extract name and age"}],
response_format={"type": "json_object"}
)
Anthropic (Claude): Claude doesn't have a native JSON mode flag. Instead, you must explicitly instruct in the prompt and use tool definitions or prompt engineering.
Google (Gemini):
response = model.generate_content(
"Extract name and age",
generation_config={"response_mime_type": "application/json"}
)
Critical Requirements
Most providers require you to explicitly mention "JSON" in your prompt when JSON mode is enabled. Without this, the API may error out or ignore the parameter. For example: "Return your response as JSON with fields 'name' and 'age'."
Common Pitfalls
- Forgetting to update the system or user message to reference JSON
- Using JSON mode with streaming (not all providers support this)
- Assuming JSON mode validates schema (it only guarantees valid JSON syntax)
Key Takeaway: Enabling JSON mode requires setting the correct provider-specific parameter in your API call AND mentioning JSON format in your prompt—both pieces are essential for reliable structured output.