langchain-ai/deepagents · error · ExtensionError
Extension import in {source.path} attempted to exit: {exc}
Error message
Extension import in {source.path} attempted to exit: {exc} What it means
Raised by _import_factory when executing the extension module raises SystemExit — the extension called sys.exit() (or exit()) at import time. The loader converts this into an ExtensionError so a stray exit call cannot terminate the host process, and cleans the module from sys.modules.
Source
Thrown at libs/code/deepagents_code/extensions/loader.py:54
else None,
)
if spec is None or spec.loader is None:
msg = f"Could not import extension {source.path}"
raise ExtensionError(msg)
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module
try:
spec.loader.exec_module(module)
except (KeyboardInterrupt, SystemExit, Exception) as exc:
sys.modules.pop(name, None)
if isinstance(exc, KeyboardInterrupt):
raise
msg = (
f"Extension import in {source.path} attempted to exit: {exc}"
if isinstance(exc, SystemExit)
else f"Failed to import {source.path}: {exc}"
)
raise ExtensionError(msg) from exc
factory = getattr(module, "extension", None)
if not callable(factory):
sys.modules.pop(name, None)
msg = f"{source.path} does not define a callable 'extension' factory"
raise ExtensionError(msg)
if not inspect.iscoroutinefunction(factory):
sys.modules.pop(name, None)
msg = f"Extension factory in {source.path} must be declared with 'async def'"
raise ExtensionError(msg)
return name, factory
async def load_extension(
source: SourceInfo,
registry: ExtensionRegistry,
*,
cwd: Path,
mode: ExtensionMode,View on GitHub (pinned to a1af029e6e)
Solutions
- Remove sys.exit()/exit() calls from the extension module's import-time code
- Move CLI/argparse logic under `if __name__ == "__main__":`
- Raise an exception or log a warning instead of exiting during import
- Test importing the extension module directly with `python -c "import <module>"` to reproduce
Example fix
// before
if not os.environ.get("API_KEY"):
sys.exit("API_KEY missing")
// after
if not os.environ.get("API_KEY"):
raise RuntimeError("API_KEY missing") # or defer check to factory call Defensive patterns
Strategy: try-catch
Validate before calling
# smoke-test import in-process before registering
import importlib.util, sys
spec = importlib.util.spec_from_file_location("_ext_probe", path)
mod = importlib.util.module_from_spec(spec)
try:
spec.loader.exec_module(mod)
except SystemExit:
raise RuntimeError("extension calls sys.exit at import time") Try / catch
try:
load_extension(source)
except ExtensionError as exc:
logger.error("extension attempted to exit: %s", exc) Prevention
- Never call sys.exit()/exit() in import-time code
- Guard CLI code with `if __name__ == "__main__":`
- Raise exceptions (catchable by the loader) instead of exiting
When it happens
Trigger: An extension module calls sys.exit(), exit(), or raises SystemExit at top level or during import-time initialization (e.g. argparse failing inside module code).
Common situations: Extension scripts written as CLI programs that call sys.exit when checks fail; copy-pasted argparse blocks running at import; guard code exiting when an env var is missing.
Related errors
- Could not import extension {source.path}
- Failed to import {source.path}: {exc}
- Entry point {entry.name!r} does not resolve to a Python modu
- Extension factory in {source.path} attempted to exit: {exc}
- Invalid class_path '{class_path}' for provider '{provider}':
AI-assisted analysis of langchain-ai/deepagents@a1af029e6e (2026-08-29).
Data as JSON: /api/errors/34e05681911a45f4.
Report an issue: GitHub.