{"record":{"id":"7cd7fd4e3676fbfd","repo":"microsoft/autogen","slug":"could-not-create-loader","errorCode":null,"errorMessage":"Could not create loader","messagePattern":"Could not create loader","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-core/src/autogen_core/code_executor/_func_with_reqs.py","lineNumber":112,"sourceCode":"    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\n\n@dataclass\nclass FunctionWithRequirements(Generic[T, P]):","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-core/src/autogen_core/code_executor/_func_with_reqs.py#L94-L130","documentation":"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.","triggerScenarios":"Same as the spec failure: constructing FunctionWithRequirementsStr under patched importlib, restricted sandboxes, or nonstandard interpreters where spec.loader can be None.","commonSituations":"Embedded/sandboxed Python where importlib.bootstrap functions are stubbed; custom meta-path finders that return specs without loaders installed globally.","solutions":["Verify with a minimal string \"def f(): pass\" to rule out your code.","Disable importlib patches/meta-path finders introduced by test or sandboxing frameworks.","Use FunctionWithRequirements.from_callable with a locally importable function instead of source strings."],"exampleFix":"// not applicable — environment-level failure","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    f = FunctionWithRequirementsStr(src)\nexcept ValueError:\n    f = FunctionWithRequirements.from_callable(my_func, python_packages=pkgs)  # env fallback","preventionTips":["Treat this branch as an environment-integrity signal: check for custom meta-path finders or sandbox hooks.","Keep a callable fallback for environments where source-string compilation is unavailable."],"tags":["python","autogen-core","code-executor","importlib","environment"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}