core,integrations[minor]: Dont error on fields in model_kwargs (#27110)

Given the current erroring behavior, every time we've moved a kwarg from
model_kwargs and made it its own field that was a breaking change.
Updating this behavior to support the old instantiations /
serializations.

Assuming build_extra_kwargs was not something that itself is being used
externally and needs to be kept backwards compatible
This commit is contained in:
Bagatur
2024-10-04 11:30:27 -07:00
committed by GitHub
parent 0495b7f441
commit 4935a14314
17 changed files with 91 additions and 68 deletions

View File

@@ -90,7 +90,7 @@ from langchain_core.utils.pydantic import (
TypeBaseModel,
is_basemodel_subclass,
)
from langchain_core.utils.utils import build_extra_kwargs, from_env, secret_from_env
from langchain_core.utils.utils import _build_model_kwargs, from_env, secret_from_env
from pydantic import BaseModel, ConfigDict, Field, SecretStr, model_validator
from typing_extensions import Self
@@ -477,10 +477,7 @@ class BaseChatOpenAI(BaseChatModel):
def build_extra(cls, values: Dict[str, Any]) -> Any:
"""Build extra kwargs from additional params that were passed in."""
all_required_field_names = get_pydantic_field_names(cls)
extra = values.get("model_kwargs", {})
values["model_kwargs"] = build_extra_kwargs(
extra, values, all_required_field_names
)
values = _build_model_kwargs(values, all_required_field_names)
return values
@model_validator(mode="after")