---
id: 18978
lang: en
title: "A Python Prompt Test in Under 150 Lines Catches Edits That Silently Break AI Answers"
published_at: "2026-12-02 14:34:54"
source_name: "Machine Learning Mastery"
source_url: "https://machinelearningmastery.com/build-a-lightweight-llm-evaluation-harness-in-python/"
canonical_url: "https://news.hamidun.com/en/news/18978"
categories: ["llm"]
publisher: "AI News by Jemal Hamidun (news.hamidun.com)"
---

# A Python Prompt Test in Under 150 Lines Catches Edits That Silently Break AI Answers

> **Краткая суть (BLUF):** You tweak a prompt, and an answer that worked yesterday silently breaks. That is a prompt regression, and Machine Learning Mastery author Iván Palomares Carrascosa catches it with a Python harness of under 150 lines: exact matching checks labels like BILLING, while an LLM judge scores live answers on a 1–5 scale. In the demo, the new version fixes the label but replies to a plan-change question with a bare "BILLING". So compare versions test by test, not by an overall percentage.  • Exact matching rejects "Category: BILLING." when the expected label is BILLING • The LLM judge returns JSON that follows a Pydantic schema: a 1–5 score, a pass flag, and a reason • Temperature 0 removes random noise between runs

Most teams test prompts by eye: they open a playground, run a couple of examples, pick the variant that looks better, and ship it. As long as things look fine, the approach holds. But a small wording change can silently break an answer that passed without issue a few days earlier. That failure is called a regression: the new version looks better overall, while one specific case has gotten worse. Iván Palomares Carrascosa, writing for Machine Learning Mastery, shows how to catch it without a heavy MLOps stack or observability platform. A lightweight Python evaluation harness fits in under 150 lines of code.

## Three Tools in One Harness

The first is exact match, for tasks with a single correct answer: classification, entity extraction, routing support tickets. The model's output is uppercased, stripped of punctuation and extra spaces, and compared against a reference. It is fast and free. For a developer, this check matters for a practical reason: if the code downstream expects a clean BILLING label, then "Category: BILLING." is a failure, even though a human can tell what it means. A match scores 5 and a mismatch scores 1. That puts exact-match scores on the same 1–5 scale as the judge's scores, so they don't need converting when you average.

The second is an LLM judge for open-ended answers, where the wording varies each time. The judge receives the user's question, the assistant's answer, and a rubric, for example: "acknowledges frustration over the outage" and "gives clear cancellation steps without being pushy." It must return JSON that follows a Pydantic schema: a score from 1 to 5, a "passed" flag (yes at a score of 4 or 5), and a one-sentence justification. Structured output removes the need for brittle regular expressions that would otherwise have to parse free text.

The third is a version log. The results of each prompt version are saved, and runs are then compared test by test. The goal is to find any case that moved from "passed" to "failed" in the new version.

## What This Looks Like in a Support Example

The demo is built around a support assistant and a small golden set of four tests. Two cover routing: "I was charged twice on invoice #4021" should go to BILLING, and "The mobile app crashes when I tap Export PDF" should go to BUG_REPORT. The other two cover live answers: an angry email demanding a subscription cancellation over outages, and a question about whether one can switch from monthly to annual billing mid-cycle.

So the code runs without an API key, the example uses pre-recorded model responses. In the v1_baseline version, the model answers the first request with "Category: BILLING." instead of the clean label, and exact matching flags it. In v2_strict, an instruction about strict labels fixes that case, and for the cancellation request the assistant apologizes for the outage and points to the path "Settings > Subscription > Cancel." But for the plan-change question, v2 returns a bare "BILLING" instead of an explanation. The strict instruction has leaked into an open-ended dialogue. The code marks this failure as a simulated regression.

That is why an overall pass rate is not a sufficient signal on its own: it can rise while one user scenario breaks. A test-by-test comparison shows exactly what changed and lets you halt the rollout.

## Running It and Practical Takeaways

The code is designed for Colab. Mock mode is on by default (USE_MOCK_LLM=True). Live calls require a free Google AI Studio key, which you store in Colab secrets under the name GEMINI_API_KEY, and then switch the flag to False. The example uses the gemini-2.5-flash model.

What to take away:

- You don't need hundreds of examples. A few representative ones are enough: typical requests, edge cases, and anything that has already broken in production.
- Set temperature to 0. Then differences in scores reflect the prompt edit rather than random token sampling. This reduces noise, though it does not guarantee full reproducibility.
- Separate the kinds of checks. Anything that can be verified with a string should be checked with a string: it is cheaper and uncontested. Reserve the judge for assessing tone and completeness.

## Limitations

This is a scaffold, not a quality-control system. Four tests provide no statistical power, and the judge is itself a model, with its own errors and biases. A real project will need a larger test set, and the judge's scores should be periodically checked against manual labeling. From the portion of the article available, it is clear how the labels, the judge, and the regression demo are structured. The full code for the version-comparison function is truncated in this excerpt, so its details should be checked in the original.

---
- **Первоисточник:** [Machine Learning Mastery](https://machinelearningmastery.com/build-a-lightweight-llm-evaluation-harness-in-python/)
- **Каноническая ссылка:** https://news.hamidun.com/en/news/18978
- **Экосистема автора:** [Бесплатный интенсив по вайбкодингу — предзапись в Нике](https://t.me/vibecodeguidebot?start=agent_intensive_news_md) · [Nomad AI](https://nomadnet.ai) · [Telegram @jemal_hamidun](https://t.me/jemal_hamidun)
