langchain-ai/deepagents · error · ModelConfigError

'{class_path}' is not a BaseChatModel subclass (got {type(cl

Error message

'{class_path}' is not a BaseChatModel subclass (got {type(cls).__name__})

What it means

Raised when the resolved `class_path` symbol exists but is not a class inheriting LangChain's `BaseChatModel`. Custom model classes must be BaseChatModel subclasses so the agent graph can invoke them; functions, modules, partials, or non-chat model classes are rejected, with the actual type name in the message.

Source

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

    try:
        module = importlib.import_module(module_path)
    except ImportError as e:
        msg = f"Could not import module '{module_path}' for provider '{provider}': {e}"
        raise ModelConfigError(msg) from e

    cls = getattr(module, class_name, None)
    if cls is None:
        msg = (
            f"Class '{class_name}' not found in module '{module_path}' "
            f"for provider '{provider}'"
        )
        raise ModelConfigError(msg)

    if not (isinstance(cls, type) and issubclass(cls, _BaseChatModel)):
        msg = (
            f"'{class_path}' is not a BaseChatModel subclass (got {type(cls).__name__})"
        )
        raise ModelConfigError(msg)

    try:
        return cls(model=model_name, **kwargs)
    except Exception as e:
        msg = f"Failed to instantiate '{class_path}' for '{provider}:{model_name}': {e}"
        raise ModelConfigError(msg) from e


def _create_model_via_init(
    model_name: str,
    provider: str,
    kwargs: dict[str, Any],
) -> BaseChatModel:
    """Create a model using langchain's `init_chat_model`.

    Args:
        model_name: Model identifier.
        provider: Provider name (may be empty for auto-detection).

View on GitHub (pinned to a1af029e6e)

Solutions

  1. Point `class_path` at the chat-model class itself, not a factory returning it
  2. Make the custom class inherit `langchain_core.language_models.BaseChatModel`
  3. Adapt an existing LLM to a chat-model subclass if only completion models are available

Example fix

// before
class_path = "my_pkg.models:build_model"  # function

// after
class_path = "my_pkg.models:MyChatModel"  # class MyChatModel(BaseChatModel)
Defensive patterns

Strategy: type-guard

Validate before calling

import importlib
from langchain_core.language_models import BaseChatModel

def is_chat_model_class(class_path: str) -> bool:
    module_path, class_name = class_path.rsplit(":", 1)
    cls = getattr(importlib.import_module(module_path), class_name, None)
    return isinstance(cls, type) and issubclass(cls, BaseChatModel)

Type guard

def is_chat_model_class(obj: object) -> TypeGuard[type[BaseChatModel]]:
    from typing import TypeGuard
    from langchain_core.language_models import BaseChatModel
    return isinstance(obj, type) and issubclass(obj, BaseChatModel)

Try / catch

from deepagents_code.model_config import ModelConfigError
try:
    model = create_model(spec, class_path=class_path)
except ModelConfigError as e:
    if "not a BaseChatModel subclass" in str(e):
        raise SystemExit("class_path must name the BaseChatModel class, not a factory")
    raise

Prevention

When it happens

Trigger: `_create_model_from_class` finds the attribute but `isinstance(cls, type) and issubclass(cls, _BaseChatModel)` is False — config points at a factory function, a plain class, a BaseLLM (completion) class, or a shadowed non-class object (config.py:5396-5400).

Common situations: Configuring a `build_model()` factory function instead of the class; pointing at a completion (non-chat) LLM; accidentally naming the module instead of the class.

Related errors


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