Help me solve this Python task: [PYTHON TASK] Context: - Python version and environment: [PYTHON VERSION AND ENVIRONMENT] - Existing code, error output, or relevant interface: [EXISTING CODE OR ERROR] - Constraints: [CONSTRAINTS] - Expected behavior and example inputs/outputs: [EXPECTED BEHAVIOR] First, ask up to 3 clarifying questions only if a required input is missing. Otherwise, provide:
1. A short plan stating the algorithm, relevant standard-library modules, and time/space complexity where it matters. 2. Complete runnable Python code in one code block. Preserve any public function names or signatures I supplied. Prefer the standard library unless a dependency is explicitly allowed. 3. A concise explanation of the non-obvious lines and the data flow. 4. A small pytest test file or test cases covering the normal case, an empty or boundary input, and the most likely invalid input or failure path. 5. Run instructions, including required installation commands only if they are necessary. Follow Python conventions: type hints for public functions, descriptive names, no hidden global state, and no broad except clauses. Validate external or user-provided input at the boundary. If this involves files, network calls, dates, money, credentials, or destructive operations, isolate that behavior and explain safe handling. Do not invent APIs, package behavior, files, or benchmark results; mark assumptions explicitly. Before answering, self-check that the code matches every stated expected behavior and that each import, method, and package is valid for the stated environment. Flag any ambiguity, unsupported claim, or edge case that needs my decision.
Fill in
| Placeholder | What to enter | Example |
|---|---|---|
| [PYTHON TASK] | Describe the program, function, bug, or automation you need help with. | Write a function that groups CSV rows by customer ID and returns each customer's latest valid order. |
| [PYTHON VERSION AND ENVIRONMENT] | Enter your Python version and where the code runs, such as local macOS, Django, or AWS Lambda. | Python 3.12, local command-line script on macOS. |
| [EXISTING CODE OR ERROR] | Paste relevant code, a traceback, or state that you are starting from scratch. | Starting from scratch; input arrives as a list of dictionaries. |
| [CONSTRAINTS] | List limits such as allowed packages, performance needs, input size, or security requirements. | Standard library only; ignore malformed dates but report their row numbers. |
| [EXPECTED BEHAVIOR] | Give concrete examples of valid inputs and the exact results you expect. | Two rows for C-12 dated 2025-04-01 and 2025-05-10 should return only the May row. |
How to use
- Replace each bracketed field with the task, runtime, and examples you actually have.
- Paste the full prompt and include the exact traceback if you are debugging.
- Run the supplied tests before merging code, then check that assumptions match your project.
- Follow up with: “Add tests for these production cases: [CASES], and show only the changed code.”
Variations
Debug a traceback
Use this when code already fails and you need a focused diagnosis.
Diagnose this Python failure without rewriting unrelated code. Code: [CODE] Traceback and observed behavior: [TRACEBACK] Expected behavior: [EXPECTED RESULT] Runtime and dependencies: [ENVIRONMENT] Identify the root cause, distinguish it from secondary symptoms, and give the smallest safe patch as a unified diff. Explain why the failure occurs, then provide 3 pytest cases: the reported failure, a nearby boundary case, and a regression case. Do not assume missing files, APIs, or package versions. Ask up to 3 questions only if required context is absent. Before answering, verify that the patch preserves the stated function interface.
Code review
Use this before shipping a Python change or opening a pull request.
Review this Python change as a senior maintainer. Requirements: [REQUIREMENTS] Code or diff: [CODE OR DIFF] Runtime: [PYTHON VERSION] Risk context: [DATA OR SECURITY CONTEXT] Return findings ordered by severity: correctness, security, reliability, maintainability, then tests. For each finding, cite the relevant line or behavior, explain the concrete failure mode, and propose a minimal fix. Include no praise or invented concerns. End with a release recommendation: approve, approve with changes, or block. Ask up to 3 questions only if a required input is missing. Check every claimed issue against the supplied code.
Build a CLI
Use this when turning a repeatable task into a command-line utility.
Design a production-ready Python command-line tool for this workflow: [WORKFLOW]. Inputs and outputs: [INPUTS AND OUTPUTS] Users will run it like: [EXAMPLE COMMAND] Constraints: [CONSTRAINTS] Provide a file layout, argparse interface with useful help text, complete core implementation, exit codes, and example commands. Handle missing files, malformed inputs, and partial failures without silently losing data. Use pathlib and the standard library unless I allow dependencies. Include pytest cases for argument parsing and one end-to-end happy path. Ask up to 3 questions only if required information is missing. Check that every documented command matches the code exactly.
Tips
- Give one representative input and one awkward input; Python bugs often live in empty lists, duplicate keys, time zones, and malformed records.
- Name the deployed Python version because syntax, typing behavior, and standard-library APIs differ across versions.
- For a bug report, paste the complete traceback from the first exception line through the final line; the last line alone often hides the cause.
- Ask for a patch or diff when modifying an established codebase so imports, function signatures, and surrounding behavior stay visible.
FAQ
Can I ask the AI to choose a Python library?
Yes, but state the runtime, licensing or deployment limits, and whether adding dependencies is allowed. Ask it to compare only packages it can support with documented behavior.
Should I paste secrets from my code?
No. Replace tokens, passwords, customer data, and private URLs with realistic placeholders while keeping the structure of the problem.
Why ask for tests in the prompt?
Tests force the proposed behavior to become concrete. They also catch code that works only for the single example in the conversation.