Downstream code needs reliable structure—JSON schema, tool calls, or vendor JSON mode—to parse model output without brittle regex.
Techniques
- Ask for JSON only, no markdown fences, with a schema example
- Use API
response_format/ JSON schema features when available - Validate with Pydantic or
json.loads; retry on parse failure
Repair loop
import json
def parse_or_repair(raw: str) -> dict:
try:
return json.loads(raw)
except json.JSONDecodeError:
# optional: second call "fix to valid JSON matching schema"
raise
Temperature for extraction
Use low temperature for machine-readable outputs; creative marketing copy can use higher values.
Important interview questions and answers
- Q: Why validate JSON in code?
A: Models occasionally emit trailing commas or commentary despite instructions.
Self-check
- Name two ways to tighten JSON outputs.
- When use low temperature?
Pitfall: Trusting JSON without schema validation—always parse and retry in code.
Interview prep
- Validate JSON?
Models violate schema; parse in code and retry or repair.
- Low temperature?
Stabilizes machine-readable outputs for automation.