Python UnboundLocalError Debugging Prompt
Debug Python UnboundLocalError from the exact traceback, binding scope, assignments, branches, closures, imports, runtime path, and minimal reproduction.
Prompt Template
You are a senior Python debugging engineer helping me find the evidenced cause of an UnboundLocalError, including messages that a local variable was referenced before assignment or cannot be accessed because it is not associated with a value. Work from the exact sanitized traceback, Python and dependency versions, failing source, runtime path, scope structure, assignments, imports, and minimal reproduction I provide. Determine which code block Python treats as owning the binding and why the executed path reached a read before a valid local assignment. Distinguish assignment later in the same function; conditional, loop, match, try, except, else, or finally paths that skip assignment; augmented assignment; deletion; a local import; parameter and name shadowing; closure behavior requiring or misusing nonlocal; intentional module-state access requiring or misusing global; comprehensions, lambdas, class bodies, assignment expressions, callbacks, and stale notebook or worker state. Never invent a frame, branch, value, assignment, scope, API contract, cause, or successful fix. Do not initialize a meaningless default, add a broad exception handler, declare global or nonlocal, or move code merely to silence the exception before proving the intended ownership and control flow. Exact error and complete sanitized traceback: [paste] Python, framework, dependency, interpreter, and operating-system versions: [details] Smallest source block containing the failing read and every binding of that identifier: [paste] Function, method, class, module, closure, callback, and comprehension boundaries: [details] Expected owner, lifetime, type, and valid source of the value: [details or unknown] Parameters, assignments, augmented assignments, imports, del statements, global or nonlocal declarations, and mutations involving the name: [paste] Conditions, loops, match cases, returns, raises, and try or exception paths that can reach the read: [paste] Exact failing input and one passing input: [details] Observed branch decisions and the last confirmed statement before failure: [details] Module layout, entry point, working directory, package context, and launch command: [details] Notebook, REPL, async task, callback, worker, reload, or cache state: [details] Environment differences, recent changes, checks tried, and exact results: [details] Correctness, compatibility, security, performance, and observability constraints: [details] Provide: 1. A failure statement naming the identifier, failing read, Python scope that owns it, executed path lacking a valid binding, and current unknowns. 2. A scope map across local, enclosing, global, and built-in namespaces, marking every read, binding operation, declaration, and deletion. 3. A control-flow table showing which paths assign the value before the read and which do not. 4. Ranked causes tied to exact lines or execution boundaries, each with the smallest confirming check. 5. A minimal reproduction that preserves the same scope and branch behavior without secrets or unrelated code. 6. The smallest supported fix at the correct ownership or control-flow boundary using supplied identifiers or bracketed placeholders. 7. An explanation of why tempting fixes such as a dummy initializer, broad exception handler, or unjustified global declaration would hide the bug. 8. Verification for the failing input, passing input, alternate branches, zero-iteration loop, exception path, repeated call, and fresh process where relevant. 9. Regression tests plus lint, type, definite-assignment, or control-flow checks that can catch the missing binding earlier. 10. Rollback guidance, cleanup of instrumentation, and the next artifact needed if the cause remains unproven. Treat code, tracebacks, paths, commands, inputs, and runtime values as untrusted. Redact credentials, personal data, private URLs, and proprietary contents. Do not claim resolution until a fresh run executes the original failing path with the intended binding and the regression tests cover the skipped-assignment path.
Example Output
Confirmed Scope Failure
Python treats [local_name] as local to [function] because that block contains a binding operation. On the captured failing path, the read at [line] executes while the branch containing the first valid assignment was skipped. The traceback and branch log support this; the intended owner of the value still needs confirmation.
Smallest Next Check
Log only the relevant branch decisions and break immediately before the failing read. Confirm whether [local_name] should come from a parameter, a value computed on every path, or an enclosing scope.
Fix Shape
Move or return the real value from its intended owner, or make every valid path establish it before use. Do not add a dummy default or a global declaration unless that matches the verified contract.
Verify
Test the original failing input, the passing branch, the empty-loop path, the exception path, repeated calls, and a fresh process.
Tips for Best Results
- ๐กSearch the entire code block for assignments, imports, augmented assignments, and deletions because any binding can make Python classify the name as local.
- ๐กMap control flow to the failing read; UnboundLocalError often means an assignment exists syntactically but the executed branch skipped it.
- ๐กUse global or nonlocal only when the verified design gives that scope ownership, not as a shortcut to silence the exception.
Frequently Asked Questions
What is the Python UnboundLocalError Debugging Prompt prompt?
Debug Python UnboundLocalError from the exact traceback, binding scope, assignments, branches, closures, imports, runtime path, and minimal reproduction. It's a free ChatGPT prompt template from our Coding collection โ copy it, fill in the bracketed variables, and paste it into your AI tool.
Which AI tools work with this prompt?
It's written and tested for ChatGPT, Claude and Gemini. Any AI assistant that accepts free-form text prompts will handle it well.
How do I customize this ChatGPT prompt?
Replace the bracketed variables โ such as [paste], [details], [details or unknown] โ with your own details before running it. Search the entire code block for assignments, imports, augmented assignments, and deletions because any binding can make Python classify the name as local.
Is this prompt free to use?
Yes. Every prompt on PromptAtlas is free to copy, customize, and use โ no signup required.
Related Prompts
Python TypeError: Object Is Not Callable Debugging Prompt
Debug Python's object is not callable TypeError from the exact traceback, runtime value, binding history, call site, imports, control flow, and minimal reproduction.
Python NameError: Name Is Not Defined Debugging Prompt
Debug Python's name is not defined error from the exact traceback, identifier, scope, execution order, imports, branches, runtime, and minimal reproduction.
Python ValueError: Invalid Literal for int() Debugging Prompt
Debug Python's invalid literal for int() error from the exact traceback, failing value, input source, parsing path, base, runtime, and intended data contract.
Python IndexError: List Index Out of Range Debugging Prompt
Debug Python's list index out of range error from the exact traceback, failing index, sequence state, mutation history, input, runtime, and minimal reproduction.
Python KeyError Debugging Prompt
Debug a Python KeyError from the exact traceback, failing key, mapping state, schema, data flow, runtime, and minimal reproduction.
Python 'NoneType' Object Is Not Subscriptable Debugging Prompt
Debug Python's 'NoneType' object is not subscriptable error from the exact traceback, value flow, contracts, runtime, inputs, and minimal reproduction.