microsoft/autogen · error · ValueError

Could not create spec

Error message

Could not create spec

What it means

FunctionWithRequirementsStr compiles a Python source string into a module via spec_from_loader + a _StringLoader. spec_from_loader returning None means the import machinery refused to create a module spec for the given loader/name combination — in practice nearly unreachable with the stdlib loader used here, so this ValueError is a defensive guard. If you see it, the string-loading path itself is broken rather than your function code.

Source

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

@dataclass
class FunctionWithRequirementsStr:
    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")

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Confirm you can reproduce with a trivial func string like "def f(): pass" — if yes, the environment's importlib is patched/broken.
  2. Remove or adjust importlib patches, import hooks, or sandbox restrictions in the process.
  3. As a workaround in unpatchable environments, pass a real callable using FunctionWithRequirements.from_callable instead of a source string.

Example fix

// not applicable — environment-level failure, no source-string change helps
Defensive patterns

Strategy: fallback

Validate before calling

from importlib.util import spec_from_loader

def can_create_spec(source: str) -> bool:
    loader = _StringLoader(source)
    return spec_from_loader("probe_module", loader) is not None

Try / catch

try:
    f = FunctionWithRequirementsStr(src, python_packages=pkgs)
except ValueError:
    # import machinery unavailable in this process; fall back to a real callable
    f = FunctionWithRequirements.from_callable(my_func, python_packages=pkgs)

Prevention

When it happens

Trigger: Constructing FunctionWithRequirementsStr(func_string) in an environment where importlib bootstrap is patched, restricted (e.g. sandboxed interpreters, some serverless runtimes), or where a sitecustomize interferes with spec creation.

Common situations: Exotic embedded interpreters; heavily patched test environments; almost never in normal CPython usage — most constructor failures are the sibling errors 591/592 instead.

Related errors


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