{"record":{"id":"0ee84976acf0110a","repo":"jax-ml/jax","slug":"code-argument-must-be-a-code-object","errorCode":null,"errorMessage":"code argument must be a code object","messagePattern":"code argument must be a code object","errorType":"validation","errorClass":"XlaRuntimeError","httpStatus":null,"severity":"error","filePath":"jaxlib/traceback.cc","lineNumber":488,"sourceCode":"          Py_DECREF(py_code);\n          traceback = traceback_type(\n              /*tb_next=*/std::move(traceback),\n              /*tb_frame=*/\n              nb::steal<nb::object>(reinterpret_cast<PyObject*>(py_frame)),\n              /*tb_lasti=*/0,\n              /*tb_lineno=*/\n              frame.line_num);\n        }\n        return traceback;\n      },\n      \"Creates a traceback from a list of frames.\",\n      nb::sig(\"def traceback_from_frames(frames: list[Frame])\"\n              \" -> traceback.TracebackType\"));\n\n  type.attr(\"code_addr2line\") = nb::cpp_function(\n      [](nb::handle code, int lasti) {\n        if (!PyCode_Check(code.ptr())) {\n          throw xla::XlaRuntimeError(\"code argument must be a code object\");\n        }\n        return PyCode_Addr2Line(reinterpret_cast<PyCodeObject*>(code.ptr()),\n                                lasti);\n      },\n      \"Python wrapper around the Python C API function PyCode_Addr2Line\",\n      nb::sig(\"def code_addr2line(code: types.CodeType, lasti: int) -> int\"));\n\n  type.attr(\"code_addr2location\") = nb::cpp_function(\n      [](nb::handle code, int lasti) {\n        if (!PyCode_Check(code.ptr())) {\n          throw xla::XlaRuntimeError(\"code argument must be a code object\");\n        }\n        int start_line, start_column, end_line, end_column;\n        if (!PyCode_Addr2Location(reinterpret_cast<PyCodeObject*>(code.ptr()),\n                                  lasti, &start_line, &start_column, &end_line,\n                                  &end_column)) {\n          throw nb::python_error();\n        }","sourceCodeStart":470,"sourceCodeEnd":506,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jaxlib/traceback.cc#L470-L506","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass the code object: use frame.f_code or func.__code__","Type-check with types.CodeType before calling"],"exampleFix":"# before\nline = traceback.code_addr2line(my_func, lasti)\n\n# after\nline = traceback.code_addr2line(my_func.__code__, lasti)","handlingStrategy":"type-guard","validationCode":"import types\ncode = func if isinstance(func, types.CodeType) else getattr(func, '__code__', None)\nif code is None:\n    raise TypeError('expected a code object')","typeGuard":"import types\ndef is_code_object(o) -> bool:\n    return isinstance(o, types.CodeType)","tryCatchPattern":null,"preventionTips":["Pass frame.f_code or func.__code__, never the function/frame itself"],"tags":["jax","traceback","type-check"],"backgroundTag":"wrong-argument-type","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}