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

  1. Remove sys.exit()/exit() calls from the extension module's import-time code
  2. Move CLI/argparse logic under `if __name__ == "__main__":`
  3. Raise an exception or log a warning instead of exiting during import
  4. 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

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


AI-assisted analysis of langchain-ai/deepagents@a1af029e6e (2026-08-29). Data as JSON: /api/errors/34e05681911a45f4. Report an issue: GitHub.