Skip to content
All guides

Capabilities & guides

Prompt Engineering & JSON Output

Master system constraints, few-shot grounding, multi-step decomposition, and strict JSON formatting.

Updated 2026-11-26 · 2 min read

Anatomy of a Production Prompt

Reliable prompts separate system rules, untrusted reference context, explicit task steps, and output format contracts. Keeping instructions distinct from input data improves compliance and reduces prompt-injection surface area.

ComponentPurposeBest Practice
System role & guardrailsEstablish domain perspective and hard boundariesState explicitly: "Use only supplied facts; return null when unknown"
Reference contextSupply retrieved chunks, tickets, or policy rulesWrap raw user or document data inside clear delimiters
Task instructionsDefine exact analytical or transformation stepsUse numbered steps rather than vague verbs like "improve this"
Output contractConstrain structure, field types, and lengthProvide an explicit JSON schema or a concise one-shot example

Reusable Enterprise Extraction Template

Adapt this template for ticket triage, contract key-term extraction, or meeting-note synthesis:

You are a precise enterprise operations analyst. Extract structured fields strictly from the [SOURCE RECORD] below:
1. Rely only on explicitly stated facts; set any missing field to null.
2. Keep "summary" under 40 words in neutral, factual language.
3. Output a single valid JSON object with no surrounding prose.

Target JSON format:
{
  "category": "incident | billing | feature_request",
  "urgency": "high | medium | low",
  "summary": "Concise issue summary",
  "action_items": ["Action 1", "Action 2"]
}

[SOURCE RECORD]
Paste sanitized business text here...

Enforcing Deterministic JSON Output

When downstream code parses model output programmatically, combine response_format with low sampling temperature and explicit field definitions:

response = client.chat.completions.create(
    model="qwen4-max",
    response_format={"type": "json_object"},
    temperature=0.1,
    messages=[
        {
            "role": "system",
            "content": "Return a valid JSON object containing risk_level (low/medium/high) and an issues string array.",
        },
        {
            "role": "user",
            "content": "Change proposal: extend webhook signature window to 60 minutes and disable nonce replay checks.",
        },
    ],
)

Note: When using json_object mode, always include the word JSON in your prompt messages so the model knows the exact target structure, and validate the parsed payload with Zod or Pydantic on your server.

Recommended Sampling Parameters

ParameterRecommended RangeTypical Use Cases
temperature0.0 – 0.2Code generation, JSON extraction, classification, and grounded RAG Q&A
temperature0.6 – 0.8Campaign copywriting, brainstorming, and creative concept variants
max_tokensSized to expected outputPrevents runaway generation while leaving headroom for reasoning models

Iterative Debugging Tips

  • Prefer positive few-shot examples over long negative rules: One concrete input-output pair resolves formatting ambiguity faster than a dozen contradictory "do not" clauses.
  • Decompose overloaded prompts: Split workflows that mix heavy extraction, arithmetic verification, and translation into focused sequential steps. To connect models to live APIs and databases, continue to Streaming & Function Calling.