microsoft/autogen · error · ValueError

Could not create loader

Error message

Could not create loader

What it means

After spec_from_loader succeeds, FunctionWithRequirementsStr requires spec.loader to be non-None before executing the module. With the stdlib machinery a spec created from a loader always carries that loader back, so 'Could not create loader' is a defensive, effectively unreachable branch. Encountering it indicates the import system in the process has been altered, not a problem with your function string.

Source

Thrown at python/packages/autogen-core/src/autogen_core/code_executor/_func_with_reqs.py:112

    func: str
    compiled_func: Callable[..., Any]
    _func_name: str
    python_packages: Sequence[str] = field(default_factory=list)
    global_imports: Sequence[Import] = field(default_factory=list)

    def __init__(self, func: str, python_packages: Sequence[str] = [], global_imports: Sequence[Import] = []):
        self.func = func
        self.python_packages = python_packages
        self.global_imports = global_imports

        module_name = "func_module"
        loader = _StringLoader(func)
        spec = spec_from_loader(module_name, loader)
        if spec is None:
            raise ValueError("Could not create spec")
        module = module_from_spec(spec)
        if spec.loader is None:
            raise ValueError("Could not create loader")

        try:
            spec.loader.exec_module(module)
        except Exception as e:
            raise ValueError(f"Could not compile function: {e}") from e

        functions = inspect.getmembers(module, inspect.isfunction)
        if len(functions) != 1:
            raise ValueError("The string must contain exactly one function")

        self._func_name, self.compiled_func = functions[0]

    def __call__(self, *args: Any, **kwargs: Any) -> None:
        raise NotImplementedError("String based function with requirement objects are not directly callable")


@dataclass
class FunctionWithRequirements(Generic[T, P]):

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Verify with a minimal string "def f(): pass" to rule out your code.
  2. Disable importlib patches/meta-path finders introduced by test or sandboxing frameworks.
  3. Use FunctionWithRequirements.from_callable with a locally importable function instead of source strings.

Example fix

// not applicable — environment-level failure
Defensive patterns

Strategy: fallback

Try / catch

try:
    f = FunctionWithRequirementsStr(src)
except ValueError:
    f = FunctionWithRequirements.from_callable(my_func, python_packages=pkgs)  # env fallback

Prevention

When it happens

Trigger: Same as the spec failure: constructing FunctionWithRequirementsStr under patched importlib, restricted sandboxes, or nonstandard interpreters where spec.loader can be None.

Common situations: Embedded/sandboxed Python where importlib.bootstrap functions are stubbed; custom meta-path finders that return specs without loaders installed globally.

Related errors


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