{"record":{"id":"3f648d352339c86d","repo":"langchain-ai/deepagents","slug":"class-path-is-not-a-basechatmodel-subclass-go","errorCode":null,"errorMessage":"'{class_path}' is not a BaseChatModel subclass (got {type(cls).__name__})","messagePattern":"'(.+?)' is not a BaseChatModel subclass \\(got (.+?)\\)","errorType":"validation","errorClass":"ModelConfigError","httpStatus":null,"severity":"error","filePath":"libs/code/deepagents_code/config.py","lineNumber":5400,"sourceCode":"    try:\n        module = importlib.import_module(module_path)\n    except ImportError as e:\n        msg = f\"Could not import module '{module_path}' for provider '{provider}': {e}\"\n        raise ModelConfigError(msg) from e\n\n    cls = getattr(module, class_name, None)\n    if cls is None:\n        msg = (\n            f\"Class '{class_name}' not found in module '{module_path}' \"\n            f\"for provider '{provider}'\"\n        )\n        raise ModelConfigError(msg)\n\n    if not (isinstance(cls, type) and issubclass(cls, _BaseChatModel)):\n        msg = (\n            f\"'{class_path}' is not a BaseChatModel subclass (got {type(cls).__name__})\"\n        )\n        raise ModelConfigError(msg)\n\n    try:\n        return cls(model=model_name, **kwargs)\n    except Exception as e:\n        msg = f\"Failed to instantiate '{class_path}' for '{provider}:{model_name}': {e}\"\n        raise ModelConfigError(msg) from e\n\n\ndef _create_model_via_init(\n    model_name: str,\n    provider: str,\n    kwargs: dict[str, Any],\n) -> BaseChatModel:\n    \"\"\"Create a model using langchain's `init_chat_model`.\n\n    Args:\n        model_name: Model identifier.\n        provider: Provider name (may be empty for auto-detection).","sourceCodeStart":5382,"sourceCodeEnd":5418,"githubUrl":"https://github.com/langchain-ai/deepagents/blob/a1af029e6e73cb17c36bff823d227747b28e91e1/libs/code/deepagents_code/config.py#L5382-L5418","documentation":"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.","triggerScenarios":"`_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).","commonSituations":"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.","solutions":["Point `class_path` at the chat-model class itself, not a factory returning it","Make the custom class inherit `langchain_core.language_models.BaseChatModel`","Adapt an existing LLM to a chat-model subclass if only completion models are available"],"exampleFix":"// before\nclass_path = \"my_pkg.models:build_model\"  # function\n\n// after\nclass_path = \"my_pkg.models:MyChatModel\"  # class MyChatModel(BaseChatModel)","handlingStrategy":"type-guard","validationCode":"import importlib\nfrom langchain_core.language_models import BaseChatModel\n\ndef is_chat_model_class(class_path: str) -> bool:\n    module_path, class_name = class_path.rsplit(\":\", 1)\n    cls = getattr(importlib.import_module(module_path), class_name, None)\n    return isinstance(cls, type) and issubclass(cls, BaseChatModel)","typeGuard":"def is_chat_model_class(obj: object) -> TypeGuard[type[BaseChatModel]]:\n    from typing import TypeGuard\n    from langchain_core.language_models import BaseChatModel\n    return isinstance(obj, type) and issubclass(obj, BaseChatModel)","tryCatchPattern":"from deepagents_code.model_config import ModelConfigError\ntry:\n    model = create_model(spec, class_path=class_path)\nexcept ModelConfigError as e:\n    if \"not a BaseChatModel subclass\" in str(e):\n        raise SystemExit(\"class_path must name the BaseChatModel class, not a factory\")\n    raise","preventionTips":["Never configure factory functions as class_path","Ensure custom classes inherit from langchain_core BaseChatModel","Run an import-and-issubclass preflight in CI for configured class paths"],"tags":["config","langchain","type-check"],"backgroundTag":"invalid-model-class","analyzedSha":"a1af029e6e73cb17c36bff823d227747b28e91e1","analyzedAt":"2026-08-29T11:43:24.718Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}