{"record":{"id":"c49d442c51846819","repo":"microsoft/autogen","slug":"could-not-create-spec","errorCode":null,"errorMessage":"Could not create spec","messagePattern":"Could not create spec","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-core/src/autogen_core/code_executor/_func_with_reqs.py","lineNumber":109,"sourceCode":"\n@dataclass\nclass FunctionWithRequirementsStr:\n    func: str\n    compiled_func: Callable[..., Any]\n    _func_name: str\n    python_packages: Sequence[str] = field(default_factory=list)\n    global_imports: Sequence[Import] = field(default_factory=list)\n\n    def __init__(self, func: str, python_packages: Sequence[str] = [], global_imports: Sequence[Import] = []):\n        self.func = func\n        self.python_packages = python_packages\n        self.global_imports = global_imports\n\n        module_name = \"func_module\"\n        loader = _StringLoader(func)\n        spec = spec_from_loader(module_name, loader)\n        if spec is None:\n            raise ValueError(\"Could not create spec\")\n        module = module_from_spec(spec)\n        if spec.loader is None:\n            raise ValueError(\"Could not create loader\")\n\n        try:\n            spec.loader.exec_module(module)\n        except Exception as e:\n            raise ValueError(f\"Could not compile function: {e}\") from e\n\n        functions = inspect.getmembers(module, inspect.isfunction)\n        if len(functions) != 1:\n            raise ValueError(\"The string must contain exactly one function\")\n\n        self._func_name, self.compiled_func = functions[0]\n\n    def __call__(self, *args: Any, **kwargs: Any) -> None:\n        raise NotImplementedError(\"String based function with requirement objects are not directly callable\")\n","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-core/src/autogen_core/code_executor/_func_with_reqs.py#L91-L127","documentation":"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.","triggerScenarios":"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.","commonSituations":"Exotic embedded interpreters; heavily patched test environments; almost never in normal CPython usage — most constructor failures are the sibling errors 591/592 instead.","solutions":["Confirm you can reproduce with a trivial func string like \"def f(): pass\" — if yes, the environment's importlib is patched/broken.","Remove or adjust importlib patches, import hooks, or sandbox restrictions in the process.","As a workaround in unpatchable environments, pass a real callable using FunctionWithRequirements.from_callable instead of a source string."],"exampleFix":"// not applicable — environment-level failure, no source-string change helps","handlingStrategy":"fallback","validationCode":"from importlib.util import spec_from_loader\n\ndef can_create_spec(source: str) -> bool:\n    loader = _StringLoader(source)\n    return spec_from_loader(\"probe_module\", loader) is not None","typeGuard":null,"tryCatchPattern":"try:\n    f = FunctionWithRequirementsStr(src, python_packages=pkgs)\nexcept ValueError:\n    # import machinery unavailable in this process; fall back to a real callable\n    f = FunctionWithRequirements.from_callable(my_func, python_packages=pkgs)","preventionTips":["Run a smoke test constructing FunctionWithRequirementsStr(\"def f(): pass\") during deployment validation of sandboxed environments.","Avoid importlib patching in processes that compile function strings."],"tags":["python","autogen-core","code-executor","importlib","environment"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}