Skip to content

Day 6 — Prompt engineering + structured JSON output

Why / क्यों

English: The same model gives good or bad answers depending on the prompt. Like briefing a new intern: a vague task gets random work; clear instructions and examples get correct work.

Hindi: एक ही model से अच्छा या खराब जवाब prompt पर निर्भर करता है। नए intern जैसा: "कुछ लिख दो" बोलोगे तो कुछ भी मिलेगा; साफ़ instructions और examples दोगे तो सही काम मिलेगा।

The 4 core techniques / चार मुख्य तरीके

flowchart TD
    P[Your task] --> A[1. Clear instructions: role, task, format, limits]
    A --> B[2. Few-shot: add 2-3 examples]
    B --> C[3. Chain of thought: think step by step]
    C --> D[4. Structured output: force a JSON schema]
    D --> R[Reliable output your code can use]

1. Zero-shot vs few-shot

import os
from dotenv import load_dotenv
from google import genai
from google.genai import types

load_dotenv()
client = genai.Client(
    api_key=os.getenv("GEMINI_API_KEY"),
    http_options=types.HttpOptions(timeout=60000),
)

def ask(prompt: str) -> str:
    response = client.models.generate_content(
        model="gemini-3.5-flash-lite",
        contents=prompt,
        config=types.GenerateContentConfig(temperature=0),
    )
    return response.text.strip()

review = "Delivery was late, but the food was amazing!"

zero_shot = f"What is the sentiment of this review? {review}"

few_shot = f"""Classify the review sentiment as one word: POSITIVE, NEGATIVE or MIXED.

Review: "Product is useless, waste of money." -> NEGATIVE
Review: "Great quality, love it!" -> POSITIVE
Review: "Price is high but it works well." -> MIXED

Review: "{review}" ->"""

print("ZERO-SHOT:\n", ask(zero_shot))
print("\nFEW-SHOT:\n", ask(few_shot))

Zero-shot gives a long explanation; few-shot gives exactly one word: MIXED.

Interview

Few-shot makes the output format consistent. In production, consistency matters as much as accuracy.

2. Structured JSON output with Pydantic

from typing import Literal
from pydantic import BaseModel

class ReviewAnalysis(BaseModel):
    sentiment: Literal["POSITIVE", "NEGATIVE", "MIXED"]
    complaint: str
    score: int

response = client.models.generate_content(
    model="gemini-3.5-flash-lite",
    contents=f"Analyze this customer review. Score is 1 to 5. Review: {review}",
    config=types.GenerateContentConfig(
        temperature=0,
        response_mime_type="application/json",
        response_schema=ReviewAnalysis,
    ),
)

result: ReviewAnalysis = response.parsed
print(result.sentiment, "|", result.complaint, "|", result.score)
  • Literal limits a field to fixed values.
  • response_schema forces the model into your Pydantic shape.
  • response.parsed gives a ready Python object.

Reliable LLM output = 4 things

Structured output + Pydantic schema + temperature 0 + retry on failure.

Quiz / अपने आप को test करो

1. What is few-shot prompting?

Giving the model 2–3 examples in the prompt so it copies the pattern and format.

2. When is chain of thought useful?

For maths and logic — asking the model to think step by step reduces mistakes.

3. Why prefer JSON output over free text?

Code can read it directly and reliably.

4. What does response_schema do?

Forces the model's answer to match your Pydantic model.

5. Why temperature 0 for classification?

You want the same, consistent answer every time.