Skip to main content

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:

  1. Install
pip install snooper-ai
  1. Store your LLM api key (either anthropic or openai):
ANTHROPIC_API_KEY=xxx
OPENAI_API_KEY=xxx
  1. 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()
  1. Run the file
snoop run file.py
  1. 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.):
  1. 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.

  2. 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:

  1. Display execution trace
snoop run file.py --show-trace
  1. View command options:
snoop run --help
snoop --help

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

snooper_ai-0.0.3.tar.gz (43.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

snooper_ai-0.0.3-py3-none-any.whl (22.7 kB view details)

Uploaded Python 3

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

Hashes for snooper_ai-0.0.3.tar.gz
Algorithm Hash digest
SHA256 aa949541ce098219aa8296cbc504a41830d00d3f42eb38812c417ad745eaf62a
MD5 d893a319716dba79100f8853a5e95251
BLAKE2b-256 584ad4ccc1b163c57abceda55240b40fa203b63ee11f7307ad0a1dca20afc3b6

See more details on using hashes here.

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

Hashes for snooper_ai-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 17d55f328e92dc9647ad6c756fdff9575d61dd15ca66e3c2263fd5cb5f2828c6
MD5 56c9ec5736c8854e47b1728ff988131e
BLAKE2b-256 a88dd73306a24a023fff848a0810bd20d1a3630a7ccea365b5e80b8f8190e9f5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page