{"record":{"id":"4adeacf98232dcad","repo":"pytorch/pytorch","slug":"unable-to-get-caller-frame","errorCode":null,"errorMessage":"Unable to get caller frame","messagePattern":"Unable to get caller frame","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"functorch/dim/__init__.py","lineNumber":80,"sourceCode":"        >>> single_dim = dims(1)\n    \"\"\"\n    specified_ndims = -1\n    found_ndims = 0\n\n    # Parse arguments\n    if sizes is not None:\n        specified_ndims = len(sizes)\n    if n is not None:\n        specified_ndims = n\n\n    # Use bytecode inspection\n    frame = inspect.currentframe()\n    if frame is None:\n        raise RuntimeError(\"Unable to get current frame\")\n    frame = frame.f_back\n    try:\n        if frame is None:\n            raise RuntimeError(\"Unable to get caller frame\")\n        code = frame.f_code\n        lasti = frame.f_lasti\n\n        decoder = _PyInstDecoder(code, lasti)\n\n        if sys.version_info >= (3, 11):\n            if decoder.opcode() == \"PRECALL\":\n                decoder.next()\n\n        # Move to next instruction after the call\n        decoder.next()\n\n        # Determine number of dimensions from bytecode\n        if _relevant_op(decoder.opcode()):\n            found_ndims = 1\n        elif decoder.opcode() == \"UNPACK_SEQUENCE\":\n            found_ndims = decoder.oparg()\n            decoder.next()  # Move past UNPACK_SEQUENCE","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/pytorch/pytorch/blob/dcd2ecae775af66439b7ede4e7a82540b058c59c/functorch/dim/__init__.py#L62-L98","documentation":"After obtaining its own frame, `dims()` steps one level up (`frame.f_back`) to read the caller's bytecode and infer how many Dims to create from the assignment target. If that caller frame is None (the current frame has no parent, e.g. the call was initiated from C code or a synthetic frame), it raises RuntimeError('Unable to get caller frame').","triggerScenarios":"Calling `dims()` from a context where the Python frame chain stops at the dims() frame: invoked from C/C++ extensions, from exec/eval with a fake globals-only frame, or via tooling that calls Python functions without a real caller frame.","commonSituations":"Embedding Python in C and calling code that uses dims(), invoking dims() through an RPC/dispatch layer that drops frames, or unusual REPL/exec harnesses.","solutions":["Ensure dims() is called from ordinary Python code (a module or function body), not directly from C or synthetic frames","Wrap the call in a normal Python function so a real caller frame exists","Pass n= or sizes= explicitly so the function does not depend on caller bytecode if the frame exists but is unusual","Avoid dims() inside exec() strings; move the call into a real .py file"],"exampleFix":"// before (called with no Python caller frame)\nresult = eval_in_synthetic_frame('dims(2)')\n\n// after\ndef make_dims():\n    return dims(2)  # real Python caller frame for bytecode inspection\nresult = make_dims()","handlingStrategy":"validation","validationCode":"import inspect\n# ensure dims() is only invoked from a normal Python caller frame\nframe = inspect.currentframe()\nif frame is None or frame.f_back is None:\n    raise RuntimeError('no Python caller frame available for dims()')","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never call dims() directly from C extensions or synthetic frames; route through a Python wrapper function","Avoid dims() inside exec()/eval strings; move calls into real modules","Document that dims() requires a live Python frame chain"],"tags":["functorch","dims","frame-inspection","embedding","runtimeerror"],"backgroundTag":null,"analyzedSha":"dcd2ecae775af66439b7ede4e7a82540b058c59c","analyzedAt":"2026-08-14T19:21:26.615Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}