Role, Context and Structured Output

Updated · 4 min read
mediumPython 3.13aipromptsjsonstructured-outputprompt-engineeringinterview

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

TechniqueWhat it addsExample
RoleThe perspective and standards to use"You are a careful data assistant."
ContextThe facts the task depends on"These are reviews for an online shop."
Structured outputA fixed format for the answer"Return only JSON with product, sentiment and issue."
A fixed format turns answers into data

Writing a Structured-Output Prompt

text
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:

  1. What do role, context and structured output each add?
  2. Why say "return only JSON, with no other text"?
  3. Why check structured outputs by hand?

Quick check: structured prompts

4 questions
1What is structured output?
2Why list allowed values, such as positive, neutral or negative?
3Why add "with no other text"?
4The JSON is valid but says "positive" for a complaint. What does this show?

Key Takeaways

Quick reference

TermMeaning
Role promptingTelling the model who to act as
Structured outputAnswers in a fixed format, such as JSON
JSONA text format of named fields and values
ValidationChecking in code that the output follows the format

Was this tutorial helpful?

Lesson 37 of 186