| 📊 View Lecture Slides | Full-screen presentation with navigation |
Session 2: Prompting & Structured Outputs
| Session Duration: 2 Hours | Block: 1 — Foundations & The Exocortex |
Learning Objectives
By the end of this session, students will be able to:
- Explain the five core components of an effective prompt
- Apply role, context, task, format, and constraint framing to control LLM output
- Extract structured JSON from unstructured text using AI Studio
- Design a reusable prompt template for their course project
Hour 1: Controlling Language Model Behaviour (Instructor-Led — 60 minutes)
1.1 Why Prompting Is a Technical Skill
In 2023, “prompt engineering” was treated as something slightly mysterious — an art form of magic phrases. In practice, it is a systematic engineering discipline with predictable rules. Understanding those rules is what separates an AI consumer from an AI product builder.
The core problem: language models predict the next token. They do not “think” or “reason” in the way humans do. They produce the statistically most likely continuation of your input. Your prompt is, therefore, directly shaping the probability distribution of the output.
This means:
- Vague prompts produce vague outputs
- Well-specified prompts produce well-specified outputs
- Specifying the format of the output makes the output programmable (parseable by code)
1.2 The Five Components of an Effective Prompt
| Component | Purpose | Example |
|---|---|---|
| Role | Set the model’s perspective and expertise level | “Act as a senior Python developer” |
| Context | Provide the background information the model needs | “The user is a first-year student with no programming background” |
| Task | State precisely what you want the model to do | “Explain how a for loop works” |
| Format | Specify the structure of the output | “Respond in three bullet points, plain English, no jargon” |
| Constraints | Limit scope, length, or content | “Do not use the words ‘iterate’ or ‘traverse’” |
You do not need all five every time. But every time your output is unsatisfactory, ask which component is missing or underspecified.
1.3 Zero-Shot, Few-Shot, and Chain-of-Thought
Zero-shot: You describe the task and provide no examples. Works for simple, well-defined tasks.
Classify the following product review as Positive, Negative, or Neutral.
Review: "The delivery was late but the product itself exceeded expectations."
Few-shot: You provide 2–5 examples before asking the model to process your actual input. Dramatically improves performance on complex or non-standard tasks.
Classify each review.
Review: "Arrived broken, useless." → Negative
Review: "Exactly as described, very happy." → Positive
Review: "It's fine, nothing special." → Neutral
Review: "The delivery was late but the product itself exceeded expectations." → ?
Chain-of-thought: You instruct the model to reason step-by-step before giving its final answer. Significantly improves accuracy on tasks requiring logic, arithmetic, or multi-step reasoning.
Think step by step. A train travels 120km in 1.5 hours.
What is its average speed in km/h? Show your reasoning.
1.4 Structured Outputs: Making AI Programmable
The most important skill for AI product engineering is extracting structured data from AI responses. If your application needs to read a field from an AI response, that field must be in a predictable location with a predictable format.
Unstructured response (not programmable):
The product is available in three sizes: small (£12), medium (£18), and large (£24).
It comes in red, blue, and green, and it ships within 5 business days.
Structured JSON response (programmable):
{
"sizes": [
{"name": "small", "price_gbp": 12},
{"name": "medium", "price_gbp": 18},
{"name": "large", "price_gbp": 24}
],
"colours": ["red", "blue", "green"],
"shipping_days": 5
}
Your application code can now reliably access response.sizes[0].price_gbp without fragile string parsing.
How to enforce JSON output:
You are a data extraction assistant.
Extract the product details from the text below.
Respond ONLY with valid JSON matching this schema:
{
"sizes": [{"name": string, "price_gbp": number}],
"colours": [string],
"shipping_days": number
}
Do not include any text before or after the JSON.
Text: [paste product description here]
Hour 2: Practical — Structured Extraction in AI Studio (60 minutes)
Lab 2.1 — Basic Prompt Refinement
Open Google AI Studio. Create a new freeform prompt.
Exercise A: Write a zero-shot prompt that asks the AI to summarise a news article in three bullet points. Paste any article from a news site. Evaluate: is the output structured? Does it cover the key points?
Exercise B: Add a Format constraint to your prompt: “Each bullet must be a single sentence under 20 words. Use plain English.” Re-run. Does the output improve?
Exercise C: Now ask the AI to produce a JSON object with keys headline, summary (array of three strings), and sentiment (positive/negative/neutral). Re-run with the same article. Does the output parse correctly?
Lab 2.2 — Extract Structured Data for Your Project
Your project will eventually process some form of input — user queries, documents, or data feeds. This exercise starts building your prompt library.
Step 1: Think about the data your project needs to process. What information needs to be extracted or structured?
Step 2: Write a prompt that extracts this information from a sample input. Specify the JSON schema explicitly in the prompt.
Step 3: Test the prompt with three different inputs. Does the output schema stay consistent? If not, what constraint is missing?
Deliverable: Save your extraction prompt and sample outputs in a file called prompts.md in your project folder. You will extend this file throughout the course.
Lab 2.3 — The System Prompt
AI Studio (and most modern APIs) supports a system prompt — a set of instructions that persist across all turns of a conversation. This is distinct from the user message.
SYSTEM PROMPT:
You are a concise technical assistant.
Always respond in JSON unless explicitly told otherwise.
Never say "I" or "As an AI".
Limit responses to 200 tokens unless asked to elaborate.
USER MESSAGE:
What are the three most important factors in choosing a database?
Create a system prompt for your project’s AI component. What persona, constraints, and output format should persist across all interactions with your application’s AI?
Key Takeaways
- Prompting is an engineering discipline: Role, Context, Task, Format, Constraints
- Few-shot examples and chain-of-thought reasoning significantly improve complex outputs
- Structured JSON outputs make AI responses programmable and reliable
- System prompts establish persistent behaviour across a conversation
- Build your prompt library as a project asset from day one
Further Reading
- OpenAI Prompt Engineering Guide: platform.openai.com/docs/guides/prompt-engineering
- Google AI Studio system instructions documentation
- “Prompt Engineering for Generative AI” (Briggs & Morales, O’Reilly)


