chore(core): raise more descriptive model error in init_chat_model (#35167)

This commit is contained in:
Mason Daugherty
2026-02-11 11:09:06 -05:00
committed by GitHub
parent 7f3c10865a
commit 65c224a3da
2 changed files with 14 additions and 0 deletions

View File

@@ -449,6 +449,13 @@ def init_chat_model(
```
""" # noqa: E501
if model is not None and not isinstance(model, str):
msg = ( # type: ignore[unreachable]
f"`model` must be a string (e.g., 'openai:gpt-4o'), got "
f"{type(model).__name__}. If you've already constructed a chat model "
f"object, use it directly instead of passing it to init_chat_model()."
)
raise TypeError(msg)
if not model and not configurable_fields:
configurable_fields = ("model", "model_provider")
config_prefix = config_prefix or ""

View File

@@ -3,6 +3,7 @@ from typing import TYPE_CHECKING
from unittest import mock
import pytest
from langchain_core.language_models.fake_chat_models import FakeChatModel
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnableConfig, RunnableSequence
from pydantic import SecretStr
@@ -52,6 +53,12 @@ def test_init_chat_model(model_name: str, model_provider: str | None) -> None:
assert llm1.dict() == llm2.dict()
def test_init_chat_model_rejects_model_object() -> None:
"""Passing a model object instead of a string should raise TypeError."""
with pytest.raises(TypeError, match="must be a string"):
init_chat_model(model=FakeChatModel()) # type: ignore[call-overload]
def test_init_missing_dep() -> None:
with pytest.raises(ImportError):
init_chat_model("mixtral-8x7b-32768", model_provider="groq")