mirror of
https://github.com/hwchase17/langchain.git
synced 2026-02-21 06:33:41 +00:00
chore(core): raise more descriptive model error in init_chat_model (#35167)
This commit is contained in:
@@ -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 ""
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user