Design a multi-step prompt sequence for a language model to assist in debugging software code.

Instruction: Design a staged diagnostic prompt sequence for the supplied Python failure, propose the smallest patch, and give regression tests. Use the observed evidence; do not execute untrusted code outside a bounded test environment.

Context:

The function must return the first positive number, or None when none exists. The observed failure is StopIteration for [-2, 0].

Python fixture

def first_positive(xs):
    return next(x for x in xs if x > 0)

Distinguish the root cause from unrelated input-contract questions. Provide the minimal correction and tests for empty input, no positive values and multiple positive values.

Updated

Example Answer

I would split code debugging into stages rather than ask for one giant answer. A good sequence is: first summarize the bug, then inspect likely root causes, then propose the smallest fix, then suggest tests.

For example:

Step 1: Read the code and explain the likely bug in plain English. Step 2: List the top 3 root-cause hypotheses and rank them. Step 3: Propose the smallest safe code change. Step 4: Explain what test cases should be added or run to verify the fix.

This structure works because debugging is a reasoning workflow. Breaking it into diagnosis, fix, and verification reduces shallow code rewriting and improves traceability.

Common Poor Answer

A weak answer says "fix this code" and skips diagnosis, alternative hypotheses, and verification.

Worked practice case

Stage 1: ask the model to restate the contract and reproduce the stated empty-result failure. Stage 2: ask it to distinguish generator exhaustion from type errors, using [-2,0] and [] as evidence. Stage 3: request the minimal patch: return next((x for x in xs if x > 0), None). Stage 4: request tests for [-2,0]→None, []→None, [-2,3,1]→3 and [2,4]→2.

Do not catch every exception and return None: unsupported element types should follow an explicitly defined input contract, not be hidden as 'no positive value.' Verify the patched function in a sandbox with only the needed inputs, and report tests actually executed separately from suggestions. This sequence uses observed evidence rather than forcing three speculative causes when one reproduction identifies the defect.

Related Questions