microsoft/autogen · error · ModuleNotFoundError

Failed to import {import_code}: Module not found. Please ens

Error message

Failed to import {import_code}: Module not found. Please ensure the module is installed.

What it means

Error "Failed to import {import_code}: Module not found. Please ensure the module is installed." thrown in microsoft/autogen.

Source

Thrown at python/packages/autogen-core/src/autogen_core/tools/_function_tool.py:161

    @classmethod
    def _from_config(cls, config: FunctionToolConfig) -> Self:
        warnings.warn(
            "\n⚠️  SECURITY WARNING ⚠️\n"
            "Loading a FunctionTool from config will execute code to import the provided global imports and and function code.\n"
            "Only load configs from TRUSTED sources to prevent arbitrary code execution.",
            UserWarning,
            stacklevel=2,
        )

        exec_globals: dict[str, Any] = {}

        # Execute imports first
        for import_stmt in config.global_imports:
            import_code = import_to_str(import_stmt)
            try:
                exec(import_code, exec_globals)
            except ModuleNotFoundError as e:
                raise ModuleNotFoundError(
                    f"Failed to import {import_code}: Module not found. Please ensure the module is installed."
                ) from e
            except ImportError as e:
                raise ImportError(f"Failed to import {import_code}: {str(e)}") from e
            except Exception as e:
                raise RuntimeError(f"Unexpected error while importing {import_code}: {str(e)}") from e

        # Execute function code
        try:
            exec(config.source_code, exec_globals)
            func_name = config.source_code.split("def ")[1].split("(")[0]
        except Exception as e:
            raise ValueError(f"Could not compile and load function: {e}") from e

        # Get function and verify it's callable
        func: Callable[..., Any] = exec_globals[func_name]
        if not callable(func):
            raise TypeError(f"Expected function but got {type(func)}")

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Install the missing module into the environment

Example fix

pip install <module> before loading the function tool

When it happens

Trigger: Thrown at python/packages/autogen-core/src/autogen_core/tools/_function_tool.py:161 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/ba8e8ff82f179c94. Report an issue: GitHub.