#10509·dspy

[Bug] PythonInterpreter: SyntaxError message is always empty

Author: qazbnm456Created Sep 26, 2026Updated Sep 26, 2026
Labelsbug

What happened?

When code run by PythonInterpreter has a syntax error, the resulting exception has no detail. The message is just Invalid Python syntax. message: with nothing after it: no error text, no line, no offset, no source line. An IndentationError in the same interpreter does carry its detail, so the information exists on the Pyodide side. It gets dropped before it reaches Python.

For dspy.RLM this means that when the model writes a malformed code cell, the only feedback it gets is an empty error, and it has to find the problem by rereading its own code. Our production traces contain 1,034 SyntaxError feedbacks, and every one of them had an empty message. The cause appears to be in dspy/primitives/runner.js, around line 366:

python
let errorArgs = [];
if (errorType !== "SyntaxError") {
  // Only python exceptions have args.
  const last_exception_args = pyodide.globals.get("last_exception_args");
  errorArgs = JSON.parse(last_exception_args()) || [];
}

Pyodide raises a real Python SyntaxError, and its args hold the useful detail, for example ('invalid syntax', ('<exec>', 1, 12, 'def broken(:\n', 1, 13)). error.message is also blank for this error, so the JSON-RPC error goes out as {"code": -32000, "message": "", "data": {"type": "SyntaxError", "args": []}}. On the Python side, python_interpreter.py (around line 843) formats the SyntaxError branch from error_message only. Unlike the branch below it, it has no args fallback.

A possible fix:

  1. In runner.js, remove the errorType !== "SyntaxError" check so args are read for every error type
  2. In python_interpreter.py, format the SyntaxError branch with error_data.get('args') or error_message

With both changes applied locally on 3.4.0:

def broken(:   -> Invalid Python syntax. message: ['invalid syntax', ['<exec>', 1, 12, 'def broken(:\n', 1, 13]]
x = (1,        -> Invalid Python syntax. message: ["'(' was never closed", ['<exec>', 1, 5, 'x = (1,\n', 1, 0]]
print(40+2)    -> 42

The same code is still on main.

AI disclosure: I found this with Claude Code while analysing our own RLM traces. I reproduced it myself before filing.

Steps to reproduce

python
from dspy.primitives.python_interpreter import PythonInterpreter

i = PythonInterpreter()
for code in ["def broken(:\n    pass", "print(1)\n  print(2)"]:
  try:
    i.execute(code)
  except Exception as e:
    print(type(e).__name__, repr(str(e)))

Output:

SyntaxError 'Invalid Python syntax. message: '
CodeExecutionError "IndentationError: ['unexpected indent', ['<exec>', 2, 2, '  print(2)\\n', 2, -1]]"

DSPy version

3.4.0 (also seen on 3.3.1). Deno 2.9.5, Python 3.13, Linux x86_64.