{"record":{"id":"d36199c567e85bb4","repo":"Lightning-AI/pytorch-lightning","slug":"unexpected-error-the-wrapped-model-should-be-a-li","errorCode":null,"errorMessage":"Unexpected error, the wrapped model should be a LightningModule, found {type(model).__name__}","messagePattern":"Unexpected error, the wrapped model should be a LightningModule, found (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/utilities/compile.py","lineNumber":79,"sourceCode":"    orig_module.predict_step = model.dynamo_ctx(orig_module.predict_step)  # type: ignore[method-assign]\n    return orig_module\n\n\ndef to_uncompiled(model: Union[\"pl.LightningModule\", \"torch._dynamo.OptimizedModule\"]) -> \"pl.LightningModule\":\n    \"\"\"Returns an instance of LightningModule without any compilation optimizations from a compiled model.\n\n    .. warning::  This is an :ref:`experimental <versioning:Experimental API>` feature.\n\n    This takes either a ``torch._dynamo.OptimizedModule`` returned by ``torch.compile()`` or a ``LightningModule``\n    returned by ``from_compiled``.\n\n    Note: this method will in-place modify the ``LightningModule`` that is passed in.\n\n    \"\"\"\n    if isinstance(model, OptimizedModule):\n        original = model._orig_mod\n        if not isinstance(original, pl.LightningModule):\n            raise TypeError(\n                f\"Unexpected error, the wrapped model should be a LightningModule, found {type(model).__name__}\"\n            )\n\n    elif isinstance(model, pl.LightningModule):\n        if model._compiler_ctx is None:\n            raise ValueError(\n                \"`model` is required to be a compiled LightningModule. Found a non-compiled LightningModule instead.\"\n            )\n        original = model\n\n    else:\n        raise ValueError(\"`model` must either be an instance of OptimizedModule or LightningModule\")\n\n    ctx = original._compiler_ctx\n    if ctx is not None:\n        original.forward = ctx[\"original_forward\"]  # type: ignore[method-assign]\n        original.training_step = ctx[\"original_training_step\"]  # type: ignore[method-assign]\n        original.validation_step = ctx[\"original_validation_step\"]  # type: ignore[method-assign]","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/utilities/compile.py#L61-L97","documentation":"_module_to_compiled.to_uncompiled handles an OptimizedModule by grabbing _orig_mod; if that inner object is not a LightningModule, it raises TypeError describing an unexpected wrapper — an invariant that should not normally occur.","triggerScenarios":"An OptimizedModule whose _orig_mod is a plain nn.Module passed through to_uncompiled; usually a corrupted wrap or manual construction of OptimizedModule-like objects.","commonSituations":"Custom dynamo wrappers, monkey-patched compile, or version mismatch between torch and lightning internals.","solutions":["Only pass modules produced by torch.compile of a LightningModule","Upgrade torch/lightning to compatible versions","Recreate the compiled model from scratch rather than reusing stale wrappers"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":"def is_valid_optimized(m) -> bool:\n    from torch._dynamo import OptimizedModule\n    import lightning.pytorch as pl\n    return isinstance(m, OptimizedModule) and isinstance(m._orig_mod, pl.LightningModule)","tryCatchPattern":"try:\n    plain = _module_to_compiled.to_uncompiled(m)\nexcept TypeError:\n    # rebuild wrapper: recompile a proper LightningModule\n    plain = my_lightning_module","preventionTips":["Keep torch and lightning versions in a tested compatible set","Never hand-construct OptimizedModule-like wrappers"],"tags":["torch-compile","unwrap","invariant-violation"],"backgroundTag":"wrong-type-passed","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}