microsoft/autogen · error · ValueError

Could not compile and load function: {e}

Error message

Could not compile and load function: {e}

What it means

Error "Could not compile and load function: {e}" thrown in microsoft/autogen.

Source

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

        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)}")

        return cls(func, name=config.name, description=config.description, global_imports=config.global_imports)

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Fix syntax or dependency errors in the function code

Example fix

Validate the source compiles with exec() before loading

When it happens

Trigger: Thrown at python/packages/autogen-core/src/autogen_core/tools/_function_tool.py:174 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/958f677165b8c3b2. Report an issue: GitHub.