Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I turn a hard-to-reproduce function bug into a small example?

Quick answer

Start with the smallest input that still makes the function fail. Keep the relevant function code, that input, what you expected, and what it actually returned. Remove surrounding setup and unrelated code one change at a time, rerunning the same check after each change. If the failure disappears, restore the last change: it may have removed something necessary to reproduce the bug.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

My function fails inside a much larger script, and I can't tell which part matters. How should I start making a small reproduction?

Geez

First, write down one failing call before changing anything: the function name, its arguments, the expected result, and the actual result or error. Keep the function code you suspect is involved, but treat everything else as removable until testing shows otherwise. The goal is not to rewrite the function or make a nicer example; it is to preserve the failure with less surrounding material.

For example, suppose a larger program calls keep_items with data assembled from several steps. If the failure still occurs with a short literal list passed directly to the function, use that list instead. Make one reduction at a time—remove setup, shorten the input, or remove an unrelated statement—and rerun the same call. If the failure stops, undo that particular reduction and try a different one. This makes it easier to see which details are necessary.

Follow-up question

What exactly should the small example contain so another person can see the mismatch?

Geez

Include only what is needed to demonstrate the issue: a short function definition (or the relevant part if the rest is essential), a concrete input, the expected result, and the actual result. A simple check makes the comparison visible. Label expectations as expectations, rather than presenting them as something the code produced.

Here is a deliberately small example. Assume the intended behavior is to keep every item except None; the function below instead drops zero as well:

def keep_items(values):
    return [value for value in values if value]

items = [0, 2]
expected = [0, 2]
actual = keep_items(items)

print("expected:", expected)
print("actual:  ", actual)

For this example, the output is expected: [0, 2] and actual: [2]. The input includes the value that exposes the mismatch, and the stated expectation explains why that value matters. For your own bug, replace these assumptions with the behavior your function is meant to provide.

Follow-up question

How do I reduce the input without accidentally removing the important part or changing the bug?

Geez

Keep a copy of the original failing call, then reduce the input in small, reversible steps. Try removing one element, shortening one string, or replacing a complicated value with a simpler one. After each edit, run the same check and compare expected with actual. If it no longer fails, restore the last edit and try another reduction; the removed detail may have triggered the behavior.

Also preserve relationships that may matter, such as repeated values, ordering, or a particular empty or boundary value. Do not silently change the expected result to match the output. If you are unsure whether a detail matters, keep it and note it. A slightly larger example that reliably shows the mismatch is more useful than a tiny example that no longer fails.

Follow-up question

What should I write down when I share the finished reproduction?

Geez

Give the example a short label and separate the observed facts from your interpretation. State the expected behavior in plain language, show the minimal call and function code, and report the actual result or exact error text. Mention any remaining assumption—for example, “I expect zero to be retained”—rather than implying that the example establishes why the code behaves that way.

A copyable format is:

Goal: [what the function should do]
Input: [smallest input that still fails]
Expected: [result you intended]
Actual: [result or error observed]
Code: [relevant function and one call]
Reduced by: [what surrounding setup or data was removed]

This format records the failure without claiming a cause. A small reproduction can help focus debugging, but it does not by itself prove which change will fix the original program.

A practical checklist

  • Record the original failing call and observed result before editing.

  • State the intended behavior and expected result separately from the actual result.

  • Keep only relevant function code, the input, and any setup needed for the failure.

  • Reduce one detail at a time and rerun the same check after each change.

  • Restore a change if the failure disappears; preserve values or relationships that may trigger it.

  • Label assumptions and include the exact error or output you observed.

A few more useful answers

What if the bug depends on data from a file or another part of my program?

Try replacing that dependency with the smallest literal input that still reproduces the failure. If that is not possible, keep the necessary setup and clearly identify which part is required.

Should I simplify the function while reducing the example?

Only remove or change function code when you can rerun the reproduction and confirm the failure remains. Keep a copy of the original so you can restore a change that turns the example into a different case.

What if I cannot find a smaller failing input?

Share the smallest reliable case you have, along with its expected and actual results. A clear, reproducible example is useful even if it is not yet minimal.

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work