From 07b1ccb23011ac7cf1196c2834a6f4ca9a862a3b Mon Sep 17 00:00:00 2001 From: Erick Friis Date: Tue, 4 Feb 2025 18:21:28 -0800 Subject: [PATCH] x --- .../openai/langchain_openai/chat_models/base.py | 6 +++++- .../langchain_tests/unit_tests/chat_models.py | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/libs/partners/openai/langchain_openai/chat_models/base.py b/libs/partners/openai/langchain_openai/chat_models/base.py index 5ac28a717ac..6e7b3455c7f 100644 --- a/libs/partners/openai/langchain_openai/chat_models/base.py +++ b/libs/partners/openai/langchain_openai/chat_models/base.py @@ -440,7 +440,7 @@ class BaseChatOpenAI(BaseChatModel): async_client: Any = Field(default=None, exclude=True) #: :meta private: root_client: Any = Field(default=None, exclude=True) #: :meta private: root_async_client: Any = Field(default=None, exclude=True) #: :meta private: - model_name: str = Field(default="gpt-3.5-turbo", alias="model") + model: str = Field(default="gpt-3.5-turbo", alias="model_name") """Model name to use.""" temperature: Optional[float] = None """What sampling temperature to use.""" @@ -545,6 +545,10 @@ class BaseChatOpenAI(BaseChatModel): model_config = ConfigDict(populate_by_name=True) + @property + def model_name(self) -> str: + return self.model + @model_validator(mode="before") @classmethod def build_extra(cls, values: Dict[str, Any]) -> Any: diff --git a/libs/standard-tests/langchain_tests/unit_tests/chat_models.py b/libs/standard-tests/langchain_tests/unit_tests/chat_models.py index caba7f8d283..08a2e1c3b55 100644 --- a/libs/standard-tests/langchain_tests/unit_tests/chat_models.py +++ b/libs/standard-tests/langchain_tests/unit_tests/chat_models.py @@ -534,6 +534,22 @@ class ChatModelUnitTests(ChatModelTests): If this test fails, ensure that the model can be initialized with a ``model`` parameter, and that the model parameter can be accessed as ``.model``. + + If not, the easiest way to configure this is likely to add + ``from pydantic import ConfigDict`` at the top of your file, and add a + ``model_config`` class attribute to your model class: + + .. code-block:: python + + class MyChatModel(BaseChatModel): + model: str = Field(alias="model_name") + model_config = ConfigDict(populate_by_name=True) + + # optional property for backwards-compatibility + # for folks accessing chat_model.model_name + @property + def model_name(self) -> str: + return self.model """ params = { **self.standard_chat_model_params,