langchain-ai/deepagents · error · ModelConfigError

Provider package ' ' is installed but failed to import for…

Error message

Provider package '{package}' is installed but failed to import for provider '{provider}': {e}

What it means

Raised as `ModelConfigError` when the provider's LangChain package IS installed (confirmed via `find_spec`) but an ImportError still escaped from `init_chat_model` — meaning an internal import inside the package failed (broken or incompatible dependency). The library deliberately surfaces the real error instead of the misleading 'missing package' hint.

Solutions

  1. Read the chained ImportError to identify the failing internal import
  2. Reinstall the package: `pip install --force-reinstall langchain-<provider>`
  3. Align versions (e.g. `pip install -U langchain-core langchain-<provider>`) so the integration matches the installed core

Example fix

// before
ModelConfigError: Provider package 'langchain-anthropic' is installed but failed to import...: No module named 'anthropic._client'

// after
$ pip install --force-reinstall --upgrade langchain-anthropic anthropic
$ dcode -m anthropic:claude-opus-5
Defensive patterns

Strategy: try-catch

Validate before calling

import importlib

def package_imports_cleanly(package: str) -> bool:
    module_name = package.replace("-", "_")
    try:
        importlib.import_module(module_name)
        return True
    except ImportError:
        return False

Try / catch

from deepagents_code.model_config import ModelConfigError
try:
    model = create_model(spec)
except ModelConfigError as e:
    if "installed but failed to import" in str(e):
        logger.error("Broken provider install: %s", e.__cause__)
        raise SystemExit("Run: pip install --force-reinstall -U " + pkg)
    raise

Prevention

When it happens

Trigger: `init_chat_model` raises ImportError for a provider, but `importlib.util.find_spec(package.replace('-', '_'))` is not None, so the code takes the 'installed but failed to import' branch in `_create_model_via_init` (config.py:5460-5478, raise at 5500).

Common situations: Partially installed or corrupted package; version conflict between `langchain-<provider>` and `langchain-core` after a partial upgrade; a broken transitive dependency; leftover files from a failed uninstall.

Related errors


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

Appendix: source

Thrown at libs/code/deepagents_code/config.py:5500

            hint = resolve_install_hint(package)
            if hint.extra is not None:
                msg = (
                    f"Missing package for provider '{provider}'. "
                    f"Install: /install {hint.extra}"
                )
            else:
                if hint.command is not None:
                    install_hint = f"Install with: {hint.command}"
                else:
                    install_hint = f"Install the '{package}' package manually"
                msg = (
                    f"Missing package for provider '{provider}'. "
                    f"{install_hint}, then retry with `/model`."
                )
            raise MissingProviderPackageError(
                msg, provider=provider, package=package
            ) from e
        raise ModelConfigError(msg) from e
    except (ValueError, TypeError) as e:
        if not provider:
            # Both app auto-detection and `init_chat_model`'s own inference
            # failed; surface a structured error so the UI can render the
            # docs URL as a clickable link.
            raise UnknownProviderError(model_spec=model_name) from e
        spec = f"{provider}:{model_name}"
        msg = f"Invalid model configuration for '{spec}': {e}"
        raise ModelConfigError(msg) from e
    except Exception as e:  # provider SDK auth/network errors
        spec = f"{provider}:{model_name}" if provider else model_name
        msg = f"Failed to initialize model '{spec}': {e}"
        raise ModelConfigError(msg) from e


@dataclass(frozen=True)
class ModelResult:
    """Result of creating a chat model, bundling the model with its metadata.

View on GitHub (pinned to a1af029e6e)