Sub topic 11
Refining output with feedback
This chapter assumes you can run pytest, read a failing test report, and use Git. It also assumes you have already generated a first draft of tests or docstrings, as in the earlier chapters. The first draft is rarely the final one. This chapter covers how to turn review findings into follow-up requests that improve the draft without breaking what already works.
The refinement loop
Each round has the same shape. You run the draft, record what is wrong, and send the assistant a narrow correction. Then you run it again.
Running the draft comes before judging it. A failing test, a doctest mismatch, or a coverage gap gives the assistant concrete evidence. "This feels weak" gives it nothing to act on.
Kinds of feedback that work
| Feedback type | Example | Why it works |
|---|---|---|
| Tool output | The pytest failure text, pasted verbatim | The assistant sees the exact assertion and values |
| Review finding | "Test 3 re-implements the function" | Names one defect in one place |
| Missing case | "Add a case for price 0" | Adds scope without rewriting the rest |
| Constraint change | "Use `pytest.approx` for floats" | Changes one convention |
Vague requests such as "make the tests better" invite a full rewrite. A rewrite can drop cases you already reviewed.
A worked round
Suppose the first draft contains this test:
from pricing import apply_discount
def test_apply_discount_matches_formula():
assert apply_discount(100, 10) == round(100 * (1 - 10 / 100), 2)The test repeats the function's own formula, so it passes even if the formula is wrong. Send feedback that names the problem and the fix:
The test test_apply_discount_matches_formula is tautological: the
expected value is computed with the same formula as the function.
Replace it with parametrized cases whose expected values are written
as literals and derived from the docstring. Include:
- price 19.99, percent 15, expected 16.99 (rounding)
- price 0, percent 50, expected 0.0
Keep every other test unchanged. Do not modify apply_discount.The assistant should now return something like this:
import pytest
from pricing import apply_discount
@pytest.mark.parametrize(
"price, percent, expected",
[
(19.99, 15, 16.99),
(0, 50, 0.0),
],
)
def test_apply_discount_uses_literal_expected_values(price, percent, expected):
assert apply_discount(price, percent) == expectedYou can check the first case by hand: 19.99 × 0.85 is 16.9915, which rounds to 16.99. Verify literals like this yourself. An assistant can write a wrong expected value, and a test built on it will either fail or lock in a bug.
Rules for each round
- Change one thing at a time when possible. Small diffs are easier to review.
- Say what must stay unchanged. "Keep the other tests as they are" prevents silent edits.
- Paste real error text instead of describing it.
- Re-run everything after each round, not only the part you changed.
- Read the diff between rounds. Assistants sometimes weaken an assertion or delete a case to make a failure go away.
Knowing when to stop
Stop when the draft meets your review checklist: it covers normal values, boundaries, and error paths; it asserts on independently derived values; and the suite passes. More rounds after that tend to add churn, such as renamed tests and reshuffled order, with no gain in confidence.
If three rounds have not fixed a problem, start a fresh request with a better prompt. Long threads accumulate stale instructions, and the assistant may follow an old one that you have since contradicted. Copy the current code and your checklist into the new prompt.
Exercise
A generated test fails with an assertion error on a rounding case. What are two things you should do before sending feedback to the assistant, and what should the feedback include?
Show answer
First, run the test again to confirm the failure, and work out by hand (from the docstring) which value is correct, so you know whether the test or the code is wrong. Then send feedback that includes the verbatim pytest output, states which side is correct, names the single test to change, and says that the function under test and the other tests must stay unchanged.
Exercise
Why is "make the tests better" a poor refinement request?
Show answer
It names no defect and no evidence, so the assistant may rewrite the whole file and drop cases you already reviewed. Specific feedback tied to a test name, an error message, or a missing case limits the change and makes the diff easy to review.