Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk4 min

Build a Tiny Python Prompt Scrubber to Spot Fixture Leakage

A standard-library Python exercise can expose some test-fixture strings pasted into AI coding prompts, but its regex and substring limits matter.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Fixture leakage occurs when expected test values are included in an AI coding prompt, giving a model a chance to copy known answers instead of implementing the rule that produces them. A small local Python script can reveal some literal overlap before you send a prompt—but it cannot prove the prompt is safe.

What you will build—and what it can catch

The exercise is a local preflight check: scan pytest-style assert ... == ... lines, collect expected fragments, then replace matching fragments in a prompt with [[REDACTED_EXPECT]]. It uses Python’s standard library; the tutorial author reports running it with Python 3.11, without pip or a notebook.

As an Amazon Associate I earn from qualifying purchases.

The underlying issue is dataflow. Test source contains literals, your prompt is another string, and a shared substring can expose an expected answer. As the tutorial’s author, Alex Chen, puts it: “Fixture leakage is not a vibe-coding slogan. It is a dataflow bug.”

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

This scrubber is a teaching gate, not a general-purpose prompt-security system. It detects some text overlap; it does not establish that a prompt is safe or that a model will generalize correctly.

Build and run the scrubber

Save the following as tiny_prompt_scrubber.py. This deliberately narrow implementation looks for quoted string literals on equality assertions, filters out fragments shorter than two characters, and replaces longer matches in the prompt.

import re

ASSERT_RE = re.compile(r"asserts+.+?==s*(?P<expected>.+?)s*$")
STRING_RE = re.compile(r"(['"])(.*?)1")


def expected_fragments(test_source: str) -> list[str]:
    fragments = []
    for line in test_source.splitlines():
        match = ASSERT_RE.search(line)
        if not match:
            continue
        expected = match.group("expected")
        fragments.extend(value for _, value in STRING_RE.findall(expected))
    return sorted({value for value in fragments if len(value) >= 2}, key=len, reverse=True)


def scrub(prompt: str, fragments: list[str]) -> str:
    for fragment in fragments:
        prompt = prompt.replace(fragment, "[[REDACTED_EXPECT]]")
    return prompt


if __name__ == "__main__":
    test_source = '''
assert result.name == "Mira Chen"
assert result.label == "North-Star"
'''
    prompt = "Write a function that returns Mira Chen for this record and uses North-Star as its label."
    fragments = expected_fragments(test_source)
    print("Expected fragments:", fragments)
    print("Before:", prompt)
    print("After:", scrub(prompt, fragments))

Run it from a terminal with python tiny_prompt_scrubber.py (or python3 tiny_prompt_scrubber.py where that is the Python 3 command). The output lists the extracted fragments and shows each matching occurrence in the prompt replaced by the marker.

Predict which strings will leak

For the example, the expected fragments are Mira Chen and North-Star. Because replacements are sorted longest first, a longer expected value is handled before any shorter fragment that might occur inside it. The script replaces substrings wherever they occur, not only when they appear as complete words or quoted values.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

That behavior is useful for spotting overlap, but it can also change unrelated prose. If the expected value is the letter a, unrestricted replacement could damage many ordinary words. The two-character minimum avoids that particular flood of replacements; it also means a one-character expected value will not be detected. This is a readability trade-off, not a security threshold.

Understand the limits before trusting the result

The regular expression is intentionally simple and does not cover many normal ways tests express expectations. It can miss pytest.mark.parametrize, doctests, values stored in helper variables, and comparisons such as assert result.name == person. It also works line by line and extracts quoted strings rather than understanding Python expressions.

  • A redacted match means a literal fragment appeared in both the selected assertion text and the prompt; it does not say whether the rest of the prompt is safe.
  • No redacted match does not mean no expected data was exposed. The test pattern may be outside the regex’s coverage, or an expected value may be shorter than the filter allows.
  • Successful generation would not validate the scrubber. A model can still produce a wrong implementation or infer an answer from other context.

Do not use this script as a production redaction filter, or to handle secrets or sensitive student data. Its narrow pattern matching and substring replacement are not suitable guarantees for those cases.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose a stronger way to separate answers from prompts

Parse Python source when you need structure

Python’s official ast documentation describes ast.parse as a way to parse source into an abstract syntax tree. Walking that tree can support more structured extraction than searching lines with a regular expression, but you still need logic for the specific test constructs and values you want to identify. The documentation also cautions that successful parsing alone does not guarantee the source can be compiled or executed. An AST is a parsing route, not a leakage-prevention guarantee.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Keep expected values out of the prompt

A more direct data-handling improvement is to store expected values in a JSON fixture that never enters the prompt. If the model does not receive the answer data, this particular route for copying fixture values is removed. Keep the fixture available to your local tests, and give the model the task and necessary input format without including the expected outputs.

What the demonstration showed

Chen reports that the original prompt exposed two fixture strings and that the scrubbed prompt exposed zero of those two. The author also reports that a later generated draft failed on a second input because it assumed a comma rather than a dash, after which delimiter handling was fixed. These are observations from one tutorial demonstration, not a benchmark or evidence of general effectiveness; the author reports no token benchmark or timing result.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.