Role, Context and Structured Output
What if you need a model's answer in a spreadsheet, not a paragraph? Navika has 800 customer reviews. Reading model summaries one by one would take days. She needs every answer in the same fixed shape, so she can count how many reviews mention late deliveries.
This lesson combines three techniques: a role, the right context, and structured output, such as JSON.
Three Techniques Together
| Technique | What it adds | Example |
|---|---|---|
| Role | The perspective and standards to use | "You are a careful data assistant." |
| Context | The facts the task depends on | "These are reviews for an online shop." |
| Structured output | A fixed format for the answer | "Return only JSON with product, sentiment and issue." |
Writing a Structured-Output Prompt
You are a careful data assistant.
These are customer reviews for an online shop.
Read the review and return only JSON, with no other text, using exactly these fields:
- "product": the product mentioned, in lowercase
- "sentiment": one of "positive", "neutral" or "negative"
- "issue": a short phrase for any problem, or null if there is none
Review: "The desk lamp is bright and pretty, but it arrived two weeks late."Each field has a name, a type and its allowed values. Saying "with no other text" stops the model adding a friendly sentence before the JSON, which would break a program reading it. Many model APIs also offer a structured output mode that enforces a format you define, as a later chapter on model APIs shows.
Common Mistakes
Describing the format loosely. "Give me the details in a table" leads to different tables each time; name the columns.
Forgetting allowed values. Without "positive, neutral or negative", you may get "mixed", "okay" and "so-so".
Assuming structured means correct. The format can be perfect while the content is wrong.
Interview Corner
Why ask a model for structured output?
So the answers can be processed automatically: counted, filtered, stored or passed to another program. A fixed format with named fields and allowed values makes outputs consistent.
How do you make structured output reliable?
Name every field, list allowed values, ask for only the structured data, give one example if needed, use the API's structured output mode where available, and validate the output in code.
Practice
Exercise: turn notes into data. Take five short notes from your week, such as messages, receipts or tasks. Write a prompt that returns JSON with three fields you choose, including one with fixed allowed values. Run it on all five. Did every output follow the format? Was every judgment right?
Check your understanding:
- What do role, context and structured output each add?
- Why say "return only JSON, with no other text"?
- Why check structured outputs by hand?
Key Takeaways
Quick reference
| Term | Meaning |
|---|---|
| Role prompting | Telling the model who to act as |
| Structured output | Answers in a fixed format, such as JSON |
| JSON | A text format of named fields and values |
| Validation | Checking in code that the output follows the format |
Was this tutorial helpful?