The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
perf[core]: remove unnecessary model validators (#31238)
Sydney Runkle7263011b24b0
diff --git a/libs/core/langchain_core/documents/base.py b/libs/core/langchain_core/documents/base.pyindex fba997f495..0323bb1e88 100644--- a/libs/core/langchain_core/documents/base.py+++ b/libs/core/langchain_core/documents/base.py@@ -8,7 +8,7 @@ from io import BufferedReader, BytesIOfrom pathlib import Path, PurePathfrom typing import TYPE_CHECKING, Any, Literal, Optional, Union, cast-from pydantic import ConfigDict, Field, field_validator, model_validator+from pydantic import ConfigDict, Field, model_validatorfrom langchain_core.load.serializable import Serializable@@ -33,7 +33,7 @@ class BaseMedia(Serializable):# The ID field is optional at the moment.# It will likely become required in a future major release after# it has been adopted by enough vectorstore implementations.- id: Optional[str] = None+ id: Optional[str] = Field(default=None, coerce_numbers_to_str=True)"""An optional identifier for the document.Ideally this should be unique across the document collection and formatted@@ -45,17 +45,6 @@ class BaseMedia(Serializable):metadata: dict = Field(default_factory=dict)"""Arbitrary metadata associated with the content."""- @field_validator("id", mode="before")- def cast_id_to_str(cls, id_value: Any) -> Optional[str]:- """Coerce the id field to a string.-- Args:- id_value: The id value to coerce.- """- if id_value is not None:- return str(id_value)- return id_value-class Blob(BaseMedia):"""Blob represents raw data by either reference or value.diff --git a/libs/core/langchain_core/messages/ai.py b/libs/core/langchain_core/messages/ai.pyindex 6b06786418..34e5c21727 100644--- a/libs/core/langchain_core/messages/ai.py+++ b/libs/core/langchain_core/messages/ai.py@@ -194,6 +194,7 @@ class AIMessage(BaseMessage):"invalid_tool_calls": self.invalid_tool_calls,}+ # TODO: remove this logic if possible, reducing breaking nature of changes@model_validator(mode="before")@classmethoddef _backwards_compat_tool_calls(cls, values: dict) -> Any:diff --git a/libs/core/langchain_core/messages/base.py b/libs/core/langchain_core/messages/base.pyindex 58049d28e9..1ce0122166 100644--- a/libs/core/langchain_core/messages/base.py+++ b/libs/core/langchain_core/messages/base.py@@ -4,7 +4,7 @@ from __future__ import annotationsfrom typing import TYPE_CHECKING, Any, Optional, Union, cast-from pydantic import ConfigDict, Field, field_validator+from pydantic import ConfigDict, Fieldfrom langchain_core.load.serializable import Serializablefrom langchain_core.utils import get_bolded_text@@ -52,7 +52,7 @@ class BaseMessage(Serializable):model implementation."""- id: Optional[str] = None+ id: Optional[str] = Field(default=None, coerce_numbers_to_str=True)"""An optional unique identifier for the message. This should ideally beprovided by the provider/model which created the message."""@@ -60,13 +60,6 @@ class BaseMessage(Serializable):extra="allow",)- @field_validator("id", mode="before")- def cast_id_to_str(cls, id_value: Any) -> Optional[str]:- """Coerce the id field to a string."""- if id_value is not None:- return str(id_value)- return id_value-def __init__(self, content: Union[str, list[Union[str, dict]]], **kwargs: Any) -> None:diff --git a/libs/core/langchain_core/outputs/chat_generation.py b/libs/core/langchain_core/outputs/chat_generation.pyindex 25ea5684e6..ed4f106634 100644--- a/libs/core/langchain_core/outputs/chat_generation.py+++ b/libs/core/langchain_core/outputs/chat_generation.py@@ -4,8 +4,7 @@ from __future__ import annotationsfrom typing import Literal, Union-from pydantic import model_validator-from typing_extensions import Self+from pydantic import computed_fieldfrom langchain_core.messages import BaseMessage, BaseMessageChunkfrom langchain_core.outputs.generation import Generation@@ -26,48 +25,30 @@ class ChatGeneration(Generation):via callbacks)."""- text: str = ""- """*SHOULD NOT BE SET DIRECTLY* The text contents of the output message."""message: BaseMessage"""The message output by the chat model."""- # Override type to be ChatGeneration, ignore mypy error as this is intentional+type: Literal["ChatGeneration"] = "ChatGeneration" # type: ignore[assignment]"""Type is used exclusively for serialization purposes."""- @model_validator(mode="after")- def set_text(self) -> Self:- """Set the text attribute to be the contents of the message.-- Args:- values: The values of the object.-- Returns:- The values of the object with the text attribute set.-- Raises:- ValueError: If the message is not a string or a list.- """- try:- text = ""- if isinstance(self.message.content, str):- text = self.message.content- # Assumes text in content blocks in OpenAI format.- # Uses first text block.- elif isinstance(self.message.content, list):- for block in self.message.content:- if isinstance(block, str):- text = block- break- if isinstance(block, dict) and "text" in block:- text = block["text"]- break- else:- pass- self.text = text- except (KeyError, AttributeError) as e:- msg = "Error while initializing ChatGeneration"- raise ValueError(msg) from e- return self+ @computed_field # type: ignore[prop-decorator]+ @property+ def text(self) -> str:+ """Set the text attribute to be the contents of the message."""+ text_ = ""+ if isinstance(self.message.content, str):+ text_ = self.message.content+ # Assumes text in content blocks in OpenAI format.+ # Uses first text block.+ elif isinstance(self.message.content, list):+ for block in self.message.content:+ if isinstance(block, str):+ text_ = block+ break+ if isinstance(block, dict) and "text" in block:+ text_ = block["text"]+ break+ return text_class ChatGenerationChunk(ChatGeneration):@@ -78,7 +59,7 @@ class ChatGenerationChunk(ChatGeneration):message: BaseMessageChunk"""The message chunk output by the chat model."""- # Override type to be ChatGeneration, ignore mypy error as this is intentional+type: Literal["ChatGenerationChunk"] = "ChatGenerationChunk" # type: ignore[assignment]"""Type is used exclusively for serialization purposes."""diff --git a/libs/core/langchain_core/outputs/generation.py b/libs/core/langchain_core/outputs/generation.pyindex 8f3bbe5a77..75ac5b81db 100644--- a/libs/core/langchain_core/outputs/generation.py+++ b/libs/core/langchain_core/outputs/generation.py@@ -4,6 +4,8 @@ from __future__ import annotationsfrom typing import Any, Literal, Optional+from pydantic import computed_field+from langchain_core.load import Serializablefrom langchain_core.utils._merge import merge_dicts@@ -24,14 +26,30 @@ class Generation(Serializable):for more information."""- text: str- """Generated text output."""+ def __init__(+ self,+ text: str = "",+ generation_info: Optional[dict[str, Any]] = None,+ **kwargs: Any,+ ):+ """Initialize a Generation."""+ super().__init__(generation_info=generation_info, **kwargs)+ self._text = text++ # workaround for ChatGeneration so that we can use a computed field to populate+ # the text field from the message content (parent class needs to have a property)+ @computed_field # type: ignore[prop-decorator]+ @property+ def text(self) -> str:+ """The text contents of the output."""+ return self._textgeneration_info: Optional[dict[str, Any]] = None"""Raw response from the provider.May include things like the reason for finishing or token log probabilities."""+type: Literal["Generation"] = "Generation""""Type is used exclusively for serialization purposes.Set to "Generation" for this class."""@@ -53,6 +71,16 @@ class Generation(Serializable):class GenerationChunk(Generation):"""Generation chunk, which can be concatenated with other Generation chunks."""+ def __init__(+ self,+ text: str = "",+ generation_info: Optional[dict[str, Any]] = None,+ **kwargs: Any,+ ):+ """Initialize a GenerationChunk."""+ super().__init__(text=text, generation_info=generation_info, **kwargs)+ self._text = text+def __add__(self, other: GenerationChunk) -> GenerationChunk:"""Concatenate two GenerationChunks."""if isinstance(other, GenerationChunk):