Python for GenAI: The Developer’s Crash Course
Everything a software engineer needs to navigate the AI stack — without getting lost in the weeds.

Python for GenAI: The Developer’s Crash Course
Everything a software engineer needs to navigate the AI stack — without getting lost in the weeds.
If you’ve been building backend systems or distributed pipelines and suddenly find yourself staring down a GenAI codebase, you’re not alone. Python shows up everywhere in the AI world — LangChain, LangGraph, RAG pipelines, Azure OpenAI integrations — and the mental model is different enough from Java or C# that a quick orientation pays dividends.
This article distills the essentials: what Python syntax you actually encounter when working with AI frameworks, and how to read it confidently. Think of it as a map, not a deep dive.
Why Python Runs the AI World
Python’s dominance in GenAI isn’t accidental. It combines readable syntax with a staggering ecosystem: TensorFlow, PyTorch, Keras for model training; LangChain and LangGraph for orchestration; NumPy and Pandas for data wrangling. It connects seamlessly to cloud APIs on AWS, Azure, and Google Cloud, making it the default lingua franca from prototype to production.
The mental model shift from statically typed languages: Python is interpreted, dynamically typed, and prioritizes readability above almost everything else. You don’t declare types. You don’t manage memory. You write code that looks almost like pseudocode — and then you run it.
Variables and Data Types
Python infers types automatically. You assign, and Python figures out the rest:
python
call_count = 10 # int
latency = 0.83 # float
model_name = "gpt-4o" # str
is_streaming = True # bool
The common types you’ll encounter in GenAI codebases: int, float, str, bool, plus the collection types below. No declarations. No semicolons. Indentation defines scope — 4 spaces per level, consistently.
Data Structures: The Four You Need
Lists — ordered, mutable sequences
python
messages = ["system", "user", "assistant"]
messages.append("tool")
messages.remove("system")
Lists are the workhorse of GenAI pipelines. Conversation history, retrieved document chunks, agent outputs — all typically stored as lists. Slicing is your friend:
python
# Last 5 messages only
recent = messages[-5:]
Tuples — ordered, immutable
python
model_config = ("gpt-4o", 0.7, 4096)
Use tuples when the data shouldn’t change: fixed configurations, coordinate pairs, anything that should be write-once.
Sets — unordered, unique elements only
python
seen_doc_ids = {"doc_123", "doc_456", "doc_789"}
seen_doc_ids.add("doc_123") # silently ignored — already present
Useful for deduplication in RAG pipelines — tracking which chunks have already been retrieved.
Dictionaries — key-value maps
python
agent_state = {
"messages": [],
"tool_calls": 0,
"model": "claude-sonnet-4-6",
"temperature": 0.3
}
Dictionaries are everywhere in GenAI. API request bodies, agent state, tool scemas, configuration objects — nearly all of it is dict-shaped. Accessing a missing key raises a KeyError, so prefer .get() for optional fields:
python
timeout = agent_state.get("timeout", 30) # default to 30 if absent
Strings: The Medium of Prompts
In GenAI, strings are your application logic. Prompt construction is a core engineering skill, and Python’s string tools make it clean.
f-strings (the most important one)
python
user_query = "What are our Q3 margins?"
context = "Revenue: $4.2M, Costs: $3.1M"
prompt = f"""
Answer the following question using only the provided context.
Question: {user_query}
Context:
{context}
If the answer is not in the context, say "I don't know."
"""
f-strings with triple quotes let you build multi-line prompts that are readable in code and correct at runtime. This is the pattern you’ll see in every LangChain, LangGraph, and OpenAI integration.
Useful string methods in pipelines
python
response.strip() # clean up whitespace from LLM output
response.lower() # normalize for comparison
text.split("\n") # parse line-by-line output
" ".join(chunks) # reassemble from a list
Control Flow
Conditionals
python
if confidence_score >= 0.85:
return answer
elif confidence_score >= 0.60:
return answer + "\n\n*Note: Low confidence — please verify.*"
else:
return "I don't have enough information to answer reliably."
Python conditionals use indentation, not braces. The elif keyword handles additional branches.
Loops
For loops — iterate over any sequence:
python
documents = retrieve_chunks(query, top_k=5)
summaries = []
for doc in documents:
summary = call_llm(f"Summarize: {doc['content']}")
summaries.append(summary)
While loops — repeat until a condition breaks:
python
retries = 0
while retries < 3:
try:
response = call_llm(prompt)
break
except RateLimitError:
retries += 1
Loop control:
break— exit immediatelycontinue— skip to the next iteration
Functions: Packaging Reusable Logic
python
def summarize_texts(texts: list, model: str = "claude-sonnet-4-6") -> list:
summaries = []
for text in texts:
prompt = f"""
Summarize the following text in one sentence:
{text}
"""
summary = call_llm(prompt, model=model)
summaries.append(summary)
return summaries
Key concepts:
defkeyword, followed by function name and parameters- Default parameter values (
model = "claude-sonnet-4-6") returnto send a value back to the caller- Parameters are the variables in the function definition; arguments are the values you pass when calling it
Variable-length arguments
You’ll see both of these in framework APIs:
python
def run_pipeline(*steps): # *args — any number of positional args, collected as tuple
for step in steps:
step.execute()
def configure_agent(**settings): # **kwargs — any number of keyword args, collected as dict
for key, value in settings.items():
agent.set(key, value)
Modules, Packages, and the Import System
python
import pandas as pd
from langchain.chat_models import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
- A module is a
.pyfile containing reusable code - A package is a directory of modules (with an
__init__.py) - A library is a published package you install via
pip
Virtual environments keep dependencies isolated per project — essential when one project needs langchain==0.1.x and another needs 0.2.x:
bash
python -m venv my_project_env
source my_project_env/bin/activate # Mac/Linux
my_project_env\Scripts\activate # Windows
pip install langchain langchain-openai
Pandas: Structured Data in AI Pipelines
Pandas is the go-to for working with tabular data — evaluation datasets, conversation logs, retrieval benchmarks.
The two core structures:
- Series — a single column (one-dimensional, labeled)
- DataFrame — a table (two-dimensional, labeled rows and columns)
python
import pandas as pd
# Load evaluation results
df = pd.read_csv("eval_results.csv")
# Filter to failing cases
failures = df[df["score"] < 0.7]
# Select specific columns
review = failures[["query", "expected", "actual", "score"]]
# loc: label-based access
row = df.loc[5, "query"]
# iloc: index-based access
first_ten = df.iloc[:10]
Common DataFrame operations in GenAI:
.dropna()— remove rows with missing values.drop(index=n)— remove a row by index.groupby("category").mean()— aggregate by category.to_csv("output.csv")— export results
Putting It Together: A Realistic GenAI Pattern
Here’s what a simplified multi-document summarization function looks like combining everything above:
python
import pandas as pd
from typing import Optional
def batch_summarize(
df: pd.DataFrame,
text_col: str,
model: str = "claude-sonnet-4-6",
max_rows: Optional[int] = None
) -> pd.DataFrame:
"""
Summarize a column of text in a DataFrame using an LLM.
Returns the original DataFrame with a new 'summary' column.
"""
results = []
rows = df.head(max_rows) if max_rows else df
for _, row in rows.iterrows():
text = row[text_col].strip()
if not text:
results.append(None)
continue
prompt = f"""
Summarize the following in one sentence:
"""
summary = call_llm(prompt, model=model)
results.append(summary)
df = df.copy()
df["summary"] = results
return df
This pattern — load data, iterate, call LLM, collect results — is the backbone of evaluation pipelines, batch enrichment jobs, and dataset construction in AI engineering.
What to Explore Next
This covers the Python you’ll encounter daily in GenAI work. To go deeper:
- NumPy — array operations and embeddings math
- LangChain / LangGraph — agent and pipeline orchestration
- Pydantic — data validation and structured LLM output
- AsyncIO — concurrent LLM calls for throughput
The good news: once you can read Python confidently, the frameworks are surprisingly approachable. Most GenAI code is just functions, dictionaries, and strings — orchestrated carefully.
메타데이터
- post_id
- 3f6c93e00919
- slug
- python-for-genai-the-developers-crash-course-3f6c93e00919
- url
- https://medium.com/@harikavaleti/python-for-genai-the-developers-crash-course-3f6c93e00919
- canonical_url
- https://medium.com/@harikavaleti/python-for-genai-the-developers-crash-course-3f6c93e00919
- author_url
- https://medium.com/@harikavaleti
- status
- ok
- fetched_at
- 2026-06-09 15:37:30