langchain-ai/deepagents · error · ImportError

Could not create module spec for {profiles_path}

Error message

Could not create module spec for {profiles_path}

What it means

Once profiles_path exists, the loader builds a module spec via importlib.util.spec_from_file_location and requires both the spec and its loader. If either is None, it raises ImportError('Could not create module spec for {profiles_path}') because the file cannot be executed as a Python module. This is rare and usually indicates an unreadable or non-Python file at that path.

Source

Thrown at libs/code/deepagents_code/model_config.py:1416

            package_dir = Path(next(iter(spec.submodule_search_locations)))
        else:
            msg = f"Cannot determine location for {package_root}"
            raise ImportError(msg)

        # Build the path to the target file (e.g., data/_profiles.py).
        relative_parts = parts[1:]  # ["data", "_profiles"]
        profiles_path = package_dir.joinpath(
            *relative_parts[:-1], f"{relative_parts[-1]}.py"
        )

        if not profiles_path.exists():
            msg = f"Profile module not found: {profiles_path}"
            raise ImportError(msg)

        file_spec = importlib.util.spec_from_file_location(module_path, profiles_path)
        if file_spec is None or file_spec.loader is None:
            msg = f"Could not create module spec for {profiles_path}"
            raise ImportError(msg)

        module = importlib.util.module_from_spec(file_spec)
        file_spec.loader.exec_module(module)
        profiles = getattr(module, "_PROFILES", {})
        _provider_profiles_cache[module_path] = profiles
        return profiles


def _profile_module_from_class_path(class_path: str) -> str | None:
    """Derive the profile module path from a `class_path` config value.

    Args:
        class_path: Fully-qualified class in `module.path:ClassName` format.

    Returns:
        Dotted module path like `langchain_baseten.data._profiles`, or None
            if `class_path` is malformed.
    """

View on GitHub (pinned to a1af029e6e)

Solutions

  1. Check what is actually at the printed path: `ls -la <profiles_path>` and `file <profiles_path>`; replace/repair if it is not a valid .py file.
  2. Force-reinstall the provider package to restore the real profiles module.
  3. Report a bug with the path and package versions if the file is a valid Python module yet the spec cannot be created.
Defensive patterns

Strategy: try-catch

Validate before calling

import pathlib
p = pathlib.Path(profiles_path)
if not p.is_file() or p.suffix != ".py":
    raise SystemExit(f"Expected a .py module at {p}; reinstall the provider package")

Try / catch

try:
    profiles = get_model_profiles(module_path)
except ImportError:
    profiles = {}

Prevention

When it happens

Trigger: Calling _discover_available_models() or get_model_profiles() when spec_from_file_location returns None/empty loader for the existing profiles_path — e.g. the path exists but is not a valid loadable Python file (wrong extension handling, permission/IO edge cases, or a spec created for a name/location importlib refuses).

Common situations: A directory or data file exists at the expected profiles path (exists() is True but it is not a .py module); corrupted install; filesystem oddities (broken symlink to a nonexistent target may pass exists-like checks in some setups).

Related errors


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