Stage 3 — Output Structure Control (Critical in Production)

If the model writes “nice text” but without strict structure, integrations break.

Stage topics

  • Explicit formats (JSON, tables, lists)
  • Response schemas
  • Output validation
  • “Nothing except JSON” rule

Why structure matters more than style

In system workflows, model output is input for another service.
You need not “creative text”, but a machine-readable contract.

Explicit formats

Define:

  • field types,
  • required keys,
  • allowed values,
  • behavior on missing data.

Response schemas

Practical approach:

  • describe JSON schema (or strict pseudo-schema),
  • forbid extra fields,
  • require valid JSON without prose wrappers.

Validation

Never trust raw model output.
Pipeline must validate:

  • syntax,
  • structure,
  • business constraints.

“Nothing except JSON”

For integration tasks, define strict response rule:

  • JSON only,
  • no markdown,
  • no comments,
  • no extra explanatory text.

Prompt pipeline

Contract-first prompting

A structured prompt should start from the consumer of the output. If a human reads the result, headings and concise paragraphs may be enough. If another service reads the result, the prompt must define a contract before generation starts. The contract should say which fields exist, what each field means, which values are allowed, and what the model must do when it cannot infer a value safely.

This is why schemas are stronger than examples alone. Examples show the shape, but schemas define the rule. In production, the safest workflow is prompt -> model output -> parser -> validator -> business decision. The model should never be the only component deciding that its own output is valid.

Contract element Example requirement Why it matters
Required keys title, risk, actions Prevents partial objects
Allowed values low, medium, high Reduces category drift
Null policy Use null only when evidence is absent Avoids invented facts
Extra fields Forbidden Keeps integrations stable

Core theory to retain

  • Model behavior must be constrained by explicit boundaries.
  • Free-form text is an anti-pattern for automated integrations.

Beginner explanation

A prompt template is useful when the same task will run many times: different users, different documents, different input data, but the same expected result. Without a template, every developer writes the request differently, and outputs start varying in structure, style, and completeness. In a product, this quickly becomes a problem because downstream code cannot reliably parse free-form prose.

The simplest template has a stable part and variables. The stable part describes the task, rules, and format. Variables are filled at runtime: support message, user language, allowed categories, or documentation excerpt. This prompt can be stored, reviewed, and tested as an artifact.

Classify the user support message.
Allowed categories: {{categories}}.
Message: {{message}}.

Return JSON:
{
  "category": "one_of_categories",
  "confidence": 0.0,
  "reason": "short explanation"
}

If the model must return JSON, the word “JSON” is not enough. Show fields, types, and constraints. Then the application must validate the response with a schema validator. The prompt asks the model to follow the contract, while the backend checks that the contract was actually followed. This matters when output goes into a database, API, queue, or automated action.

Mini scenarios from real projects

  • Integration fails at parsing: model adds explanatory text around JSON.
  • Output is syntactically valid but fails business checks: schema does not encode required constraints.
  • Different endpoints return different structures for the same task: no unified output contract.

Fast decision rules

  • For integrations, always define strict output format and field requirements.
  • Validation should be two-layered: syntax (schema) and business rules.
  • Use free-form text only where product value explicitly requires it.
Glossary
LLM
A large language model trained to process and generate text or other token sequences.
prompt
The input instructions and context given to a language model.
context window
The maximum amount of input and output tokens a model can consider at once.
system prompt
High-priority instructions that define model behavior for a conversation or task.
instruction
A directive that tells a model or program what to do.
role
A prompt element that frames the model's perspective, responsibility, or behavior.
constraint
A rule or limitation that narrows what an answer or system is allowed to do.
few-shot
A prompting style that gives the model a few examples before asking it to solve a task.
zero-shot
A prompting style where the model receives no examples for the specific task.
chain of thought
Step-by-step reasoning used to work through a problem.
hallucination
Model output that sounds plausible but is false, unsupported, or fabricated.
grounding
Connecting model output to provided context, data, documents, or verified sources.
RAG
Retrieval-Augmented Generation, a pattern where relevant external information is retrieved before generation.
retrieval
Finding relevant information from documents, databases, or tools before answering.
citation
A reference to the source that supports a statement or answer.
evaluation
Measuring output quality against criteria, examples, tests, or expected behavior.
iteration
Repeating a process with improvements based on feedback or results.
tool calling
A model capability where the model requests an external tool or function to perform an action.
function calling
A structured form of tool calling where the model selects a function and provides arguments.
prompt injection
An attack or failure mode where untrusted text tries to override intended instructions.
token
A unit of text or data that a language model processes inside its context window.