langchain-ai/deepagents · error · ExtensionError

Failed to import {source.path}: {exc}

Error message

Failed to import {source.path}: {exc}

What it means

Raised by _import_factory when executing the extension module raises any other exception (other than KeyboardInterrupt, which is re-raised). The original exception is chained as __cause__ so the real import-time failure is visible. The message includes the failing path and the underlying error text.

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. Read the chained __cause__ traceback for the actual root-cause error
  2. Install the missing dependency the extension imports (check its requirements)
  3. Fix syntax/runtime errors in the extension file; test with `python <path>` or `python -c "import ..."`
  4. Remove import-time side effects (network, file access) and defer them into the extension factory
  5. Verify the extension runs under the same Python interpreter/environment the agent uses

Example fix

// before
import requests  # not installed
resp = requests.get("https://example.com")  # import-time network call

// after
# do imports/side effects lazily
def extension(api):
    import requests
    resp = requests.get("https://example.com")
Defensive patterns

Strategy: try-catch

Validate before calling

import subprocess, sys
result = subprocess.run([sys.executable, "-c", f"import runpy; runpy.run_path({path!r})"], capture_output=True)
assert result.returncode == 0, result.stderr.decode()

Try / catch

try:
    load_extension(source)
except ExtensionError as exc:
    logger.error("import of %s failed: %s", source.path, exc.__cause__)

Prevention

When it happens

Trigger: Any uncaught exception while running the extension module's top-level code: a missing third-party import, a syntax error surfacing at exec, failing module-level initialization (bad config, network call at import), etc.

Common situations: Extension imports a dependency that is not installed in the current environment; module-level code reads a config file that is missing; Python version incompatibility; syntax errors after an edit; network calls at import time failing.

Related errors


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