Debug your Python code with AI assistance
Project description
snooper-ai ๐
snooper-ai is a simple fork of PySnooper. It sends the entire execution trace (variable values you'll typically examine in a debugger) to an LLM for debugging, so it fully understands what happened in your code.
Disclaimer: This was implemented simply and may not be very robust. Feel free to submit issues.
Usage:
- Install
pip install snooper-ai
- Store your LLM api key (either anthropic or openai):
ANTHROPIC_API_KEY=xxx
OPENAI_API_KEY=xxx
- Add a decorator to the function you want to inquire:
from snooper_ai import snoop
@snoop()
def loop_index_error():
items = ['apple', 'banana', 'cherry']
total = 0
for i in range(5):
total += len(items[i])
return total
loop_index_error()
- Run the file
snoop run file.py
- Tell the LLM what you're confused about:
โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ ๐ snooper-ai: Debug your Python code with AI โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
What would you like to know about the code execution? (e.g. Error messages, unexpected behavior, etc.):
-
Run it to get a detailed explanation! If your code errors out, the execution trace and the error message will be sent to the LLM.
-
To choose which model you're using (This will be saved in your pyproject.toml):
snoop config
What's sent to the LLM:
Previous example:
17:15:51.845171 call 5 def loop_index_error():
17:15:51.845285 line 6 items = ['apple', 'banana', 'cherry']
New var:....... items = ['apple', 'banana', 'cherry']
17:15:51.845298 line 7 total = 0
New var:....... total = 0
17:15:51.845320 line 8 for i in range(5):
New var:....... i = 0
17:15:51.845335 line 9 total += len(items[i])
Modified var:.. total = 5
17:15:51.845347 line 8 for i in range(5):
Modified var:.. i = 1
17:15:51.845358 line 9 total += len(items[i])
Modified var:.. total = 11
17:15:51.845368 line 8 for i in range(5):
Modified var:.. i = 2
17:15:51.845377 line 9 total += len(items[i])
Modified var:.. total = 17
17:15:51.845386 line 8 for i in range(5):
Modified var:.. i = 3
17:15:51.845395 line 9 total += len(items[i])
17:15:51.845404 exception 9 total += len(items[i])
Exception:..... IndexError: list index out of range
Call ended by exception
Elapsed time: 00:00:00.000450
Error occurred:
Traceback (most recent call last):
File "/path/to/file.py", line 117, in run_file
spec.loader.exec_module(module)
File "<frozen importlib._bootstrap_external>", line 999, in exec_module
File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
File "/path/to/file.py", line 12, in <module>
loop_index_error()
File "/path/to/snooper-ai/snooper_ai/tracer.py", line 319, in simple_wrapper
return function(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/file.py", line 9, in loop_index_error
total += len(items[i])
~~~~~^^^
IndexError: list index out of range
Example response from LLM:
Analysis from Claude:
โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ # IndexError Analysis โ
โ โ
โ The problem is that your code is trying to access elements from the `items` list that don't exist. Here's what's happening: โ
โ โ
โ 1. You define `items = ['apple', 'banana', 'cherry']` which has 3 elements (at indices 0, 1, and 2) โ
โ 2. Then you loop `for i in range(5)` which tries to iterate through indices 0, 1, 2, 3, and 4 โ
โ 3. The trace shows successful iterations for: โ
โ - i=0: Accessed 'apple', total becomes 5 โ
โ - i=1: Accessed 'banana', total becomes 11 โ
โ - i=2: Accessed 'cherry', total becomes 17 โ
โ 4. When i=3, the code tries to access `items[3]`, but the list only has elements at indices 0, 1, and 2 โ
โ 5. This causes the `IndexError: list index out of range` exception โ
โ โ
โ To fix this, you should either: โ
โ 1. Make your loop match the length of your list: `for i in range(len(items))` โ
โ 2. Or use a try/except block to handle cases where the index might be out of range โ
โ 3. Or simply iterate over the items directly: `for item in items:` โ
โ โ
โ The error occurs because you're trying to access more elements than exist in your list. โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
Other usage options:
- Display execution trace
snoop run file.py --show-trace
- View command options:
snoop run --help
snoop --help
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file snooper_ai-0.0.3.tar.gz.
File metadata
- Download URL: snooper_ai-0.0.3.tar.gz
- Upload date:
- Size: 43.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa949541ce098219aa8296cbc504a41830d00d3f42eb38812c417ad745eaf62a
|
|
| MD5 |
d893a319716dba79100f8853a5e95251
|
|
| BLAKE2b-256 |
584ad4ccc1b163c57abceda55240b40fa203b63ee11f7307ad0a1dca20afc3b6
|
File details
Details for the file snooper_ai-0.0.3-py3-none-any.whl.
File metadata
- Download URL: snooper_ai-0.0.3-py3-none-any.whl
- Upload date:
- Size: 22.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
17d55f328e92dc9647ad6c756fdff9575d61dd15ca66e3c2263fd5cb5f2828c6
|
|
| MD5 |
56c9ec5736c8854e47b1728ff988131e
|
|
| BLAKE2b-256 |
a88dd73306a24a023fff848a0810bd20d1a3630a7ccea365b5e80b8f8190e9f5
|