🐍 Python for Everyone

← Back to the project

Chapter 26 — Solo → AI-Paired → Critique 25 min

Now Pair With an AI

Step 1: You already built this solo

You just built the Production Incident Runbook yourself, unaided. That project is unchanged — this page is additive. Now the same project gets built a second way: by prompting an AI assistant, and then reviewing what it hands back with the same skepticism you'd bring to a coworker's pull request.

Step 2: Now pair with an AI

Here's a real prompt a learner could give an AI assistant to build the same runbook — an extended version of the prompt from the classify_symptom() box on the main project page:

the-prompt.txt
Write a Python "incident runbook" using only the standard library.
I need:

1. A CATALOG list of dicts, each with "id", "exception", "keywords"
   (a list of strings), "root_cause", and "fix".
2. classify_symptom(description) -- match a free-text symptom
   description against CATALOG's keywords and return the matching
   entries.
3. diagnose(description) -- call classify_symptom() and return a
   formatted report string, or a "no match" message if nothing
   matched.

Keep it to one file, no third-party packages, and make sure it
actually finds every relevant match, not just the first one.

And here's a plausible response — the kind of code an AI assistant might hand back on the first try. Read it as if it just landed in a pull request from a teammate, before scrolling to the critique below.

ai_generated_runbook.py
CATALOG = [
    {
        "id": 1,
        "exception": "KeyError",
        "keywords": ["keyerror", "missing key"],
        "root_cause": "A dict was accessed with [] on a key that isn't present.",
        "fix": "Use .get(key, default) or validate required keys at startup.",
    },
    {
        "id": 8,
        "exception": "ZeroDivisionError",
        "keywords": ["zerodivisionerror", "division by zero"],
        "root_cause": "A denominator sourced from external data was zero.",
        "fix": "Check the denominator before dividing.",
    },
]


def classify_symptom(description):
    """Find every catalog entry whose keywords match the description."""
    matches = []
    for entry in CATALOG:
        for keyword in entry["keywords"]:
            if keyword in description:
                matches.append(entry)
    return matches


def diagnose(description):
    """Return a formatted runbook entry for the best match."""
    matches = classify_symptom(description)
    if not matches:
        return "No matching catalog entry for this symptom yet."
    best_match = matches[0]
    return (
        f"Issue #{best_match['id']}: {best_match['exception']}\n"
        f"  Root cause: {best_match['root_cause']}\n"
        f"  Fix: {best_match['fix']}"
    )


if __name__ == "__main__":
    print(diagnose("Service crashes with a KeyError for a missing config key"))
    print(diagnose("Getting a KEYERROR in production, missing key issue"))

It's clean, it's organized the same way the lesson's version is, and the docstrings read well. It's also wrong in three separate, genuinely easy-to-miss ways.

Step 3: Where did the AI's code lie to you?

Click to see the critique

What it looks like it does: classify_symptom() checks if keyword in description for each of the catalog's lowercase keywords — looks like a normal, working substring search.

What it actually does: description is never lowercased. Python's in substring check is case-sensitive, so a keyword like "keyerror" only matches text containing exactly that lowercase spelling. The second call above — "Getting a KEYERROR in production, missing key issue" — contains KEYERROR in all caps, and "keyerror" is not a substring of that, so it's silently skipped. It still matches on "missing key" by luck, but that's incidental, not the fix.

Why it matters: real symptom descriptions — copy-pasted error messages, Slack messages, bug reports — are never guaranteed to be lowercase. A runbook that only works on lowercase input will miss real incidents constantly, and nothing about running it once on a lucky example reveals that.

The fix: lowercase description once at the top of the function (description = description.lower()) before checking any keyword against it — exactly what this chapter's own classify_symptom() does.

What it looks like it does: the prompt explicitly asked for code that "actually finds every relevant match, not just the first one," and classify_symptom() does return a full list of matches — looks like the requirement was met.

What it actually does: diagnose() takes that full list and then only ever uses matches[0] — best_match — silently throwing away every other match. If a symptom description genuinely matches two different catalog issues (a realistic case — a real incident often has more than one contributing cause), the caller never finds out about the second one at all, with no error or warning that anything was dropped.

Why it matters: this directly contradicts the one requirement the prompt was explicit about. It's also the kind of bug that's invisible in a demo with one clean symptom per call, and only shows up once a real-world, messier description comes through.

The fix: format and return every entry in matches, not just matches[0] — join them with a blank line between entries, the way this chapter's own diagnose() does.

What it looks like it does: the nested loop checks every keyword of every entry against the description and appends the entry when a keyword matches — looks like straightforward "does this entry match" logic.

What it actually does: because the inner loop doesn't stop after the first keyword match, an entry with two keywords that both appear in the description (e.g. a description containing both "keyerror" and "missing key") gets appended to matches twice — once per matching keyword, not once per matching entry. Combined with Flaw 2 this happens to be masked (only matches[0] is used either way), but fixing Flaw 2 without also fixing this one would start printing the same issue twice in one report.

Why it matters: a runbook that reports the same root cause and fix twice looks unpolished at best, and at worst makes a reader think there are two distinct issues when there's only one — exactly the kind of noise an incident runbook exists to cut through, not add.

The fix: use any(keyword in lowered for keyword in entry["keywords"]) to test the whole entry once, appending it at most one time — this chapter's own classify_symptom() does exactly this instead of a nested append-per-keyword loop.

Step 4: Your entry for this chapter's playbook

Fill this in for yourself — it's not saved or sent anywhere, just yours to keep (copy it into your own notes before you close this tab).

← Back to the Project All Chapters →