mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-28 14:05:02 +00:00
fix(langchain): warn on unsupported models and add test (#32413)
This commit is contained in:
parent
56ee00cb1d
commit
8c702397cb
@ -369,6 +369,12 @@ def _init_chat_model_helper(
|
||||
**kwargs: Any,
|
||||
) -> Union[BaseChatModel, BaseChatModelV1]:
|
||||
model, model_provider = _parse_model(model, model_provider)
|
||||
if message_version != "v0" and model_provider not in ("openai",):
|
||||
warnings.warn(
|
||||
f"Model provider {model_provider} does not support "
|
||||
f"message_version={message_version}. Defaulting to v0.",
|
||||
stacklevel=2,
|
||||
)
|
||||
if model_provider == "openai":
|
||||
_check_pkg("langchain_openai")
|
||||
if message_version == "v0":
|
||||
|
@ -6,6 +6,7 @@ import pytest
|
||||
from langchain_core.language_models import BaseChatModel
|
||||
from langchain_core.prompts import ChatPromptTemplate
|
||||
from langchain_core.runnables import RunnableConfig, RunnableSequence
|
||||
from langchain_core.v1.chat_models import BaseChatModel as BaseChatModelV1
|
||||
from pydantic import SecretStr
|
||||
|
||||
from langchain.chat_models.base import __all__, init_chat_model
|
||||
@ -51,6 +52,22 @@ def test_init_chat_model(model_name: str, model_provider: Optional[str]) -> None
|
||||
assert llm1.dict() == llm2.dict()
|
||||
|
||||
|
||||
@pytest.mark.requires("langchain_openai")
|
||||
def test_message_version() -> None:
|
||||
model = init_chat_model("openai:gpt-4.1", api_key="foo")
|
||||
assert isinstance(model, BaseChatModel)
|
||||
|
||||
model_v1 = init_chat_model("openai:gpt-4.1", api_key="foo", message_version="v1")
|
||||
assert isinstance(model_v1, BaseChatModelV1)
|
||||
|
||||
# Test we emit a warning for unsupported providers
|
||||
with (
|
||||
pytest.warns(match="Model provider bar does not support message_version=v1"),
|
||||
pytest.raises(ValueError, match="Unsupported model_provider='bar'."),
|
||||
):
|
||||
init_chat_model("foo", model_provider="bar", message_version="v1")
|
||||
|
||||
|
||||
def test_init_missing_dep() -> None:
|
||||
with pytest.raises(ImportError):
|
||||
init_chat_model("mixtral-8x7b-32768", model_provider="groq")
|
||||
|
Loading…
Reference in New Issue
Block a user