{"record":{"id":"20aa8e7247097461","repo":"microsoft/autogen","slug":"failed-to-import-import-code-str-e","errorCode":null,"errorMessage":"Failed to import {import_code}: {str(e)}","messagePattern":"Failed to import (.+?): (.+?)","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-core/src/autogen_core/tools/_function_tool.py","lineNumber":165,"sourceCode":"            \"Loading a FunctionTool from config will execute code to import the provided global imports and and function code.\\n\"\n            \"Only load configs from TRUSTED sources to prevent arbitrary code execution.\",\n            UserWarning,\n            stacklevel=2,\n        )\n\n        exec_globals: dict[str, Any] = {}\n\n        # Execute imports first\n        for import_stmt in config.global_imports:\n            import_code = import_to_str(import_stmt)\n            try:\n                exec(import_code, exec_globals)\n            except ModuleNotFoundError as e:\n                raise ModuleNotFoundError(\n                    f\"Failed to import {import_code}: Module not found. Please ensure the module is installed.\"\n                ) from e\n            except ImportError as e:\n                raise ImportError(f\"Failed to import {import_code}: {str(e)}\") from e\n            except Exception as e:\n                raise RuntimeError(f\"Unexpected error while importing {import_code}: {str(e)}\") from e\n\n        # Execute function code\n        try:\n            exec(config.source_code, exec_globals)\n            func_name = config.source_code.split(\"def \")[1].split(\"(\")[0]\n        except Exception as e:\n            raise ValueError(f\"Could not compile and load function: {e}\") from e\n\n        # Get function and verify it's callable\n        func: Callable[..., Any] = exec_globals[func_name]\n        if not callable(func):\n            raise TypeError(f\"Expected function but got {type(func)}\")\n\n        return cls(func, name=config.name, description=config.description, global_imports=config.global_imports)\n","sourceCodeStart":147,"sourceCodeEnd":182,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-core/src/autogen_core/tools/_function_tool.py#L147-L182","documentation":"Error \"Failed to import {import_code}: {str(e)}\" thrown in microsoft/autogen.","triggerScenarios":"Thrown at python/packages/autogen-core/src/autogen_core/tools/_function_tool.py:165 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Fix the import error reported in the message"],"exampleFix":"Check the module path and dependencies, then retry the import","handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}