{"record":{"id":"d9743e2a33dac0bf","repo":"PrefectHQ/fastmcp","slug":"codemode-requires-pydantic-monty-for-the-monty-san","errorCode":null,"errorMessage":"CodeMode requires pydantic-monty for the Monty sandbox provider. Install it with `fastmcp[code-mode]` or pass a custom SandboxProvider.","messagePattern":"CodeMode requires pydantic-monty for the Monty sandbox provider\\. Install it with `fastmcp\\[code-mode\\]` or pass a custom SandboxProvider\\.","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"fastmcp_slim/fastmcp/experimental/transforms/code_mode.py","lineNumber":153,"sourceCode":"    ) -> None:\n        # Copy the baseline so each provider owns its dict — `limits` is a\n        # mutable public attribute, and sharing the module-level object would\n        # let one provider's edits leak into every other default provider.\n        self.limits: ResourceLimits | None = (\n            _DEFAULT_LIMITS.copy() if isinstance(limits, _UnsetType) else limits\n        )\n\n    async def run(\n        self,\n        code: str,\n        *,\n        inputs: dict[str, Any] | None = None,\n        external_functions: dict[str, Callable[..., Any]] | None = None,\n    ) -> Any:\n        try:\n            pydantic_monty = importlib.import_module(\"pydantic_monty\")\n        except ModuleNotFoundError as exc:\n            raise ImportError(\n                \"CodeMode requires pydantic-monty for the Monty sandbox provider. \"\n                \"Install it with `fastmcp[code-mode]` or pass a custom SandboxProvider.\"\n            ) from exc\n\n        inputs = inputs or {}\n        async_functions = {\n            key: _ensure_async(value)\n            for key, value in (external_functions or {}).items()\n        }\n\n        monty = pydantic_monty.Monty(code, inputs=list(inputs))\n        future = asyncio.ensure_future(\n            self._run_monty(\n                monty,\n                inputs=inputs or None,\n                external_functions=async_functions or None,\n            )\n        )","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/PrefectHQ/fastmcp/blob/1f021142978e0861cd910c8df4e8074bc7cf3978/fastmcp_slim/fastmcp/experimental/transforms/code_mode.py#L135-L171","documentation":"CodeMode transform's default sandbox provider runs generated code in the Monty sandbox from the pydantic-monty package. If that package isn't installed, run() raises this ImportError pointing to the `fastmcp[code-mode]` extra or the option of supplying a custom SandboxProvider.","triggerScenarios":"Calling CodeModeTransform.execute() (which invokes run()) without pydantic-monty installed and without a custom sandbox_provider configured.","commonSituations":"Installing fastmcp without the code-mode extra; deploying to an environment where optional extras were pruned; enabling code mode in a server whose dependencies were pinned before the extra was added.","solutions":["Install the extra: pip install 'fastmcp[code-mode]' (or add pydantic-monty directly)","Pass a custom SandboxProvider to CodeModeTransform that doesn't require pydantic-monty","Add `fastmcp[code-mode]` to your project's dependency spec so environments get it automatically"],"exampleFix":"// before\ntransform = CodeModeTransform()  # ImportError at run()\n\n// after\n# pip install 'fastmcp[code-mode]'\ntransform = CodeModeTransform()  # works with default Monty sandbox","handlingStrategy":"fallback","validationCode":"import importlib.util\nif importlib.util.find_spec(\"pydantic_monty\") is None and sandbox_provider is None:\n    raise RuntimeError(\"Install 'fastmcp[code-mode]' or provide a custom SandboxProvider\")","typeGuard":"def code_mode_ready(transform) -> bool:\n    import importlib.util\n    return importlib.util.find_spec(\"pydantic_monty\") is not None or type(transform.sandbox_provider).__name__ != \"MontySandboxProvider\"","tryCatchPattern":"try:\n    result = await transform.execute(code)\nexcept ImportError as e:\n    if \"pydantic-monty\" in str(e):\n        raise RuntimeError(\"Install the code-mode extra: fastmcp[code-mode]\") from e\n    raise","preventionTips":["Include 'fastmcp[code-mode]' in your project dependencies wherever CodeMode is used","Or inject a custom SandboxProvider that doesn't need pydantic-monty","Add a startup check that pydantic_monty imports before serving"],"tags":["python","import-error","optional-dependency","code-mode","fastmcp"],"backgroundTag":"missing-optional-dependency","analyzedSha":"1f021142978e0861cd910c8df4e8074bc7cf3978","analyzedAt":"2026-08-29T14:31:16.082Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}