RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Eruo Fredoline
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /Courses
  5. /Prompt Engineering Fundamentals
  6. /Ch. 10
Prompt Engineering Fundamentals

10. JSON Mode

Chapter 10 of 25 · 20 min
KEY INSIGHT

JSON mode ensures parseable output but does not enforce your schema—combine with explicit field specifications.

Many models support a JSON mode that constrains output to valid JSON. This reduces parsing errors and ensures structured data regardless of prompt phrasing.

JSON mode is activated differently depending on your inference stack:

Ollama:

curl -X POST http://localhost:11434/api/generate \
  -d '{"model": "llama3.1", "prompt": "...", "format": "json"}'

LM Studio: Enable JSON mode in the chat settings, or use the API:

curl -X POST http://localhost:1234/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{"messages": [{"role": "user", "content": "..."}], "response_format": {"type": "json_object"}}'

llama.cpp server:

curl -X POST http://localhost:8080/completion \
  -d '{"prompt": "...", "json_schema": {"type": "object", "properties": {"sentiment": {"type": "string"}}}}'

JSON mode does not enforce your schema—it only ensures the output is parseable JSON. You still need to specify what fields you want:

Return valid JSON with these fields:
- sentiment: "positive" | "negative" | "neutral"
- confidence: number between 0 and 1
- reason: one sentence explaining the classification

Input: [text]

Without field specification, JSON mode produces syntactically valid but semantically wrong output.

Common issues with JSON mode:

  1. Schema drift: The model invents fields not in your spec
  2. Type errors: Model outputs string where number is expected
  3. Incomplete objects: Model truncates output mid-generation

To prevent schema drift, include a schema definition:

Return JSON matching this schema exactly. Do not add fields not in this schema:

{
  "type": "object",
  "properties": {
    "entities": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "name": {"type": "string"},
          "type": {"enum": ["person", "organization", "location"]}
        },
        "required": ["name", "type"]
      }
    }
  },
  "required": ["entities"]
}

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

EXERCISE

Configure JSON mode in your inference stack and test a prompt with complex output requirements. Validate the JSON structure programmatically.

← Chapter 9
Output Format Control
Chapter 11 →
Markdown Output