mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-04 20:46:45 +00:00
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:
@@ -32,6 +32,7 @@ from langchain_core.utils.utils import (
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"build_extra_kwargs",
|
||||
"StrictFormatter",
|
||||
"check_package_version",
|
||||
"convert_to_secret_str",
|
||||
@@ -46,7 +47,6 @@ __all__ = [
|
||||
"raise_for_status_with_text",
|
||||
"xor_args",
|
||||
"try_load_from_hub",
|
||||
"build_extra_kwargs",
|
||||
"image",
|
||||
"get_from_env",
|
||||
"get_from_dict_or_env",
|
||||
|
@@ -210,6 +210,51 @@ def get_pydantic_field_names(pydantic_cls: Any) -> set[str]:
|
||||
return all_required_field_names
|
||||
|
||||
|
||||
def _build_model_kwargs(
|
||||
values: dict[str, Any],
|
||||
all_required_field_names: set[str],
|
||||
) -> dict[str, Any]:
|
||||
"""Build "model_kwargs" param from Pydanitc constructor values.
|
||||
|
||||
Args:
|
||||
values: All init args passed in by user.
|
||||
all_required_field_names: All required field names for the pydantic class.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: Extra kwargs.
|
||||
|
||||
Raises:
|
||||
ValueError: If a field is specified in both values and extra_kwargs.
|
||||
ValueError: If a field is specified in model_kwargs.
|
||||
"""
|
||||
extra_kwargs = values.get("model_kwargs", {})
|
||||
for field_name in list(values):
|
||||
if field_name in extra_kwargs:
|
||||
raise ValueError(f"Found {field_name} supplied twice.")
|
||||
if field_name not in all_required_field_names:
|
||||
warnings.warn(
|
||||
f"""WARNING! {field_name} is not default parameter.
|
||||
{field_name} was transferred to model_kwargs.
|
||||
Please confirm that {field_name} is what you intended.""",
|
||||
stacklevel=7,
|
||||
)
|
||||
extra_kwargs[field_name] = values.pop(field_name)
|
||||
|
||||
invalid_model_kwargs = all_required_field_names.intersection(extra_kwargs.keys())
|
||||
if invalid_model_kwargs:
|
||||
warnings.warn(
|
||||
f"Parameters {invalid_model_kwargs} should be specified explicitly. "
|
||||
f"Instead they were passed in as part of `model_kwargs` parameter.",
|
||||
stacklevel=7,
|
||||
)
|
||||
for k in invalid_model_kwargs:
|
||||
values[k] = extra_kwargs.pop(k)
|
||||
|
||||
values["model_kwargs"] = extra_kwargs
|
||||
return values
|
||||
|
||||
|
||||
# DON'T USE! Kept for backwards-compatibility but should never have been public.
|
||||
def build_extra_kwargs(
|
||||
extra_kwargs: dict[str, Any],
|
||||
values: dict[str, Any],
|
||||
|
@@ -17,8 +17,8 @@ EXPECTED_ALL = [
|
||||
"raise_for_status_with_text",
|
||||
"xor_args",
|
||||
"try_load_from_hub",
|
||||
"build_extra_kwargs",
|
||||
"image",
|
||||
"build_extra_kwargs",
|
||||
"get_from_dict_or_env",
|
||||
"get_from_env",
|
||||
"stringify_dict",
|
||||
|
Reference in New Issue
Block a user