I need expert Python help for this task: [TASK DESCRIPTION]. Context: - Python version and runtime: [PYTHON ENVIRONMENT] - Existing code, errors, or relevant interfaces: [CURRENT CODE OR ERROR] - Inputs, expected outputs, constraints, and performance needs: [REQUIREMENTS] - Libraries I may use, libraries I must avoid, and deployment context: [DEPENDENCIES AND CONSTRAINTS] Work as a senior Python engineer. First, restate the problem as a precise contract: inputs, outputs, invariants, error behavior, and any assumptions. If a required detail is absent, ask up to 3 focused clarifying questions; otherwise proceed.
Then produce: 1. A recommended approach in 3-6 bullets, including why it fits the stated constraints. 2. Complete runnable Python code, not pseudocode. Include imports, type hints where they improve correctness, docstrings for public functions, and clear names. 3. A brief explanation of the important control flow and data structures. 4. A pytest test block covering the normal case, boundary cases, invalid inputs, and the most likely regression case. Use only test data that follows from my notes. 5. Complexity analysis for time and space when relevant. 6. Integration notes: dependencies, configuration, file placement, and how to run the code. Use standard-library tools before adding a dependency unless a named library materially improves correctness. Do not silently catch broad Exception, use mutable default arguments, or rely on undocumented behavior. Preserve the public API in my existing code unless I explicitly authorize a change. Before answering, self-check that every function call and import is defined or identified as a dependency, and that the code handles empty, null-like, malformed, and unusually large inputs where applicable. Flag any requirement, claim, or expected result that cannot be supported by the information I provided.
Fill in
| Placeholder | What to enter | Example |
|---|---|---|
| [TASK DESCRIPTION] | Describe the Python feature, bug, automation, or algorithm you need help with. | Parse monthly CSV exports and flag duplicate invoice numbers with conflicting amounts. |
| [PYTHON ENVIRONMENT] | State the Python version, operating system, framework, and execution environment if relevant. | Python 3.12, FastAPI service on Linux, data arrives as uploaded files. |
| [CURRENT CODE OR ERROR] | Paste the smallest relevant code sample, stack trace, API signature, or say that you are starting fresh. | Starting fresh; the endpoint will receive a file-like object. |
| [REQUIREMENTS] | List example inputs and outputs, business rules, scale, latency, and compatibility constraints. | Return invoice number, row numbers, and amounts; files can have 250,000 rows; blank invoice IDs should be reported separately. |
| [DEPENDENCIES AND CONSTRAINTS] | List allowed or prohibited packages and where the code will run. | Standard library preferred; pandas is not available; memory limit is 512 MB. |
How to use
- Replace every bracketed field, especially the expected input and output examples.
- Paste the prompt and include the smallest reproducible code sample or stack trace when debugging.
- Run the generated tests before adopting the code, then compare the error behavior with your application contract.
- Follow up with: "Revise this for these failing test results: [TEST OUTPUT]. Keep the public function signature unchanged."
Variations
Debug a traceback
Use when code already fails and you need a targeted diagnosis.
I need help debugging this Python failure: [TRACEBACK AND CODE]. My environment is [PYTHON VERSION AND PACKAGES], and the behavior I expected is [EXPECTED BEHAVIOR]. Identify the most likely root cause, distinguish it from secondary issues, and give the smallest safe patch. Explain why the failure occurs, then provide a pytest regression test that fails before the patch and passes after it. Do not invent missing code or configuration; ask up to 3 questions only if needed. Self-check that your proposed patch matches the traceback line and does not mask the exception.
Code review
Use before merging a Python change.
Review this Python change for production risks: [CODE OR DIFF]. It runs in [RUNTIME CONTEXT] and must satisfy [REQUIREMENTS]. Return findings ordered by severity: correctness, security, data loss, performance, maintainability, then style. For each material finding, cite the relevant line or construct, describe a concrete failure scenario, and show a minimal correction. Also list missing tests. Do not recommend speculative rewrites. Self-check that every finding is supported by the supplied code and that proposed APIs exist in the stated environment.
Design an API
Use when planning a Python module or service before coding.
Design a Python API for [FEATURE]. Consumers are [CALLERS], inputs and outputs are [DATA CONTRACT], and constraints are [CONSTRAINTS]. Propose module boundaries, public function or endpoint signatures, validation rules, error types, and a test plan. Include one small runnable skeleton using type hints, but do not implement unrequested infrastructure. Explain tradeoffs between the leading design and one simpler alternative. Ask up to 3 questions only for missing required facts. Self-check that the design handles versioning and invalid input without exposing internal exceptions.
Tips
- Give the model a minimal reproducible example: the function, the input that fails, the actual output, and the expected output.
- State the Python and package versions because typing behavior, standard-library APIs, and framework conventions can differ materially.
- Ask for a regression test tied to the reported bug; a patch without that test is easy to re-break later.
- For code that processes money, dates, credentials, or user input, specify the representation and validation rules instead of accepting convenient defaults.
FAQ
Can AI write production-ready Python code?
It can produce useful starting code, but you should run it in the target environment and review tests, dependencies, security boundaries, and error handling before release.
What should I include with a Python error?
Include the full traceback, Python and package versions, the smallest relevant code, sample input, and the output you expected.
Should I ask for type hints?
Usually yes for public functions and complex data flows. They improve review and tooling, but they do not replace runtime validation at system boundaries.