core[patch]: support Field deprecation (#25004)

![Screenshot 2024-08-02 at 4 23 17
PM](https://github.com/user-attachments/assets/c757e093-877e-4af6-9dcd-984195454158)
This commit is contained in:
Bagatur 2024-08-07 13:57:55 -07:00 committed by GitHub
parent 803eba3163
commit df99b832a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 40 additions and 5 deletions

View File

@ -133,6 +133,7 @@ def deprecated(
_package: str = package,
) -> T:
"""Implementation of the decorator returned by `deprecated`."""
from pydantic.v1.fields import FieldInfo # pydantic: ignore
def emit_warning() -> None:
"""Emit the warning."""
@ -207,6 +208,25 @@ def deprecated(
)
return cast(T, obj)
elif isinstance(obj, FieldInfo):
from langchain_core.pydantic_v1 import Field
wrapped = None
if not _obj_type:
_obj_type = "attribute"
if not _name:
raise ValueError()
old_doc = obj.description
def finalize(wrapper: Callable[..., Any], new_doc: str) -> T:
return Field(
default=obj.default,
default_factory=obj.default_factory,
description=new_doc,
alias=obj.alias,
exclude=obj.exclude,
)
elif isinstance(obj, property):
if not _obj_type:
_obj_type = "attribute"

View File

@ -208,11 +208,18 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
""" # noqa: E501
callback_manager: Optional[BaseCallbackManager] = Field(default=None, exclude=True)
"""[DEPRECATED] Callback manager to add to the run trace."""
callback_manager: Optional[BaseCallbackManager] = deprecated(
name="callback_manager", since="0.1.7", removal="0.3.0", alternative="callbacks"
)(
Field(
default=None,
exclude=True,
description="Callback manager to add to the run trace.",
)
)
rate_limiter: Optional[BaseRateLimiter] = Field(default=None, exclude=True)
"""An optional rate limiter to use for limiting the number of requests."""
"An optional rate limiter to use for limiting the number of requests."
disable_streaming: Union[bool, Literal["tool_calling"]] = False
"""Whether to disable streaming for this model.

View File

@ -330,8 +330,16 @@ class ChildTool(BaseTool):
callbacks: Callbacks = Field(default=None, exclude=True)
"""Callbacks to be called during tool execution."""
callback_manager: Optional[BaseCallbackManager] = Field(default=None, exclude=True)
"""Deprecated. Please use callbacks instead."""
callback_manager: Optional[BaseCallbackManager] = deprecated(
name="callback_manager", since="0.1.7", removal="0.3.0", alternative="callbacks"
)(
Field(
default=None,
exclude=True,
description="Callback manager to add to the run trace.",
)
)
tags: Optional[List[str]] = None
"""Optional list of tags associated with the tool. Defaults to None.
These tags will be associated with each call to this tool,