| 📊 View Lecture Slides | Full-screen presentation with navigation |
Session 2: Prompting & Structured Outputs
| Session Duration: 2 Hours | Block: 1 — Foundations & The Exocortex |
Session clock
| Minutes | Mode | Focus |
|---|---|---|
| 0–50 | Lecture | Theoretical Foundation & Concepts |
| 50–110 | Core lab | AI Studio & Structured JSON Prompts |
| 110–120 | Checkpoint | Pair share / show artifact |
Note: Stretch work starts only after the Core checkpoint is completed.
Learning Objectives
By the end of this session, students will be able to:
- Explain the five core components of an effective prompt and why each matters.
- Apply role, context, task, format, and constraint framing to exert granular control over LLM outputs.
- Extract reliably structured JSON data from unstructured text inputs using Google AI Studio.
- Design a reusable, foundational prompt template specifically tailored for their course project.
Part 1: Theoretical Foundation — Controlling Language Model Behaviour
1.1 Why Prompting Is a Technical Engineering Skill
In the early days of generative AI (circa 2022-2023), “prompt engineering” was often treated as a dark art—a collection of “magic phrases” or “hacks” used to trick a model into giving a good answer. Today, we recognize it as a systematic, predictable engineering discipline. Understanding its underlying mechanics is what separates a casual AI consumer from a professional AI product builder.
The core reality you must understand is this: Large Language Models (LLMs) fundamentally predict the next most likely token. They do not “think,” “reason,” or “understand” in the human sense. They produce the statistically most probable continuation of the text you provide them, based on billions of parameters shaped during their training.
Therefore, your prompt is not a question to an oracle; it is the mathematical context that shapes the probability distribution of the output.
- Vague prompts cast a wide statistical net, producing vague, generic, or hallucinated outputs.
- Well-specified prompts narrow the probability distribution, producing precise, domain-specific outputs.
- Specifying the format of the output makes the response programmable, meaning your application code can parse and use it reliably.
1.2 The Five Components of an Effective Prompt
A professional prompt is rarely a single sentence. It is a structured document built from five distinct components. You do not always need all five, but whenever a model’s output is unsatisfactory, you should immediately ask: Which of these components is missing or underspecified?
| Component | Purpose & Mechanics | Example Implementation |
|---|---|---|
| Role | Sets the model’s persona, perspective, and expected level of expertise. This nudges the model into a specific subset of its training data. | “Act as a senior JavaScript performance engineer with 10 years of experience.” |
| Context | Provides the background information the model needs to understand the scenario. Without this, the model makes generic assumptions. | “The user is a first-year university student who has never programmed before and is feeling overwhelmed.” |
| Task | States precisely what action you want the model to perform. This should be clear and unambiguous. | “Explain how a for loop works in JavaScript, using a real-world analogy.” |
| Format | Specifies the exact structure and presentation of the output. This is critical for software integration. | “Respond using exactly three bullet points, written in plain English, with no technical jargon.” |
| Constraints | Limits the scope, length, tone, or content of the response. | “Do not use the words ‘iterate’, ‘traverse’, or ‘array’. Limit your response to 100 words.” |
1.3 Advanced Prompting: Zero-Shot, Few-Shot, and Chain-of-Thought
Beyond the five components, the strategy of how you present the task matters immensely.
Zero-Shot Prompting: You describe the task and provide no prior examples. The model relies entirely on its pre-trained knowledge to understand the format. This works well for simple, common, and well-defined tasks.
Example: “Classify the following product review as Positive, Negative, or Neutral. \n Review: ‘The delivery was late but the product itself exceeded expectations.’”
Few-Shot Prompting: You provide 2 to 5 concrete examples of the inputs and expected outputs before asking the model to process your actual input. This is incredibly powerful. It dramatically improves performance on complex, non-standard, or highly formatted tasks because the model learns the pattern in-context.
Example: 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 (CoT) Prompting: You explicitly instruct the model to reason step-by-step before arriving at its final answer. Because LLMs compute text as they generate it, forcing them to output their “thought process” gives them more tokens (and thus more computational depth) to solve the problem. This significantly improves accuracy on tasks requiring logic, arithmetic, or multi-step reasoning.
Example: “Think step by step. A train travels 120km in 1.5 hours. What is its average speed in km/h? Show your reasoning before providing the final number.”
1.4 Structured Outputs: Making AI Programmable
This is arguably the most important concept in AI product engineering: Extracting structured data from AI responses.
If you are building an application, your code cannot easily parse a conversational paragraph. If your app needs to display a price, that price must be in a predictable location with a predictable data format (like JSON).
The Problem: Unstructured Responses (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.
If a developer tries to write a regex to extract the price of the medium shirt from the above text, the code will break the moment the AI decides to rephrase the sentence tomorrow.
The Solution: Structured JSON Responses (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 (e.g., in JavaScript) can now safely and reliably access response.sizes[1].price_gbp without brittle string parsing.
How to Enforce JSON Output via Prompting: To achieve this, your prompt must explicitly dictate the schema and forbid conversational filler (often called “chatty” text).
Prompt Example: You are a highly accurate data extraction API. Extract the product details from the text below. Respond ONLY with valid, minified JSON matching this exact schema: { “sizes”: [{“name”: string, “price_gbp”: number}], “colours”: [string], “shipping_days”: number } Do not include markdown formatting, backticks, or any conversational text before or after the JSON.
Text to process: [insert text here]
Part 2: Practical Labs — Structured Extraction in AI Studio
Lab 2.1 — Basic Prompt Refinement & Iteration
Open Google AI Studio (aistudio.google.com) and create a new Freeform prompt.
- Exercise A (Zero-Shot): Write a basic, zero-shot prompt asking the AI to summarise a news article. Paste the text of any recent article from a news website.
- Evaluate: Is the output structured? Is it too long? Does it miss key details?
- Exercise B (Adding Constraints & Format): Modify your prompt by adding strict constraints. For example: “Each bullet must be exactly one sentence and under 20 words. Use plain English.” Re-run the prompt.
- Evaluate: Notice how the model’s output drastically tightens and obeys the rules.
- Exercise C (Enforcing JSON): Now, rewrite the prompt to act as an API. Ask the AI to produce a JSON object with specific keys:
headline(string),summary(array of three strings), andsentiment(enum: positive/negative/neutral). Re-run the prompt with the same article.- Evaluate: Does the output parse as valid JSON? Are there stray backticks or conversational text? Adjust the prompt until it outputs pure JSON.
Lab 2.2 — Extract Structured Data for Your Project
Think about the product idea you defined in Session 1. Your project will eventually need to process some form of unstructured input—user questions, uploaded documents, or web data—and turn it into structured outputs.
Step 1: Identify the core data your project needs to process. What specific information must be extracted or structured? Step 2: Write a robust prompt that extracts this information from a sample input. Explicitly define the JSON schema within the prompt, using the 5 components (Role, Context, Task, Format, Constraints). Step 3: Test your new prompt with three vastly different inputs.
- Does the JSON schema stay consistent?
- Does it hallucinate fields if the data is missing?
- If it breaks, add a constraint (e.g., “If a field is missing in the text, output null.”).
Deliverable: In your local project folder (the one you set up in VS Code), create a file called prompts.md. Save your final extraction prompt and a sample of the JSON output here. This file will become your Prompt Library, a critical asset for your application.
Lab 2.3 — Mastering the System Prompt
In modern AI APIs (like the Gemini API), you don’t just send a single prompt. You configure a System Prompt (or System Instructions) that dictates the model’s overarching behaviour across the entire application lifecycle.
The System Prompt is distinct from the User Message. It acts as the immutable ruleset.
Example Structure:
SYSTEM PROMPT (Hidden from user, sent by the app):
You are a concise, highly technical assistant for a DevOps team.
Always respond in strictly formatted JSON unless explicitly told otherwise.
Never use the phrases "I am an AI" or "As an AI language model".
If you do not know the answer, output {"status": "error", "message": "UNKNOWN"}.
USER MESSAGE (Input by the human):
How do I restart a Kubernetes pod?
Your Task: Draft a System Prompt for your specific course project.
- What is the persona?
- What are the absolute constraints?
- What formatting rules must persist across all interactions?
Add this System Prompt draft to your
prompts.mdfile.
Key Takeaways
- Prompt engineering is a technical discipline requiring structure: always consider the Role, Context, Task, Format, and Constraints.
- Supplying examples (Few-Shot) and asking for step-by-step logic (Chain-of-Thought) are the most reliable ways to improve complex outputs.
- Applications require Structured JSON outputs to be programmable. You must forcefully constrain the model to output valid JSON without conversational filler.
- System Prompts establish the persistent rules of engagement and persona for your application. Build and version-control your prompts like you do your code.
Further Reading & Resources
- OpenAI Prompt Engineering Guide: platform.openai.com/docs/guides/prompt-engineering - Excellent, platform-agnostic strategies for controlling LLMs.
- Google AI Studio Docs: Read up on how to configure “System Instructions” in the AI Studio interface.
- “Prompt Engineering for Generative AI” by James Briggs and Alvaro Fuentes - A deeper dive into enterprise prompt architecture.


