Quick Start
A viewer asks, “What can our family watch in Tamil within 90 minutes?” A general LLM can write a convincing recommendation, but it may choose a title that is unavailable, too long or unsuitable for the requested rating.
This Byte separates two jobs that beginners often mix together. The LLM proposes a readable answer; the Python application decides whether that proposal matches approved catalogue facts. You will deliberately test one valid ID and one invented ID to see the boundary clearly.
Byte project
Build a grounded Movie Discovery Assistant that returns one verified title, a reason, suitability, confidence and any missing information.
Separate the model proposal from the application decision
The LLM may write a confident answer. Python compares its title ID and constraints with approved catalogue evidence before the user sees it.
{
"title_id": "mv-101",
"confidence": 0.94
}if title_id not in catalogue:
reject("Unknown title")
elif runtime > 90:
reject("Too long")
else:
approve()Model confidence is not proof.
Start from Basics
| Term | Purpose |
|---|---|
| Model | Generates or transforms language |
| System instruction | Stable role, limits and output rules |
| User instruction | The current request |
| Context | Verified facts needed for this request |
| Structured output | Response fields your code can validate |
| Grounding | Connecting generation to approved evidence |
| Hallucination | Unsupported or invented output |
An LLM call produces text. An application adds validated input, authorised context, output contracts, privacy controls, failure handling and a user experience.
Core Explanation
01from pydantic import BaseModel, Field0203class MovieRecommendation(BaseModel):04title_id: str05reason: str06suitability: str07confidence: float = Field(ge=0, le=1)08missing_information: list[str]
MovieRecommendation(
title_id="mv-101",
confidence=0.86,
missing_information=[]
)The model should return this shape, but Python must still verify that title_id exists in the retrieved catalogue and that rating, language and runtime match the request.
01system_instruction = """02You are a movie discovery assistant.03Recommend only a title present in AVAILABLE_TITLES.04If required viewer information is missing, list it instead of guessing.05Return the requested structured fields.06"""
Allowed: recommend IDs in AVAILABLE_TITLES
Abstain: when required viewer data is missingGood context is selective. Send the viewer constraints and eligible title facts—not an unlimited chat history or unrelated personal profile.
Validate the proposal against application-owned facts:
01def verify_recommendation(result, catalogue, profile):02title = catalogue.get(result.title_id)03if title is None:04return {"status": "rejected", "reason": "Unknown title"}05if title["language"] != profile["language"]:06return {"status": "rejected", "reason": "Language mismatch"}07if title["runtime_minutes"] > profile["available_minutes"]:08return {"status": "rejected", "reason": "Too long"}09return {"status": "accepted", "title": title}
title_id: mv-101
status: accepted
reason: Tamil · U · 88 minutes
shown_to_user: YesArchitecture / Flow Diagram
This is an evidence chain: viewer need → approved facts → model proposal → deterministic Python checks → response. The model never becomes the source of catalogue truth.
Types / Components
| Component | Owns | Must not do |
|---|---|---|
| Input model | required viewer fields | accept arbitrary unvalidated data |
| Context builder | selected verified facts | send the entire database |
| Prompt template | role, task and response rules | mix secrets into instructions |
| LLM client | provider request | decide business truth |
| Output model | required response shape | prove factual correctness alone |
| Business validator | IDs, rating, runtime, availability | trust model confidence |
| Review boundary | consequential or uncertain decisions | hide missing information |
Streaming improves perceived speed for conversational text. For structured decisions, collect and validate the complete object before acting on it.
Tool / Technology Comparison
| Pattern | Best use | Main risk |
|---|---|---|
| Prompt only | low-risk drafting | no dependable factual boundary |
| Prompt + structured output | machine-readable results | valid shape may still contain false facts |
| RAG / retrieval grounding | current approved knowledge | poor retrieval produces weak evidence |
| Fine-tuning | stable style or repeated behaviour | not a live knowledge database |
| Deterministic Python rule | exact business constraint | cannot understand every natural-language variation |
Strong applications combine model flexibility with deterministic checks rather than forcing one technique to do every job.
Real-World Examples
- Before
- A model suggests a title unavailable in India.
- Engineering action
- Ground it with the current regional catalogue.
- Evidence after
- Python displays only a verified title.
- Before
- A fluent answer ignores the requested rating.
- Engineering action
- Validate rating and duration deterministically.
- Evidence after
- Unsafe or unsuitable output is rejected.
- Before
- The viewer gives no language or genre.
- Engineering action
- Return missing_information instead of guessing.
- Evidence after
- The interface asks one useful follow-up.
The same pattern supports a product assistant grounded in current stock, an HR assistant grounded in approved policy, and a support assistant grounded in published help content. Always keep source identity and access rules visible.
Imagine Pannunga
Brief a knowledgeable assistant with the correct file
“Suggest something” gives an assistant too much room to guess. “Choose one Tamil family title under 90 minutes from this approved list” provides a decision boundary and evidence.
Simple Tanglish: “Edhaavadhu suggest pannunga” romba vague. Language, rating, available time, verified catalogue details kuduthaa recommendation useful-ah irukkum; Python final checks pannum.
Use Cases
Assemble grounded prompts and structured output
Clear ownershipOwn validation and business rules
Clear ownershipDefine abstention and human review
Clear ownershipTrack quality, latency and token cost
Clear ownershipPractical project
Use a small verified catalogue with title_id, language, rating, genres, runtime and region. Retrieve eligible entries, request a structured recommendation, validate the identifier and constraints, and return missing information instead of guessing.
Track model name, prompt version, input/output tokens, latency and estimated cost. Do not log raw private conversations by default.
Key Takeaways
Key takeaways
- A production GenAI feature is more than one model call.
- System instruction, user instruction and verified context have different jobs.
- Grounding gives the model relevant approved evidence.
- Structured output enables parsing but does not prove factual correctness.
- Python must validate identifiers and deterministic business rules.
- Missing information should trigger clarification, not confident guessing.
- Streaming changes delivery timing, not the trust boundary.
- Privacy, cost tracking and human review belong in the initial design.
Final Thought + Next Path
Let the model handle language; let the application own truth, permissions and evidence. You now have a grounded feature that can explain one recommendation without inventing catalogue facts. Next, you will give the application bounded tools and learn how an agent chooses, acts, observes and stops.
FAQ + Knowledge Check
Choose an answer, inspect the explanation and explain the idea in your own words.
Is structured output automatically correct? No. It can match the schema while containing an unknown ID or unsuitable value.
Is RAG the same as fine-tuning? No. RAG supplies retrieved evidence at request time; fine-tuning changes model behaviour from training examples.
Should the whole conversation always be sent? No. Send only useful, permitted and appropriately retained context.
When is human review required? When uncertainty or consequence is high—for example paid, shared-profile or sensitive decisions.
- Who verifies title availability? A) Model confidence B) Python against catalogue C) UI colour
- What should happen when language is missing? A) Guess B) Ask or return missing information C) Invent
- Does a valid schema prove truth? A) Yes B) No C) Only with JSON
- What should grounding contain? A) Relevant approved evidence B) Every record C) Secrets
- What should be tracked? A) Only output text B) Model, prompt, tokens, latency and cost C) Nothing
Answers: 1-B, 2-B, 3-B, 4-A, 5-B. Score 4/5 or better before continuing.