jax-ml/jax · error · XlaRuntimeError

code argument must be a code object

Error message

code argument must be a code object

What it means

The traceback.code_addr2line helper wraps PyCode_Addr2Line and validates that its first argument is a Python code object (types.CodeType). Passing anything else raises XlaRuntimeError.

Source

Thrown at jaxlib/traceback.cc:488

          Py_DECREF(py_code);
          traceback = traceback_type(
              /*tb_next=*/std::move(traceback),
              /*tb_frame=*/
              nb::steal<nb::object>(reinterpret_cast<PyObject*>(py_frame)),
              /*tb_lasti=*/0,
              /*tb_lineno=*/
              frame.line_num);
        }
        return traceback;
      },
      "Creates a traceback from a list of frames.",
      nb::sig("def traceback_from_frames(frames: list[Frame])"
              " -> traceback.TracebackType"));

  type.attr("code_addr2line") = nb::cpp_function(
      [](nb::handle code, int lasti) {
        if (!PyCode_Check(code.ptr())) {
          throw xla::XlaRuntimeError("code argument must be a code object");
        }
        return PyCode_Addr2Line(reinterpret_cast<PyCodeObject*>(code.ptr()),
                                lasti);
      },
      "Python wrapper around the Python C API function PyCode_Addr2Line",
      nb::sig("def code_addr2line(code: types.CodeType, lasti: int) -> int"));

  type.attr("code_addr2location") = nb::cpp_function(
      [](nb::handle code, int lasti) {
        if (!PyCode_Check(code.ptr())) {
          throw xla::XlaRuntimeError("code argument must be a code object");
        }
        int start_line, start_column, end_line, end_column;
        if (!PyCode_Addr2Location(reinterpret_cast<PyCodeObject*>(code.ptr()),
                                  lasti, &start_line, &start_column, &end_line,
                                  &end_column)) {
          throw nb::python_error();
        }

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Pass the code object: use frame.f_code or func.__code__
  2. Type-check with types.CodeType before calling

Example fix

# before
line = traceback.code_addr2line(my_func, lasti)

# after
line = traceback.code_addr2line(my_func.__code__, lasti)
Defensive patterns

Strategy: type-guard

Validate before calling

import types
code = func if isinstance(func, types.CodeType) else getattr(func, '__code__', None)
if code is None:
    raise TypeError('expected a code object')

Type guard

import types
def is_code_object(o) -> bool:
    return isinstance(o, types.CodeType)

Prevention

When it happens

Trigger: Calling jaxlib.traceback.code_addr2line(obj, lasti) where obj is not a code object — e.g. a function (pass obj.__code__), a string, or None.

Common situations: Rebuilding tracebacks from raw frames and passing the function or frame instead of its __code__; introspection tooling that assumes any callable is a code object.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/0ee84976acf0110a. Report an issue: GitHub.