Python for GenAI and Agentic AI Handbook · PRACTICAL GUIDE

Build a Generative AI Application with Python

Build a grounded Python movie advisor using viewer context, approved catalogue facts, structured LLM output, validation and human review.

HANDBOOK JOURNEYByte 3 of 5View all Bytes
HANDBOOK JOURNEYByte 3 of 5

Python for GenAI and Agentic AI Handbook

34 min focused reading
  1. BYTE 01Python Foundations for AI Apps
  2. BYTE 02APIs, JSON and LLM Responses
  3. 04BYTE 04Build Tool-Using AI Agents
  4. 05BYTE 05Test, Secure and Deploy Python AI
FAMILIAR SCENARIO

A movie desk using a real catalogue

The desk asks what you like, checks available films and proposes options that truly exist before replying.

01Ask
02Find facts
03Recommend
04Verify

Connect the idea: Ground an AI suggestion in approved data.

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.

FROM MODEL CALL TO APPLICATION

Byte project

Build a grounded Movie Discovery Assistant that returns one verified title, a reason, suitability, confidence and any missing information.

PYTHON LEARNING LAB 03

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.

CODE → BEHAVIOUR → OUTPUT
Viewer needTamil · U rating · ≤ 90 minApproved cataloguemv-101 · 88 min · Tamil · U
LLM PROPOSAL
{
  "title_id": "mv-101",
  "confidence": 0.94
}
DETERMINISTIC PYTHON CHECK
if title_id not in catalogue:
  reject("Unknown title")
elif runtime > 90:
  reject("Too long")
else:
  approve()
USER-FACING RESULTWaiting for Python

Model confidence is not proof.

Interactive learning model — it does not execute code or change an external system.

Start from Basics

TermPurpose
ModelGenerates or transforms language
System instructionStable role, limits and output rules
User instructionThe current request
ContextVerified facts needed for this request
Structured outputResponse fields your code can validate
GroundingConnecting generation to approved evidence
HallucinationUnsupported 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

PYTHON
01from pydantic import BaseModel, Field0203class MovieRecommendation(BaseModel):04    title_id: str05    reason: str06    suitability: str07    confidence: float = Field(ge=0, le=1)08    missing_information: list[str]
CODE RESULTVALIDATED MODEL OBJECT
TRACE THE EXECUTION
EXPECTED OUTPUT
MovieRecommendation(
  title_id="mv-101",
  confidence=0.86,
  missing_information=[]
)
VISUAL EXECUTIONFollow the value through the program
LLM JSONProposed fields
PydanticShape + range checks
Business validationCatalogue truth

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.

PYTHON
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"""
CODE RESULTMODEL BOUNDARY
TRACE THE EXECUTION
EXPECTED OUTPUT
Allowed: recommend IDs in AVAILABLE_TITLES
Abstain: when required viewer data is missing
VISUAL EXECUTIONFollow the value through the program
System ruleRole + limits
Approved contextCurrent facts
Model proposalNot final truth

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

PYTHON
01def verify_recommendation(result, catalogue, profile):02    title = catalogue.get(result.title_id)03    if title is None:04        return {"status": "rejected", "reason": "Unknown title"}05    if title["language"] != profile["language"]:06        return {"status": "rejected", "reason": "Language mismatch"}07    if title["runtime_minutes"] > profile["available_minutes"]:08        return {"status": "rejected", "reason": "Too long"}09    return {"status": "accepted", "title": title}
CODE RESULTVERIFIED RECOMMENDATION
TRACE THE EXECUTION
EXPECTED OUTPUT
title_id: mv-101
status: accepted
reason: Tamil · U · 88 minutes
shown_to_user: Yes
VISUAL EXECUTIONFollow the value through the program
Model proposalmv-101
Python checksID + constraints
User resultVerified and shown

Architecture / Flow Diagram

GROUNDED COURSE ADVISORCombine learner context and approved knowledge before generation
FOLLOW THE FLOW
01
Learner profileSkills + goal
02
CatalogueVerified courses
03
LLMStructured proposal
04
Python validationFinal answer
Remember: The model recommends; Python verifies course identity, prerequisites and required fields.

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

ComponentOwnsMust not do
Input modelrequired viewer fieldsaccept arbitrary unvalidated data
Context builderselected verified factssend the entire database
Prompt templaterole, task and response rulesmix secrets into instructions
LLM clientprovider requestdecide business truth
Output modelrequired response shapeprove factual correctness alone
Business validatorIDs, rating, runtime, availabilitytrust model confidence
Review boundaryconsequential or uncertain decisionshide 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

PatternBest useMain risk
Prompt onlylow-risk draftingno dependable factual boundary
Prompt + structured outputmachine-readable resultsvalid shape may still contain false facts
RAG / retrieval groundingcurrent approved knowledgepoor retrieval produces weak evidence
Fine-tuningstable style or repeated behaviournot a live knowledge database
Deterministic Python ruleexact business constraintcannot 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 → ENGINEERING ACTION → VISIBLE RESULTFollow the problem until the team can prove the result.
Family recommendation
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.
RESULT VERIFIED
Age suitability
Before
A fluent answer ignores the requested rating.
Engineering action
Validate rating and duration deterministically.
Evidence after
Unsafe or unsuitable output is rejected.
RESULT VERIFIED
Missing preference
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.
RESULT VERIFIED
These scenarios describe observable application behaviour—not private claims about any company’s internal systems.

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

SEE IT IN PRACTICE

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

WHO USES THIS — AND WHAT DO THEY OWN?Connect each concept to an engineering responsibility.
GenAI engineer

Assemble grounded prompts and structured output

Clear ownership
Backend developer

Own validation and business rules

Clear ownership
Responsible AI reviewer

Define abstention and human review

Clear ownership
AI operations engineer

Track quality, latency and token cost

Clear ownership
One Python AI application is a team system: code, quality, security and operations work together.

Practical 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

REMEMBER THIS

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

LESSON CHECKPOINTConfirm the concept before moving forward

Choose an answer, inspect the explanation and explain the idea in your own words.

RETENTION

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.

  1. Who verifies title availability? A) Model confidence B) Python against catalogue C) UI colour
  2. What should happen when language is missing? A) Guess B) Ask or return missing information C) Invent
  3. Does a valid schema prove truth? A) Yes B) No C) Only with JSON
  4. What should grounding contain? A) Relevant approved evidence B) Every record C) Secrets
  5. 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.

Learning rule: explain the answer in your own words before checking the next Byte.

Primary sources

OPTIONAL LEARNING CONNECTIONS

Continue by concept

Choose only what supports your next goal. This Byte does not require either link.