Skip to main content

debuggpt

The realest AI debugger in the game.

Example code:

/test/test_debug.py:

from debuggpt import gpt_debug
import pickle

@gpt_debug(show_prompt_only=True)
def func():
    with open('test.pkl', 'rb') as f:
        df = pickle.load(f)
    df['c'] = df.a + df.b

func()

Here's what happens when the above code runs:

test.pkl stores a pandas DataFrame: a b 0 1 4 1 2 foo 2 3 6

Since column b contains a string, 'foo', we get an error when adding it to a:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

This is tricky to debug because:

  1. Our code NEVER indicates that df is a Pandas DataFrame. In fact, we don't even import pandas!
  2. Our code NEVER mentions the data types, or contents, of df.
  3. We can't know which column ('a' or 'b') has mixed types, not even by looking at the error or traceback.

But GPT-4 knew all of this. Including #3.

Here's its response:

The error "TypeError: unsupported operand type(s) for +: 'int' and 'str'" is occurring because you are trying to add an integer and a string together in line 9 of test/test_debug.py:

df['c'] = df.a + df.b

The DataFrame df has mixed types in column 'b' - it contains both integers and the string 'foo'. You cannot add an integer and a string together directly, which is causing the error.

To fix this issue, you need to decide how you want to handle the mixed types in column 'b'. One option is to convert the non-numeric values to a default value, like 0, before performing the addition. You can do this using the pd.to_numeric function with the errors='coerce' argument, which will replace non-numeric values with NaN, and then use the fillna method to replace NaN with 0.

Here's the updated code for func:

def func():
    with open('test.pkl', 'rb') as f:
        df = pickle.load(f)

    df['b'] = pd.to_numeric(df['b'], errors='coerce').fillna(0)
    df['c'] = df.a + df.b

This code will first convert any non-numeric values in column 'b' to NaN, then replace NaN with 0, and finally perform the addition. If you have a different way you'd like to handle the mixed types in column 'b', you can modify the code accordingly.

This is an excellent response.

GPT-4 could see everything.

  • It knew the type and exact contents of df, the value returned by pickle.load.
  • It could see all relevant code, not just the traceback (more on this later)
  • It could see everything that was printed during life of our program (including the original error traceback).

Here's what the original traceback looked like:

Traceback (most recent call last):
  File "/Users/Ryan.Young3/.pyenv/versions/debuggpt/lib/python3.11/site-packages/pandas/core/ops/array_ops.py", line 171, in _na_arithmetic_op
    result = func(left, right)
             ^^^^^^^^^^^^^^^^^
  File "/Users/Ryan.Young3/.pyenv/versions/debuggpt/lib/python3.11/site-packages/pandas/core/computation/expressions.py", line 239, in evaluate
    return _evaluate(op, op_str, a, b)  # type: ignore[misc]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Ryan.Young3/.pyenv/versions/debuggpt/lib/python3.11/site-packages/pandas/core/computation/expressions.py", line 70, in _evaluate_standard
    return op(a, b)
           ^^^^^^^^
TypeError: unsupported operand type(s) for +: 'int' and 'str'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/Ryan.Young3/main/code/debuggpt/test/test_debug.py", line 10, in <module>
    func()
  File "/Users/Ryan.Young3/main/code/debuggpt/test/test_debug.py", line 8, in func
    df['c'] = df.a + df.b
              ~~~~~^~~~~~
  File "/Users/Ryan.Young3/.pyenv/versions/debuggpt/lib/python3.11/site-packages/pandas/core/ops/common.py", line 81, in new_method
    return method(self, other)
           ^^^^^^^^^^^^^^^^^^^
  File "/Users/Ryan.Young3/.pyenv/versions/debuggpt/lib/python3.11/site-packages/pandas/core/arraylike.py", line 186, in __add__
    return self._arith_method(other, operator.add)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Ryan.Young3/.pyenv/versions/debuggpt/lib/python3.11/site-packages/pandas/core/series.py", line 6108, in _arith_method
    return base.IndexOpsMixin._arith_method(self, other, op)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Ryan.Young3/.pyenv/versions/debuggpt/lib/python3.11/site-packages/pandas/core/base.py", line 1348, in _arith_method
    result = ops.arithmetic_op(lvalues, rvalues, op)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Ryan.Young3/.pyenv/versions/debuggpt/lib/python3.11/site-packages/pandas/core/ops/array_ops.py", line 232, in arithmetic_op
    res_values = _na_arithmetic_op(left, right, op)  # type: ignore[arg-type]
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Ryan.Young3/.pyenv/versions/debuggpt/lib/python3.11/site-packages/pandas/core/ops/array_ops.py", line 178, in _na_arithmetic_op
    result = _masked_arith_op(left, right, op)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Ryan.Young3/.pyenv/versions/debuggpt/lib/python3.11/site-packages/pandas/core/ops/array_ops.py", line 116, in _masked_arith_op
    result[mask] = op(xrav[mask], yrav[mask])
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: unsupported operand type(s) for +: 'int' and 'str'

While we do pass this traceback to the model, the magic comes from what we provide next: the code report.

Here is the exact code report we gave the model (in addition, we also provided a prompt, and the printed stdout history)


You are debugging a call to `func`.

CONTEXT SURROUNDING THE LAST CALL TO `func` BEFORE THE ERROR WAS RAISED:
File "/test/test_debug.py", line 10
```python
1| from debuggpt import gpt_debug
2| import pickle
3| 
4| @gpt_debug
5| def func():
6|     with open('test.pkl', 'rb') as f:
7|         df = pickle.load(f)
8|     df['c'] = df.a + df.b  # <-- LINE 8, THE ORIGINAL LINE THAT CAUSED THE ERROR
9| 
10| func()  # <-- LINE 10 CALLED
```

LOCAL NAMES, TYPES, AND VALUES, IN THE ABOVE CONTEXT (module, '/test/test_debug.py') WHEN THE ERROR OCCURRED:
gpt_debug: function
pickle: module
func: function


SOURCE CODE FOR `func`, THE FUNCTION YOU'RE DEBUGGING:
File "/test/test_debug.py", line 8
```python
4| @gpt_debug
5| def func():
6|     with open('test.pkl', 'rb') as f:
7|         df = pickle.load(f)
8|     df['c'] = df.a + df.b  # <-- LINE 8, THE ORIGINAL LINE THAT CAUSED THE ERROR
```

LOCAL NAMES, TYPES, AND VALUES, IN THE ABOVE CONTEXT (function, `func`) WHEN THE ERROR OCCURRED:
f: BufferedReader =
<_io.BufferedReader name='test.pkl'>

df: DataFrame =
   a     b
0  1     4
1  2  blah
2  3     6

The report stops here because the remaining calls in the traceback are from pandas code. But normally, if there are more calls from our code, we would provide an extended traceback, showing the context for each call in the stack.

Metadata

Release files for debuggpt 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for debuggpt 0.1.1
File Size Uploaded
debuggpt-0.1.1.tar.gz 9.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for debuggpt 0.1.1
File Interpreter ABI Platform
debuggpt-0.1.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 19.1 kB

Release files / debuggpt-0.1.1.tar.gz

Download URL debuggpt-0.1.1.tar.gz
Size 9.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ba39a34ac01646210fc9c670996b017d4f3d2f1ab75a927d28c90490e224ec7a
BLAKE2b-256 checksum
How to use checksums
f770929810ef9b27627d83beb6dafe9f6d5a23506f0a1840c3a62b6cb20a245c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.2

Release files / debuggpt-0.1.1-py2.py3-none-any.whl

Download URL debuggpt-0.1.1-py2.py3-none-any.whl
Size 9.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
8b5813900033433af72414edf029991c09634c5f8c1945612ec0b6fa96d6a19c
BLAKE2b-256 checksum
How to use checksums
9214c0d438cebbc3d5f56a6e8666d0d5327d58cdf916c596d62f5a2d3c0d2736
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.2

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page