"AI-generated executive report" invites the assumption that the model is doing everything — crunching the data, making the charts, writing the summary. In a reliable pipeline, it does exactly one of those three things: writing prose about numbers someone else (pandas) already computed correctly. Splitting the pipeline this way means a factual error in the executive summary can only be a wording problem, never an arithmetic one.
Building the three-stage pipeline
1. Compute the real metrics with pandas. Aggregate whatever the report needs (monthly revenue, top categories, month-over-month change) using standard pandas operations — this is where the actual numbers in the report come from.
2. Render charts with matplotlib and save as images. Generate the visual elements as PNG/SVG files using a real charting library, then embed them in the report layout — don't ask an LLM to "draw" anything.
3. Feed the computed numbers (not raw data) to the LLM for a narrative summary. Give it the finished metrics — "revenue was $42,300, up 8% from last month" — and ask for 2-3 sentences of executive framing, then assemble everything into a PDF with WeasyPrint or ReportLab.
# pip install pandas matplotlib anthropic weasyprint
import pandas as pd
import anthropic
df = pd.read_csv("monthly_sales.csv")
total_revenue = df["amount"].sum() # real arithmetic, done in pandas
top_category = df.groupby("category")["amount"].sum().idxmax()
client = anthropic.Anthropic()
summary = client.messages.create(
model="claude-sonnet-5",
max_tokens=150,
system="Write a 2-sentence executive summary from these pre-computed figures. "
"Do not calculate or estimate any numbers yourself -- use only what's given.",
messages=[{"role": "user",
"content": f"Total revenue: ${total_revenue:,.2f}. Top category: {top_category}."}]
)
# Next: render a matplotlib chart and pass both to WeasyPrint/ReportLab
| Report Component | Generated By |
|---|---|
| Totals, aggregates, trend calculations | pandas (deterministic arithmetic) |
| Charts and visualizations | matplotlib (real rendering) |
| Narrative summary text | LLM, working only from pre-computed figures |
The pipeline is only as trustworthy as its weakest link — and the weakest link would be letting the LLM touch the raw numbers directly. Keep it constrained to writing about figures it's handed, and a reviewer only needs to check the prose reads accurately, not re-verify the math.
Practical Challenge
Build the pandas aggregation and LLM summary steps above for a sample sales CSV, then verify that the summary text's stated numbers exactly match what pandas actually computed.
Concept Check
Sources & Further Reading
- WeasyPrint — the open-source HTML/CSS-to-PDF rendering library referenced for final report assembly.
- Matplotlib Documentation — reference for generating the chart images embedded in the report.
AI