Prompting & structured output
Write instructions a model can follow reliably and force outputs your code can parse.
A good prompt states the role, the task, the constraints and the output format, and shows an example or two of the exact shape wanted. Be specific about what to do when information is missing, otherwise the model will invent it.
When code consumes the output, ask for a schema-constrained response rather than free text and parse it with a validator such as Zod or Pydantic. Provider features for structured outputs or tool-call arguments reduce malformed responses, but you still validate and handle failure.
Treat prompts as code. Version them, keep them in files rather than scattered strings, and change them only with an eval that shows the change helped. Anecdotes from three test inputs are not evidence.
Key points
- Specify the format and the behaviour for missing or ambiguous input.
- Validate every structured output and have a repair or retry path.
- Few-shot examples teach format and tone faster than long rules.
- Version prompts and evaluate changes.
Answer these out loud
- 1Write a prompt that extracts invoice fields as JSON and refuses to guess missing values. What does the schema look like?
- 2What do you do when the model returns valid JSON that fails your business rules?
Build it
Build an extractor with a Zod schema, a validator and a single automatic repair attempt, then count how often repair is needed across 50 samples.
Use this from an agent
Any MCP client, including Grok, can read this lesson. See how to connect.
curl https://engineeros-delta.vercel.app/api/v1/topics/prompting-and-structured-output