Don't trust the paragraph: validating AI call summaries against the transcript in Python
LLM-generated summaries of calls, meetings and tickets increasingly get written straight into CRMs and helpdesks. The trouble is that the people who read them weren't in the conversation, so they can't spot when "let me check" became "agreed", or when a due date appeared that nobody said.
NIST's Generative AI Profile (NIST AI 600-1) calls this confabulation: confidently presented content that is false or diverges from the input. Prompting helps. Anthropic's guide to reducing hallucinations recommends grounding output in verbatim quotes and retracting claims that can't be supported by one. But the same guide says these techniques don't eliminate hallucinations entirely.
So in this tutorial we'll do the part that can be made deterministic: check every summary item's evidence in code before it's saved. No model calls and no network. It runs after whatever model you use.
The contract: typed items with evidence
Ask your model (via structured output, JSON mode or a tool schema) to return items like this instead of a paragraph:
{"type": "commitment", "text": "Agent will raise a credit request for 3 seats today.", "owner": "agent", "due": "same day", "evidence_turns": ["t2"], "quote": "raise a credit request for the three extra seats today"} Four item types cover most business handoffs: fact, commitment, customer_request and open_question. An optional inferred: true marks things like sentiment or risk that weren't stated outright.
The rules we enforce
Reject (don't save) when:
- no
evidence_turnsare cited, or a cited turn doesn't exist; - the quote is shorter than three words (too easy to match by accident);
- the quote doesn't appear in any cited turn, after normalising case, whitespace and punctuation;
- a commitment has no owner.
Route to review when:
- a commitment has no due date (confirm it; never let the model guess);
- the owner isn't the speaker of any cited turn;
- the item is marked as an inference.
Everything else is accepted.
The code
summary_check.py (standard library only, Python 3.9+):
"""Evidence-linked call summary checker. Validates an AI-generated call summary against the transcript it claims to summarise. Every summary item must cite transcript turn IDs and include a short verbatim quote that actually appears in one of the cited turns. Commitments must name an owner; a missing due date is flagged for review, never invented. No model or network access is needed: this runs AFTER whatever model produced the summary, as a deterministic gate before the summary is saved to a CRM or handed to the next person. """ from __future__ import annotations import json import re import sys from dataclasses import dataclass, field ITEM_TYPES = {"fact", "commitment", "open_question", "customer_request"} def _norm(text: str) -> str: """Lower-case and collapse whitespace/punctuation so quoting is fair.""" text = text.lower().replace("\u2019", "'") text = re.sub(r"[^\w'$%.:/ -]+", " ", text) return re.sub(r"\s+", " ", text).strip() @dataclass class Finding: item_index: int level: str # "reject" or "review" message: str @dataclass class Result: accepted: list[dict] = field(default_factory=list) needs_review: list[dict] = field(default_factory=list) rejected: list[dict] = field(default_factory=list) findings: list[Finding] = field(default_factory=list) def check_summary(transcript: list[dict], summary: dict, min_quote_words: int = 3) -> Result: turns = {t["id"]: t for t in transcript} result = Result() for i, item in enumerate(summary.get("items", [])): problems: list[Finding] = [] kind = item.get("type") if kind not in ITEM_TYPES: problems.append(Finding(i, "reject", f"unknown item type {kind!r}")) cites = item.get("evidence_turns") or [] quote = (item.get("quote") or "").strip() missing = [c for c in cites if c not in turns] if not cites: problems.append(Finding(i, "reject", "no evidence_turns cited")) if missing: problems.append(Finding(i, "reject", f"cites turns that do not exist: {missing}")) if len(quote.split()) < min_quote_words: problems.append(Finding(i, "reject", f"quote shorter than {min_quote_words} words")) elif cites and not missing: if not any(_norm(quote) in _norm(turns[c]["text"]) for c in cites): problems.append(Finding(i, "reject", "quote not found verbatim in cited turns")) if kind == "commitment": owner = item.get("owner") if not owner: problems.append(Finding(i, "reject", "commitment has no owner")) elif cites and not missing and owner not in {turns[c]["speaker_role"] for c in cites}: problems.append(Finding(i, "review", f"owner {owner!r} is not the speaker of any cited turn")) if not item.get("due"): problems.append(Finding(i, "review", "commitment has no due date: confirm, do not guess")) if item.get("inferred"): problems.append(Finding(i, "review", "item is marked as an inference, not a stated fact")) result.findings.extend(problems) if any(p.level == "reject" for p in problems): result.rejected.append(item) elif problems: result.needs_review.append(item) else: result.accepted.append(item) return result def main(path: str) -> int: with open(path, encoding="utf-8") as fh: data = json.load(fh) res = check_summary(data["transcript"], data["summary"]) print(f"accepted={len(res.accepted)} needs_review={len(res.needs_review)} rejected={len(res.rejected)}") for f in res.findings: print(f" item {f.item_index} [{f.level}] {f.message}") return 1 if res.rejected else 0 if __name__ == "__main__": sys.exit(main(sys.argv[1] if len(sys.argv) > 1 else "sample_call.json")) A worked (hypothetical) example
The sample data below is invented for illustration, not a real customer or call. It deliberately contains the failure modes we care about: a fabricated "we will move your billing date" promise, an inference about churn, a commitment without a date, and a citation to a turn that doesn't exist.
{ "_note": "HYPOTHETICAL example data for illustration only. Not a real customer or call.", "transcript": [ {"id": "t1", "speaker_role": "customer", "text": "Hi, our March invoice charged us for 14 seats but we only have 11 people now."}, {"id": "t2", "speaker_role": "agent", "text": "I can see that. I'll raise a credit request for the three extra seats today."}, {"id": "t3", "speaker_role": "customer", "text": "Great. Also, can we move billing to the first of the month?"}, {"id": "t4", "speaker_role": "agent", "text": "I'm not sure that's possible on your plan. Let me check with billing and get back to you."}, {"id": "t5", "speaker_role": "customer", "text": "Okay. Honestly we're evaluating other vendors, so the sooner the better."} ], "summary": { "items": [ {"type": "fact", "text": "Customer was billed for 14 seats but has 11 users.", "evidence_turns": ["t1"], "quote": "charged us for 14 seats but we only have 11 people"}, {"type": "commitment", "text": "Agent will raise a credit request for 3 seats today.", "owner": "agent", "due": "same day", "evidence_turns": ["t2"], "quote": "raise a credit request for the three extra seats today"}, {"type": "commitment", "text": "Agent will confirm with billing whether the billing date can move.", "owner": "agent", "evidence_turns": ["t4"], "quote": "check with billing and get back to you"}, {"type": "customer_request", "text": "Customer asked to move billing to the 1st and was promised it would be done.", "evidence_turns": ["t3"], "quote": "we will move your billing date"}, {"type": "fact", "text": "Customer is at churn risk.", "evidence_turns": ["t5"], "quote": "we're evaluating other vendors", "inferred": true}, {"type": "fact", "text": "Customer prefers email follow-up.", "evidence_turns": ["t9"], "quote": "please email me the details"} ] } } Running python3 summary_check.py sample_call.json prints:
accepted=2 needs_review=2 rejected=2 item 2 [review] commitment has no due date: confirm, do not guess item 3 [reject] quote not found verbatim in cited turns item 4 [review] item is marked as an inference, not a stated fact item 5 [reject] cites turns that do not exist: ['t9'] Two items are saved, two go to a person, and the two fabricated ones never reach the CRM. The script exits with status 1 when anything is rejected, so you can use it as a pipeline gate.
Tests
The repository folder includes test_summary_check.py with 11 unittest cases (grounded fact accepted, case/punctuation-insensitive matching, paraphrased quote rejected, missing turn, missing citation, short quote, commitment without owner, missing due date, owner not the speaker, inference, and the sample file). Run them with:
python3 -m unittest -v test_summary_check Limits worth being honest about
- A matching quote is necessary, not sufficient. The model can quote a real line and still summarise it wrongly ("let me check" quoted, "agreed" claimed). That's why commitments get extra rules, and why sampling summaries against the source still matters.
- Exact matching is strict on purpose. Transcription errors or a model "tidying" a quote will cause rejections. Treat a rising rejection rate as a signal to inspect, not as a reason to loosen to fuzzy matching by default.
- This validates grounding, not completeness. It can't tell you the summary missed an important open question. Counting
open_questionitems per call (zero on a long, messy call is suspicious) is a cheap complementary check. - Mind the data. Transcripts and quotes can contain personal information; keep storing and logging them in line with your own retention policies.
Where to take it next
- Generate the items with your provider's structured-output feature so malformed JSON never reaches the checker.
- Track reject and review rates per item type over time; that tells you where your prompt or transcription is weakest.
- Require human approval before any summary item automatically does something (creates a task, changes a deal stage, emails a customer).
From the Call Karma team. We write about practical AI in customer conversations at callkarma.ai.