diff --git a/libs/community/Makefile b/libs/community/Makefile index 1eff4253ca1a7..2e3430ff236ca 100644 --- a/libs/community/Makefile +++ b/libs/community/Makefile @@ -22,7 +22,7 @@ integration_tests: poetry run pytest $(TEST_FILE) test_watch: - poetry run ptw --disable-socket --allow-unix-socket --snapshot-update --now . -- -vv -x tests/unit_tests + poetry run ptw --disable-socket --allow-unix-socket --snapshot-update --now . -- -vv tests/unit_tests check_imports: $(shell find langchain_community -name '*.py') poetry run python ./scripts/check_imports.py $^ @@ -45,7 +45,6 @@ lint_tests: PYTHON_FILES=tests lint_tests: MYPY_CACHE=.mypy_cache_test lint lint_diff lint_package lint_tests: - ./scripts/check_pydantic.sh . ./scripts/lint_imports.sh . ./scripts/check_pickle.sh . [ "$(PYTHON_FILES)" = "" ] || poetry run ruff check $(PYTHON_FILES) diff --git a/libs/community/langchain_community/adapters/openai.py b/libs/community/langchain_community/adapters/openai.py index 8ec943a498239..8db939d5c9705 100644 --- a/libs/community/langchain_community/adapters/openai.py +++ b/libs/community/langchain_community/adapters/openai.py @@ -25,7 +25,7 @@ SystemMessage, ToolMessage, ) -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel from typing_extensions import Literal diff --git a/libs/community/langchain_community/agent_toolkits/ainetwork/toolkit.py b/libs/community/langchain_community/agent_toolkits/ainetwork/toolkit.py index 64d021bbc0ca8..abce2b6ed44fb 100644 --- a/libs/community/langchain_community/agent_toolkits/ainetwork/toolkit.py +++ b/libs/community/langchain_community/agent_toolkits/ainetwork/toolkit.py @@ -1,10 +1,10 @@ from __future__ import annotations -from typing import TYPE_CHECKING, List, Literal, Optional +from typing import TYPE_CHECKING, Any, List, Literal, Optional -from langchain_core.pydantic_v1 import root_validator from langchain_core.tools import BaseTool from langchain_core.tools.base import BaseToolkit +from pydantic import ConfigDict, model_validator from langchain_community.tools.ainetwork.app import AINAppOps from langchain_community.tools.ainetwork.owner import AINOwnerOps @@ -36,8 +36,9 @@ class AINetworkToolkit(BaseToolkit): network: Optional[Literal["mainnet", "testnet"]] = "testnet" interface: Optional[Ain] = None - @root_validator(pre=True) - def set_interface(cls, values: dict) -> dict: + @model_validator(mode="before") + @classmethod + def set_interface(cls, values: dict) -> Any: """Set the interface if not provided. If the interface is not provided, attempt to authenticate with the @@ -53,9 +54,10 @@ def set_interface(cls, values: dict) -> dict: values["interface"] = authenticate(network=values.get("network", "testnet")) return values - class Config: - arbitrary_types_allowed = True - validate_all = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + validate_default=True, + ) def get_tools(self) -> List[BaseTool]: """Get the tools in the toolkit.""" diff --git a/libs/community/langchain_community/agent_toolkits/amadeus/toolkit.py b/libs/community/langchain_community/agent_toolkits/amadeus/toolkit.py index 9e6c159c75d8d..87f8165322974 100644 --- a/libs/community/langchain_community/agent_toolkits/amadeus/toolkit.py +++ b/libs/community/langchain_community/agent_toolkits/amadeus/toolkit.py @@ -3,9 +3,9 @@ from typing import TYPE_CHECKING, List, Optional from langchain_core.language_models import BaseLanguageModel -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool from langchain_core.tools.base import BaseToolkit +from pydantic import ConfigDict, Field from langchain_community.tools.amadeus.closest_airport import AmadeusClosestAirport from langchain_community.tools.amadeus.flight_search import AmadeusFlightSearch @@ -26,8 +26,9 @@ class AmadeusToolkit(BaseToolkit): client: Client = Field(default_factory=authenticate) llm: Optional[BaseLanguageModel] = Field(default=None) - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def get_tools(self) -> List[BaseTool]: """Get the tools in the toolkit.""" diff --git a/libs/community/langchain_community/agent_toolkits/cassandra_database/toolkit.py b/libs/community/langchain_community/agent_toolkits/cassandra_database/toolkit.py index 11930f1c37458..2e017e994798e 100644 --- a/libs/community/langchain_community/agent_toolkits/cassandra_database/toolkit.py +++ b/libs/community/langchain_community/agent_toolkits/cassandra_database/toolkit.py @@ -2,9 +2,9 @@ from typing import List -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool from langchain_core.tools.base import BaseToolkit +from pydantic import ConfigDict, Field from langchain_community.tools.cassandra_database.tool import ( GetSchemaCassandraDatabaseTool, @@ -24,8 +24,9 @@ class CassandraDatabaseToolkit(BaseToolkit): db: CassandraDatabase = Field(exclude=True) - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def get_tools(self) -> List[BaseTool]: """Get the tools in the toolkit.""" diff --git a/libs/community/langchain_community/agent_toolkits/connery/toolkit.py b/libs/community/langchain_community/agent_toolkits/connery/toolkit.py index cf86f00f8fd81..05b15d18c269b 100644 --- a/libs/community/langchain_community/agent_toolkits/connery/toolkit.py +++ b/libs/community/langchain_community/agent_toolkits/connery/toolkit.py @@ -1,8 +1,8 @@ -from typing import List +from typing import Any, List -from langchain_core.pydantic_v1 import root_validator from langchain_core.tools import BaseTool from langchain_core.tools.base import BaseToolkit +from pydantic import model_validator from langchain_community.tools.connery import ConneryService @@ -23,8 +23,9 @@ def get_tools(self) -> List[BaseTool]: """ return self.tools - @root_validator(pre=True) - def validate_attributes(cls, values: dict) -> dict: + @model_validator(mode="before") + @classmethod + def validate_attributes(cls, values: dict) -> Any: """ Validate the attributes of the ConneryToolkit class. diff --git a/libs/community/langchain_community/agent_toolkits/file_management/toolkit.py b/libs/community/langchain_community/agent_toolkits/file_management/toolkit.py index 435817d638f3b..90b1f618ec1fb 100644 --- a/libs/community/langchain_community/agent_toolkits/file_management/toolkit.py +++ b/libs/community/langchain_community/agent_toolkits/file_management/toolkit.py @@ -1,10 +1,10 @@ from __future__ import annotations -from typing import Dict, List, Optional, Type +from typing import Any, Dict, List, Optional, Type -from langchain_core.pydantic_v1 import root_validator from langchain_core.tools import BaseTool, BaseToolkit from langchain_core.utils.pydantic import get_fields +from pydantic import model_validator from langchain_community.tools.file_management.copy import CopyFileTool from langchain_community.tools.file_management.delete import DeleteFileTool @@ -63,8 +63,9 @@ class FileManagementToolkit(BaseToolkit): selected_tools: Optional[List[str]] = None """If provided, only provide the selected tools. Defaults to all.""" - @root_validator(pre=True) - def validate_tools(cls, values: dict) -> dict: + @model_validator(mode="before") + @classmethod + def validate_tools(cls, values: dict) -> Any: selected_tools = values.get("selected_tools") or [] for tool_name in selected_tools: if tool_name not in _FILE_TOOLS_MAP: diff --git a/libs/community/langchain_community/agent_toolkits/financial_datasets/toolkit.py b/libs/community/langchain_community/agent_toolkits/financial_datasets/toolkit.py index 89708212e2577..0fd509d0017cb 100644 --- a/libs/community/langchain_community/agent_toolkits/financial_datasets/toolkit.py +++ b/libs/community/langchain_community/agent_toolkits/financial_datasets/toolkit.py @@ -2,9 +2,9 @@ from typing import List -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool from langchain_core.tools.base import BaseToolkit +from pydantic import ConfigDict, Field from langchain_community.tools.financial_datasets.balance_sheets import BalanceSheets from langchain_community.tools.financial_datasets.cash_flow_statements import ( @@ -31,8 +31,9 @@ def __init__(self, api_wrapper: FinancialDatasetsAPIWrapper): super().__init__() self.api_wrapper = api_wrapper - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def get_tools(self) -> List[BaseTool]: """Get the tools in the toolkit.""" diff --git a/libs/community/langchain_community/agent_toolkits/github/toolkit.py b/libs/community/langchain_community/agent_toolkits/github/toolkit.py index adeb0bd72519f..963c3727aa888 100644 --- a/libs/community/langchain_community/agent_toolkits/github/toolkit.py +++ b/libs/community/langchain_community/agent_toolkits/github/toolkit.py @@ -2,9 +2,9 @@ from typing import Dict, List -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool from langchain_core.tools.base import BaseToolkit +from pydantic import BaseModel, Field from langchain_community.tools.github.prompt import ( COMMENT_ON_ISSUE_PROMPT, diff --git a/libs/community/langchain_community/agent_toolkits/gmail/toolkit.py b/libs/community/langchain_community/agent_toolkits/gmail/toolkit.py index 66b3c2d252a5f..815bf89c2a84c 100644 --- a/libs/community/langchain_community/agent_toolkits/gmail/toolkit.py +++ b/libs/community/langchain_community/agent_toolkits/gmail/toolkit.py @@ -2,9 +2,9 @@ from typing import TYPE_CHECKING, List -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool from langchain_core.tools.base import BaseToolkit +from pydantic import ConfigDict, Field from langchain_community.tools.gmail.create_draft import GmailCreateDraft from langchain_community.tools.gmail.get_message import GmailGetMessage @@ -117,8 +117,9 @@ class GmailToolkit(BaseToolkit): api_resource: Resource = Field(default_factory=build_resource_service) - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def get_tools(self) -> List[BaseTool]: """Get the tools in the toolkit.""" diff --git a/libs/community/langchain_community/agent_toolkits/multion/toolkit.py b/libs/community/langchain_community/agent_toolkits/multion/toolkit.py index 133232e4561a8..5a67cb13f1121 100644 --- a/libs/community/langchain_community/agent_toolkits/multion/toolkit.py +++ b/libs/community/langchain_community/agent_toolkits/multion/toolkit.py @@ -6,6 +6,7 @@ from langchain_core.tools import BaseTool from langchain_core.tools.base import BaseToolkit +from pydantic import ConfigDict from langchain_community.tools.multion.close_session import MultionCloseSession from langchain_community.tools.multion.create_session import MultionCreateSession @@ -25,8 +26,9 @@ class MultionToolkit(BaseToolkit): See https://python.langchain.com/docs/security for more information. """ - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def get_tools(self) -> List[BaseTool]: """Get the tools in the toolkit.""" diff --git a/libs/community/langchain_community/agent_toolkits/nla/toolkit.py b/libs/community/langchain_community/agent_toolkits/nla/toolkit.py index b7216956fba3f..88fb6e87fbf6b 100644 --- a/libs/community/langchain_community/agent_toolkits/nla/toolkit.py +++ b/libs/community/langchain_community/agent_toolkits/nla/toolkit.py @@ -3,9 +3,9 @@ from typing import Any, List, Optional, Sequence from langchain_core.language_models import BaseLanguageModel -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool from langchain_core.tools.base import BaseToolkit +from pydantic import Field from langchain_community.agent_toolkits.nla.tool import NLATool from langchain_community.tools.openapi.utils.openapi_utils import OpenAPISpec diff --git a/libs/community/langchain_community/agent_toolkits/office365/toolkit.py b/libs/community/langchain_community/agent_toolkits/office365/toolkit.py index 3ee1e9f135a09..4bd826591d6ba 100644 --- a/libs/community/langchain_community/agent_toolkits/office365/toolkit.py +++ b/libs/community/langchain_community/agent_toolkits/office365/toolkit.py @@ -2,9 +2,9 @@ from typing import TYPE_CHECKING, List -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool from langchain_core.tools.base import BaseToolkit +from pydantic import ConfigDict, Field from langchain_community.tools.office365.create_draft_message import ( O365CreateDraftMessage, @@ -40,8 +40,9 @@ class O365Toolkit(BaseToolkit): account: Account = Field(default_factory=authenticate) - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def get_tools(self) -> List[BaseTool]: """Get the tools in the toolkit.""" diff --git a/libs/community/langchain_community/agent_toolkits/openapi/planner.py b/libs/community/langchain_community/agent_toolkits/openapi/planner.py index fc96e3284e77b..fbd3fc36b43aa 100644 --- a/libs/community/langchain_community/agent_toolkits/openapi/planner.py +++ b/libs/community/langchain_community/agent_toolkits/openapi/planner.py @@ -9,8 +9,8 @@ from langchain_core.callbacks import BaseCallbackManager from langchain_core.language_models import BaseLanguageModel from langchain_core.prompts import BasePromptTemplate, PromptTemplate -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool, Tool +from pydantic import Field from langchain_community.agent_toolkits.openapi.planner_prompt import ( API_CONTROLLER_PROMPT, @@ -69,7 +69,7 @@ class RequestsGetToolWithParsing(BaseRequestsTool, BaseTool): name: str = "requests_get" """Tool name.""" - description = REQUESTS_GET_TOOL_DESCRIPTION + description: str = REQUESTS_GET_TOOL_DESCRIPTION """Tool description.""" response_length: int = MAX_RESPONSE_LENGTH """Maximum length of the response to be returned.""" @@ -103,7 +103,7 @@ class RequestsPostToolWithParsing(BaseRequestsTool, BaseTool): name: str = "requests_post" """Tool name.""" - description = REQUESTS_POST_TOOL_DESCRIPTION + description: str = REQUESTS_POST_TOOL_DESCRIPTION """Tool description.""" response_length: int = MAX_RESPONSE_LENGTH """Maximum length of the response to be returned.""" @@ -134,7 +134,7 @@ class RequestsPatchToolWithParsing(BaseRequestsTool, BaseTool): name: str = "requests_patch" """Tool name.""" - description = REQUESTS_PATCH_TOOL_DESCRIPTION + description: str = REQUESTS_PATCH_TOOL_DESCRIPTION """Tool description.""" response_length: int = MAX_RESPONSE_LENGTH """Maximum length of the response to be returned.""" @@ -167,7 +167,7 @@ class RequestsPutToolWithParsing(BaseRequestsTool, BaseTool): name: str = "requests_put" """Tool name.""" - description = REQUESTS_PUT_TOOL_DESCRIPTION + description: str = REQUESTS_PUT_TOOL_DESCRIPTION """Tool description.""" response_length: int = MAX_RESPONSE_LENGTH """Maximum length of the response to be returned.""" @@ -198,7 +198,7 @@ class RequestsDeleteToolWithParsing(BaseRequestsTool, BaseTool): name: str = "requests_delete" """The name of the tool.""" - description = REQUESTS_DELETE_TOOL_DESCRIPTION + description: str = REQUESTS_DELETE_TOOL_DESCRIPTION """The description of the tool.""" response_length: Optional[int] = MAX_RESPONSE_LENGTH diff --git a/libs/community/langchain_community/agent_toolkits/playwright/toolkit.py b/libs/community/langchain_community/agent_toolkits/playwright/toolkit.py index 3cb5d430220b4..6d69383e469cb 100644 --- a/libs/community/langchain_community/agent_toolkits/playwright/toolkit.py +++ b/libs/community/langchain_community/agent_toolkits/playwright/toolkit.py @@ -2,10 +2,10 @@ from __future__ import annotations -from typing import TYPE_CHECKING, List, Optional, Type, cast +from typing import TYPE_CHECKING, Any, List, Optional, Type, cast -from langchain_core.pydantic_v1 import root_validator from langchain_core.tools import BaseTool, BaseToolkit +from pydantic import ConfigDict, model_validator from langchain_community.tools.playwright.base import ( BaseBrowserTool, @@ -68,12 +68,14 @@ class PlayWrightBrowserToolkit(BaseToolkit): sync_browser: Optional["SyncBrowser"] = None async_browser: Optional["AsyncBrowser"] = None - class Config: - arbitrary_types_allowed = True - extra = "forbid" + model_config = ConfigDict( + arbitrary_types_allowed=True, + extra="forbid", + ) - @root_validator(pre=True) - def validate_imports_and_browser_provided(cls, values: dict) -> dict: + @model_validator(mode="before") + @classmethod + def validate_imports_and_browser_provided(cls, values: dict) -> Any: """Check that the arguments are valid.""" lazy_import_playwright_browsers() if values.get("async_browser") is None and values.get("sync_browser") is None: diff --git a/libs/community/langchain_community/agent_toolkits/powerbi/toolkit.py b/libs/community/langchain_community/agent_toolkits/powerbi/toolkit.py index 2fe081bd4c0f7..424d967abbf1e 100644 --- a/libs/community/langchain_community/agent_toolkits/powerbi/toolkit.py +++ b/libs/community/langchain_community/agent_toolkits/powerbi/toolkit.py @@ -13,9 +13,9 @@ HumanMessagePromptTemplate, SystemMessagePromptTemplate, ) -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool from langchain_core.tools.base import BaseToolkit +from pydantic import ConfigDict, Field from langchain_community.tools.powerbi.prompt import ( QUESTION_TO_QUERY_BASE, @@ -63,8 +63,9 @@ class PowerBIToolkit(BaseToolkit): output_token_limit: Optional[int] = None tiktoken_model_name: Optional[str] = None - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def get_tools(self) -> List[BaseTool]: """Get the tools in the toolkit.""" diff --git a/libs/community/langchain_community/agent_toolkits/slack/toolkit.py b/libs/community/langchain_community/agent_toolkits/slack/toolkit.py index abec2a8e676a4..fd61311326d70 100644 --- a/libs/community/langchain_community/agent_toolkits/slack/toolkit.py +++ b/libs/community/langchain_community/agent_toolkits/slack/toolkit.py @@ -2,9 +2,9 @@ from typing import TYPE_CHECKING, List -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool from langchain_core.tools.base import BaseToolkit +from pydantic import ConfigDict, Field from langchain_community.tools.slack.get_channel import SlackGetChannel from langchain_community.tools.slack.get_message import SlackGetMessage @@ -91,8 +91,9 @@ class SlackToolkit(BaseToolkit): client: WebClient = Field(default_factory=login) - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def get_tools(self) -> List[BaseTool]: """Get the tools in the toolkit.""" diff --git a/libs/community/langchain_community/agent_toolkits/spark_sql/toolkit.py b/libs/community/langchain_community/agent_toolkits/spark_sql/toolkit.py index 85c08f20678bf..a339307452e08 100644 --- a/libs/community/langchain_community/agent_toolkits/spark_sql/toolkit.py +++ b/libs/community/langchain_community/agent_toolkits/spark_sql/toolkit.py @@ -3,9 +3,9 @@ from typing import List from langchain_core.language_models import BaseLanguageModel -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool from langchain_core.tools.base import BaseToolkit +from pydantic import ConfigDict, Field from langchain_community.tools.spark_sql.tool import ( InfoSparkSQLTool, @@ -27,8 +27,9 @@ class SparkSQLToolkit(BaseToolkit): db: SparkSQL = Field(exclude=True) llm: BaseLanguageModel = Field(exclude=True) - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def get_tools(self) -> List[BaseTool]: """Get the tools in the toolkit.""" diff --git a/libs/community/langchain_community/agent_toolkits/sql/toolkit.py b/libs/community/langchain_community/agent_toolkits/sql/toolkit.py index b0ea6428f7491..6920349cd3f29 100644 --- a/libs/community/langchain_community/agent_toolkits/sql/toolkit.py +++ b/libs/community/langchain_community/agent_toolkits/sql/toolkit.py @@ -3,9 +3,9 @@ from typing import List from langchain_core.language_models import BaseLanguageModel -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool from langchain_core.tools.base import BaseToolkit +from pydantic import ConfigDict, Field from langchain_community.tools.sql_database.tool import ( InfoSQLDatabaseTool, @@ -83,8 +83,9 @@ def dialect(self) -> str: """Return string representation of SQL dialect to use.""" return self.db.dialect - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def get_tools(self) -> List[BaseTool]: """Get the tools in the toolkit.""" diff --git a/libs/community/langchain_community/agents/openai_assistant/base.py b/libs/community/langchain_community/agents/openai_assistant/base.py index 07bd8566099b8..136917eaceccf 100644 --- a/libs/community/langchain_community/agents/openai_assistant/base.py +++ b/libs/community/langchain_community/agents/openai_assistant/base.py @@ -15,10 +15,11 @@ from langchain_core._api import beta from langchain_core.callbacks import CallbackManager from langchain_core.load import dumpd -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator from langchain_core.runnables import RunnableConfig, ensure_config from langchain_core.tools import BaseTool from langchain_core.utils.function_calling import convert_to_openai_tool +from pydantic import BaseModel, Field, model_validator +from typing_extensions import Self if TYPE_CHECKING: import openai @@ -209,14 +210,14 @@ def execute_agent(agent, tools, input): as_agent: bool = False """Use as a LangChain agent, compatible with the AgentExecutor.""" - @root_validator(pre=False, skip_on_failure=True) - def validate_async_client(cls, values: dict) -> dict: - if values["async_client"] is None: + @model_validator(mode="after") + def validate_async_client(self) -> Self: + if self.async_client is None: import openai - api_key = values["client"].api_key - values["async_client"] = openai.AsyncOpenAI(api_key=api_key) - return values + api_key = self.client.api_key + self.async_client = openai.AsyncOpenAI(api_key=api_key) + return self @classmethod def create_assistant( diff --git a/libs/community/langchain_community/chains/ernie_functions/base.py b/libs/community/langchain_community/chains/ernie_functions/base.py index 3db70397b9f76..51e479c58fd02 100644 --- a/libs/community/langchain_community/chains/ernie_functions/base.py +++ b/libs/community/langchain_community/chains/ernie_functions/base.py @@ -22,9 +22,9 @@ BaseOutputParser, ) from langchain_core.prompts import BasePromptTemplate -from langchain_core.pydantic_v1 import BaseModel from langchain_core.runnables import Runnable from langchain_core.utils.pydantic import is_basemodel_subclass +from pydantic import BaseModel from langchain_community.output_parsers.ernie_functions import ( JsonOutputFunctionsParser, diff --git a/libs/community/langchain_community/chains/graph_qa/arangodb.py b/libs/community/langchain_community/chains/graph_qa/arangodb.py index 9add2ffc719ab..0af3e49b62646 100644 --- a/libs/community/langchain_community/chains/graph_qa/arangodb.py +++ b/libs/community/langchain_community/chains/graph_qa/arangodb.py @@ -10,7 +10,7 @@ from langchain_core.callbacks import CallbackManagerForChainRun from langchain_core.language_models import BaseLanguageModel from langchain_core.prompts import BasePromptTemplate -from langchain_core.pydantic_v1 import Field +from pydantic import Field from langchain_community.chains.graph_qa.prompts import ( AQL_FIX_PROMPT, diff --git a/libs/community/langchain_community/chains/graph_qa/base.py b/libs/community/langchain_community/chains/graph_qa/base.py index 79ba406033bef..8f13712acd664 100644 --- a/libs/community/langchain_community/chains/graph_qa/base.py +++ b/libs/community/langchain_community/chains/graph_qa/base.py @@ -9,7 +9,7 @@ from langchain_core.callbacks.manager import CallbackManagerForChainRun from langchain_core.language_models import BaseLanguageModel from langchain_core.prompts import BasePromptTemplate -from langchain_core.pydantic_v1 import Field +from pydantic import Field from langchain_community.chains.graph_qa.prompts import ( ENTITY_EXTRACTION_PROMPT, diff --git a/libs/community/langchain_community/chains/graph_qa/cypher.py b/libs/community/langchain_community/chains/graph_qa/cypher.py index 2d6263cf8b058..a3175603786f6 100644 --- a/libs/community/langchain_community/chains/graph_qa/cypher.py +++ b/libs/community/langchain_community/chains/graph_qa/cypher.py @@ -22,8 +22,8 @@ HumanMessagePromptTemplate, MessagesPlaceholder, ) -from langchain_core.pydantic_v1 import Field from langchain_core.runnables import Runnable +from pydantic import Field from langchain_community.chains.graph_qa.cypher_utils import ( CypherQueryCorrector, diff --git a/libs/community/langchain_community/chains/graph_qa/falkordb.py b/libs/community/langchain_community/chains/graph_qa/falkordb.py index 9f8ac20e56eb3..360c4ead90903 100644 --- a/libs/community/langchain_community/chains/graph_qa/falkordb.py +++ b/libs/community/langchain_community/chains/graph_qa/falkordb.py @@ -10,7 +10,7 @@ from langchain_core.callbacks import CallbackManagerForChainRun from langchain_core.language_models import BaseLanguageModel from langchain_core.prompts import BasePromptTemplate -from langchain_core.pydantic_v1 import Field +from pydantic import Field from langchain_community.chains.graph_qa.prompts import ( CYPHER_GENERATION_PROMPT, diff --git a/libs/community/langchain_community/chains/graph_qa/gremlin.py b/libs/community/langchain_community/chains/graph_qa/gremlin.py index e208e78b353c6..d323a19462268 100644 --- a/libs/community/langchain_community/chains/graph_qa/gremlin.py +++ b/libs/community/langchain_community/chains/graph_qa/gremlin.py @@ -10,7 +10,7 @@ from langchain_core.language_models import BaseLanguageModel from langchain_core.prompts import BasePromptTemplate from langchain_core.prompts.prompt import PromptTemplate -from langchain_core.pydantic_v1 import Field +from pydantic import Field from langchain_community.chains.graph_qa.prompts import ( CYPHER_QA_PROMPT, diff --git a/libs/community/langchain_community/chains/graph_qa/hugegraph.py b/libs/community/langchain_community/chains/graph_qa/hugegraph.py index 8608fce6310c7..f88d8d456b4c1 100644 --- a/libs/community/langchain_community/chains/graph_qa/hugegraph.py +++ b/libs/community/langchain_community/chains/graph_qa/hugegraph.py @@ -9,7 +9,7 @@ from langchain_core.callbacks import CallbackManagerForChainRun from langchain_core.language_models import BaseLanguageModel from langchain_core.prompts import BasePromptTemplate -from langchain_core.pydantic_v1 import Field +from pydantic import Field from langchain_community.chains.graph_qa.prompts import ( CYPHER_QA_PROMPT, diff --git a/libs/community/langchain_community/chains/graph_qa/kuzu.py b/libs/community/langchain_community/chains/graph_qa/kuzu.py index 950885bec60d5..b79d1e69a86dc 100644 --- a/libs/community/langchain_community/chains/graph_qa/kuzu.py +++ b/libs/community/langchain_community/chains/graph_qa/kuzu.py @@ -10,7 +10,7 @@ from langchain_core.callbacks import CallbackManagerForChainRun from langchain_core.language_models import BaseLanguageModel from langchain_core.prompts import BasePromptTemplate -from langchain_core.pydantic_v1 import Field +from pydantic import Field from langchain_community.chains.graph_qa.prompts import ( CYPHER_QA_PROMPT, diff --git a/libs/community/langchain_community/chains/graph_qa/nebulagraph.py b/libs/community/langchain_community/chains/graph_qa/nebulagraph.py index 3e1067b8a4442..f0e305a7edd93 100644 --- a/libs/community/langchain_community/chains/graph_qa/nebulagraph.py +++ b/libs/community/langchain_community/chains/graph_qa/nebulagraph.py @@ -9,7 +9,7 @@ from langchain_core.callbacks import CallbackManagerForChainRun from langchain_core.language_models import BaseLanguageModel from langchain_core.prompts import BasePromptTemplate -from langchain_core.pydantic_v1 import Field +from pydantic import Field from langchain_community.chains.graph_qa.prompts import ( CYPHER_QA_PROMPT, diff --git a/libs/community/langchain_community/chains/graph_qa/neptune_cypher.py b/libs/community/langchain_community/chains/graph_qa/neptune_cypher.py index 5b786f9f57670..01de2641f6bb6 100644 --- a/libs/community/langchain_community/chains/graph_qa/neptune_cypher.py +++ b/libs/community/langchain_community/chains/graph_qa/neptune_cypher.py @@ -9,7 +9,7 @@ from langchain_core.callbacks import CallbackManagerForChainRun from langchain_core.language_models import BaseLanguageModel from langchain_core.prompts.base import BasePromptTemplate -from langchain_core.pydantic_v1 import Field +from pydantic import Field from langchain_community.chains.graph_qa.prompts import ( CYPHER_QA_PROMPT, diff --git a/libs/community/langchain_community/chains/graph_qa/neptune_sparql.py b/libs/community/langchain_community/chains/graph_qa/neptune_sparql.py index 07042ed7bf5e6..62aa49079fc08 100644 --- a/libs/community/langchain_community/chains/graph_qa/neptune_sparql.py +++ b/libs/community/langchain_community/chains/graph_qa/neptune_sparql.py @@ -12,7 +12,7 @@ from langchain_core.language_models import BaseLanguageModel from langchain_core.prompts.base import BasePromptTemplate from langchain_core.prompts.prompt import PromptTemplate -from langchain_core.pydantic_v1 import Field +from pydantic import Field from langchain_community.chains.graph_qa.prompts import SPARQL_QA_PROMPT from langchain_community.graphs import NeptuneRdfGraph diff --git a/libs/community/langchain_community/chains/graph_qa/ontotext_graphdb.py b/libs/community/langchain_community/chains/graph_qa/ontotext_graphdb.py index ef15bc186121d..71081a99c48b8 100644 --- a/libs/community/langchain_community/chains/graph_qa/ontotext_graphdb.py +++ b/libs/community/langchain_community/chains/graph_qa/ontotext_graphdb.py @@ -12,7 +12,7 @@ from langchain_core.callbacks.manager import CallbackManager, CallbackManagerForChainRun from langchain_core.language_models import BaseLanguageModel from langchain_core.prompts.base import BasePromptTemplate -from langchain_core.pydantic_v1 import Field +from pydantic import Field from langchain_community.chains.graph_qa.prompts import ( GRAPHDB_QA_PROMPT, diff --git a/libs/community/langchain_community/chains/graph_qa/sparql.py b/libs/community/langchain_community/chains/graph_qa/sparql.py index 43d8135e4fe55..b61e262ffbaba 100644 --- a/libs/community/langchain_community/chains/graph_qa/sparql.py +++ b/libs/community/langchain_community/chains/graph_qa/sparql.py @@ -11,7 +11,7 @@ from langchain_core.callbacks import CallbackManagerForChainRun from langchain_core.language_models import BaseLanguageModel from langchain_core.prompts.base import BasePromptTemplate -from langchain_core.pydantic_v1 import Field +from pydantic import Field from langchain_community.chains.graph_qa.prompts import ( SPARQL_GENERATION_SELECT_PROMPT, diff --git a/libs/community/langchain_community/chains/llm_requests.py b/libs/community/langchain_community/chains/llm_requests.py index a7a42807dd1ee..6fc23683d2036 100644 --- a/libs/community/langchain_community/chains/llm_requests.py +++ b/libs/community/langchain_community/chains/llm_requests.py @@ -7,7 +7,7 @@ from langchain.chains import LLMChain from langchain.chains.base import Chain from langchain_core.callbacks import CallbackManagerForChainRun -from langchain_core.pydantic_v1 import Field, root_validator +from pydantic import ConfigDict, Field, model_validator from langchain_community.utilities.requests import TextRequestsWrapper @@ -38,9 +38,10 @@ class LLMRequestsChain(Chain): input_key: str = "url" #: :meta private: output_key: str = "output" #: :meta private: - class Config: - arbitrary_types_allowed = True - extra = "forbid" + model_config = ConfigDict( + arbitrary_types_allowed=True, + extra="forbid", + ) @property def input_keys(self) -> List[str]: @@ -58,8 +59,9 @@ def output_keys(self) -> List[str]: """ return [self.output_key] - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" try: from bs4 import BeautifulSoup # noqa: F401 diff --git a/libs/community/langchain_community/chains/openapi/chain.py b/libs/community/langchain_community/chains/openapi/chain.py index 0ae9de6637d34..983efea7cc8da 100644 --- a/libs/community/langchain_community/chains/openapi/chain.py +++ b/libs/community/langchain_community/chains/openapi/chain.py @@ -11,7 +11,7 @@ from langchain.chains.llm import LLMChain from langchain_core.callbacks import CallbackManagerForChainRun, Callbacks from langchain_core.language_models import BaseLanguageModel -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field from requests import Response from langchain_community.tools.openapi.utils.api_models import APIOperation diff --git a/libs/community/langchain_community/chains/pebblo_retrieval/base.py b/libs/community/langchain_community/chains/pebblo_retrieval/base.py index ee595061d95cf..2d4b550f1a999 100644 --- a/libs/community/langchain_community/chains/pebblo_retrieval/base.py +++ b/libs/community/langchain_community/chains/pebblo_retrieval/base.py @@ -17,8 +17,8 @@ ) from langchain_core.documents import Document from langchain_core.language_models import BaseLanguageModel -from langchain_core.pydantic_v1 import Field, validator from langchain_core.vectorstores import VectorStoreRetriever +from pydantic import ConfigDict, Field, validator from langchain_community.chains.pebblo_retrieval.enforcement_filters import ( SUPPORTED_VECTORSTORES, @@ -189,10 +189,11 @@ async def _acall( else: return {self.output_key: answer} - class Config: - allow_population_by_field_name = True - arbitrary_types_allowed = True - extra = "forbid" + model_config = ConfigDict( + populate_by_name=True, + arbitrary_types_allowed=True, + extra="forbid", + ) @property def input_keys(self) -> List[str]: diff --git a/libs/community/langchain_community/chains/pebblo_retrieval/models.py b/libs/community/langchain_community/chains/pebblo_retrieval/models.py index 315905d18ddf1..97e29769ced6f 100644 --- a/libs/community/langchain_community/chains/pebblo_retrieval/models.py +++ b/libs/community/langchain_community/chains/pebblo_retrieval/models.py @@ -2,7 +2,7 @@ from typing import Any, List, Optional, Union -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel class AuthContext(BaseModel): diff --git a/libs/community/langchain_community/chains/pebblo_retrieval/utilities.py b/libs/community/langchain_community/chains/pebblo_retrieval/utilities.py index e42fe6b00e0ea..e6e36a505a947 100644 --- a/libs/community/langchain_community/chains/pebblo_retrieval/utilities.py +++ b/libs/community/langchain_community/chains/pebblo_retrieval/utilities.py @@ -10,9 +10,9 @@ from aiohttp import ClientTimeout from langchain_core.documents import Document from langchain_core.env import get_runtime_environment -from langchain_core.pydantic_v1 import BaseModel from langchain_core.utils import get_from_dict_or_env from langchain_core.vectorstores import VectorStoreRetriever +from pydantic import BaseModel from requests import Response, request from requests.exceptions import RequestException diff --git a/libs/community/langchain_community/chat_models/anthropic.py b/libs/community/langchain_community/chat_models/anthropic.py index 7ee675e6bac56..cd7160eb554c8 100644 --- a/libs/community/langchain_community/chat_models/anthropic.py +++ b/libs/community/langchain_community/chat_models/anthropic.py @@ -20,6 +20,7 @@ ) from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult from langchain_core.prompt_values import PromptValue +from pydantic import ConfigDict from langchain_community.llms.anthropic import _AnthropicCommon @@ -91,9 +92,10 @@ class ChatAnthropic(BaseChatModel, _AnthropicCommon): model = ChatAnthropic(model="", anthropic_api_key="my-api-key") """ - class Config: - allow_population_by_field_name = True - arbitrary_types_allowed = True + model_config = ConfigDict( + populate_by_name=True, + arbitrary_types_allowed=True, + ) @property def lc_secrets(self) -> Dict[str, str]: diff --git a/libs/community/langchain_community/chat_models/anyscale.py b/libs/community/langchain_community/chat_models/anyscale.py index 570eaed04167b..bb1f83ad1b257 100644 --- a/libs/community/langchain_community/chat_models/anyscale.py +++ b/libs/community/langchain_community/chat_models/anyscale.py @@ -5,12 +5,12 @@ import logging import os import sys -from typing import TYPE_CHECKING, Dict, Optional, Set +from typing import TYPE_CHECKING, Any, Dict, Optional, Set import requests from langchain_core.messages import BaseMessage -from langchain_core.pydantic_v1 import Field, SecretStr, root_validator from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env +from pydantic import Field, SecretStr, model_validator from langchain_community.adapters.openai import convert_message_to_dict from langchain_community.chat_models.openai import ( @@ -102,8 +102,9 @@ def get_available_models( return {model["id"] for model in models_response.json()["data"]} - @root_validator(pre=True) - def validate_environment(cls, values: dict) -> dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: dict) -> Any: """Validate that api key and python package exists in environment.""" values["anyscale_api_key"] = convert_to_secret_str( get_from_dict_or_env( diff --git a/libs/community/langchain_community/chat_models/azure_openai.py b/libs/community/langchain_community/chat_models/azure_openai.py index 28e27876613fa..83bc551d86a1b 100644 --- a/libs/community/langchain_community/chat_models/azure_openai.py +++ b/libs/community/langchain_community/chat_models/azure_openai.py @@ -9,8 +9,8 @@ from langchain_core._api.deprecation import deprecated from langchain_core.outputs import ChatResult -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.utils import get_from_dict_or_env, pre_init +from pydantic import BaseModel, Field from langchain_community.chat_models.openai import ChatOpenAI from langchain_community.utils.openai import is_openai_v1 diff --git a/libs/community/langchain_community/chat_models/baichuan.py b/libs/community/langchain_community/chat_models/baichuan.py index ea426507253d1..73f1a671fa355 100644 --- a/libs/community/langchain_community/chat_models/baichuan.py +++ b/libs/community/langchain_community/chat_models/baichuan.py @@ -44,7 +44,6 @@ parse_tool_call, ) from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult -from langchain_core.pydantic_v1 import BaseModel, Field, SecretStr, root_validator from langchain_core.runnables import Runnable from langchain_core.tools import BaseTool from langchain_core.utils import ( @@ -53,6 +52,13 @@ get_pydantic_field_names, ) from langchain_core.utils.function_calling import convert_to_openai_tool +from pydantic import ( + BaseModel, + ConfigDict, + Field, + SecretStr, + model_validator, +) from langchain_community.chat_models.llamacpp import ( _lc_invalid_tool_call_to_openai_tool_call, @@ -375,11 +381,13 @@ def lc_serializable(self) -> bool: model_kwargs: Dict[str, Any] = Field(default_factory=dict) """Holds any model parameters valid for API call not explicitly specified.""" - class Config: - allow_population_by_field_name = True + model_config = ConfigDict( + populate_by_name=True, + ) - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + 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", {}) @@ -404,8 +412,9 @@ def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: values["model_kwargs"] = extra return values - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: values["baichuan_api_base"] = get_from_dict_or_env( values, "baichuan_api_base", diff --git a/libs/community/langchain_community/chat_models/baidu_qianfan_endpoint.py b/libs/community/langchain_community/chat_models/baidu_qianfan_endpoint.py index d088054261494..a4fe51e3b4232 100644 --- a/libs/community/langchain_community/chat_models/baidu_qianfan_endpoint.py +++ b/libs/community/langchain_community/chat_models/baidu_qianfan_endpoint.py @@ -41,17 +41,18 @@ PydanticToolsParser, ) from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult -from langchain_core.pydantic_v1 import ( - BaseModel, - Field, - SecretStr, - root_validator, -) from langchain_core.runnables import Runnable, RunnableMap, RunnablePassthrough from langchain_core.tools import BaseTool from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env from langchain_core.utils.function_calling import convert_to_openai_tool from langchain_core.utils.pydantic import get_fields, is_basemodel_subclass +from pydantic import ( + BaseModel, + ConfigDict, + Field, + SecretStr, + model_validator, +) logger = logging.getLogger(__name__) @@ -248,7 +249,7 @@ class QianfanChatEndpoint(BaseChatModel): Tool calling: .. code-block:: python - from langchain_core.pydantic_v1 import BaseModel, Field + from pydantic import BaseModel, Field class GetWeather(BaseModel): @@ -287,7 +288,7 @@ class GetPopulation(BaseModel): from typing import Optional - from langchain_core.pydantic_v1 import BaseModel, Field + from pydantic import BaseModel, Field class Joke(BaseModel): @@ -380,11 +381,13 @@ class Joke(BaseModel): endpoint: Optional[str] = None """Endpoint of the Qianfan LLM, required if custom model used.""" - class Config: - allow_population_by_field_name = True + model_config = ConfigDict( + populate_by_name=True, + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: values["qianfan_ak"] = convert_to_secret_str( get_from_dict_or_env( values, ["qianfan_ak", "api_key"], "QIANFAN_AK", default="" @@ -747,7 +750,7 @@ def with_structured_output( .. code-block:: python from langchain_mistralai import QianfanChatEndpoint - from langchain_core.pydantic_v1 import BaseModel + from pydantic import BaseModel class AnswerWithJustification(BaseModel): '''An answer to the user question along with justification for the answer.''' @@ -768,7 +771,7 @@ class AnswerWithJustification(BaseModel): .. code-block:: python from langchain_mistralai import QianfanChatEndpoint - from langchain_core.pydantic_v1 import BaseModel + from pydantic import BaseModel class AnswerWithJustification(BaseModel): '''An answer to the user question along with justification for the answer.''' @@ -789,7 +792,7 @@ class AnswerWithJustification(BaseModel): .. code-block:: python from langchain_mistralai import QianfanChatEndpoint - from langchain_core.pydantic_v1 import BaseModel + from pydantic import BaseModel from langchain_core.utils.function_calling import convert_to_openai_tool class AnswerWithJustification(BaseModel): diff --git a/libs/community/langchain_community/chat_models/bedrock.py b/libs/community/langchain_community/chat_models/bedrock.py index db326d520f12b..6b36208390379 100644 --- a/libs/community/langchain_community/chat_models/bedrock.py +++ b/libs/community/langchain_community/chat_models/bedrock.py @@ -16,6 +16,7 @@ SystemMessage, ) from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult +from pydantic import ConfigDict from langchain_community.chat_models.anthropic import ( convert_messages_to_prompt_anthropic, @@ -231,8 +232,9 @@ def lc_attributes(self) -> Dict[str, Any]: return attributes - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def _stream( self, diff --git a/libs/community/langchain_community/chat_models/cohere.py b/libs/community/langchain_community/chat_models/cohere.py index 857cd39dbadb1..d2e8560a15158 100644 --- a/libs/community/langchain_community/chat_models/cohere.py +++ b/libs/community/langchain_community/chat_models/cohere.py @@ -19,6 +19,7 @@ SystemMessage, ) from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult +from pydantic import ConfigDict from langchain_community.llms.cohere import BaseCohere @@ -117,9 +118,10 @@ class ChatCohere(BaseChatModel, BaseCohere): chat.invoke(messages) """ - class Config: - allow_population_by_field_name = True - arbitrary_types_allowed = True + model_config = ConfigDict( + populate_by_name=True, + arbitrary_types_allowed=True, + ) @property def _llm_type(self) -> str: diff --git a/libs/community/langchain_community/chat_models/coze.py b/libs/community/langchain_community/chat_models/coze.py index ce941b4a78663..4bce2fba14f38 100644 --- a/libs/community/langchain_community/chat_models/coze.py +++ b/libs/community/langchain_community/chat_models/coze.py @@ -19,11 +19,11 @@ HumanMessageChunk, ) from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult -from langchain_core.pydantic_v1 import Field, SecretStr, root_validator from langchain_core.utils import ( convert_to_secret_str, get_from_dict_or_env, ) +from pydantic import ConfigDict, Field, SecretStr, model_validator logger = logging.getLogger(__name__) @@ -111,11 +111,13 @@ def lc_serializable(self) -> bool: "Streaming response" will provide real-time response of the model to the client, and the client needs to assemble the final reply based on the type of message. """ - class Config: - allow_population_by_field_name = True + model_config = ConfigDict( + populate_by_name=True, + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: values["coze_api_base"] = get_from_dict_or_env( values, "coze_api_base", diff --git a/libs/community/langchain_community/chat_models/dappier.py b/libs/community/langchain_community/chat_models/dappier.py index 457eac7dd2b16..fc32b5a95bcff 100644 --- a/libs/community/langchain_community/chat_models/dappier.py +++ b/libs/community/langchain_community/chat_models/dappier.py @@ -13,8 +13,8 @@ BaseMessage, ) from langchain_core.outputs import ChatGeneration, ChatResult -from langchain_core.pydantic_v1 import Field, SecretStr, root_validator from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env +from pydantic import ConfigDict, Field, SecretStr, model_validator from langchain_community.utilities.requests import Requests @@ -70,11 +70,13 @@ class ChatDappierAI(BaseChatModel): dappier_api_key: Optional[SecretStr] = Field(None, description="Dappier API Token") - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key exists in environment.""" values["dappier_api_key"] = convert_to_secret_str( get_from_dict_or_env(values, "dappier_api_key", "DAPPIER_API_KEY") diff --git a/libs/community/langchain_community/chat_models/deepinfra.py b/libs/community/langchain_community/chat_models/deepinfra.py index 3de532b0c4912..061bed9e8b0c7 100644 --- a/libs/community/langchain_community/chat_models/deepinfra.py +++ b/libs/community/langchain_community/chat_models/deepinfra.py @@ -54,11 +54,12 @@ ChatGenerationChunk, ChatResult, ) -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator from langchain_core.runnables import Runnable from langchain_core.tools import BaseTool from langchain_core.utils import get_from_dict_or_env from langchain_core.utils.function_calling import convert_to_openai_tool +from pydantic import BaseModel, ConfigDict, Field, model_validator +from typing_extensions import Self from langchain_community.utilities.requests import Requests @@ -222,10 +223,9 @@ class ChatDeepInfra(BaseChatModel): streaming: bool = False max_retries: int = 1 - class Config: - """Configuration for this pydantic object.""" - - allow_population_by_field_name = True + model_config = ConfigDict( + populate_by_name=True, + ) @property def _default_params(self) -> Dict[str, Any]: @@ -291,8 +291,9 @@ async def _completion_with_retry(**kwargs: Any) -> Any: return await _completion_with_retry(**kwargs) - @root_validator(pre=True) - def init_defaults(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def init_defaults(cls, values: Dict) -> Any: """Validate api key, python package exists, temperature, top_p, and top_k.""" # For compatibility with LiteLLM api_key = get_from_dict_or_env( @@ -309,18 +310,18 @@ def init_defaults(cls, values: Dict) -> Dict: ) return values - @root_validator(pre=False, skip_on_failure=True) - def validate_environment(cls, values: Dict) -> Dict: - if values["temperature"] is not None and not 0 <= values["temperature"] <= 1: + @model_validator(mode="after") + def validate_environment(self) -> Self: + if self.temperature is not None and not 0 <= self.temperature <= 1: raise ValueError("temperature must be in the range [0.0, 1.0]") - if values["top_p"] is not None and not 0 <= values["top_p"] <= 1: + if self.top_p is not None and not 0 <= self.top_p <= 1: raise ValueError("top_p must be in the range [0.0, 1.0]") - if values["top_k"] is not None and values["top_k"] <= 0: + if self.top_k is not None and self.top_k <= 0: raise ValueError("top_k must be positive") - return values + return self def _generate( self, diff --git a/libs/community/langchain_community/chat_models/edenai.py b/libs/community/langchain_community/chat_models/edenai.py index 0e03f08d00a15..2a41e5e7d550f 100644 --- a/libs/community/langchain_community/chat_models/edenai.py +++ b/libs/community/langchain_community/chat_models/edenai.py @@ -47,16 +47,17 @@ PydanticToolsParser, ) from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult -from langchain_core.pydantic_v1 import ( - BaseModel, - Field, - SecretStr, -) from langchain_core.runnables import Runnable, RunnableMap, RunnablePassthrough from langchain_core.tools import BaseTool from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init from langchain_core.utils.function_calling import convert_to_openai_tool from langchain_core.utils.pydantic import is_basemodel_subclass +from pydantic import ( + BaseModel, + ConfigDict, + Field, + SecretStr, +) from langchain_community.utilities.requests import Requests @@ -296,8 +297,9 @@ class ChatEdenAI(BaseChatModel): edenai_api_key: Optional[SecretStr] = Field(None, description="EdenAI API Token") - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/chat_models/ernie.py b/libs/community/langchain_community/chat_models/ernie.py index b8681fb867b60..41cf635cfaf9f 100644 --- a/libs/community/langchain_community/chat_models/ernie.py +++ b/libs/community/langchain_community/chat_models/ernie.py @@ -13,8 +13,8 @@ HumanMessage, ) from langchain_core.outputs import ChatGeneration, ChatResult -from langchain_core.pydantic_v1 import root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import model_validator logger = logging.getLogger(__name__) @@ -108,8 +108,9 @@ class ErnieBotChat(BaseChatModel): _lock = threading.Lock() - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: values["ernie_api_base"] = get_from_dict_or_env( values, "ernie_api_base", "ERNIE_API_BASE", "https://aip.baidubce.com" ) diff --git a/libs/community/langchain_community/chat_models/everlyai.py b/libs/community/langchain_community/chat_models/everlyai.py index 4122dae280cb7..160389b57c885 100644 --- a/libs/community/langchain_community/chat_models/everlyai.py +++ b/libs/community/langchain_community/chat_models/everlyai.py @@ -4,11 +4,11 @@ import logging import sys -from typing import TYPE_CHECKING, Dict, Optional, Set +from typing import TYPE_CHECKING, Any, Dict, Optional, Set from langchain_core.messages import BaseMessage -from langchain_core.pydantic_v1 import Field, root_validator from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env +from pydantic import Field, model_validator from langchain_community.adapters.openai import convert_message_to_dict from langchain_community.chat_models.openai import ( @@ -76,8 +76,9 @@ def get_available_models() -> Set[str]: ] ) - @root_validator(pre=True) - def validate_environment_override(cls, values: dict) -> dict: + @model_validator(mode="before") + @classmethod + def validate_environment_override(cls, values: dict) -> Any: """Validate that api key and python package exists in environment.""" values["openai_api_key"] = convert_to_secret_str( get_from_dict_or_env( diff --git a/libs/community/langchain_community/chat_models/fireworks.py b/libs/community/langchain_community/chat_models/fireworks.py index e3a5ac0e12da1..2211f186ae2f9 100644 --- a/libs/community/langchain_community/chat_models/fireworks.py +++ b/libs/community/langchain_community/chat_models/fireworks.py @@ -32,9 +32,9 @@ SystemMessageChunk, ) from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult -from langchain_core.pydantic_v1 import Field, SecretStr, root_validator from langchain_core.utils import convert_to_secret_str from langchain_core.utils.env import get_from_dict_or_env +from pydantic import Field, SecretStr, model_validator from langchain_community.adapters.openai import convert_message_to_dict @@ -112,8 +112,9 @@ def get_lc_namespace(cls) -> List[str]: """Get the namespace of the langchain object.""" return ["langchain", "chat_models", "fireworks"] - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key in environment.""" try: import fireworks.client diff --git a/libs/community/langchain_community/chat_models/google_palm.py b/libs/community/langchain_community/chat_models/google_palm.py index bbdc7efc32b90..77038256c7d82 100644 --- a/libs/community/langchain_community/chat_models/google_palm.py +++ b/libs/community/langchain_community/chat_models/google_palm.py @@ -21,8 +21,8 @@ ChatGeneration, ChatResult, ) -from langchain_core.pydantic_v1 import BaseModel, SecretStr from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import BaseModel, SecretStr from tenacity import ( before_sleep_log, retry, diff --git a/libs/community/langchain_community/chat_models/gpt_router.py b/libs/community/langchain_community/chat_models/gpt_router.py index cff7df8421ba9..c833b8e42404f 100644 --- a/libs/community/langchain_community/chat_models/gpt_router.py +++ b/libs/community/langchain_community/chat_models/gpt_router.py @@ -30,8 +30,9 @@ from langchain_core.language_models.llms import create_base_retry_decorator from langchain_core.messages import AIMessageChunk, BaseMessage, BaseMessageChunk from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult -from langchain_core.pydantic_v1 import BaseModel, Field, SecretStr, root_validator from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env +from pydantic import BaseModel, Field, SecretStr, model_validator +from typing_extensions import Self from langchain_community.adapters.openai import ( convert_dict_to_message, @@ -150,7 +151,7 @@ class GPTRouter(BaseChatModel): """ client: Any = Field(default=None, exclude=True) #: :meta private: - models_priority_list: List[GPTRouterModel] = Field(min_items=1) + models_priority_list: List[GPTRouterModel] = Field(min_length=1) gpt_router_api_base: str = Field(default=None) """WriteSonic GPTRouter custom endpoint""" gpt_router_api_key: Optional[SecretStr] = None @@ -167,8 +168,9 @@ class GPTRouter(BaseChatModel): """Number of chat completions to generate for each prompt.""" max_tokens: int = 256 - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: values["gpt_router_api_base"] = get_from_dict_or_env( values, "gpt_router_api_base", @@ -185,8 +187,8 @@ def validate_environment(cls, values: Dict) -> Dict: ) return values - @root_validator(pre=True, skip_on_failure=True) - def post_init(cls, values: Dict) -> Dict: + @model_validator(mode="after") + def post_init(self) -> Self: try: from gpt_router.client import GPTRouterClient @@ -197,12 +199,14 @@ def post_init(cls, values: Dict) -> Dict: ) gpt_router_client = GPTRouterClient( - values["gpt_router_api_base"], - values["gpt_router_api_key"].get_secret_value(), + self.gpt_router_api_base, + self.gpt_router_api_key.get_secret_value() + if self.gpt_router_api_key + else None, ) - values["client"] = gpt_router_client + self.client = gpt_router_client - return values + return self @property def lc_secrets(self) -> Dict[str, str]: diff --git a/libs/community/langchain_community/chat_models/huggingface.py b/libs/community/langchain_community/chat_models/huggingface.py index b5d3f905a2461..fe029d6d6ca7a 100644 --- a/libs/community/langchain_community/chat_models/huggingface.py +++ b/libs/community/langchain_community/chat_models/huggingface.py @@ -25,7 +25,8 @@ ChatResult, LLMResult, ) -from langchain_core.pydantic_v1 import root_validator +from pydantic import model_validator +from typing_extensions import Self from langchain_community.llms.huggingface_endpoint import HuggingFaceEndpoint from langchain_community.llms.huggingface_hub import HuggingFaceHub @@ -76,17 +77,17 @@ def __init__(self, **kwargs: Any): else self.tokenizer ) - @root_validator(pre=False, skip_on_failure=True) - def validate_llm(cls, values: dict) -> dict: + @model_validator(mode="after") + def validate_llm(self) -> Self: if not isinstance( - values["llm"], + self.llm, (HuggingFaceTextGenInference, HuggingFaceEndpoint, HuggingFaceHub), ): raise TypeError( "Expected llm to be one of HuggingFaceTextGenInference, " - f"HuggingFaceEndpoint, HuggingFaceHub, received {type(values['llm'])}" + f"HuggingFaceEndpoint, HuggingFaceHub, received {type(self.llm)}" ) - return values + return self def _stream( self, diff --git a/libs/community/langchain_community/chat_models/human.py b/libs/community/langchain_community/chat_models/human.py index 030516a822f7c..c24964a678edd 100644 --- a/libs/community/langchain_community/chat_models/human.py +++ b/libs/community/langchain_community/chat_models/human.py @@ -15,7 +15,7 @@ messages_to_dict, ) from langchain_core.outputs import ChatGeneration, ChatResult -from langchain_core.pydantic_v1 import Field +from pydantic import Field from langchain_community.llms.utils import enforce_stop_tokens diff --git a/libs/community/langchain_community/chat_models/hunyuan.py b/libs/community/langchain_community/chat_models/hunyuan.py index c16dc7d114173..cd744956263bd 100644 --- a/libs/community/langchain_community/chat_models/hunyuan.py +++ b/libs/community/langchain_community/chat_models/hunyuan.py @@ -19,13 +19,13 @@ SystemMessage, ) from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult -from langchain_core.pydantic_v1 import Field, SecretStr, root_validator from langchain_core.utils import ( convert_to_secret_str, get_from_dict_or_env, get_pydantic_field_names, pre_init, ) +from pydantic import ConfigDict, Field, SecretStr, model_validator logger = logging.getLogger(__name__) @@ -138,11 +138,13 @@ def lc_serializable(self) -> bool: model_kwargs: Dict[str, Any] = Field(default_factory=dict) """Holds any model parameters valid for API call not explicitly specified.""" - class Config: - allow_population_by_field_name = True + model_config = ConfigDict( + populate_by_name=True, + ) - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + 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", {}) diff --git a/libs/community/langchain_community/chat_models/javelin_ai_gateway.py b/libs/community/langchain_community/chat_models/javelin_ai_gateway.py index af7f401e1a4f9..d09fb3bcaf67b 100644 --- a/libs/community/langchain_community/chat_models/javelin_ai_gateway.py +++ b/libs/community/langchain_community/chat_models/javelin_ai_gateway.py @@ -18,7 +18,7 @@ ChatGeneration, ChatResult, ) -from langchain_core.pydantic_v1 import BaseModel, Field, SecretStr +from pydantic import BaseModel, ConfigDict, Field, SecretStr logger = logging.getLogger(__name__) @@ -62,14 +62,15 @@ class ChatJavelinAIGateway(BaseChatModel): params: Optional[ChatParams] = None """Parameters for the Javelin AI Gateway LLM.""" - client: Any + client: Any = None """javelin client.""" javelin_api_key: Optional[SecretStr] = Field(None, alias="api_key") """The API key for the Javelin AI Gateway.""" - class Config: - allow_population_by_field_name = True + model_config = ConfigDict( + populate_by_name=True, + ) def __init__(self, **kwargs: Any): try: diff --git a/libs/community/langchain_community/chat_models/jinachat.py b/libs/community/langchain_community/chat_models/jinachat.py index ecda4528a4b7a..f258674856b86 100644 --- a/libs/community/langchain_community/chat_models/jinachat.py +++ b/libs/community/langchain_community/chat_models/jinachat.py @@ -40,13 +40,13 @@ SystemMessageChunk, ) from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult -from langchain_core.pydantic_v1 import Field, SecretStr, root_validator from langchain_core.utils import ( convert_to_secret_str, get_from_dict_or_env, get_pydantic_field_names, pre_init, ) +from pydantic import ConfigDict, Field, SecretStr, model_validator from tenacity import ( before_sleep_log, retry, @@ -188,11 +188,13 @@ def is_lc_serializable(cls) -> bool: max_tokens: Optional[int] = None """Maximum number of tokens to generate.""" - class Config: - allow_population_by_field_name = True + model_config = ConfigDict( + populate_by_name=True, + ) - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + 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", {}) diff --git a/libs/community/langchain_community/chat_models/kinetica.py b/libs/community/langchain_community/chat_models/kinetica.py index 9088afd950fed..1de6915a20188 100644 --- a/libs/community/langchain_community/chat_models/kinetica.py +++ b/libs/community/langchain_community/chat_models/kinetica.py @@ -26,7 +26,7 @@ ) from langchain_core.output_parsers.transform import BaseOutputParser from langchain_core.outputs import ChatGeneration, ChatResult, Generation -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, ConfigDict, Field LOG = logging.getLogger(__name__) @@ -543,8 +543,9 @@ class KineticaSqlResponse(BaseModel): dataframe: Any = Field(default=None) """The Pandas dataframe containing the fetched data.""" - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) class KineticaSqlOutputParser(BaseOutputParser[KineticaSqlResponse]): @@ -582,8 +583,9 @@ class KineticaSqlOutputParser(BaseOutputParser[KineticaSqlResponse]): kdbc: Any = Field(exclude=True) """ Kinetica DB connection. """ - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def parse(self, text: str) -> KineticaSqlResponse: df = self.kdbc.to_df(text) diff --git a/libs/community/langchain_community/chat_models/konko.py b/libs/community/langchain_community/chat_models/konko.py index d4ee329d835dc..a2d16e51179e6 100644 --- a/libs/community/langchain_community/chat_models/konko.py +++ b/libs/community/langchain_community/chat_models/konko.py @@ -23,8 +23,8 @@ ) from langchain_core.messages import AIMessageChunk, BaseMessage from langchain_core.outputs import ChatGenerationChunk, ChatResult -from langchain_core.pydantic_v1 import Field, SecretStr from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import Field, SecretStr from langchain_community.adapters.openai import ( convert_message_to_dict, diff --git a/libs/community/langchain_community/chat_models/litellm.py b/libs/community/langchain_community/chat_models/litellm.py index f0cf69a71910d..97e6a3403fce2 100644 --- a/libs/community/langchain_community/chat_models/litellm.py +++ b/libs/community/langchain_community/chat_models/litellm.py @@ -52,11 +52,11 @@ ChatGenerationChunk, ChatResult, ) -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.runnables import Runnable from langchain_core.tools import BaseTool from langchain_core.utils import get_from_dict_or_env, pre_init from langchain_core.utils.function_calling import convert_to_openai_tool +from pydantic import BaseModel, Field logger = logging.getLogger(__name__) diff --git a/libs/community/langchain_community/chat_models/llama_edge.py b/libs/community/langchain_community/chat_models/llama_edge.py index 8edda85c3b023..0c096d89608de 100644 --- a/libs/community/langchain_community/chat_models/llama_edge.py +++ b/libs/community/langchain_community/chat_models/llama_edge.py @@ -21,8 +21,8 @@ SystemMessage, ) from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult -from langchain_core.pydantic_v1 import root_validator from langchain_core.utils import get_pydantic_field_names +from pydantic import ConfigDict, model_validator logger = logging.getLogger(__name__) @@ -84,11 +84,13 @@ class LlamaEdgeChatService(BaseChatModel): streaming: bool = False """Whether to stream the results or not.""" - class Config: - allow_population_by_field_name = True + model_config = ConfigDict( + populate_by_name=True, + ) - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + 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", {}) diff --git a/libs/community/langchain_community/chat_models/llamacpp.py b/libs/community/langchain_community/chat_models/llamacpp.py index a3b7f8a3a2527..ed8d0a528ada1 100644 --- a/libs/community/langchain_community/chat_models/llamacpp.py +++ b/libs/community/langchain_community/chat_models/llamacpp.py @@ -46,15 +46,16 @@ parse_tool_call, ) from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult -from langchain_core.pydantic_v1 import ( - BaseModel, - Field, - root_validator, -) from langchain_core.runnables import Runnable, RunnableMap, RunnablePassthrough from langchain_core.tools import BaseTool from langchain_core.utils.function_calling import convert_to_openai_tool from langchain_core.utils.pydantic import is_basemodel_subclass +from pydantic import ( + BaseModel, + Field, + model_validator, +) +from typing_extensions import Self class ChatLlamaCpp(BaseChatModel): @@ -172,8 +173,8 @@ class ChatLlamaCpp(BaseChatModel): verbose: bool = True """Print verbose output to stderr.""" - @root_validator(pre=False, skip_on_failure=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="after") + def validate_environment(self) -> Self: """Validate that llama-cpp-python library is installed.""" try: from llama_cpp import Llama, LlamaGrammar @@ -184,7 +185,7 @@ def validate_environment(cls, values: Dict) -> Dict: "use this embedding model: pip install llama-cpp-python" ) - model_path = values["model_path"] + model_path = self.model_path model_param_names = [ "rope_freq_scale", "rope_freq_base", @@ -203,35 +204,35 @@ def validate_environment(cls, values: Dict) -> Dict: "last_n_tokens_size", "verbose", ] - model_params = {k: values[k] for k in model_param_names} + model_params = {k: getattr(self, k) for k in model_param_names} # For backwards compatibility, only include if non-null. - if values["n_gpu_layers"] is not None: - model_params["n_gpu_layers"] = values["n_gpu_layers"] + if self.n_gpu_layers is not None: + model_params["n_gpu_layers"] = self.n_gpu_layers - model_params.update(values["model_kwargs"]) + model_params.update(self.model_kwargs) try: - values["client"] = Llama(model_path, **model_params) + self.client = Llama(model_path, **model_params) except Exception as e: raise ValueError( f"Could not load Llama model from path: {model_path}. " f"Received error {e}" ) - if values["grammar"] and values["grammar_path"]: - grammar = values["grammar"] - grammar_path = values["grammar_path"] + if self.grammar and self.grammar_path: + grammar = self.grammar + grammar_path = self.grammar_path raise ValueError( "Can only pass in one of grammar and grammar_path. Received " f"{grammar=} and {grammar_path=}." ) - elif isinstance(values["grammar"], str): - values["grammar"] = LlamaGrammar.from_string(values["grammar"]) - elif values["grammar_path"]: - values["grammar"] = LlamaGrammar.from_file(values["grammar_path"]) + elif isinstance(self.grammar, str): + self.grammar = LlamaGrammar.from_string(self.grammar) + elif self.grammar_path: + self.grammar = LlamaGrammar.from_file(self.grammar_path) else: pass - return values + return self def _get_parameters(self, stop: Optional[List[str]]) -> Dict[str, Any]: """ @@ -433,7 +434,7 @@ def with_structured_output( .. code-block:: python from langchain_community.chat_models import ChatLlamaCpp - from langchain_core.pydantic_v1 import BaseModel + from pydantic import BaseModel class AnswerWithJustification(BaseModel): '''An answer to the user question along with justification for the answer.''' @@ -465,7 +466,7 @@ class AnswerWithJustification(BaseModel): .. code-block:: python from langchain_community.chat_models import ChatLlamaCpp - from langchain_core.pydantic_v1 import BaseModel + from pydantic import BaseModel class AnswerWithJustification(BaseModel): '''An answer to the user question along with justification for the answer.''' @@ -497,7 +498,7 @@ class AnswerWithJustification(BaseModel): .. code-block:: python from langchain_community.chat_models import ChatLlamaCpp - from langchain_core.pydantic_v1 import BaseModel + from pydantic import BaseModel from langchain_core.utils.function_calling import convert_to_openai_tool class AnswerWithJustification(BaseModel): diff --git a/libs/community/langchain_community/chat_models/maritalk.py b/libs/community/langchain_community/chat_models/maritalk.py index f06f2917e715d..9e089a174aeca 100644 --- a/libs/community/langchain_community/chat_models/maritalk.py +++ b/libs/community/langchain_community/chat_models/maritalk.py @@ -16,7 +16,7 @@ SystemMessage, ) from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult -from langchain_core.pydantic_v1 import Field +from pydantic import Field from requests import Response from requests.exceptions import HTTPError diff --git a/libs/community/langchain_community/chat_models/minimax.py b/libs/community/langchain_community/chat_models/minimax.py index b8b47ae838586..46cd4c1950165 100644 --- a/libs/community/langchain_community/chat_models/minimax.py +++ b/libs/community/langchain_community/chat_models/minimax.py @@ -44,12 +44,18 @@ PydanticToolsParser, ) from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult -from langchain_core.pydantic_v1 import BaseModel, Field, SecretStr, root_validator from langchain_core.runnables import Runnable, RunnableMap, RunnablePassthrough from langchain_core.tools import BaseTool from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env from langchain_core.utils.function_calling import convert_to_openai_tool from langchain_core.utils.pydantic import get_fields +from pydantic import ( + BaseModel, + ConfigDict, + Field, + SecretStr, + model_validator, +) logger = logging.getLogger(__name__) @@ -267,7 +273,7 @@ class MiniMaxChat(BaseChatModel): Tool calling: .. code-block:: python - from langchain_core.pydantic_v1 import BaseModel, Field + from pydantic import BaseModel, Field class GetWeather(BaseModel): @@ -307,7 +313,7 @@ class GetPopulation(BaseModel): from typing import Optional - from langchain_core.pydantic_v1 import BaseModel, Field + from pydantic import BaseModel, Field class Joke(BaseModel): @@ -384,11 +390,13 @@ def _default_params(self) -> Dict[str, Any]: streaming: bool = False """Whether to stream the results or not.""" - class Config: - allow_population_by_field_name = True + model_config = ConfigDict( + populate_by_name=True, + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" values["minimax_api_key"] = convert_to_secret_str( get_from_dict_or_env( @@ -694,7 +702,7 @@ def with_structured_output( .. code-block:: python from langchain_community.chat_models import MiniMaxChat - from langchain_core.pydantic_v1 import BaseModel + from pydantic import BaseModel class AnswerWithJustification(BaseModel): '''An answer to the user question along with justification for the answer.''' @@ -715,7 +723,7 @@ class AnswerWithJustification(BaseModel): .. code-block:: python from langchain_community.chat_models import MiniMaxChat - from langchain_core.pydantic_v1 import BaseModel + from pydantic import BaseModel class AnswerWithJustification(BaseModel): '''An answer to the user question along with justification for the answer.''' @@ -737,7 +745,7 @@ class AnswerWithJustification(BaseModel): .. code-block:: python from langchain_community.chat_models import MiniMaxChat - from langchain_core.pydantic_v1 import BaseModel + from pydantic import BaseModel from langchain_core.utils.function_calling import convert_to_openai_tool class AnswerWithJustification(BaseModel): diff --git a/libs/community/langchain_community/chat_models/mlflow.py b/libs/community/langchain_community/chat_models/mlflow.py index 101c2cb040c6b..9a1c528fd7538 100644 --- a/libs/community/langchain_community/chat_models/mlflow.py +++ b/libs/community/langchain_community/chat_models/mlflow.py @@ -42,14 +42,14 @@ parse_tool_call, ) from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult -from langchain_core.pydantic_v1 import ( +from langchain_core.runnables import Runnable, RunnableConfig +from langchain_core.tools import BaseTool +from langchain_core.utils.function_calling import convert_to_openai_tool +from pydantic import ( BaseModel, Field, PrivateAttr, ) -from langchain_core.runnables import Runnable, RunnableConfig -from langchain_core.tools import BaseTool -from langchain_core.utils.function_calling import convert_to_openai_tool logger = logging.getLogger(__name__) diff --git a/libs/community/langchain_community/chat_models/mlflow_ai_gateway.py b/libs/community/langchain_community/chat_models/mlflow_ai_gateway.py index f85eebed002e1..e33a656466eae 100644 --- a/libs/community/langchain_community/chat_models/mlflow_ai_gateway.py +++ b/libs/community/langchain_community/chat_models/mlflow_ai_gateway.py @@ -18,7 +18,7 @@ ChatGeneration, ChatResult, ) -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel logger = logging.getLogger(__name__) diff --git a/libs/community/langchain_community/chat_models/oci_generative_ai.py b/libs/community/langchain_community/chat_models/oci_generative_ai.py index 3207e2d93a693..a900e488ffa42 100644 --- a/libs/community/langchain_community/chat_models/oci_generative_ai.py +++ b/libs/community/langchain_community/chat_models/oci_generative_ai.py @@ -38,10 +38,10 @@ PydanticToolsParser, ) from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult -from langchain_core.pydantic_v1 import BaseModel from langchain_core.runnables import Runnable from langchain_core.tools import BaseTool from langchain_core.utils.function_calling import convert_to_openai_function +from pydantic import BaseModel, ConfigDict from langchain_community.llms.oci_generative_ai import OCIGenAIBase from langchain_community.llms.utils import enforce_stop_tokens @@ -499,8 +499,10 @@ class ChatOCIGenAI(BaseChatModel, OCIGenAIBase): """ # noqa: E501 - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + arbitrary_types_allowed=True, + ) @property def _llm_type(self) -> str: @@ -546,6 +548,9 @@ def _prepare_request( chat_params = {**_model_kwargs, **kwargs, **oci_params} + if not self.model_id: + raise ValueError("Model ID is required to chat") + if self.model_id.startswith(CUSTOM_ENDPOINT_PREFIX): serving_mode = models.DedicatedServingMode(endpoint_id=self.model_id) else: diff --git a/libs/community/langchain_community/chat_models/octoai.py b/libs/community/langchain_community/chat_models/octoai.py index f79f9d192c077..77d7572d48da8 100644 --- a/libs/community/langchain_community/chat_models/octoai.py +++ b/libs/community/langchain_community/chat_models/octoai.py @@ -2,8 +2,8 @@ from typing import Dict -from langchain_core.pydantic_v1 import Field, SecretStr from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import Field, SecretStr from langchain_community.chat_models.openai import ChatOpenAI from langchain_community.utils.openai import is_openai_v1 diff --git a/libs/community/langchain_community/chat_models/openai.py b/libs/community/langchain_community/chat_models/openai.py index 54326d0acc288..d67eb0f10b69f 100644 --- a/libs/community/langchain_community/chat_models/openai.py +++ b/libs/community/langchain_community/chat_models/openai.py @@ -44,13 +44,13 @@ ToolMessageChunk, ) from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator from langchain_core.runnables import Runnable from langchain_core.utils import ( get_from_dict_or_env, get_pydantic_field_names, pre_init, ) +from pydantic import BaseModel, ConfigDict, Field, model_validator from langchain_community.adapters.openai import ( convert_dict_to_message, @@ -244,11 +244,13 @@ def is_lc_serializable(cls) -> bool: http_client: Union[Any, None] = None """Optional httpx.Client.""" - class Config: - allow_population_by_field_name = True + model_config = ConfigDict( + populate_by_name=True, + ) - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + 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", {}) diff --git a/libs/community/langchain_community/chat_models/pai_eas_endpoint.py b/libs/community/langchain_community/chat_models/pai_eas_endpoint.py index 48c7eb9b42a0e..55dd0ccbf93c3 100644 --- a/libs/community/langchain_community/chat_models/pai_eas_endpoint.py +++ b/libs/community/langchain_community/chat_models/pai_eas_endpoint.py @@ -17,8 +17,8 @@ SystemMessage, ) from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult -from langchain_core.pydantic_v1 import root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import model_validator from langchain_community.llms.utils import enforce_stop_tokens @@ -67,8 +67,9 @@ class PaiEasChatEndpoint(BaseChatModel): timeout: Optional[int] = 5000 - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" values["eas_service_url"] = get_from_dict_or_env( values, "eas_service_url", "EAS_SERVICE_URL" diff --git a/libs/community/langchain_community/chat_models/perplexity.py b/libs/community/langchain_community/chat_models/perplexity.py index ee93f48cbf894..88547c67d29db 100644 --- a/libs/community/langchain_community/chat_models/perplexity.py +++ b/libs/community/langchain_community/chat_models/perplexity.py @@ -35,8 +35,12 @@ ToolMessageChunk, ) from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult -from langchain_core.pydantic_v1 import Field, root_validator -from langchain_core.utils import get_from_dict_or_env, get_pydantic_field_names +from langchain_core.utils import ( + from_env, + get_pydantic_field_names, +) +from pydantic import ConfigDict, Field, model_validator +from typing_extensions import Self logger = logging.getLogger(__name__) @@ -60,14 +64,16 @@ class ChatPerplexity(BaseChatModel): ) """ - client: Any #: :meta private: + client: Any = None #: :meta private: model: str = "llama-3.1-sonar-small-128k-online" """Model name.""" temperature: float = 0.7 """What sampling temperature to use.""" model_kwargs: Dict[str, Any] = Field(default_factory=dict) """Holds any model parameters valid for `create` call not explicitly specified.""" - pplx_api_key: Optional[str] = Field(None, alias="api_key") + pplx_api_key: Optional[str] = Field( + default_factory=from_env("PPLX_API_KEY", default=None), alias="api_key" + ) """Base URL path for API requests, leave blank if not using a proxy or service emulator.""" request_timeout: Optional[Union[float, Tuple[float, float]]] = Field( @@ -81,15 +87,17 @@ class ChatPerplexity(BaseChatModel): max_tokens: Optional[int] = None """Maximum number of tokens to generate.""" - class Config: - allow_population_by_field_name = True + model_config = ConfigDict( + populate_by_name=True, + ) @property def lc_secrets(self) -> Dict[str, str]: return {"pplx_api_key": "PPLX_API_KEY"} - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + 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", {}) @@ -114,12 +122,9 @@ def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: values["model_kwargs"] = extra return values - @root_validator(pre=False, skip_on_failure=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="after") + def validate_environment(self) -> Self: """Validate that api key and python package exists in environment.""" - values["pplx_api_key"] = get_from_dict_or_env( - values, "pplx_api_key", "PPLX_API_KEY" - ) try: import openai except ImportError: @@ -128,8 +133,8 @@ def validate_environment(cls, values: Dict) -> Dict: "Please install it with `pip install openai`." ) try: - values["client"] = openai.OpenAI( - api_key=values["pplx_api_key"], base_url="https://api.perplexity.ai" + self.client = openai.OpenAI( + api_key=self.pplx_api_key, base_url="https://api.perplexity.ai" ) except AttributeError: raise ValueError( @@ -137,7 +142,7 @@ def validate_environment(cls, values: Dict) -> Dict: "due to an old version of the openai package. Try upgrading it " "with `pip install --upgrade openai`." ) - return values + return self @property def _default_params(self) -> Dict[str, Any]: diff --git a/libs/community/langchain_community/chat_models/premai.py b/libs/community/langchain_community/chat_models/premai.py index 5498ac9e40016..60fba53350867 100644 --- a/libs/community/langchain_community/chat_models/premai.py +++ b/libs/community/langchain_community/chat_models/premai.py @@ -38,15 +38,16 @@ ToolMessage, ) from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult -from langchain_core.pydantic_v1 import ( - BaseModel, - Field, - SecretStr, -) from langchain_core.runnables import Runnable from langchain_core.tools import BaseTool from langchain_core.utils import get_from_dict_or_env, pre_init from langchain_core.utils.function_calling import convert_to_openai_tool +from pydantic import ( + BaseModel, + ConfigDict, + Field, + SecretStr, +) if TYPE_CHECKING: from premai.api.chat_completions.v1_chat_completions_create import ( @@ -306,10 +307,11 @@ class ChatPremAI(BaseChatModel, BaseModel): client: Any - class Config: - allow_population_by_field_name = True - arbitrary_types_allowed = True - extra = "forbid" + model_config = ConfigDict( + populate_by_name=True, + arbitrary_types_allowed=True, + extra="forbid", + ) @pre_init def validate_environments(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/chat_models/snowflake.py b/libs/community/langchain_community/chat_models/snowflake.py index f5af38afdd2a2..524838188303d 100644 --- a/libs/community/langchain_community/chat_models/snowflake.py +++ b/libs/community/langchain_community/chat_models/snowflake.py @@ -11,7 +11,6 @@ SystemMessage, ) from langchain_core.outputs import ChatGeneration, ChatResult -from langchain_core.pydantic_v1 import Field, SecretStr, root_validator from langchain_core.utils import ( convert_to_secret_str, get_from_dict_or_env, @@ -19,6 +18,7 @@ pre_init, ) from langchain_core.utils.utils import build_extra_kwargs +from pydantic import Field, SecretStr, model_validator SUPPORTED_ROLES: List[str] = [ "system", @@ -126,8 +126,9 @@ class ChatSnowflakeCortex(BaseChatModel): snowflake_role: Optional[str] = Field(default=None, alias="role") """Automatically inferred from env var `SNOWFLAKE_ROLE` if not provided.""" - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + 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", {}) diff --git a/libs/community/langchain_community/chat_models/solar.py b/libs/community/langchain_community/chat_models/solar.py index d8a3cb04c89df..10eba5da5a7a7 100644 --- a/libs/community/langchain_community/chat_models/solar.py +++ b/libs/community/langchain_community/chat_models/solar.py @@ -3,8 +3,8 @@ from typing import Dict from langchain_core._api import deprecated -from langchain_core.pydantic_v1 import Field from langchain_core.utils import get_from_dict_or_env, pre_init +from pydantic import ConfigDict, Field from langchain_community.chat_models import ChatOpenAI from langchain_community.llms.solar import SOLAR_SERVICE_URL_BASE, SolarCommon @@ -30,10 +30,11 @@ class SolarChat(SolarCommon, ChatOpenAI): max_tokens: int = Field(default=1024) # this is needed to match ChatOpenAI superclass - class Config: - allow_population_by_field_name = True - arbitrary_types_allowed = True - extra = "ignore" + model_config = ConfigDict( + populate_by_name=True, + arbitrary_types_allowed=True, + extra="ignore", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/chat_models/sparkllm.py b/libs/community/langchain_community/chat_models/sparkllm.py index adfeed5517d1b..996c8c2a2f67f 100644 --- a/libs/community/langchain_community/chat_models/sparkllm.py +++ b/libs/community/langchain_community/chat_models/sparkllm.py @@ -41,12 +41,12 @@ ChatGenerationChunk, ChatResult, ) -from langchain_core.pydantic_v1 import Field, root_validator from langchain_core.utils import ( get_from_dict_or_env, get_pydantic_field_names, ) from langchain_core.utils.pydantic import get_fields +from pydantic import ConfigDict, Field, model_validator logger = logging.getLogger(__name__) @@ -296,11 +296,13 @@ def lc_secrets(self) -> Dict[str, str]: model_kwargs: Dict[str, Any] = Field(default_factory=dict) """Holds any model parameters valid for API call not explicitly specified.""" - class Config: - allow_population_by_field_name = True + model_config = ConfigDict( + populate_by_name=True, + ) - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + 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", {}) @@ -326,8 +328,9 @@ def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: return values - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: values["spark_app_id"] = get_from_dict_or_env( values, ["spark_app_id", "app_id"], diff --git a/libs/community/langchain_community/chat_models/symblai_nebula.py b/libs/community/langchain_community/chat_models/symblai_nebula.py index e3638bada8f24..adacc508ca987 100644 --- a/libs/community/langchain_community/chat_models/symblai_nebula.py +++ b/libs/community/langchain_community/chat_models/symblai_nebula.py @@ -16,8 +16,8 @@ ) from langchain_core.messages import AIMessage, AIMessageChunk, BaseMessage from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult -from langchain_core.pydantic_v1 import Field, SecretStr from langchain_core.utils import convert_to_secret_str +from pydantic import ConfigDict, Field, SecretStr def _convert_role(role: str) -> str: @@ -89,11 +89,10 @@ class ChatNebula(BaseChatModel): nebula_api_key: Optional[SecretStr] = Field(None, description="Nebula API Token") - class Config: - """Configuration for this pydantic object.""" - - allow_population_by_field_name = True - arbitrary_types_allowed = True + model_config = ConfigDict( + populate_by_name=True, + arbitrary_types_allowed=True, + ) def __init__(self, **kwargs: Any) -> None: if "nebula_api_key" in kwargs: diff --git a/libs/community/langchain_community/chat_models/tongyi.py b/libs/community/langchain_community/chat_models/tongyi.py index 2e36987840de3..6e0a2d59787dd 100644 --- a/libs/community/langchain_community/chat_models/tongyi.py +++ b/libs/community/langchain_community/chat_models/tongyi.py @@ -53,16 +53,17 @@ ChatGenerationChunk, ChatResult, ) -from langchain_core.pydantic_v1 import ( - BaseModel, - Field, - SecretStr, -) from langchain_core.runnables import Runnable, RunnableMap, RunnablePassthrough from langchain_core.tools import BaseTool from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init from langchain_core.utils.function_calling import convert_to_openai_tool from langchain_core.utils.pydantic import is_basemodel_subclass +from pydantic import ( + BaseModel, + ConfigDict, + Field, + SecretStr, +) from requests.exceptions import HTTPError from tenacity import ( before_sleep_log, @@ -348,7 +349,7 @@ class ChatTongyi(BaseChatModel): Tool calling: .. code-block:: python - from langchain_core.pydantic_v1 import BaseModel, Field + from pydantic import BaseModel, Field class GetWeather(BaseModel): @@ -386,7 +387,7 @@ class GetPopulation(BaseModel): from typing import Optional - from langchain_core.pydantic_v1 import BaseModel, Field + from pydantic import BaseModel, Field class Joke(BaseModel): @@ -457,8 +458,9 @@ def lc_secrets(self) -> Dict[str, str]: max_retries: int = 10 """Maximum number of retries to make when generating.""" - class Config: - allow_population_by_field_name = True + model_config = ConfigDict( + populate_by_name=True, + ) @property def _llm_type(self) -> str: diff --git a/libs/community/langchain_community/chat_models/yi.py b/libs/community/langchain_community/chat_models/yi.py index d5da5a0f95672..cb5c8f445743e 100644 --- a/libs/community/langchain_community/chat_models/yi.py +++ b/libs/community/langchain_community/chat_models/yi.py @@ -25,12 +25,12 @@ SystemMessage, ) from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult -from langchain_core.pydantic_v1 import Field, SecretStr from langchain_core.utils import ( convert_to_secret_str, get_from_dict_or_env, get_pydantic_field_names, ) +from pydantic import ConfigDict, Field, SecretStr logger = logging.getLogger(__name__) @@ -115,8 +115,9 @@ def lc_serializable(self) -> bool: top_p: float = 0.7 model_kwargs: Dict[str, Any] = Field(default_factory=dict) - class Config: - allow_population_by_field_name = True + model_config = ConfigDict( + populate_by_name=True, + ) def __init__(self, **kwargs: Any) -> None: kwargs["yi_api_key"] = convert_to_secret_str( diff --git a/libs/community/langchain_community/chat_models/yuan2.py b/libs/community/langchain_community/chat_models/yuan2.py index dc63eb83d246c..9266dadcadc58 100644 --- a/libs/community/langchain_community/chat_models/yuan2.py +++ b/libs/community/langchain_community/chat_models/yuan2.py @@ -40,12 +40,12 @@ SystemMessageChunk, ) from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator from langchain_core.utils import ( get_from_dict_or_env, get_pydantic_field_names, pre_init, ) +from pydantic import BaseModel, ConfigDict, Field, model_validator from tenacity import ( before_sleep_log, retry, @@ -122,8 +122,9 @@ class ChatYuan2(BaseChatModel): repeat_penalty: Optional[float] = 1.18 """The penalty to apply to repeated tokens.""" - class Config: - allow_population_by_field_name = True + model_config = ConfigDict( + populate_by_name=True, + ) @property def lc_secrets(self) -> Dict[str, str]: @@ -141,8 +142,9 @@ def lc_attributes(self) -> Dict[str, Any]: return attributes - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + 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", {}) diff --git a/libs/community/langchain_community/chat_models/zhipuai.py b/libs/community/langchain_community/chat_models/zhipuai.py index 8093e3cc52157..63994b3a6e1b8 100644 --- a/libs/community/langchain_community/chat_models/zhipuai.py +++ b/libs/community/langchain_community/chat_models/zhipuai.py @@ -50,11 +50,11 @@ PydanticToolsParser, ) from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator from langchain_core.runnables import Runnable, RunnableMap, RunnablePassthrough from langchain_core.tools import BaseTool from langchain_core.utils import get_from_dict_or_env from langchain_core.utils.function_calling import convert_to_openai_tool +from pydantic import BaseModel, ConfigDict, Field, model_validator logger = logging.getLogger(__name__) @@ -331,7 +331,7 @@ class ChatZhipuAI(BaseChatModel): Tool calling: .. code-block:: python - from langchain_core.pydantic_v1 import BaseModel, Field + from pydantic import BaseModel, Field class GetWeather(BaseModel): @@ -371,7 +371,7 @@ class GetPopulation(BaseModel): from typing import Optional - from langchain_core.pydantic_v1 import BaseModel, Field + from pydantic import BaseModel, Field class Joke(BaseModel): @@ -480,11 +480,13 @@ def _default_params(self) -> Dict[str, Any]: max_tokens: Optional[int] = None """Maximum number of tokens to generate.""" - class Config: - allow_population_by_field_name = True + model_config = ConfigDict( + populate_by_name=True, + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict[str, Any]) -> Any: values["zhipuai_api_key"] = get_from_dict_or_env( values, ["zhipuai_api_key", "api_key"], "ZHIPUAI_API_KEY" ) @@ -773,7 +775,7 @@ def with_structured_output( .. code-block:: python from langchain_community.chat_models import ChatZhipuAI - from langchain_core.pydantic_v1 import BaseModel + from pydantic import BaseModel class AnswerWithJustification(BaseModel): '''An answer to the user question along with justification for the answer.''' @@ -793,7 +795,7 @@ class AnswerWithJustification(BaseModel): .. code-block:: python from langchain_community.chat_models import ChatZhipuAI - from langchain_core.pydantic_v1 import BaseModel + from pydantic import BaseModel class AnswerWithJustification(BaseModel): '''An answer to the user question along with justification for the answer.''' @@ -814,7 +816,7 @@ class AnswerWithJustification(BaseModel): .. code-block:: python from langchain_community.chat_models import ChatZhipuAI - from langchain_core.pydantic_v1 import BaseModel + from pydantic import BaseModel from langchain_core.utils.function_calling import convert_to_openai_tool class AnswerWithJustification(BaseModel): diff --git a/libs/community/langchain_community/cross_encoders/fake.py b/libs/community/langchain_community/cross_encoders/fake.py index 91e1f702d8c8a..5c38175e50f4e 100644 --- a/libs/community/langchain_community/cross_encoders/fake.py +++ b/libs/community/langchain_community/cross_encoders/fake.py @@ -1,7 +1,7 @@ from difflib import SequenceMatcher from typing import List, Tuple -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel from langchain_community.cross_encoders.base import BaseCrossEncoder diff --git a/libs/community/langchain_community/cross_encoders/huggingface.py b/libs/community/langchain_community/cross_encoders/huggingface.py index 8d51d47cf940b..fa10f228d4cd1 100644 --- a/libs/community/langchain_community/cross_encoders/huggingface.py +++ b/libs/community/langchain_community/cross_encoders/huggingface.py @@ -1,6 +1,6 @@ from typing import Any, Dict, List, Tuple -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, ConfigDict, Field from langchain_community.cross_encoders.base import BaseCrossEncoder @@ -45,8 +45,9 @@ def __init__(self, **kwargs: Any): self.model_name, **self.model_kwargs ) - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def score(self, text_pairs: List[Tuple[str, str]]) -> List[float]: """Compute similarity scores using a HuggingFace transformer model. diff --git a/libs/community/langchain_community/cross_encoders/sagemaker_endpoint.py b/libs/community/langchain_community/cross_encoders/sagemaker_endpoint.py index 8da86cf6ddfdb..b809095532d30 100644 --- a/libs/community/langchain_community/cross_encoders/sagemaker_endpoint.py +++ b/libs/community/langchain_community/cross_encoders/sagemaker_endpoint.py @@ -1,7 +1,7 @@ import json from typing import Any, Dict, List, Optional, Tuple -from langchain_core.pydantic_v1 import BaseModel, root_validator +from pydantic import BaseModel, ConfigDict, model_validator from langchain_community.cross_encoders.base import BaseCrossEncoder @@ -89,12 +89,14 @@ class SagemakerEndpointCrossEncoder(BaseModel, BaseCrossEncoder): .. _boto3: """ - class Config: - arbitrary_types_allowed = True - extra = "forbid" + model_config = ConfigDict( + arbitrary_types_allowed=True, + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that AWS credentials to and python package exists in environment.""" try: import boto3 diff --git a/libs/community/langchain_community/document_compressors/dashscope_rerank.py b/libs/community/langchain_community/document_compressors/dashscope_rerank.py index 702d0b3e8bad3..6e298c2c30989 100644 --- a/libs/community/langchain_community/document_compressors/dashscope_rerank.py +++ b/libs/community/langchain_community/document_compressors/dashscope_rerank.py @@ -5,8 +5,8 @@ from langchain_core.callbacks.base import Callbacks from langchain_core.documents import BaseDocumentCompressor, Document -from langchain_core.pydantic_v1 import Field, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import ConfigDict, Field, model_validator class DashScopeRerank(BaseDocumentCompressor): @@ -25,13 +25,15 @@ class DashScopeRerank(BaseDocumentCompressor): """DashScope API key. Must be specified directly or via environment variable DASHSCOPE_API_KEY.""" - class Config: - allow_population_by_field_name = True - arbitrary_types_allowed = True - extra = "forbid" + model_config = ConfigDict( + populate_by_name=True, + arbitrary_types_allowed=True, + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" if not values.get("client"): diff --git a/libs/community/langchain_community/document_compressors/flashrank_rerank.py b/libs/community/langchain_community/document_compressors/flashrank_rerank.py index c21e17f8e639b..3b8e116fd72ac 100644 --- a/libs/community/langchain_community/document_compressors/flashrank_rerank.py +++ b/libs/community/langchain_community/document_compressors/flashrank_rerank.py @@ -1,10 +1,10 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Dict, Optional, Sequence +from typing import TYPE_CHECKING, Any, Dict, Optional, Sequence from langchain_core.callbacks.manager import Callbacks from langchain_core.documents import BaseDocumentCompressor, Document -from langchain_core.pydantic_v1 import root_validator +from pydantic import ConfigDict, model_validator if TYPE_CHECKING: from flashrank import Ranker, RerankRequest @@ -33,12 +33,14 @@ class FlashrankRerank(BaseDocumentCompressor): prefix_metadata: str = "" """Prefix for flashrank_rerank metadata keys""" - class Config: - arbitrary_types_allowed = True - extra = "forbid" + model_config = ConfigDict( + arbitrary_types_allowed=True, + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" if "client" in values: return values diff --git a/libs/community/langchain_community/document_compressors/jina_rerank.py b/libs/community/langchain_community/document_compressors/jina_rerank.py index 60922334e872f..0a769b311e54e 100644 --- a/libs/community/langchain_community/document_compressors/jina_rerank.py +++ b/libs/community/langchain_community/document_compressors/jina_rerank.py @@ -6,8 +6,8 @@ import requests from langchain_core.callbacks import Callbacks from langchain_core.documents import BaseDocumentCompressor, Document -from langchain_core.pydantic_v1 import root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import ConfigDict, model_validator JINA_API_URL: str = "https://api.jina.ai/v1/rerank" @@ -27,12 +27,14 @@ class JinaRerank(BaseDocumentCompressor): user_agent: str = "langchain" """Identifier for the application making the request.""" - class Config: - arbitrary_types_allowed = True - extra = "forbid" + model_config = ConfigDict( + arbitrary_types_allowed=True, + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key exists in environment.""" jina_api_key = get_from_dict_or_env(values, "jina_api_key", "JINA_API_KEY") user_agent = values.get("user_agent", "langchain") diff --git a/libs/community/langchain_community/document_compressors/llmlingua_filter.py b/libs/community/langchain_community/document_compressors/llmlingua_filter.py index dae183922462f..0c88a045180bb 100644 --- a/libs/community/langchain_community/document_compressors/llmlingua_filter.py +++ b/libs/community/langchain_community/document_compressors/llmlingua_filter.py @@ -8,7 +8,7 @@ from langchain_core.documents.compressor import ( BaseDocumentCompressor, ) -from langchain_core.pydantic_v1 import root_validator +from pydantic import ConfigDict, Field, model_validator DEFAULT_LLM_LINGUA_INSTRUCTION = ( "Given this documents, please answer the final question" @@ -35,9 +35,9 @@ class LLMLinguaCompressor(BaseDocumentCompressor): """The target number of compressed tokens""" rank_method: str = "longllmlingua" """The ranking method to use""" - model_config: dict = {} + model_configuration: dict = Field(default_factory=dict, alias="model_config") """Custom configuration for the model""" - open_api_config: dict = {} + open_api_config: dict = Field(default_factory=dict) """open_api configuration""" instruction: str = DEFAULT_LLM_LINGUA_INSTRUCTION """The instruction for the LLM""" @@ -52,8 +52,9 @@ class LLMLinguaCompressor(BaseDocumentCompressor): lingua: Any """The instance of the llm linqua""" - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that the python package exists in environment.""" try: from llmlingua import PromptCompressor @@ -71,9 +72,11 @@ def validate_environment(cls, values: Dict) -> Dict: ) return values - class Config: - arbitrary_types_allowed = True - extra = "forbid" + model_config = ConfigDict( + arbitrary_types_allowed=True, + extra="forbid", + populate_by_name=True, + ) @staticmethod def _format_context(docs: Sequence[Document]) -> List[str]: diff --git a/libs/community/langchain_community/document_compressors/openvino_rerank.py b/libs/community/langchain_community/document_compressors/openvino_rerank.py index 24fb92673010d..9257a88f1890c 100644 --- a/libs/community/langchain_community/document_compressors/openvino_rerank.py +++ b/libs/community/langchain_community/document_compressors/openvino_rerank.py @@ -5,7 +5,7 @@ from langchain_core.callbacks import Callbacks from langchain_core.documents import Document from langchain_core.documents.compressor import BaseDocumentCompressor -from langchain_core.pydantic_v1 import Field +from pydantic import Field class RerankRequest: diff --git a/libs/community/langchain_community/document_compressors/rankllm_rerank.py b/libs/community/langchain_community/document_compressors/rankllm_rerank.py index 7e461ea878468..bcf18652928e2 100644 --- a/libs/community/langchain_community/document_compressors/rankllm_rerank.py +++ b/libs/community/langchain_community/document_compressors/rankllm_rerank.py @@ -7,8 +7,8 @@ from langchain.retrievers.document_compressors.base import BaseDocumentCompressor from langchain_core.callbacks.manager import Callbacks from langchain_core.documents import Document -from langchain_core.pydantic_v1 import Field, PrivateAttr, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import ConfigDict, Field, PrivateAttr, model_validator if TYPE_CHECKING: from rank_llm.data import Candidate, Query, Request @@ -36,12 +36,14 @@ class RankLLMRerank(BaseDocumentCompressor): """OpenAI model name.""" _retriever: Any = PrivateAttr() - class Config: - arbitrary_types_allowed = True - extra = "forbid" + model_config = ConfigDict( + arbitrary_types_allowed=True, + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate python package exists in environment.""" if not values.get("client"): diff --git a/libs/community/langchain_community/document_compressors/volcengine_rerank.py b/libs/community/langchain_community/document_compressors/volcengine_rerank.py index 9e516f7391575..e7b0cab2c1a8c 100644 --- a/libs/community/langchain_community/document_compressors/volcengine_rerank.py +++ b/libs/community/langchain_community/document_compressors/volcengine_rerank.py @@ -5,8 +5,8 @@ from langchain_core.callbacks.base import Callbacks from langchain_core.documents import BaseDocumentCompressor, Document -from langchain_core.pydantic_v1 import root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import ConfigDict, model_validator class VolcengineRerank(BaseDocumentCompressor): @@ -32,13 +32,15 @@ class VolcengineRerank(BaseDocumentCompressor): top_n: Optional[int] = 3 """Number of documents to return.""" - class Config: - allow_population_by_field_name = True - arbitrary_types_allowed = True - extra = "forbid" + model_config = ConfigDict( + populate_by_name=True, + arbitrary_types_allowed=True, + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" if not values.get("client"): diff --git a/libs/community/langchain_community/document_loaders/apify_dataset.py b/libs/community/langchain_community/document_loaders/apify_dataset.py index 339a25c1ede55..7148f1084c914 100644 --- a/libs/community/langchain_community/document_loaders/apify_dataset.py +++ b/libs/community/langchain_community/document_loaders/apify_dataset.py @@ -1,7 +1,7 @@ from typing import Any, Callable, Dict, List from langchain_core.documents import Document -from langchain_core.pydantic_v1 import BaseModel, root_validator +from pydantic import BaseModel, model_validator from langchain_community.document_loaders.base import BaseLoader @@ -49,8 +49,9 @@ def __init__( dataset_id=dataset_id, dataset_mapping_function=dataset_mapping_function ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate environment. Args: diff --git a/libs/community/langchain_community/document_loaders/base_o365.py b/libs/community/langchain_community/document_loaders/base_o365.py index 98c1188cf1760..86a17d5e0ec02 100644 --- a/libs/community/langchain_community/document_loaders/base_o365.py +++ b/libs/community/langchain_community/document_loaders/base_o365.py @@ -10,13 +10,13 @@ from pathlib import Path, PurePath from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Sequence, Union -from langchain_core.pydantic_v1 import ( +from pydantic import ( BaseModel, - BaseSettings, Field, FilePath, SecretStr, ) +from pydantic_settings import BaseSettings, SettingsConfigDict from langchain_community.document_loaders.base import BaseLoader from langchain_community.document_loaders.blob_loaders.file_system import ( @@ -34,13 +34,12 @@ class _O365Settings(BaseSettings): - client_id: str = Field(..., env="O365_CLIENT_ID") - client_secret: SecretStr = Field(..., env="O365_CLIENT_SECRET") + client_id: str = Field(..., alias="O365_CLIENT_ID") + client_secret: SecretStr = Field(..., alias="O365_CLIENT_SECRET") - class Config: - case_sentive = False - env_file = ".env" - env_prefix = "" + model_config = SettingsConfigDict( + case_sensitive=False, env_file=".env", env_prefix="" + ) class _O365TokenStorage(BaseSettings): diff --git a/libs/community/langchain_community/document_loaders/docugami.py b/libs/community/langchain_community/document_loaders/docugami.py index c3463a009bbb2..38c078351ba90 100644 --- a/libs/community/langchain_community/document_loaders/docugami.py +++ b/libs/community/langchain_community/document_loaders/docugami.py @@ -8,7 +8,7 @@ import requests from langchain_core._api.deprecation import deprecated from langchain_core.documents import Document -from langchain_core.pydantic_v1 import BaseModel, root_validator +from pydantic import BaseModel, model_validator from langchain_community.document_loaders.base import BaseLoader @@ -69,10 +69,10 @@ class DocugamiLoader(BaseLoader, BaseModel): """Set to False if you want to full whitespace formatting in the original XML doc, including indentation.""" - docset_id: Optional[str] + docset_id: Optional[str] = None """The Docugami API docset ID to use.""" - document_ids: Optional[Sequence[str]] + document_ids: Optional[Sequence[str]] = None """The Docugami API document IDs to use.""" file_paths: Optional[Sequence[Union[Path, str]]] @@ -81,8 +81,9 @@ class DocugamiLoader(BaseLoader, BaseModel): include_project_metadata_in_doc_metadata: bool = True """Set to True if you want to include the project metadata in the doc metadata.""" - @root_validator(pre=True) - def validate_local_or_remote(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def validate_local_or_remote(cls, values: Dict[str, Any]) -> Any: """Validate that either local file paths are given, or remote API docset ID. Args: diff --git a/libs/community/langchain_community/document_loaders/dropbox.py b/libs/community/langchain_community/document_loaders/dropbox.py index f2a7b603cefc3..c689e3a455633 100644 --- a/libs/community/langchain_community/document_loaders/dropbox.py +++ b/libs/community/langchain_community/document_loaders/dropbox.py @@ -12,7 +12,7 @@ from typing import Any, Dict, List, Optional from langchain_core.documents import Document -from langchain_core.pydantic_v1 import BaseModel, root_validator +from pydantic import BaseModel, model_validator from langchain_community.document_loaders.base import BaseLoader @@ -33,8 +33,9 @@ class DropboxLoader(BaseLoader, BaseModel): recursive: bool = False """Flag to indicate whether to load files recursively from subfolders.""" - @root_validator(pre=True) - def validate_inputs(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def validate_inputs(cls, values: Dict[str, Any]) -> Any: """Validate that either folder_path or file_paths is set, but not both.""" if ( values.get("dropbox_folder_path") is not None diff --git a/libs/community/langchain_community/document_loaders/github.py b/libs/community/langchain_community/document_loaders/github.py index df02a7894fbad..a92d854b7ea21 100644 --- a/libs/community/langchain_community/document_loaders/github.py +++ b/libs/community/langchain_community/document_loaders/github.py @@ -1,12 +1,12 @@ import base64 from abc import ABC from datetime import datetime -from typing import Callable, Dict, Iterator, List, Literal, Optional, Union +from typing import Any, Callable, Dict, Iterator, List, Literal, Optional, Union import requests from langchain_core.documents import Document -from langchain_core.pydantic_v1 import BaseModel, root_validator, validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, model_validator, validator from langchain_community.document_loaders.base import BaseLoader @@ -21,8 +21,9 @@ class BaseGitHubLoader(BaseLoader, BaseModel, ABC): github_api_url: str = "https://api.github.com" """URL of GitHub API""" - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that access token exists in environment.""" values["access_token"] = get_from_dict_or_env( values, "access_token", "GITHUB_PERSONAL_ACCESS_TOKEN" diff --git a/libs/community/langchain_community/document_loaders/googledrive.py b/libs/community/langchain_community/document_loaders/googledrive.py index 5646950a79064..ea525c10f17e6 100644 --- a/libs/community/langchain_community/document_loaders/googledrive.py +++ b/libs/community/langchain_community/document_loaders/googledrive.py @@ -12,7 +12,7 @@ from langchain_core._api.deprecation import deprecated from langchain_core.documents import Document -from langchain_core.pydantic_v1 import BaseModel, root_validator, validator +from pydantic import BaseModel, model_validator, validator from langchain_community.document_loaders.base import BaseLoader @@ -52,8 +52,9 @@ class GoogleDriveLoader(BaseLoader, BaseModel): file_loader_kwargs: Dict["str", Any] = {} """The file loader kwargs to use.""" - @root_validator(pre=True) - def validate_inputs(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def validate_inputs(cls, values: Dict[str, Any]) -> Any: """Validate that either folder_id or document_ids is set, but not both.""" if values.get("folder_id") and ( values.get("document_ids") or values.get("file_ids") diff --git a/libs/community/langchain_community/document_loaders/onedrive.py b/libs/community/langchain_community/document_loaders/onedrive.py index d33777dd6a081..ecc1d232bfe4c 100644 --- a/libs/community/langchain_community/document_loaders/onedrive.py +++ b/libs/community/langchain_community/document_loaders/onedrive.py @@ -6,7 +6,7 @@ from typing import TYPE_CHECKING, Iterator, List, Optional, Sequence, Union from langchain_core.documents import Document -from langchain_core.pydantic_v1 import Field +from pydantic import Field from langchain_community.document_loaders.base_o365 import ( O365BaseLoader, diff --git a/libs/community/langchain_community/document_loaders/onedrive_file.py b/libs/community/langchain_community/document_loaders/onedrive_file.py index fcb0c7c58f4de..d92cfdea91777 100644 --- a/libs/community/langchain_community/document_loaders/onedrive_file.py +++ b/libs/community/langchain_community/document_loaders/onedrive_file.py @@ -4,7 +4,7 @@ from typing import TYPE_CHECKING, List from langchain_core.documents import Document -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, ConfigDict, Field from langchain_community.document_loaders.base import BaseLoader from langchain_community.document_loaders.unstructured import UnstructuredFileLoader @@ -21,8 +21,9 @@ class OneDriveFileLoader(BaseLoader, BaseModel): file: File = Field(...) """The file to load.""" - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def load(self) -> List[Document]: """Load Documents""" diff --git a/libs/community/langchain_community/document_loaders/onenote.py b/libs/community/langchain_community/document_loaders/onenote.py index 13ba4a831bd71..c58640e0846e8 100644 --- a/libs/community/langchain_community/document_loaders/onenote.py +++ b/libs/community/langchain_community/document_loaders/onenote.py @@ -1,29 +1,32 @@ """Loads data from OneNote Notebooks""" from pathlib import Path -from typing import Dict, Iterator, List, Optional +from typing import Any, Dict, Iterator, List, Optional import requests from langchain_core.documents import Document -from langchain_core.pydantic_v1 import ( +from pydantic import ( BaseModel, - BaseSettings, Field, FilePath, SecretStr, + model_validator, ) +from pydantic_settings import BaseSettings, SettingsConfigDict from langchain_community.document_loaders.base import BaseLoader class _OneNoteGraphSettings(BaseSettings): - client_id: str = Field(..., env="MS_GRAPH_CLIENT_ID") - client_secret: SecretStr = Field(..., env="MS_GRAPH_CLIENT_SECRET") + client_id: str = Field(...) + client_secret: SecretStr = Field(...) - class Config: - case_sentive = False - env_file = ".env" - env_prefix = "" + model_config = SettingsConfigDict( + case_sensitive=False, + populate_by_name=True, + env_file=".env", + env_prefix="MS_GRAPH_", + ) class OneNoteLoader(BaseLoader, BaseModel): @@ -50,6 +53,14 @@ class OneNoteLoader(BaseLoader, BaseModel): object_ids: Optional[List[str]] = None """ The IDs of the objects to load data from.""" + @model_validator(mode="before") + @classmethod + def init(cls, values: Dict) -> Any: + """Initialize the class.""" + if "settings" in values and isinstance(values["settings"], dict): + values["settings"] = _OneNoteGraphSettings(**values["settings"]) + return values + def lazy_load(self) -> Iterator[Document]: """ Get pages from OneNote notebooks. diff --git a/libs/community/langchain_community/document_loaders/sharepoint.py b/libs/community/langchain_community/document_loaders/sharepoint.py index e589a58447c4d..06426a7038fdd 100644 --- a/libs/community/langchain_community/document_loaders/sharepoint.py +++ b/libs/community/langchain_community/document_loaders/sharepoint.py @@ -9,7 +9,7 @@ import requests # type: ignore from langchain_core.document_loaders import BaseLoader from langchain_core.documents import Document -from langchain_core.pydantic_v1 import Field +from pydantic import Field from langchain_community.document_loaders.base_o365 import ( O365BaseLoader, diff --git a/libs/community/langchain_community/document_loaders/youtube.py b/libs/community/langchain_community/document_loaders/youtube.py index bfe1cb0b1391e..67c1569adf8d6 100644 --- a/libs/community/langchain_community/document_loaders/youtube.py +++ b/libs/community/langchain_community/document_loaders/youtube.py @@ -10,8 +10,8 @@ from xml.etree.ElementTree import ParseError # OK: trusted-source from langchain_core.documents import Document -from langchain_core.pydantic_v1 import root_validator -from langchain_core.pydantic_v1.dataclasses import dataclass +from pydantic import model_validator +from pydantic.dataclasses import dataclass from langchain_community.document_loaders.base import BaseLoader @@ -50,10 +50,9 @@ class GoogleApiClient: def __post_init__(self) -> None: self.creds = self._load_credentials() - @root_validator(pre=True) - def validate_channel_or_videoIds_is_set( - cls, values: Dict[str, Any] - ) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def validate_channel_or_videoIds_is_set(cls, values: Dict[str, Any]) -> Any: """Validate that either folder_id or document_ids is set, but not both.""" if not values.get("credentials_path") and not values.get( @@ -391,10 +390,9 @@ def _build_youtube_client(self, creds: Any) -> Any: return build("youtube", "v3", credentials=creds) - @root_validator(pre=True) - def validate_channel_or_videoIds_is_set( - cls, values: Dict[str, Any] - ) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def validate_channel_or_videoIds_is_set(cls, values: Dict[str, Any]) -> Any: """Validate that either folder_id or document_ids is set, but not both.""" if not values.get("channel_name") and not values.get("video_ids"): raise ValueError("Must specify either channel_name or video_ids") diff --git a/libs/community/langchain_community/document_transformers/embeddings_redundant_filter.py b/libs/community/langchain_community/document_transformers/embeddings_redundant_filter.py index 1675ad331730a..f46c55ae3656d 100644 --- a/libs/community/langchain_community/document_transformers/embeddings_redundant_filter.py +++ b/libs/community/langchain_community/document_transformers/embeddings_redundant_filter.py @@ -5,7 +5,7 @@ import numpy as np from langchain_core.documents import BaseDocumentTransformer, Document from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, ConfigDict, Field from langchain_community.utils.math import cosine_similarity @@ -153,8 +153,9 @@ class EmbeddingsRedundantFilter(BaseDocumentTransformer, BaseModel): """Threshold for determining when two documents are similar enough to be considered redundant.""" - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def transform_documents( self, documents: Sequence[Document], **kwargs: Any @@ -201,8 +202,9 @@ class EmbeddingsClusteringFilter(BaseDocumentTransformer, BaseModel): clusters. """ - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def transform_documents( self, documents: Sequence[Document], **kwargs: Any diff --git a/libs/community/langchain_community/document_transformers/long_context_reorder.py b/libs/community/langchain_community/document_transformers/long_context_reorder.py index 729138fa46762..2884b63f098cb 100644 --- a/libs/community/langchain_community/document_transformers/long_context_reorder.py +++ b/libs/community/langchain_community/document_transformers/long_context_reorder.py @@ -3,7 +3,7 @@ from typing import Any, List, Sequence from langchain_core.documents import BaseDocumentTransformer, Document -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel, ConfigDict def _litm_reordering(documents: List[Document]) -> List[Document]: @@ -29,8 +29,9 @@ class LongContextReorder(BaseDocumentTransformer, BaseModel): in the middle of long contexts. See: https://arxiv.org/abs//2307.03172""" - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def transform_documents( self, documents: Sequence[Document], **kwargs: Any diff --git a/libs/community/langchain_community/document_transformers/openai_functions.py b/libs/community/langchain_community/document_transformers/openai_functions.py index 18415ab242271..7ba087a3e1315 100644 --- a/libs/community/langchain_community/document_transformers/openai_functions.py +++ b/libs/community/langchain_community/document_transformers/openai_functions.py @@ -5,7 +5,7 @@ from langchain_core.documents import BaseDocumentTransformer, Document from langchain_core.language_models import BaseLanguageModel from langchain_core.prompts import ChatPromptTemplate -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel class OpenAIMetadataTagger(BaseDocumentTransformer, BaseModel): diff --git a/libs/community/langchain_community/embeddings/aleph_alpha.py b/libs/community/langchain_community/embeddings/aleph_alpha.py index 409373d8bff8e..96426fdac8a91 100644 --- a/libs/community/langchain_community/embeddings/aleph_alpha.py +++ b/libs/community/langchain_community/embeddings/aleph_alpha.py @@ -1,8 +1,8 @@ from typing import Any, Dict, List, Optional from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, model_validator class AlephAlphaAsymmetricSemanticEmbedding(BaseModel, Embeddings): @@ -80,8 +80,9 @@ class AlephAlphaAsymmetricSemanticEmbedding(BaseModel, Embeddings): nice to other users by de-prioritizing your request below concurrent ones.""" - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" aleph_alpha_api_key = get_from_dict_or_env( values, "aleph_alpha_api_key", "ALEPH_ALPHA_API_KEY" diff --git a/libs/community/langchain_community/embeddings/anyscale.py b/libs/community/langchain_community/embeddings/anyscale.py index 831946a4da5a5..76ba0132a1ec6 100644 --- a/libs/community/langchain_community/embeddings/anyscale.py +++ b/libs/community/langchain_community/embeddings/anyscale.py @@ -4,8 +4,8 @@ from typing import Dict -from langchain_core.pydantic_v1 import Field, SecretStr from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import Field, SecretStr from langchain_community.embeddings.openai import OpenAIEmbeddings from langchain_community.utils.openai import is_openai_v1 diff --git a/libs/community/langchain_community/embeddings/ascend.py b/libs/community/langchain_community/embeddings/ascend.py index 2b94f4ff668ff..dd8c9c67fd94c 100644 --- a/libs/community/langchain_community/embeddings/ascend.py +++ b/libs/community/langchain_community/embeddings/ascend.py @@ -2,7 +2,7 @@ from typing import Any, Dict, List, Optional from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, root_validator +from pydantic import BaseModel, model_validator class AscendEmbeddings(Embeddings, BaseModel): @@ -54,8 +54,9 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: self.model.half() self.encode([f"warmup {i} times" for i in range(10)]) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: if "model_path" not in values: raise ValueError("model_path is required") if not os.access(values["model_path"], os.F_OK): diff --git a/libs/community/langchain_community/embeddings/awa.py b/libs/community/langchain_community/embeddings/awa.py index 6f09b2d064c04..27cb422423fde 100644 --- a/libs/community/langchain_community/embeddings/awa.py +++ b/libs/community/langchain_community/embeddings/awa.py @@ -1,7 +1,7 @@ from typing import Any, Dict, List from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, root_validator +from pydantic import BaseModel, model_validator class AwaEmbeddings(BaseModel, Embeddings): @@ -16,8 +16,9 @@ class AwaEmbeddings(BaseModel, Embeddings): client: Any #: :meta private: model: str = "all-mpnet-base-v2" - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that awadb library is installed.""" try: diff --git a/libs/community/langchain_community/embeddings/azure_openai.py b/libs/community/langchain_community/embeddings/azure_openai.py index cf06a795d0377..f760d796feb93 100644 --- a/libs/community/langchain_community/embeddings/azure_openai.py +++ b/libs/community/langchain_community/embeddings/azure_openai.py @@ -4,11 +4,12 @@ import os import warnings -from typing import Callable, Dict, Optional, Union +from typing import Any, Callable, Dict, Optional, Union from langchain_core._api.deprecation import deprecated -from langchain_core.pydantic_v1 import Field, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import Field, model_validator +from typing_extensions import Self from langchain_community.embeddings.openai import OpenAIEmbeddings from langchain_community.utils.openai import is_openai_v1 @@ -54,8 +55,9 @@ class AzureOpenAIEmbeddings(OpenAIEmbeddings): """Automatically inferred from env var `OPENAI_API_VERSION` if not provided.""" validate_base_url: bool = True - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" # Check OPENAI_KEY for backwards compatibility. # TODO: Remove OPENAI_API_KEY support to avoid possible conflict when using @@ -138,32 +140,32 @@ def validate_environment(cls, values: Dict) -> Dict: values["deployment"] = None return values - @root_validator(pre=False, skip_on_failure=True) - def post_init_validator(cls, values: Dict) -> Dict: + @model_validator(mode="after") + def post_init_validator(self) -> Self: """Validate that the base url is set.""" import openai if is_openai_v1(): client_params = { - "api_version": values["openai_api_version"], - "azure_endpoint": values["azure_endpoint"], - "azure_deployment": values["deployment"], - "api_key": values["openai_api_key"], - "azure_ad_token": values["azure_ad_token"], - "azure_ad_token_provider": values["azure_ad_token_provider"], - "organization": values["openai_organization"], - "base_url": values["openai_api_base"], - "timeout": values["request_timeout"], - "max_retries": values["max_retries"], - "default_headers": values["default_headers"], - "default_query": values["default_query"], - "http_client": values["http_client"], + "api_version": self.openai_api_version, + "azure_endpoint": self.azure_endpoint, + "azure_deployment": self.deployment, + "api_key": self.openai_api_key, + "azure_ad_token": self.azure_ad_token, + "azure_ad_token_provider": self.azure_ad_token_provider, + "organization": self.openai_organization, + "base_url": self.openai_api_base, + "timeout": self.request_timeout, + "max_retries": self.max_retries, + "default_headers": self.default_headers, + "default_query": self.default_query, + "http_client": self.http_client, } - values["client"] = openai.AzureOpenAI(**client_params).embeddings - values["async_client"] = openai.AsyncAzureOpenAI(**client_params).embeddings + self.client = openai.AzureOpenAI(**client_params).embeddings + self.async_client = openai.AsyncAzureOpenAI(**client_params).embeddings else: - values["client"] = openai.Embedding - return values + self.client = openai.Embedding + return self @property def _llm_type(self) -> str: diff --git a/libs/community/langchain_community/embeddings/baichuan.py b/libs/community/langchain_community/embeddings/baichuan.py index fe81993000208..68fc6d8d954d8 100644 --- a/libs/community/langchain_community/embeddings/baichuan.py +++ b/libs/community/langchain_community/embeddings/baichuan.py @@ -1,14 +1,19 @@ -from typing import Any, Dict, List, Optional +from typing import Any, List, Optional import requests from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, Field, SecretStr, root_validator from langchain_core.utils import ( - convert_to_secret_str, - get_from_dict_or_env, secret_from_env, ) +from pydantic import ( + BaseModel, + ConfigDict, + Field, + SecretStr, + model_validator, +) from requests import RequestException +from typing_extensions import Self BAICHUAN_API_URL: str = "http://api.baichuan-ai.com/v1/embeddings" @@ -57,42 +62,31 @@ class BaichuanTextEmbeddings(BaseModel, Embeddings): session: Any #: :meta private: model_name: str = Field(default="Baichuan-Text-Embedding", alias="model") """The model used to embed the documents.""" - baichuan_api_key: Optional[SecretStr] = Field( + baichuan_api_key: SecretStr = Field( alias="api_key", - default_factory=secret_from_env("BAICHUAN_API_KEY", default=None), + default_factory=secret_from_env(["BAICHUAN_API_KEY", "BAICHUAN_AUTH_TOKEN"]), ) """Automatically inferred from env var `BAICHUAN_API_KEY` if not provided.""" chunk_size: int = 16 """Chunk size when multiple texts are input""" - class Config: - allow_population_by_field_name = True + model_config = ConfigDict( + populate_by_name=True, + ) - @root_validator(pre=False, skip_on_failure=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="after") + def validate_environment(self) -> Self: """Validate that auth token exists in environment.""" - if values["baichuan_api_key"] is None: - # This is likely here for some backwards compatibility with - # BAICHUAN_AUTH_TOKEN - baichuan_api_key = convert_to_secret_str( - get_from_dict_or_env( - values, "baichuan_auth_token", "BAICHUAN_AUTH_TOKEN" - ) - ) - values["baichuan_api_key"] = baichuan_api_key - else: - baichuan_api_key = values["baichuan_api_key"] - session = requests.Session() session.headers.update( { - "Authorization": f"Bearer {baichuan_api_key.get_secret_value()}", + "Authorization": f"Bearer {self.baichuan_api_key.get_secret_value()}", "Accept-Encoding": "identity", "Content-type": "application/json", } ) - values["session"] = session - return values + self.session = session + return self def _embed(self, texts: List[str]) -> Optional[List[List[float]]]: """Internal method to call Baichuan Embedding API and return embeddings. diff --git a/libs/community/langchain_community/embeddings/baidu_qianfan_endpoint.py b/libs/community/langchain_community/embeddings/baidu_qianfan_endpoint.py index 6aa9df92364a5..981378eb19f3c 100644 --- a/libs/community/langchain_community/embeddings/baidu_qianfan_endpoint.py +++ b/libs/community/langchain_community/embeddings/baidu_qianfan_endpoint.py @@ -4,8 +4,8 @@ from typing import Any, Dict, List, Optional from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, Field, SecretStr from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import BaseModel, Field, SecretStr logger = logging.getLogger(__name__) diff --git a/libs/community/langchain_community/embeddings/bedrock.py b/libs/community/langchain_community/embeddings/bedrock.py index 70d4f41191449..671073e743fd4 100644 --- a/libs/community/langchain_community/embeddings/bedrock.py +++ b/libs/community/langchain_community/embeddings/bedrock.py @@ -6,8 +6,9 @@ import numpy as np from langchain_core._api.deprecation import deprecated from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.runnables.config import run_in_executor +from pydantic import BaseModel, ConfigDict, model_validator +from typing_extensions import Self @deprecated( @@ -74,33 +75,34 @@ class BedrockEmbeddings(BaseModel, Embeddings): normalize: bool = False """Whether the embeddings should be normalized to unit vectors""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=False, skip_on_failure=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="after") + def validate_environment(self) -> Self: """Validate that AWS credentials to and python package exists in environment.""" - if values["client"] is not None: - return values + if self.client is not None: + return self try: import boto3 - if values["credentials_profile_name"] is not None: - session = boto3.Session(profile_name=values["credentials_profile_name"]) + if self.credentials_profile_name is not None: + session = boto3.Session(profile_name=self.credentials_profile_name) else: # use default credentials session = boto3.Session() client_params = {} - if values["region_name"]: - client_params["region_name"] = values["region_name"] + if self.region_name: + client_params["region_name"] = self.region_name - if values["endpoint_url"]: - client_params["endpoint_url"] = values["endpoint_url"] + if self.endpoint_url: + client_params["endpoint_url"] = self.endpoint_url - values["client"] = session.client("bedrock-runtime", **client_params) + self.client = session.client("bedrock-runtime", **client_params) except ImportError: raise ImportError( @@ -114,7 +116,7 @@ def validate_environment(cls, values: Dict) -> Dict: f"profile name are valid. Bedrock error: {e}" ) from e - return values + return self def _embedding_func(self, text: str) -> List[float]: """Call out to Bedrock embedding endpoint.""" diff --git a/libs/community/langchain_community/embeddings/bookend.py b/libs/community/langchain_community/embeddings/bookend.py index f8388712d7cf4..c5390e19e71f0 100644 --- a/libs/community/langchain_community/embeddings/bookend.py +++ b/libs/community/langchain_community/embeddings/bookend.py @@ -5,7 +5,7 @@ import requests from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field API_URL = "https://api.bookend.ai/" DEFAULT_TASK = "embeddings" diff --git a/libs/community/langchain_community/embeddings/clarifai.py b/libs/community/langchain_community/embeddings/clarifai.py index a465fc737eee2..4c35fcf75b804 100644 --- a/libs/community/langchain_community/embeddings/clarifai.py +++ b/libs/community/langchain_community/embeddings/clarifai.py @@ -2,7 +2,7 @@ from typing import Any, Dict, List, Optional from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator +from pydantic import BaseModel, ConfigDict, Field, model_validator logger = logging.getLogger(__name__) @@ -43,11 +43,13 @@ class ClarifaiEmbeddings(BaseModel, Embeddings): model: Any = Field(default=None, exclude=True) #: :meta private: api_base: str = "https://api.clarifai.com" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that we have all required info to access Clarifai platform and python package exists in environment.""" diff --git a/libs/community/langchain_community/embeddings/cloudflare_workersai.py b/libs/community/langchain_community/embeddings/cloudflare_workersai.py index 03b6617bf2e62..45fadb39c6885 100644 --- a/libs/community/langchain_community/embeddings/cloudflare_workersai.py +++ b/libs/community/langchain_community/embeddings/cloudflare_workersai.py @@ -2,7 +2,7 @@ import requests from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel, ConfigDict DEFAULT_MODEL_NAME = "@cf/baai/bge-base-en-v1.5" @@ -43,8 +43,9 @@ def __init__(self, **kwargs: Any): self.headers = {"Authorization": f"Bearer {self.api_token}"} - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def embed_documents(self, texts: List[str]) -> List[List[float]]: """Compute doc embeddings using Cloudflare Workers AI. diff --git a/libs/community/langchain_community/embeddings/clova.py b/libs/community/langchain_community/embeddings/clova.py index 1e4cecc0ed2b3..551dc63526192 100644 --- a/libs/community/langchain_community/embeddings/clova.py +++ b/libs/community/langchain_community/embeddings/clova.py @@ -1,11 +1,11 @@ from __future__ import annotations -from typing import Dict, List, Optional, cast +from typing import Any, Dict, List, Optional, cast import requests from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, SecretStr, root_validator from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, SecretStr, model_validator class ClovaEmbeddings(BaseModel, Embeddings): @@ -53,11 +53,13 @@ class ClovaEmbeddings(BaseModel, Embeddings): app_id: Optional[SecretStr] = None """Application ID for identifying your application.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate api key exists in environment.""" values["clova_emb_api_key"] = convert_to_secret_str( get_from_dict_or_env(values, "clova_emb_api_key", "CLOVA_EMB_API_KEY") diff --git a/libs/community/langchain_community/embeddings/cohere.py b/libs/community/langchain_community/embeddings/cohere.py index 8c3ea9e101826..2b4e0efe57bd1 100644 --- a/libs/community/langchain_community/embeddings/cohere.py +++ b/libs/community/langchain_community/embeddings/cohere.py @@ -2,8 +2,8 @@ from langchain_core._api.deprecation import deprecated from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator from langchain_community.llms.cohere import _create_retry_decorator @@ -49,11 +49,13 @@ class CohereEmbeddings(BaseModel, Embeddings): user_agent: str = "langchain" """Identifier for the application making the request.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" cohere_api_key = get_from_dict_or_env( values, "cohere_api_key", "COHERE_API_KEY" diff --git a/libs/community/langchain_community/embeddings/dashscope.py b/libs/community/langchain_community/embeddings/dashscope.py index 33ad8095c5609..e2f69712145fd 100644 --- a/libs/community/langchain_community/embeddings/dashscope.py +++ b/libs/community/langchain_community/embeddings/dashscope.py @@ -10,8 +10,8 @@ ) from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator from requests.exceptions import HTTPError from tenacity import ( before_sleep_log, @@ -108,11 +108,13 @@ class DashScopeEmbeddings(BaseModel, Embeddings): max_retries: int = 5 """Maximum number of retries to make when generating.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: import dashscope """Validate that api key and python package exists in environment.""" diff --git a/libs/community/langchain_community/embeddings/deepinfra.py b/libs/community/langchain_community/embeddings/deepinfra.py index b115b8c00ec83..05f3ed982b40e 100644 --- a/libs/community/langchain_community/embeddings/deepinfra.py +++ b/libs/community/langchain_community/embeddings/deepinfra.py @@ -2,8 +2,8 @@ import requests from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel from langchain_core.utils import get_from_dict_or_env, pre_init +from pydantic import BaseModel, ConfigDict DEFAULT_MODEL_ID = "sentence-transformers/clip-ViT-B-32" MAX_BATCH_SIZE = 1024 @@ -54,8 +54,9 @@ class DeepInfraEmbeddings(BaseModel, Embeddings): batch_size: int = MAX_BATCH_SIZE """Batch size for embedding requests.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/embeddings/edenai.py b/libs/community/langchain_community/embeddings/edenai.py index bddbd8824c55a..097c730ae423a 100644 --- a/libs/community/langchain_community/embeddings/edenai.py +++ b/libs/community/langchain_community/embeddings/edenai.py @@ -1,12 +1,13 @@ from typing import Any, Dict, List, Optional from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import ( +from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import ( BaseModel, + ConfigDict, Field, SecretStr, ) -from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init from langchain_community.utilities.requests import Requests @@ -28,8 +29,9 @@ class EdenAiEmbeddings(BaseModel, Embeddings): available models are shown on https://docs.edenai.co/ under 'available providers' """ - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/embeddings/embaas.py b/libs/community/langchain_community/embeddings/embaas.py index 861cab09280f6..78fd42bf8501d 100644 --- a/libs/community/langchain_community/embeddings/embaas.py +++ b/libs/community/langchain_community/embeddings/embaas.py @@ -2,8 +2,8 @@ import requests from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, SecretStr from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import BaseModel, ConfigDict, SecretStr from requests.adapters import HTTPAdapter, Retry from typing_extensions import NotRequired, TypedDict @@ -56,8 +56,9 @@ class EmbaasEmbeddings(BaseModel, Embeddings): """request timeout in seconds""" timeout: Optional[int] = 30 - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/embeddings/ernie.py b/libs/community/langchain_community/embeddings/ernie.py index c640680f9c8f2..42a49e2c6ca6f 100644 --- a/libs/community/langchain_community/embeddings/ernie.py +++ b/libs/community/langchain_community/embeddings/ernie.py @@ -6,9 +6,9 @@ import requests from langchain_core._api.deprecation import deprecated from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel from langchain_core.runnables.config import run_in_executor from langchain_core.utils import get_from_dict_or_env, pre_init +from pydantic import BaseModel logger = logging.getLogger(__name__) diff --git a/libs/community/langchain_community/embeddings/fake.py b/libs/community/langchain_community/embeddings/fake.py index fcc33496aa863..67dc1d6b6f5b5 100644 --- a/libs/community/langchain_community/embeddings/fake.py +++ b/libs/community/langchain_community/embeddings/fake.py @@ -3,7 +3,7 @@ import numpy as np from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel class FakeEmbeddings(Embeddings, BaseModel): diff --git a/libs/community/langchain_community/embeddings/fastembed.py b/libs/community/langchain_community/embeddings/fastembed.py index 3a0940c866c4f..fe654aa44594f 100644 --- a/libs/community/langchain_community/embeddings/fastembed.py +++ b/libs/community/langchain_community/embeddings/fastembed.py @@ -4,8 +4,8 @@ import numpy as np from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel from langchain_core.utils import pre_init +from pydantic import BaseModel, ConfigDict MIN_VERSION = "0.2.0" @@ -67,8 +67,9 @@ class FastEmbedEmbeddings(BaseModel, Embeddings): _model: Any # : :meta private: - class Config: - extra = "allow" + model_config = ConfigDict( + extra="allow", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/embeddings/gigachat.py b/libs/community/langchain_community/embeddings/gigachat.py index 09ac5b3757851..eea276e2444b1 100644 --- a/libs/community/langchain_community/embeddings/gigachat.py +++ b/libs/community/langchain_community/embeddings/gigachat.py @@ -5,9 +5,9 @@ from typing import Any, Dict, List, Optional from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel from langchain_core.utils import pre_init from langchain_core.utils.pydantic import get_fields +from pydantic import BaseModel logger = logging.getLogger(__name__) diff --git a/libs/community/langchain_community/embeddings/google_palm.py b/libs/community/langchain_community/embeddings/google_palm.py index 2c960c93e84c1..363214caead72 100644 --- a/libs/community/langchain_community/embeddings/google_palm.py +++ b/libs/community/langchain_community/embeddings/google_palm.py @@ -4,8 +4,8 @@ from typing import Any, Callable, Dict, List, Optional from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel from langchain_core.utils import get_from_dict_or_env, pre_init +from pydantic import BaseModel from tenacity import ( before_sleep_log, retry, diff --git a/libs/community/langchain_community/embeddings/gpt4all.py b/libs/community/langchain_community/embeddings/gpt4all.py index 338acc50389ce..ce92ab84b6d23 100644 --- a/libs/community/langchain_community/embeddings/gpt4all.py +++ b/libs/community/langchain_community/embeddings/gpt4all.py @@ -1,7 +1,7 @@ from typing import Any, Dict, List, Optional from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, root_validator +from pydantic import BaseModel, model_validator class GPT4AllEmbeddings(BaseModel, Embeddings): @@ -28,8 +28,9 @@ class GPT4AllEmbeddings(BaseModel, Embeddings): gpt4all_kwargs: Optional[dict] = {} client: Any #: :meta private: - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that GPT4All library is installed.""" try: from gpt4all import Embed4All diff --git a/libs/community/langchain_community/embeddings/gradient_ai.py b/libs/community/langchain_community/embeddings/gradient_ai.py index 6e6f0cedf76a3..f33c80a6eca31 100644 --- a/libs/community/langchain_community/embeddings/gradient_ai.py +++ b/libs/community/langchain_community/embeddings/gradient_ai.py @@ -1,9 +1,10 @@ from typing import Any, Dict, List, Optional from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env from packaging.version import parse +from pydantic import BaseModel, ConfigDict, model_validator +from typing_extensions import Self __all__ = ["GradientEmbeddings"] @@ -50,11 +51,13 @@ class GradientEmbeddings(BaseModel, Embeddings): """Gradient client.""" # LLM call kwargs - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" values["gradient_access_token"] = get_from_dict_or_env( @@ -72,8 +75,8 @@ def validate_environment(cls, values: Dict) -> Dict: ) return values - @root_validator(pre=False, skip_on_failure=True) - def post_init(cls, values: Dict) -> Dict: + @model_validator(mode="after") + def post_init(self) -> Self: try: import gradientai except ImportError: @@ -87,12 +90,12 @@ def post_init(cls, values: Dict) -> Dict: ) gradient = gradientai.Gradient( - access_token=values["gradient_access_token"], - workspace_id=values["gradient_workspace_id"], - host=values["gradient_api_url"], + access_token=self.gradient_access_token, + workspace_id=self.gradient_workspace_id, + host=self.gradient_api_url, ) - values["client"] = gradient.get_embeddings_model(slug=values["model"]) - return values + self.client = gradient.get_embeddings_model(slug=self.model) + return self def embed_documents(self, texts: List[str]) -> List[List[float]]: """Call out to Gradient's embedding endpoint. diff --git a/libs/community/langchain_community/embeddings/huggingface.py b/libs/community/langchain_community/embeddings/huggingface.py index ed188c7e61e3a..f4c2f38f8fdf8 100644 --- a/libs/community/langchain_community/embeddings/huggingface.py +++ b/libs/community/langchain_community/embeddings/huggingface.py @@ -4,7 +4,7 @@ import requests from langchain_core._api import deprecated, warn_deprecated from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, Field, SecretStr +from pydantic import BaseModel, ConfigDict, Field, SecretStr DEFAULT_MODEL_NAME = "sentence-transformers/all-mpnet-base-v2" DEFAULT_INSTRUCT_MODEL = "hkunlp/instructor-large" @@ -93,8 +93,9 @@ def __init__(self, **kwargs: Any): self.model_name, cache_folder=self.cache_folder, **self.model_kwargs ) - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def embed_documents(self, texts: List[str]) -> List[List[float]]: """Compute doc embeddings using a HuggingFace transformer model. @@ -208,8 +209,9 @@ def __init__(self, **kwargs: Any): ) self.show_progress = self.encode_kwargs.pop("show_progress_bar") - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def embed_documents(self, texts: List[str]) -> List[List[float]]: """Compute doc embeddings using a HuggingFace instruct model. @@ -348,8 +350,9 @@ def __init__(self, **kwargs: Any): ) self.show_progress = self.encode_kwargs.pop("show_progress_bar") - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def embed_documents(self, texts: List[str]) -> List[List[float]]: """Compute doc embeddings using a HuggingFace transformer model. diff --git a/libs/community/langchain_community/embeddings/huggingface_hub.py b/libs/community/langchain_community/embeddings/huggingface_hub.py index 6b688913b9b95..cea56ad11c753 100644 --- a/libs/community/langchain_community/embeddings/huggingface_hub.py +++ b/libs/community/langchain_community/embeddings/huggingface_hub.py @@ -3,8 +3,9 @@ from langchain_core._api import deprecated from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator +from typing_extensions import Self DEFAULT_MODEL = "sentence-transformers/all-mpnet-base-v2" VALID_TASKS = ("feature-extraction",) @@ -47,11 +48,13 @@ class HuggingFaceHubEmbeddings(BaseModel, Embeddings): huggingfacehub_api_token: Optional[str] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" huggingfacehub_api_token = get_from_dict_or_env( values, "huggingfacehub_api_token", "HUGGINGFACEHUB_API_TOKEN" @@ -88,15 +91,15 @@ def validate_environment(cls, values: Dict) -> Dict: ) return values - @root_validator(pre=False, skip_on_failure=True) - def post_init(cls, values: Dict) -> Dict: + @model_validator(mode="after") + def post_init(self) -> Self: """Post init validation for the class.""" - if values["task"] not in VALID_TASKS: + if self.task not in VALID_TASKS: raise ValueError( - f"Got invalid task {values['task']}, " + f"Got invalid task {self.task}, " f"currently only {VALID_TASKS} are supported" ) - return values + return self def embed_documents(self, texts: List[str]) -> List[List[float]]: """Call out to HuggingFaceHub's embedding endpoint for embedding search docs. diff --git a/libs/community/langchain_community/embeddings/infinity.py b/libs/community/langchain_community/embeddings/infinity.py index f9032427d5dc9..dbbc9f83142f6 100644 --- a/libs/community/langchain_community/embeddings/infinity.py +++ b/libs/community/langchain_community/embeddings/infinity.py @@ -8,8 +8,8 @@ import numpy as np import requests from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator __all__ = ["InfinityEmbeddings"] @@ -44,11 +44,13 @@ class InfinityEmbeddings(BaseModel, Embeddings): """Infinity client.""" # LLM call kwargs - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" values["infinity_api_url"] = get_from_dict_or_env( diff --git a/libs/community/langchain_community/embeddings/infinity_local.py b/libs/community/langchain_community/embeddings/infinity_local.py index 6697d63a83bc9..610ccaf3e88f0 100644 --- a/libs/community/langchain_community/embeddings/infinity_local.py +++ b/libs/community/langchain_community/embeddings/infinity_local.py @@ -2,10 +2,11 @@ import asyncio from logging import getLogger -from typing import Any, Dict, List, Optional +from typing import Any, List, Optional from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, root_validator +from pydantic import BaseModel, ConfigDict, model_validator +from typing_extensions import Self __all__ = ["InfinityEmbeddingsLocal"] @@ -57,11 +58,12 @@ class InfinityEmbeddingsLocal(BaseModel, Embeddings): """Infinity's AsyncEmbeddingEngine.""" # LLM call kwargs - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=False, skip_on_failure=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="after") + def validate_environment(self) -> Self: """Validate that api key and python package exists in environment.""" try: @@ -72,17 +74,15 @@ def validate_environment(cls, values: Dict) -> Dict: "`pip install 'infinity_emb[optimum,torch]>=0.0.24'` " "package to use the InfinityEmbeddingsLocal." ) - logger.debug(f"Using InfinityEmbeddingsLocal with kwargs {values}") - - values["engine"] = AsyncEmbeddingEngine( - model_name_or_path=values["model"], - device=values["device"], - revision=values["revision"], - model_warmup=values["model_warmup"], - batch_size=values["batch_size"], - engine=values["backend"], + self.engine = AsyncEmbeddingEngine( + model_name_or_path=self.model, + device=self.device, + revision=self.revision, + model_warmup=self.model_warmup, + batch_size=self.batch_size, + engine=self.backend, ) - return values + return self async def __aenter__(self) -> None: """start the background worker. diff --git a/libs/community/langchain_community/embeddings/ipex_llm.py b/libs/community/langchain_community/embeddings/ipex_llm.py index 5b1a6a4a25e0e..be18e2f346104 100644 --- a/libs/community/langchain_community/embeddings/ipex_llm.py +++ b/libs/community/langchain_community/embeddings/ipex_llm.py @@ -4,7 +4,7 @@ from typing import Any, Dict, List, Optional from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, ConfigDict, Field DEFAULT_BGE_MODEL = "BAAI/bge-small-en-v1.5" DEFAULT_QUERY_BGE_INSTRUCTION_EN = ( @@ -106,8 +106,9 @@ def __init__(self, **kwargs: Any): if "-zh" in self.model_name: self.query_instruction = DEFAULT_QUERY_BGE_INSTRUCTION_ZH - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def embed_documents(self, texts: List[str]) -> List[List[float]]: """Compute doc embeddings using a HuggingFace transformer model. diff --git a/libs/community/langchain_community/embeddings/itrex.py b/libs/community/langchain_community/embeddings/itrex.py index 67273acebc588..2fcd0afae3d7c 100644 --- a/libs/community/langchain_community/embeddings/itrex.py +++ b/libs/community/langchain_community/embeddings/itrex.py @@ -3,7 +3,7 @@ from typing import Any, Dict, List, Optional from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel, ConfigDict class QuantizedBgeEmbeddings(BaseModel, Embeddings): @@ -118,8 +118,9 @@ def load_model(self) -> None: onnx_model_path, use_embedding_runtime=True ) - class Config: - extra = "allow" + model_config = ConfigDict( + extra="allow", + ) def _embed(self, inputs: Any) -> Any: import torch diff --git a/libs/community/langchain_community/embeddings/javelin_ai_gateway.py b/libs/community/langchain_community/embeddings/javelin_ai_gateway.py index 61baac869da1f..205e58e1c30c5 100644 --- a/libs/community/langchain_community/embeddings/javelin_ai_gateway.py +++ b/libs/community/langchain_community/embeddings/javelin_ai_gateway.py @@ -3,7 +3,7 @@ from typing import Any, Iterator, List, Optional from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel def _chunk(texts: List[str], size: int) -> Iterator[List[str]]: diff --git a/libs/community/langchain_community/embeddings/jina.py b/libs/community/langchain_community/embeddings/jina.py index 73f75b24b8fb6..718345f1afd23 100644 --- a/libs/community/langchain_community/embeddings/jina.py +++ b/libs/community/langchain_community/embeddings/jina.py @@ -5,8 +5,8 @@ import requests from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, SecretStr, root_validator from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env +from pydantic import BaseModel, SecretStr, model_validator JINA_API_URL: str = "https://api.jina.ai/v1/embeddings" @@ -46,8 +46,9 @@ class JinaEmbeddings(BaseModel, Embeddings): model_name: str = "jina-embeddings-v2-base-en" jina_api_key: Optional[SecretStr] = None - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that auth token exists in environment.""" try: jina_api_key = convert_to_secret_str( diff --git a/libs/community/langchain_community/embeddings/johnsnowlabs.py b/libs/community/langchain_community/embeddings/johnsnowlabs.py index 23296149eb491..4223114aa0b15 100644 --- a/libs/community/langchain_community/embeddings/johnsnowlabs.py +++ b/libs/community/langchain_community/embeddings/johnsnowlabs.py @@ -3,7 +3,7 @@ from typing import Any, List from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel, ConfigDict class JohnSnowLabsEmbeddings(BaseModel, Embeddings): @@ -58,8 +58,9 @@ def __init__( except Exception as exc: raise Exception("Failure loading model") from exc - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def embed_documents(self, texts: List[str]) -> List[List[float]]: """Compute doc embeddings using a JohnSnowLabs transformer model. diff --git a/libs/community/langchain_community/embeddings/laser.py b/libs/community/langchain_community/embeddings/laser.py index 98bcaf26db411..4525bc2aee07e 100644 --- a/libs/community/langchain_community/embeddings/laser.py +++ b/libs/community/langchain_community/embeddings/laser.py @@ -2,8 +2,8 @@ import numpy as np from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel from langchain_core.utils import pre_init +from pydantic import BaseModel, ConfigDict LASER_MULTILINGUAL_MODEL: str = "laser2" @@ -37,8 +37,9 @@ class LaserEmbeddings(BaseModel, Embeddings): _encoder_pipeline: Any # : :meta private: - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/embeddings/llamacpp.py b/libs/community/langchain_community/embeddings/llamacpp.py index 49091b5aa6bfb..5a90b05aef93b 100644 --- a/libs/community/langchain_community/embeddings/llamacpp.py +++ b/libs/community/langchain_community/embeddings/llamacpp.py @@ -1,7 +1,8 @@ -from typing import Any, Dict, List, Optional +from typing import Any, List, Optional from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator +from pydantic import BaseModel, ConfigDict, Field, model_validator +from typing_extensions import Self class LlamaCppEmbeddings(BaseModel, Embeddings): @@ -60,13 +61,14 @@ class LlamaCppEmbeddings(BaseModel, Embeddings): device: Optional[str] = Field(None, alias="device") """Device type to use and pass to the model""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=False, skip_on_failure=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="after") + def validate_environment(self) -> Self: """Validate that llama-cpp-python library is installed.""" - model_path = values["model_path"] + model_path = self.model_path model_param_names = [ "n_ctx", "n_parts", @@ -80,15 +82,15 @@ def validate_environment(cls, values: Dict) -> Dict: "verbose", "device", ] - model_params = {k: values[k] for k in model_param_names} + model_params = {k: getattr(self, k) for k in model_param_names} # For backwards compatibility, only include if non-null. - if values["n_gpu_layers"] is not None: - model_params["n_gpu_layers"] = values["n_gpu_layers"] + if self.n_gpu_layers is not None: + model_params["n_gpu_layers"] = self.n_gpu_layers try: from llama_cpp import Llama - values["client"] = Llama(model_path, embedding=True, **model_params) + self.client = Llama(model_path, embedding=True, **model_params) except ImportError: raise ImportError( "Could not import llama-cpp-python library. " @@ -101,7 +103,7 @@ def validate_environment(cls, values: Dict) -> Dict: f"Received error {e}" ) - return values + return self def embed_documents(self, texts: List[str]) -> List[List[float]]: """Embed a list of documents using the Llama model. diff --git a/libs/community/langchain_community/embeddings/llamafile.py b/libs/community/langchain_community/embeddings/llamafile.py index 310f61a03e1d0..247b1a923ac58 100644 --- a/libs/community/langchain_community/embeddings/llamafile.py +++ b/libs/community/langchain_community/embeddings/llamafile.py @@ -3,7 +3,7 @@ import requests from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel logger = logging.getLogger(__name__) diff --git a/libs/community/langchain_community/embeddings/llm_rails.py b/libs/community/langchain_community/embeddings/llm_rails.py index 7de390ae72d03..92bb8c6a10cd3 100644 --- a/libs/community/langchain_community/embeddings/llm_rails.py +++ b/libs/community/langchain_community/embeddings/llm_rails.py @@ -4,8 +4,8 @@ import requests from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, SecretStr from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import BaseModel, ConfigDict, SecretStr class LLMRailsEmbeddings(BaseModel, Embeddings): @@ -32,8 +32,9 @@ class LLMRailsEmbeddings(BaseModel, Embeddings): api_key: Optional[SecretStr] = None """LLMRails API key.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/embeddings/localai.py b/libs/community/langchain_community/embeddings/localai.py index 7a57eab2bc73e..b59433ad2e203 100644 --- a/libs/community/langchain_community/embeddings/localai.py +++ b/libs/community/langchain_community/embeddings/localai.py @@ -16,12 +16,12 @@ ) from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator from langchain_core.utils import ( get_from_dict_or_env, get_pydantic_field_names, pre_init, ) +from pydantic import BaseModel, ConfigDict, Field, model_validator from tenacity import ( AsyncRetrying, before_sleep_log, @@ -166,11 +166,13 @@ class LocalAIEmbeddings(BaseModel, Embeddings): model_kwargs: Dict[str, Any] = Field(default_factory=dict) """Holds any model parameters valid for `create` call not explicitly specified.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + 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", {}) diff --git a/libs/community/langchain_community/embeddings/minimax.py b/libs/community/langchain_community/embeddings/minimax.py index 3633029ed56b5..14262786158e5 100644 --- a/libs/community/langchain_community/embeddings/minimax.py +++ b/libs/community/langchain_community/embeddings/minimax.py @@ -5,8 +5,8 @@ import requests from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, Field, SecretStr from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import BaseModel, ConfigDict, Field, SecretStr from tenacity import ( before_sleep_log, retry, @@ -117,9 +117,10 @@ class MiniMaxEmbeddings(BaseModel, Embeddings): minimax_api_key: Optional[SecretStr] = Field(default=None, alias="api_key") """API Key for MiniMax API.""" - class Config: - allow_population_by_field_name = True - extra = "forbid" + model_config = ConfigDict( + populate_by_name=True, + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/embeddings/mlflow.py b/libs/community/langchain_community/embeddings/mlflow.py index 6261d76c16fd9..2dbff7c2eb8c2 100644 --- a/libs/community/langchain_community/embeddings/mlflow.py +++ b/libs/community/langchain_community/embeddings/mlflow.py @@ -4,7 +4,7 @@ from urllib.parse import urlparse from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, PrivateAttr +from pydantic import BaseModel, PrivateAttr def _chunk(texts: List[str], size: int) -> Iterator[List[str]]: diff --git a/libs/community/langchain_community/embeddings/mlflow_gateway.py b/libs/community/langchain_community/embeddings/mlflow_gateway.py index e722d9e35ce6a..9a7a9643fe40f 100644 --- a/libs/community/langchain_community/embeddings/mlflow_gateway.py +++ b/libs/community/langchain_community/embeddings/mlflow_gateway.py @@ -4,7 +4,7 @@ from typing import Any, Iterator, List, Optional from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel def _chunk(texts: List[str], size: int) -> Iterator[List[str]]: diff --git a/libs/community/langchain_community/embeddings/modelscope_hub.py b/libs/community/langchain_community/embeddings/modelscope_hub.py index 77a53770bede4..878f05a4fe253 100644 --- a/libs/community/langchain_community/embeddings/modelscope_hub.py +++ b/libs/community/langchain_community/embeddings/modelscope_hub.py @@ -1,7 +1,7 @@ from typing import Any, List, Optional from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel, ConfigDict class ModelScopeEmbeddings(BaseModel, Embeddings): @@ -39,8 +39,9 @@ def __init__(self, **kwargs: Any): model_revision=self.model_revision, ) - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def embed_documents(self, texts: List[str]) -> List[List[float]]: """Compute doc embeddings using a modelscope embedding model. diff --git a/libs/community/langchain_community/embeddings/mosaicml.py b/libs/community/langchain_community/embeddings/mosaicml.py index 01acee54ec580..cf5f8646b34ab 100644 --- a/libs/community/langchain_community/embeddings/mosaicml.py +++ b/libs/community/langchain_community/embeddings/mosaicml.py @@ -2,8 +2,8 @@ import requests from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator class MosaicMLInstructorEmbeddings(BaseModel, Embeddings): @@ -41,11 +41,13 @@ class MosaicMLInstructorEmbeddings(BaseModel, Embeddings): mosaicml_api_token: Optional[str] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" mosaicml_api_token = get_from_dict_or_env( values, "mosaicml_api_token", "MOSAICML_API_TOKEN" diff --git a/libs/community/langchain_community/embeddings/nemo.py b/libs/community/langchain_community/embeddings/nemo.py index 95d00c3bbb4ce..38a20ffb45071 100644 --- a/libs/community/langchain_community/embeddings/nemo.py +++ b/libs/community/langchain_community/embeddings/nemo.py @@ -8,8 +8,8 @@ import requests from langchain_core._api.deprecation import deprecated from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel from langchain_core.utils import pre_init +from pydantic import BaseModel def is_endpoint_live(url: str, headers: Optional[dict], payload: Any) -> bool: diff --git a/libs/community/langchain_community/embeddings/nlpcloud.py b/libs/community/langchain_community/embeddings/nlpcloud.py index 69f08d28572f4..decf8ac2b803e 100644 --- a/libs/community/langchain_community/embeddings/nlpcloud.py +++ b/libs/community/langchain_community/embeddings/nlpcloud.py @@ -1,8 +1,8 @@ from typing import Any, Dict, List from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel from langchain_core.utils import get_from_dict_or_env, pre_init +from pydantic import BaseModel class NLPCloudEmbeddings(BaseModel, Embeddings): diff --git a/libs/community/langchain_community/embeddings/oci_generative_ai.py b/libs/community/langchain_community/embeddings/oci_generative_ai.py index cf5cec0e36253..72c4059d2be88 100644 --- a/libs/community/langchain_community/embeddings/oci_generative_ai.py +++ b/libs/community/langchain_community/embeddings/oci_generative_ai.py @@ -2,8 +2,8 @@ from typing import Any, Dict, Iterator, List, Mapping, Optional from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel from langchain_core.utils import pre_init +from pydantic import BaseModel, ConfigDict CUSTOM_ENDPOINT_PREFIX = "ocid1.generativeaiendpoint" @@ -46,9 +46,9 @@ class OCIGenAIEmbeddings(BaseModel, Embeddings): ) """ - client: Any #: :meta private: + client: Any = None #: :meta private: - service_models: Any #: :meta private: + service_models: Any = None #: :meta private: auth_type: Optional[str] = "API_KEY" """Authentication type, could be @@ -66,16 +66,16 @@ class OCIGenAIEmbeddings(BaseModel, Embeddings): If not specified , DEFAULT will be used """ - model_id: str = None # type: ignore[assignment] + model_id: Optional[str] = None """Id of the model to call, e.g., cohere.embed-english-light-v2.0""" model_kwargs: Optional[Dict] = None """Keyword arguments to pass to the model""" - service_endpoint: str = None # type: ignore[assignment] + service_endpoint: Optional[str] = None """service endpoint url""" - compartment_id: str = None # type: ignore[assignment] + compartment_id: Optional[str] = None """OCID of compartment""" truncate: Optional[str] = "END" @@ -85,8 +85,9 @@ class OCIGenAIEmbeddings(BaseModel, Embeddings): """Batch size of OCI GenAI embedding requests. OCI GenAI may handle up to 96 texts per request""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: # pylint: disable=no-self-argument @@ -180,6 +181,9 @@ def embed_documents(self, texts: List[str]) -> List[List[float]]: """ from oci.generative_ai_inference import models + if not self.model_id: + raise ValueError("Model ID is required to embed documents") + if self.model_id.startswith(CUSTOM_ENDPOINT_PREFIX): serving_mode = models.DedicatedServingMode(endpoint_id=self.model_id) else: diff --git a/libs/community/langchain_community/embeddings/octoai_embeddings.py b/libs/community/langchain_community/embeddings/octoai_embeddings.py index 287c04c264589..102ba313f54f8 100644 --- a/libs/community/langchain_community/embeddings/octoai_embeddings.py +++ b/libs/community/langchain_community/embeddings/octoai_embeddings.py @@ -1,7 +1,7 @@ from typing import Dict -from langchain_core.pydantic_v1 import Field, SecretStr from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import Field, SecretStr from langchain_community.embeddings.openai import OpenAIEmbeddings from langchain_community.utils.openai import is_openai_v1 diff --git a/libs/community/langchain_community/embeddings/ollama.py b/libs/community/langchain_community/embeddings/ollama.py index 86774c45a5896..13be9a3cd84f0 100644 --- a/libs/community/langchain_community/embeddings/ollama.py +++ b/libs/community/langchain_community/embeddings/ollama.py @@ -3,7 +3,7 @@ import requests from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel, ConfigDict logger = logging.getLogger(__name__) @@ -141,8 +141,9 @@ def _identifying_params(self) -> Mapping[str, Any]: """Get the identifying parameters.""" return {**{"model": self.model}, **self._default_params} - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def _process_emb_response(self, input: str) -> List[float]: """Process a response from the API. diff --git a/libs/community/langchain_community/embeddings/openai.py b/libs/community/langchain_community/embeddings/openai.py index 0c097b11aca03..a7abde76ea32a 100644 --- a/libs/community/langchain_community/embeddings/openai.py +++ b/libs/community/langchain_community/embeddings/openai.py @@ -21,12 +21,12 @@ import numpy as np from langchain_core._api.deprecation import deprecated from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator from langchain_core.utils import ( get_from_dict_or_env, get_pydantic_field_names, pre_init, ) +from pydantic import BaseModel, ConfigDict, Field, model_validator from tenacity import ( AsyncRetrying, before_sleep_log, @@ -254,12 +254,14 @@ class OpenAIEmbeddings(BaseModel, Embeddings): http_client: Union[Any, None] = None """Optional httpx.Client.""" - class Config: - allow_population_by_field_name = True - extra = "forbid" + model_config = ConfigDict( + populate_by_name=True, + extra="forbid", + ) - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + 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", {}) diff --git a/libs/community/langchain_community/embeddings/openvino.py b/libs/community/langchain_community/embeddings/openvino.py index f8dfc4077c819..8a83cfd16792a 100644 --- a/libs/community/langchain_community/embeddings/openvino.py +++ b/libs/community/langchain_community/embeddings/openvino.py @@ -2,7 +2,7 @@ from typing import Any, Dict, List from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, ConfigDict, Field DEFAULT_QUERY_INSTRUCTION = ( "Represent the question for retrieving supporting documents: " @@ -254,8 +254,9 @@ def run_mean_pooling(model_output: Any, attention_mask: Any) -> Any: return all_embeddings - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def embed_documents(self, texts: List[str]) -> List[List[float]]: """Compute doc embeddings using a HuggingFace transformer model. diff --git a/libs/community/langchain_community/embeddings/optimum_intel.py b/libs/community/langchain_community/embeddings/optimum_intel.py index a9c40ff4e185b..657cba32c78c4 100644 --- a/libs/community/langchain_community/embeddings/optimum_intel.py +++ b/libs/community/langchain_community/embeddings/optimum_intel.py @@ -1,7 +1,7 @@ from typing import Any, Dict, List, Optional from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel, ConfigDict class QuantizedBiEncoderEmbeddings(BaseModel, Embeddings): @@ -100,8 +100,9 @@ def load_model(self) -> None: ) self.transformer_model.eval() - class Config: - extra = "allow" + model_config = ConfigDict( + extra="allow", + ) def _embed(self, inputs: Any) -> Any: try: diff --git a/libs/community/langchain_community/embeddings/oracleai.py b/libs/community/langchain_community/embeddings/oracleai.py index a7adbf6cad813..4b81787206ebd 100644 --- a/libs/community/langchain_community/embeddings/oracleai.py +++ b/libs/community/langchain_community/embeddings/oracleai.py @@ -14,7 +14,7 @@ from typing import TYPE_CHECKING, Any, Dict, List, Optional from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel, ConfigDict if TYPE_CHECKING: from oracledb import Connection @@ -37,8 +37,9 @@ class OracleEmbeddings(BaseModel, Embeddings): def __init__(self, **kwargs: Any): super().__init__(**kwargs) - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) """ 1 - user needs to have create procedure, diff --git a/libs/community/langchain_community/embeddings/ovhcloud.py b/libs/community/langchain_community/embeddings/ovhcloud.py index a943e2a70edf8..51b0dd2f34aa6 100644 --- a/libs/community/langchain_community/embeddings/ovhcloud.py +++ b/libs/community/langchain_community/embeddings/ovhcloud.py @@ -4,7 +4,7 @@ import requests from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel, ConfigDict logger = logging.getLogger(__name__) @@ -23,8 +23,9 @@ class OVHCloudEmbeddings(BaseModel, Embeddings): """ OVHcloud AI Endpoints region""" region: str = "kepler" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def __init__(self, **kwargs: Any): super().__init__(**kwargs) diff --git a/libs/community/langchain_community/embeddings/premai.py b/libs/community/langchain_community/embeddings/premai.py index 0ae42759776b0..ed4a74534439b 100644 --- a/libs/community/langchain_community/embeddings/premai.py +++ b/libs/community/langchain_community/embeddings/premai.py @@ -5,8 +5,8 @@ from langchain_core.embeddings import Embeddings from langchain_core.language_models.llms import create_base_retry_decorator -from langchain_core.pydantic_v1 import BaseModel, SecretStr from langchain_core.utils import get_from_dict_or_env, pre_init +from pydantic import BaseModel, SecretStr logger = logging.getLogger(__name__) diff --git a/libs/community/langchain_community/embeddings/sagemaker_endpoint.py b/libs/community/langchain_community/embeddings/sagemaker_endpoint.py index dfa58056ab47c..d5fa48f3b03c9 100644 --- a/libs/community/langchain_community/embeddings/sagemaker_endpoint.py +++ b/libs/community/langchain_community/embeddings/sagemaker_endpoint.py @@ -1,8 +1,8 @@ from typing import Any, Dict, List, Optional from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel from langchain_core.utils import pre_init +from pydantic import BaseModel, ConfigDict from langchain_community.llms.sagemaker_endpoint import ContentHandlerBase @@ -110,9 +110,10 @@ def transform_output(self, output: bytes) -> List[List[float]]: .. _boto3: """ - class Config: - arbitrary_types_allowed = True - extra = "forbid" + model_config = ConfigDict( + arbitrary_types_allowed=True, + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/embeddings/sambanova.py b/libs/community/langchain_community/embeddings/sambanova.py index 57601b5c6294a..0687295c8d9e5 100644 --- a/libs/community/langchain_community/embeddings/sambanova.py +++ b/libs/community/langchain_community/embeddings/sambanova.py @@ -3,8 +3,8 @@ import requests from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel from langchain_core.utils import get_from_dict_or_env, pre_init +from pydantic import BaseModel class SambaStudioEmbeddings(BaseModel, Embeddings): diff --git a/libs/community/langchain_community/embeddings/self_hosted.py b/libs/community/langchain_community/embeddings/self_hosted.py index 48188dedeea2f..8099c7018ed41 100644 --- a/libs/community/langchain_community/embeddings/self_hosted.py +++ b/libs/community/langchain_community/embeddings/self_hosted.py @@ -1,6 +1,7 @@ from typing import Any, Callable, List from langchain_core.embeddings import Embeddings +from pydantic import ConfigDict from langchain_community.llms.self_hosted import SelfHostedPipeline @@ -65,8 +66,9 @@ def get_pipeline(): inference_kwargs: Any = None """Any kwargs to pass to the model's inference function.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def embed_documents(self, texts: List[str]) -> List[List[float]]: """Compute doc embeddings using a HuggingFace transformer model. diff --git a/libs/community/langchain_community/embeddings/solar.py b/libs/community/langchain_community/embeddings/solar.py index e441b0cea1727..e730d7e6e9625 100644 --- a/libs/community/langchain_community/embeddings/solar.py +++ b/libs/community/langchain_community/embeddings/solar.py @@ -6,8 +6,8 @@ import requests from langchain_core._api import deprecated from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, SecretStr from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import BaseModel, ConfigDict, SecretStr from tenacity import ( before_sleep_log, retry, @@ -75,8 +75,9 @@ class SolarEmbeddings(BaseModel, Embeddings): solar_api_key: Optional[SecretStr] = None """API Key for Solar API.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/embeddings/spacy_embeddings.py b/libs/community/langchain_community/embeddings/spacy_embeddings.py index d424ad70bc59d..63c71e07c3f4f 100644 --- a/libs/community/langchain_community/embeddings/spacy_embeddings.py +++ b/libs/community/langchain_community/embeddings/spacy_embeddings.py @@ -2,7 +2,7 @@ from typing import Any, Dict, List, Optional from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, root_validator +from pydantic import BaseModel, ConfigDict, model_validator class SpacyEmbeddings(BaseModel, Embeddings): @@ -22,11 +22,13 @@ class SpacyEmbeddings(BaseModel, Embeddings): model_name: str = "en_core_web_sm" nlp: Optional[Any] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """ Validates that the spaCy package and the model are installed. diff --git a/libs/community/langchain_community/embeddings/sparkllm.py b/libs/community/langchain_community/embeddings/sparkllm.py index 5f98ee74de269..6f0f1a1055267 100644 --- a/libs/community/langchain_community/embeddings/sparkllm.py +++ b/libs/community/langchain_community/embeddings/sparkllm.py @@ -12,11 +12,11 @@ import numpy as np import requests from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, Field, SecretStr from langchain_core.utils import ( secret_from_env, ) from numpy import ndarray +from pydantic import BaseModel, ConfigDict, Field, SecretStr # SparkLLMTextEmbeddings is an embedding model provided by iFLYTEK Co., Ltd.. (https://iflytek.com/en/). @@ -124,8 +124,9 @@ class SparkLLMTextEmbeddings(BaseModel, Embeddings): If "para"(default), it belongs to document Embedding. If "query", it belongs to query Embedding.""" - class Config: - allow_population_by_field_name = True + model_config = ConfigDict( + populate_by_name=True, + ) def _embed(self, texts: List[str], host: str) -> Optional[List[List[float]]]: """Internal method to call Spark Embedding API and return embeddings. diff --git a/libs/community/langchain_community/embeddings/tensorflow_hub.py b/libs/community/langchain_community/embeddings/tensorflow_hub.py index 4b72c071eab80..15c329417482c 100644 --- a/libs/community/langchain_community/embeddings/tensorflow_hub.py +++ b/libs/community/langchain_community/embeddings/tensorflow_hub.py @@ -1,7 +1,7 @@ from typing import Any, List from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel, ConfigDict DEFAULT_MODEL_URL = "https://tfhub.dev/google/universal-sentence-encoder-multilingual/3" @@ -43,8 +43,9 @@ def __init__(self, **kwargs: Any): self.embed = tensorflow_hub.load(self.model_url) - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def embed_documents(self, texts: List[str]) -> List[List[float]]: """Compute doc embeddings using a TensorflowHub embedding model. diff --git a/libs/community/langchain_community/embeddings/text2vec.py b/libs/community/langchain_community/embeddings/text2vec.py index 537249aa93d3d..9b0cf353ec006 100644 --- a/libs/community/langchain_community/embeddings/text2vec.py +++ b/libs/community/langchain_community/embeddings/text2vec.py @@ -3,7 +3,7 @@ from typing import Any, List, Optional from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel class Text2vecEmbeddings(Embeddings, BaseModel): diff --git a/libs/community/langchain_community/embeddings/textembed.py b/libs/community/langchain_community/embeddings/textembed.py index 5559acf804f1b..57fc85211abb0 100644 --- a/libs/community/langchain_community/embeddings/textembed.py +++ b/libs/community/langchain_community/embeddings/textembed.py @@ -17,8 +17,9 @@ import numpy as np import requests from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, root_validator -from langchain_core.utils import get_from_dict_or_env +from langchain_core.utils import from_env, secret_from_env +from pydantic import BaseModel, ConfigDict, Field, SecretStr, model_validator +from typing_extensions import Self __all__ = ["TextEmbedEmbeddings"] @@ -50,35 +51,30 @@ class TextEmbedEmbeddings(BaseModel, Embeddings): model: str """Underlying TextEmbed model id.""" - api_url: str = "http://localhost:8000/v1" + api_url: str = Field( + default_factory=from_env( + "TEXTEMBED_API_URL", default="http://localhost:8000/v1" + ) + ) """Endpoint URL to use.""" - api_key: str = "None" + api_key: SecretStr = Field(default_factory=secret_from_env("TEXTEMBED_API_KEY")) """API Key for authentication""" client: Any = None """TextEmbed client.""" - class Config: - extra = "forbid" - - @root_validator(pre=False, skip_on_failure=True) - def validate_environment(cls, values: Dict) -> Dict: - """Validate that api key and URL exist in the environment. - - Args: - values (Dict): Dictionary of values to validate. - - Returns: - Dict: Validated values. - """ - values["api_url"] = get_from_dict_or_env(values, "api_url", "API_URL") - values["api_key"] = get_from_dict_or_env(values, "api_key", "API_KEY") + model_config = ConfigDict( + extra="forbid", + ) - values["client"] = AsyncOpenAITextEmbedEmbeddingClient( - host=values["api_url"], api_key=values["api_key"] + @model_validator(mode="after") + def validate_environment(self) -> Self: + """Validate that api key and URL exist in the environment.""" + self.client = AsyncOpenAITextEmbedEmbeddingClient( + host=self.api_url, api_key=self.api_key.get_secret_value() ) - return values + return self def embed_documents(self, texts: List[str]) -> List[List[float]]: """Call out to TextEmbed's embedding endpoint. diff --git a/libs/community/langchain_community/embeddings/titan_takeoff.py b/libs/community/langchain_community/embeddings/titan_takeoff.py index bf82f54936e31..b171be0f2918f 100644 --- a/libs/community/langchain_community/embeddings/titan_takeoff.py +++ b/libs/community/langchain_community/embeddings/titan_takeoff.py @@ -2,7 +2,7 @@ from typing import Any, Dict, List, Optional, Set, Union from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel, ConfigDict class TakeoffEmbeddingException(Exception): @@ -24,8 +24,9 @@ class Device(str, Enum): class ReaderConfig(BaseModel): """Configuration for the reader to be deployed in Takeoff.""" - class Config: - protected_namespaces = () + model_config = ConfigDict( + protected_namespaces=(), + ) model_name: str """The name of the model to use""" diff --git a/libs/community/langchain_community/embeddings/volcengine.py b/libs/community/langchain_community/embeddings/volcengine.py index 9b8e399264819..6417e5e5a388e 100644 --- a/libs/community/langchain_community/embeddings/volcengine.py +++ b/libs/community/langchain_community/embeddings/volcengine.py @@ -4,8 +4,8 @@ from typing import Any, Dict, List, Optional from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel from langchain_core.utils import get_from_dict_or_env, pre_init +from pydantic import BaseModel logger = logging.getLogger(__name__) diff --git a/libs/community/langchain_community/embeddings/voyageai.py b/libs/community/langchain_community/embeddings/voyageai.py index e9c24b666abb5..2ef1477ac6c71 100644 --- a/libs/community/langchain_community/embeddings/voyageai.py +++ b/libs/community/langchain_community/embeddings/voyageai.py @@ -16,8 +16,8 @@ import requests from langchain_core._api.deprecation import deprecated from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, SecretStr, root_validator from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, SecretStr, model_validator from tenacity import ( before_sleep_log, retry, @@ -99,11 +99,13 @@ class VoyageEmbeddings(BaseModel, Embeddings): length, before vectorized by the embedding model. If False, an error will be raised if any given text exceeds the context length.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" values["voyage_api_key"] = convert_to_secret_str( get_from_dict_or_env(values, "voyage_api_key", "VOYAGE_API_KEY") diff --git a/libs/community/langchain_community/embeddings/yandex.py b/libs/community/langchain_community/embeddings/yandex.py index dbddf245234df..fdc6dc6873ebb 100644 --- a/libs/community/langchain_community/embeddings/yandex.py +++ b/libs/community/langchain_community/embeddings/yandex.py @@ -7,8 +7,8 @@ from typing import Any, Callable, Dict, List, Sequence from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, Field, SecretStr from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import BaseModel, ConfigDict, Field, SecretStr from tenacity import ( before_sleep_log, retry, @@ -71,8 +71,9 @@ class YandexGPTEmbeddings(BaseModel, Embeddings): If you provide personal data, confidential information, disable logging.""" grpc_metadata: Sequence - class Config: - allow_population_by_field_name = True + model_config = ConfigDict( + populate_by_name=True, + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/embeddings/zhipuai.py b/libs/community/langchain_community/embeddings/zhipuai.py index b8415aed74712..73ced5fa01934 100644 --- a/libs/community/langchain_community/embeddings/zhipuai.py +++ b/libs/community/langchain_community/embeddings/zhipuai.py @@ -1,8 +1,8 @@ from typing import Any, Dict, List, Optional from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, Field, model_validator class ZhipuAIEmbeddings(BaseModel, Embeddings): @@ -76,8 +76,9 @@ class ZhipuAIEmbeddings(BaseModel, Embeddings): Only supported in `embedding-3` and later models. """ - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that auth token exists in environment.""" values["api_key"] = get_from_dict_or_env(values, "api_key", "ZHIPUAI_API_KEY") try: diff --git a/libs/community/langchain_community/example_selectors/ngram_overlap.py b/libs/community/langchain_community/example_selectors/ngram_overlap.py index 76f536f83cb6f..fdd2e34eaf7dd 100644 --- a/libs/community/langchain_community/example_selectors/ngram_overlap.py +++ b/libs/community/langchain_community/example_selectors/ngram_overlap.py @@ -4,12 +4,12 @@ https://aclanthology.org/P02-1040.pdf """ -from typing import Dict, List +from typing import Any, Dict, List import numpy as np from langchain_core.example_selectors import BaseExampleSelector from langchain_core.prompts import PromptTemplate -from langchain_core.pydantic_v1 import BaseModel, root_validator +from pydantic import BaseModel, model_validator def ngram_overlap_score(source: List[str], example: List[str]) -> float: @@ -65,8 +65,9 @@ class NGramOverlapExampleSelector(BaseExampleSelector, BaseModel): and excludes examples with no ngram overlap with input. """ - @root_validator(pre=True) - def check_dependencies(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def check_dependencies(cls, values: Dict) -> Any: """Check that valid dependencies exist.""" try: from nltk.translate.bleu_score import ( # noqa: F401 diff --git a/libs/community/langchain_community/graphs/graph_document.py b/libs/community/langchain_community/graphs/graph_document.py index 3e9a597cc565f..ff82ca4b4341a 100644 --- a/libs/community/langchain_community/graphs/graph_document.py +++ b/libs/community/langchain_community/graphs/graph_document.py @@ -4,7 +4,7 @@ from langchain_core.documents import Document from langchain_core.load.serializable import Serializable -from langchain_core.pydantic_v1 import Field +from pydantic import Field class Node(Serializable): diff --git a/libs/community/langchain_community/graphs/index_creator.py b/libs/community/langchain_community/graphs/index_creator.py index 9394da264e2f2..f686096092712 100644 --- a/libs/community/langchain_community/graphs/index_creator.py +++ b/libs/community/langchain_community/graphs/index_creator.py @@ -1,7 +1,7 @@ from typing import Optional, Type -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel from langchain_core.language_models import BaseLanguageModel from langchain_core.prompts import BasePromptTemplate from langchain_core.prompts.prompt import PromptTemplate diff --git a/libs/community/langchain_community/llms/ai21.py b/libs/community/langchain_community/llms/ai21.py index 9956af4e2cd44..08afd82a947fa 100644 --- a/libs/community/langchain_community/llms/ai21.py +++ b/libs/community/langchain_community/llms/ai21.py @@ -3,8 +3,8 @@ import requests from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import BaseModel, SecretStr from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import BaseModel, ConfigDict, SecretStr class AI21PenaltyData(BaseModel): @@ -68,8 +68,9 @@ class AI21(LLM): base_url: Optional[str] = None """Base url to use, if None decides based on model name.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/llms/aleph_alpha.py b/libs/community/langchain_community/llms/aleph_alpha.py index ec366c1649cd8..f0a6735ecf593 100644 --- a/libs/community/langchain_community/llms/aleph_alpha.py +++ b/libs/community/langchain_community/llms/aleph_alpha.py @@ -2,8 +2,8 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import SecretStr from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import ConfigDict, SecretStr from langchain_community.llms.utils import enforce_stop_tokens @@ -162,8 +162,9 @@ class AlephAlpha(LLM): nice to other users by de-prioritizing your request below concurrent ones.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/llms/amazon_api_gateway.py b/libs/community/langchain_community/llms/amazon_api_gateway.py index 61e589a7f7cfa..61c088c8cdfab 100644 --- a/libs/community/langchain_community/llms/amazon_api_gateway.py +++ b/libs/community/langchain_community/llms/amazon_api_gateway.py @@ -3,6 +3,7 @@ import requests from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM +from pydantic import ConfigDict from langchain_community.llms.utils import enforce_stop_tokens @@ -43,8 +44,9 @@ class AmazonAPIGateway(LLM): and the endpoint. """ - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @property def _identifying_params(self) -> Mapping[str, Any]: diff --git a/libs/community/langchain_community/llms/anthropic.py b/libs/community/langchain_community/llms/anthropic.py index f6b977ab4559f..07b40d6eda705 100644 --- a/libs/community/langchain_community/llms/anthropic.py +++ b/libs/community/langchain_community/llms/anthropic.py @@ -20,7 +20,6 @@ from langchain_core.language_models.llms import LLM from langchain_core.outputs import GenerationChunk from langchain_core.prompt_values import PromptValue -from langchain_core.pydantic_v1 import Field, SecretStr, root_validator from langchain_core.utils import ( check_package_version, get_from_dict_or_env, @@ -28,6 +27,7 @@ pre_init, ) from langchain_core.utils.utils import build_extra_kwargs, convert_to_secret_str +from pydantic import ConfigDict, Field, SecretStr, model_validator class _AnthropicCommon(BaseLanguageModel): @@ -66,8 +66,9 @@ class _AnthropicCommon(BaseLanguageModel): count_tokens: Optional[Callable[[str], int]] = None model_kwargs: Dict[str, Any] = Field(default_factory=dict) - @root_validator(pre=True) - def build_extra(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def build_extra(cls, values: Dict) -> Any: extra = values.get("model_kwargs", {}) all_required_field_names = get_pydantic_field_names(cls) values["model_kwargs"] = build_extra_kwargs( @@ -180,9 +181,10 @@ class Anthropic(LLM, _AnthropicCommon): response = model.invoke(prompt) """ - class Config: - allow_population_by_field_name = True - arbitrary_types_allowed = True + model_config = ConfigDict( + populate_by_name=True, + arbitrary_types_allowed=True, + ) @pre_init def raise_warning(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/llms/anyscale.py b/libs/community/langchain_community/llms/anyscale.py index 2e43fa38ae5a0..2ae364c390630 100644 --- a/libs/community/langchain_community/llms/anyscale.py +++ b/libs/community/langchain_community/llms/anyscale.py @@ -14,8 +14,8 @@ CallbackManagerForLLMRun, ) from langchain_core.outputs import Generation, GenerationChunk, LLMResult -from langchain_core.pydantic_v1 import Field, SecretStr from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import Field, SecretStr from langchain_community.llms.openai import ( BaseOpenAI, diff --git a/libs/community/langchain_community/llms/aphrodite.py b/libs/community/langchain_community/llms/aphrodite.py index d8bf1c1871c1d..7d752b07e7986 100644 --- a/libs/community/langchain_community/llms/aphrodite.py +++ b/libs/community/langchain_community/llms/aphrodite.py @@ -3,8 +3,8 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models import BaseLLM from langchain_core.outputs import Generation, LLMResult -from langchain_core.pydantic_v1 import Field from langchain_core.utils import pre_init +from pydantic import Field class Aphrodite(BaseLLM): diff --git a/libs/community/langchain_community/llms/arcee.py b/libs/community/langchain_community/llms/arcee.py index 9bd7ff97ff9db..42fcef87bb7af 100644 --- a/libs/community/langchain_community/llms/arcee.py +++ b/libs/community/langchain_community/llms/arcee.py @@ -2,8 +2,8 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import SecretStr, root_validator from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env +from pydantic import ConfigDict, SecretStr, model_validator from langchain_community.utilities.arcee import ArceeWrapper, DALMFilter @@ -51,9 +51,9 @@ class Arcee(LLM): model_kwargs: Optional[Dict[str, Any]] = None """Keyword arguments to pass to the model.""" - class Config: - extra = "forbid" - underscore_attrs_are_private = True + model_config = ConfigDict( + extra="forbid", + ) @property def _llm_type(self) -> str: @@ -73,8 +73,9 @@ def __init__(self, **data: Any) -> None: model_name=self.model, ) - @root_validator(pre=True) - def validate_environments(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environments(cls, values: Dict) -> Any: """Validate Arcee environment variables.""" # validate env vars diff --git a/libs/community/langchain_community/llms/aviary.py b/libs/community/langchain_community/llms/aviary.py index 73dcbacb48d6a..95fd5730fac5d 100644 --- a/libs/community/langchain_community/llms/aviary.py +++ b/libs/community/langchain_community/llms/aviary.py @@ -5,8 +5,8 @@ import requests from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import ConfigDict, model_validator from langchain_community.llms.utils import enforce_stop_tokens @@ -122,11 +122,13 @@ class Aviary(LLM): # API version to use for Aviary version: Optional[str] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" aviary_url = get_from_dict_or_env(values, "aviary_url", "AVIARY_URL") aviary_token = get_from_dict_or_env(values, "aviary_token", "AVIARY_TOKEN") diff --git a/libs/community/langchain_community/llms/azureml_endpoint.py b/libs/community/langchain_community/llms/azureml_endpoint.py index 7dd4f6aee4225..f2b5eed2e1c02 100644 --- a/libs/community/langchain_community/llms/azureml_endpoint.py +++ b/libs/community/langchain_community/llms/azureml_endpoint.py @@ -8,8 +8,8 @@ from langchain_core.callbacks.manager import CallbackManagerForLLMRun from langchain_core.language_models.llms import BaseLLM from langchain_core.outputs import Generation, LLMResult -from langchain_core.pydantic_v1 import BaseModel, SecretStr, root_validator, validator from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env +from pydantic import BaseModel, SecretStr, model_validator, validator DEFAULT_TIMEOUT = 50 @@ -382,8 +382,9 @@ class AzureMLBaseEndpoint(BaseModel): model_kwargs: Optional[dict] = None """Keyword arguments to pass to the model.""" - @root_validator(pre=True) - def validate_environ(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environ(cls, values: Dict) -> Any: values["endpoint_api_key"] = convert_to_secret_str( get_from_dict_or_env(values, "endpoint_api_key", "AZUREML_ENDPOINT_API_KEY") ) diff --git a/libs/community/langchain_community/llms/baichuan.py b/libs/community/langchain_community/llms/baichuan.py index 4cc614ce8ac4a..4026e14b90efe 100644 --- a/libs/community/langchain_community/llms/baichuan.py +++ b/libs/community/langchain_community/llms/baichuan.py @@ -7,8 +7,8 @@ import requests from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import Field, SecretStr from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import Field, SecretStr from langchain_community.llms.utils import enforce_stop_tokens diff --git a/libs/community/langchain_community/llms/baidu_qianfan_endpoint.py b/libs/community/langchain_community/llms/baidu_qianfan_endpoint.py index 601f952bddbb4..6e4bc68b57fd9 100644 --- a/libs/community/langchain_community/llms/baidu_qianfan_endpoint.py +++ b/libs/community/langchain_community/llms/baidu_qianfan_endpoint.py @@ -16,8 +16,8 @@ ) from langchain_core.language_models.llms import LLM from langchain_core.outputs import GenerationChunk -from langchain_core.pydantic_v1 import Field, SecretStr from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import Field, SecretStr logger = logging.getLogger(__name__) diff --git a/libs/community/langchain_community/llms/bananadev.py b/libs/community/langchain_community/llms/bananadev.py index c1be0bb1fb6a7..9ed76e816b336 100644 --- a/libs/community/langchain_community/llms/bananadev.py +++ b/libs/community/langchain_community/llms/bananadev.py @@ -3,9 +3,10 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import Field, SecretStr, root_validator -from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init -from langchain_core.utils.pydantic import get_fields +from langchain_core.utils import ( + secret_from_env, +) +from pydantic import ConfigDict, Field, SecretStr, model_validator from langchain_community.llms.utils import enforce_stop_tokens @@ -39,16 +40,19 @@ class Banana(LLM): """Holds any model parameters valid for `create` call not explicitly specified.""" - banana_api_key: Optional[SecretStr] = None + banana_api_key: Optional[SecretStr] = Field( + default_factory=secret_from_env("BANANA_API_KEY", default=None) + ) - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def build_extra(cls, values: Dict[str, Any]) -> Any: """Build extra kwargs from additional params that were passed in.""" - all_required_field_names = {field.alias for field in get_fields(cls).values()} - + all_required_field_names = set(list(cls.model_fields.keys())) extra = values.get("model_kwargs", {}) for field_name in list(values): if field_name not in all_required_field_names: @@ -62,15 +66,6 @@ def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: values["model_kwargs"] = extra return values - @pre_init - def validate_environment(cls, values: Dict) -> Dict: - """Validate that api key and python package exists in environment.""" - banana_api_key = convert_to_secret_str( - get_from_dict_or_env(values, "banana_api_key", "BANANA_API_KEY") - ) - values["banana_api_key"] = banana_api_key - return values - @property def _identifying_params(self) -> Mapping[str, Any]: """Get the identifying parameters.""" diff --git a/libs/community/langchain_community/llms/baseten.py b/libs/community/langchain_community/llms/baseten.py index 9859106f51143..5b7ce87eb827e 100644 --- a/libs/community/langchain_community/llms/baseten.py +++ b/libs/community/langchain_community/llms/baseten.py @@ -5,7 +5,7 @@ import requests from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import Field +from pydantic import Field logger = logging.getLogger(__name__) diff --git a/libs/community/langchain_community/llms/beam.py b/libs/community/langchain_community/llms/beam.py index 63d51499e9a1b..fe8bf5f5ff8c0 100644 --- a/libs/community/langchain_community/llms/beam.py +++ b/libs/community/langchain_community/llms/beam.py @@ -9,9 +9,9 @@ import requests from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import Field, root_validator from langchain_core.utils import get_from_dict_or_env, pre_init from langchain_core.utils.pydantic import get_fields +from pydantic import ConfigDict, Field, model_validator logger = logging.getLogger(__name__) @@ -73,11 +73,13 @@ class Beam(LLM): beam_client_secret: str = "" app_id: Optional[str] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def build_extra(cls, values: Dict[str, Any]) -> Any: """Build extra kwargs from additional params that were passed in.""" all_required_field_names = {field.alias for field in get_fields(cls).values()} diff --git a/libs/community/langchain_community/llms/bedrock.py b/libs/community/langchain_community/llms/bedrock.py index d47a762973bea..444d50eb24b48 100644 --- a/libs/community/langchain_community/llms/bedrock.py +++ b/libs/community/langchain_community/llms/bedrock.py @@ -21,8 +21,8 @@ ) from langchain_core.language_models.llms import LLM from langchain_core.outputs import GenerationChunk -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.utils import get_from_dict_or_env, pre_init +from pydantic import BaseModel, ConfigDict, Field from langchain_community.llms.utils import enforce_stop_tokens from langchain_community.utilities.anthropic import ( @@ -778,8 +778,9 @@ def lc_attributes(self) -> Dict[str, Any]: return attributes - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def _stream( self, diff --git a/libs/community/langchain_community/llms/cerebriumai.py b/libs/community/langchain_community/llms/cerebriumai.py index 9e69b026c9cb1..b267033721124 100644 --- a/libs/community/langchain_community/llms/cerebriumai.py +++ b/libs/community/langchain_community/llms/cerebriumai.py @@ -4,9 +4,8 @@ import requests from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import Field, SecretStr, root_validator from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init -from langchain_core.utils.pydantic import get_fields +from pydantic import ConfigDict, Field, SecretStr, model_validator from langchain_community.llms.utils import enforce_stop_tokens @@ -40,13 +39,15 @@ class CerebriumAI(LLM): cerebriumai_api_key: Optional[SecretStr] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def build_extra(cls, values: Dict[str, Any]) -> Any: """Build extra kwargs from additional params that were passed in.""" - all_required_field_names = {field.alias for field in get_fields(cls).values()} + all_required_field_names = set(list(cls.model_fields.keys())) extra = values.get("model_kwargs", {}) for field_name in list(values): diff --git a/libs/community/langchain_community/llms/chatglm3.py b/libs/community/langchain_community/llms/chatglm3.py index aa3255a50f6df..796a592f4af88 100644 --- a/libs/community/langchain_community/llms/chatglm3.py +++ b/libs/community/langchain_community/llms/chatglm3.py @@ -11,7 +11,7 @@ HumanMessage, SystemMessage, ) -from langchain_core.pydantic_v1 import Field +from pydantic import Field from langchain_community.llms.utils import enforce_stop_tokens diff --git a/libs/community/langchain_community/llms/clarifai.py b/libs/community/langchain_community/llms/clarifai.py index 706e8bea2b993..c7d6fcde6a975 100644 --- a/libs/community/langchain_community/llms/clarifai.py +++ b/libs/community/langchain_community/llms/clarifai.py @@ -4,8 +4,8 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM from langchain_core.outputs import Generation, LLMResult -from langchain_core.pydantic_v1 import Field from langchain_core.utils import pre_init +from pydantic import ConfigDict, Field from langchain_community.llms.utils import enforce_stop_tokens @@ -49,8 +49,9 @@ class Clarifai(LLM): model: Any = Field(default=None, exclude=True) #: :meta private: api_base: str = "https://api.clarifai.com" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/llms/cohere.py b/libs/community/langchain_community/llms/cohere.py index 9d075b25a4506..875e9228122ca 100644 --- a/libs/community/langchain_community/llms/cohere.py +++ b/libs/community/langchain_community/llms/cohere.py @@ -10,8 +10,8 @@ ) from langchain_core.language_models.llms import LLM from langchain_core.load.serializable import Serializable -from langchain_core.pydantic_v1 import Field, SecretStr from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import ConfigDict, Field, SecretStr from tenacity import ( before_sleep_log, retry, @@ -159,8 +159,9 @@ class Cohere(LLM, BaseCohere): max_retries: int = 10 """Maximum number of retries to make when generating.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @property def _default_params(self) -> Dict[str, Any]: diff --git a/libs/community/langchain_community/llms/ctranslate2.py b/libs/community/langchain_community/llms/ctranslate2.py index 22c5de078acc4..71bfeb1f79864 100644 --- a/libs/community/langchain_community/llms/ctranslate2.py +++ b/libs/community/langchain_community/llms/ctranslate2.py @@ -3,8 +3,8 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import BaseLLM from langchain_core.outputs import Generation, LLMResult -from langchain_core.pydantic_v1 import Field from langchain_core.utils import pre_init +from pydantic import Field class CTranslate2(BaseLLM): diff --git a/libs/community/langchain_community/llms/databricks.py b/libs/community/langchain_community/llms/databricks.py index 8e5ab49f7ef0b..6d774d38ca2a3 100644 --- a/libs/community/langchain_community/llms/databricks.py +++ b/libs/community/langchain_community/llms/databricks.py @@ -7,11 +7,12 @@ import requests from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models import LLM -from langchain_core.pydantic_v1 import ( +from pydantic import ( BaseModel, + ConfigDict, Field, PrivateAttr, - root_validator, + model_validator, validator, ) @@ -97,8 +98,9 @@ def __init__(self, **data: Any): def llm(self) -> bool: return self.task in ("llm/v1/chat", "llm/v1/completions", "llama2/chat") - @root_validator(pre=True) - def set_api_url(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def set_api_url(cls, values: Dict[str, Any]) -> Any: if "api_url" not in values: host = values["host"] endpoint_name = values["endpoint_name"] @@ -141,8 +143,9 @@ class _DatabricksClusterDriverProxyClient(_DatabricksClientBase): cluster_id: str cluster_driver_port: str - @root_validator(pre=True) - def set_api_url(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def set_api_url(cls, values: Dict[str, Any]) -> Any: if "api_url" not in values: host = values["host"] cluster_id = values["cluster_id"] @@ -395,9 +398,9 @@ class Databricks(LLM): _client: _DatabricksClientBase = PrivateAttr() - class Config: - extra = "forbid" - underscore_attrs_are_private = True + model_config = ConfigDict( + extra="forbid", + ) @property def _llm_params(self) -> Dict[str, Any]: diff --git a/libs/community/langchain_community/llms/deepinfra.py b/libs/community/langchain_community/llms/deepinfra.py index 303113f191aa4..47551dd4e6520 100644 --- a/libs/community/langchain_community/llms/deepinfra.py +++ b/libs/community/langchain_community/llms/deepinfra.py @@ -9,6 +9,7 @@ from langchain_core.language_models.llms import LLM from langchain_core.outputs import GenerationChunk from langchain_core.utils import get_from_dict_or_env, pre_init +from pydantic import ConfigDict from langchain_community.utilities.requests import Requests @@ -37,8 +38,9 @@ class DeepInfra(LLM): deepinfra_api_token: Optional[str] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/llms/deepsparse.py b/libs/community/langchain_community/llms/deepsparse.py index 106750e273146..9375a3f772264 100644 --- a/libs/community/langchain_community/llms/deepsparse.py +++ b/libs/community/langchain_community/llms/deepsparse.py @@ -2,7 +2,7 @@ from langchain_core.utils import pre_init from typing import Any, AsyncIterator, Dict, Iterator, List, Optional, Union from langchain_core.utils import pre_init -from langchain_core.pydantic_v1 import root_validator +from pydantic import root_validator from langchain_core.utils import pre_init from langchain_core.callbacks import ( AsyncCallbackManagerForLLMRun, @@ -33,7 +33,7 @@ class DeepSparse(LLM): model: str """The path to a model file or directory or the name of a SparseZoo model stub.""" - model_config: Optional[Dict[str, Any]] = None + model_configuration: Optional[Dict[str, Any]] = None """Keyword arguments passed to the pipeline construction. Common parameters are sequence_length, prompt_sequence_length""" @@ -51,7 +51,7 @@ def _identifying_params(self) -> Dict[str, Any]: """Get the identifying parameters.""" return { "model": self.model, - "model_config": self.model_config, + "model_config": self.model_configuration, "generation_config": self.generation_config, "streaming": self.streaming, } @@ -72,7 +72,7 @@ def validate_environment(cls, values: Dict) -> Dict: "Please install it with `pip install deepsparse[llm]`" ) - model_config = values["model_config"] or {} + model_config = values["model_configuration"] or {} values["pipeline"] = Pipeline.create( task="text_generation", diff --git a/libs/community/langchain_community/llms/edenai.py b/libs/community/langchain_community/llms/edenai.py index 31cdaee92aeda..a43e8a71bec68 100644 --- a/libs/community/langchain_community/llms/edenai.py +++ b/libs/community/langchain_community/llms/edenai.py @@ -9,9 +9,9 @@ CallbackManagerForLLMRun, ) from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import Field, root_validator from langchain_core.utils import get_from_dict_or_env, pre_init from langchain_core.utils.pydantic import get_fields +from pydantic import ConfigDict, Field, model_validator from langchain_community.llms.utils import enforce_stop_tokens from langchain_community.utilities.requests import Requests @@ -69,8 +69,9 @@ class EdenAI(LLM): stop_sequences: Optional[List[str]] = None """Stop sequences to use.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: @@ -80,8 +81,9 @@ def validate_environment(cls, values: Dict) -> Dict: ) return values - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def build_extra(cls, values: Dict[str, Any]) -> Any: """Build extra kwargs from additional params that were passed in.""" all_required_field_names = {field.alias for field in get_fields(cls).values()} diff --git a/libs/community/langchain_community/llms/exllamav2.py b/libs/community/langchain_community/llms/exllamav2.py index 3e577cbf97788..d91a38034c2f3 100644 --- a/libs/community/langchain_community/llms/exllamav2.py +++ b/libs/community/langchain_community/llms/exllamav2.py @@ -1,10 +1,10 @@ -from typing import Any, Dict, Iterator, List, Optional +from typing import Any, Callable, Dict, Iterator, List, Optional from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models import LLM from langchain_core.outputs import GenerationChunk -from langchain_core.pydantic_v1 import Field from langchain_core.utils import pre_init +from pydantic import Field class ExLlamaV2(LLM): @@ -41,7 +41,7 @@ class ExLlamaV2(LLM): settings: Any = None # Langchain parameters - logfunc = print + logfunc: Callable = print stop_sequences: List[str] = Field("") """Sequences that immediately will stop the generator.""" diff --git a/libs/community/langchain_community/llms/fireworks.py b/libs/community/langchain_community/llms/fireworks.py index 73948062e6c76..f7af9e90b8d24 100644 --- a/libs/community/langchain_community/llms/fireworks.py +++ b/libs/community/langchain_community/llms/fireworks.py @@ -9,9 +9,9 @@ ) from langchain_core.language_models.llms import BaseLLM, create_base_retry_decorator from langchain_core.outputs import Generation, GenerationChunk, LLMResult -from langchain_core.pydantic_v1 import Field, SecretStr from langchain_core.utils import convert_to_secret_str, pre_init from langchain_core.utils.env import get_from_dict_or_env +from pydantic import Field, SecretStr def _stream_response_to_generation_chunk( diff --git a/libs/community/langchain_community/llms/forefrontai.py b/libs/community/langchain_community/llms/forefrontai.py index 9f1ac129ebbba..8c47304438982 100644 --- a/libs/community/langchain_community/llms/forefrontai.py +++ b/libs/community/langchain_community/llms/forefrontai.py @@ -3,8 +3,8 @@ import requests from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import SecretStr, root_validator from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env +from pydantic import ConfigDict, SecretStr, model_validator from langchain_community.llms.utils import enforce_stop_tokens @@ -46,11 +46,13 @@ class ForefrontAI(LLM): base_url: Optional[str] = None """Base url to use, if None decides based on model name.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key exists in environment.""" values["forefrontai_api_key"] = convert_to_secret_str( get_from_dict_or_env(values, "forefrontai_api_key", "FOREFRONTAI_API_KEY") diff --git a/libs/community/langchain_community/llms/friendli.py b/libs/community/langchain_community/llms/friendli.py index 5f137b078e172..e6d13155cd834 100644 --- a/libs/community/langchain_community/llms/friendli.py +++ b/libs/community/langchain_community/llms/friendli.py @@ -10,10 +10,10 @@ from langchain_core.language_models.llms import LLM from langchain_core.load.serializable import Serializable from langchain_core.outputs import GenerationChunk, LLMResult -from langchain_core.pydantic_v1 import Field, SecretStr from langchain_core.utils import pre_init from langchain_core.utils.env import get_from_dict_or_env from langchain_core.utils.utils import convert_to_secret_str +from pydantic import Field, SecretStr def _stream_response_to_generation_chunk(stream_response: Any) -> GenerationChunk: diff --git a/libs/community/langchain_community/llms/gigachat.py b/libs/community/langchain_community/llms/gigachat.py index 4172081dac4de..468d68cd9f4a8 100644 --- a/libs/community/langchain_community/llms/gigachat.py +++ b/libs/community/langchain_community/llms/gigachat.py @@ -13,6 +13,7 @@ from langchain_core.outputs import Generation, GenerationChunk, LLMResult from langchain_core.utils import pre_init from langchain_core.utils.pydantic import get_fields +from pydantic import ConfigDict if TYPE_CHECKING: import gigachat @@ -330,5 +331,6 @@ async def _astream( if run_manager: await run_manager.on_llm_new_token(content) - class Config: - extra = "allow" + model_config = ConfigDict( + extra="allow", + ) diff --git a/libs/community/langchain_community/llms/google_palm.py b/libs/community/langchain_community/llms/google_palm.py index e996c67b92df1..715792a3a0619 100644 --- a/libs/community/langchain_community/llms/google_palm.py +++ b/libs/community/langchain_community/llms/google_palm.py @@ -6,8 +6,8 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models import LanguageModelInput from langchain_core.outputs import Generation, GenerationChunk, LLMResult -from langchain_core.pydantic_v1 import BaseModel, SecretStr from langchain_core.utils import get_from_dict_or_env, pre_init +from pydantic import BaseModel, SecretStr from langchain_community.llms import BaseLLM from langchain_community.utilities.vertexai import create_retry_decorator diff --git a/libs/community/langchain_community/llms/gooseai.py b/libs/community/langchain_community/llms/gooseai.py index 255908a20c8e8..8a2dbdf37dbfc 100644 --- a/libs/community/langchain_community/llms/gooseai.py +++ b/libs/community/langchain_community/llms/gooseai.py @@ -3,9 +3,12 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import Field, SecretStr, root_validator -from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init -from langchain_core.utils.pydantic import get_fields +from langchain_core.utils import ( + convert_to_secret_str, + get_from_dict_or_env, + get_pydantic_field_names, +) +from pydantic import ConfigDict, Field, SecretStr, model_validator logger = logging.getLogger(__name__) @@ -63,13 +66,15 @@ class GooseAI(LLM): gooseai_api_key: Optional[SecretStr] = None - class Config: - extra = "ignore" + model_config = ConfigDict( + extra="ignore", + ) - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def build_extra(cls, values: Dict[str, Any]) -> Any: """Build extra kwargs from additional params that were passed in.""" - all_required_field_names = {field.alias for field in get_fields(cls).values()} + all_required_field_names = get_pydantic_field_names(cls) extra = values.get("model_kwargs", {}) for field_name in list(values): @@ -83,11 +88,7 @@ def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: ) extra[field_name] = values.pop(field_name) values["model_kwargs"] = extra - return values - @pre_init - def validate_environment(cls, values: Dict) -> Dict: - """Validate that api key and python package exists in environment.""" gooseai_api_key = convert_to_secret_str( get_from_dict_or_env(values, "gooseai_api_key", "GOOSEAI_API_KEY") ) diff --git a/libs/community/langchain_community/llms/gpt4all.py b/libs/community/langchain_community/llms/gpt4all.py index 0700410c9f89d..85c47ac691ec8 100644 --- a/libs/community/langchain_community/llms/gpt4all.py +++ b/libs/community/langchain_community/llms/gpt4all.py @@ -3,8 +3,8 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import Field from langchain_core.utils import pre_init +from pydantic import ConfigDict, Field from langchain_community.llms.utils import enforce_stop_tokens @@ -96,8 +96,9 @@ class GPT4All(LLM): client: Any = None #: :meta private: - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @staticmethod def _model_param_names() -> Set[str]: diff --git a/libs/community/langchain_community/llms/gradient_ai.py b/libs/community/langchain_community/llms/gradient_ai.py index 0da21c79ac29b..b603fd498d9a2 100644 --- a/libs/community/langchain_community/llms/gradient_ai.py +++ b/libs/community/langchain_community/llms/gradient_ai.py @@ -11,8 +11,9 @@ ) from langchain_core.language_models.llms import BaseLLM from langchain_core.outputs import Generation, LLMResult -from langchain_core.pydantic_v1 import Field, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import ConfigDict, Field, model_validator +from typing_extensions import Self from langchain_community.llms.utils import enforce_stop_tokens @@ -73,12 +74,14 @@ class GradientLLM(BaseLLM): """ClientSession, private, subject to change in upcoming releases.""" # LLM call kwargs - class Config: - allow_population_by_field_name = True - extra = "forbid" - - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + model_config = ConfigDict( + populate_by_name=True, + extra="forbid", + ) + + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" values["gradient_access_token"] = get_from_dict_or_env( @@ -93,8 +96,8 @@ def validate_environment(cls, values: Dict) -> Dict: ) return values - @root_validator(pre=False, skip_on_failure=True) - def post_init(cls, values: Dict) -> Dict: + @model_validator(mode="after") + def post_init(self) -> Self: """Post init validation.""" # Can be most to post_init_validation try: @@ -108,20 +111,14 @@ def post_init(cls, values: Dict) -> Dict: pass # Can be most to post_init_validation - if ( - values["gradient_access_token"] is None - or len(values["gradient_access_token"]) < 10 - ): + if self.gradient_access_token is None or len(self.gradient_access_token) < 10: raise ValueError("env variable `GRADIENT_ACCESS_TOKEN` must be set") - if ( - values["gradient_workspace_id"] is None - or len(values["gradient_access_token"]) < 3 - ): + if self.gradient_workspace_id is None or len(self.gradient_access_token) < 3: raise ValueError("env variable `GRADIENT_WORKSPACE_ID` must be set") - if values["model_kwargs"]: - kw = values["model_kwargs"] + if self.model_kwargs: + kw = self.model_kwargs if not 0 <= kw.get("temperature", 0.5) <= 1: raise ValueError("`temperature` must be in the range [0.0, 1.0]") @@ -134,7 +131,7 @@ def post_init(cls, values: Dict) -> Dict: if 0 >= kw.get("max_generated_token_count", 1): raise ValueError("`max_generated_token_count` must be positive") - return values + return self @property def _identifying_params(self) -> Mapping[str, Any]: diff --git a/libs/community/langchain_community/llms/huggingface_endpoint.py b/libs/community/langchain_community/llms/huggingface_endpoint.py index 27c1adce07f12..e9b4279a7d6e4 100644 --- a/libs/community/langchain_community/llms/huggingface_endpoint.py +++ b/libs/community/langchain_community/llms/huggingface_endpoint.py @@ -10,11 +10,11 @@ ) from langchain_core.language_models.llms import LLM from langchain_core.outputs import GenerationChunk -from langchain_core.pydantic_v1 import Field, root_validator from langchain_core.utils import ( get_pydantic_field_names, pre_init, ) +from pydantic import ConfigDict, Field, model_validator logger = logging.getLogger(__name__) @@ -126,11 +126,13 @@ class HuggingFaceEndpoint(LLM): """Task to call the model with. Should be a task that returns `generated_text` or `summary_text`.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + 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", {}) diff --git a/libs/community/langchain_community/llms/huggingface_hub.py b/libs/community/langchain_community/llms/huggingface_hub.py index 9c207c491f32f..7d1472fc5d844 100644 --- a/libs/community/langchain_community/llms/huggingface_hub.py +++ b/libs/community/langchain_community/llms/huggingface_hub.py @@ -5,6 +5,7 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM from langchain_core.utils import get_from_dict_or_env, pre_init +from pydantic import ConfigDict from langchain_community.llms.utils import enforce_stop_tokens @@ -55,8 +56,9 @@ class HuggingFaceHub(LLM): huggingfacehub_api_token: Optional[str] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/llms/huggingface_pipeline.py b/libs/community/langchain_community/llms/huggingface_pipeline.py index 1ccbc76954a6e..852e49c9385d4 100644 --- a/libs/community/langchain_community/llms/huggingface_pipeline.py +++ b/libs/community/langchain_community/llms/huggingface_pipeline.py @@ -8,6 +8,7 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import BaseLLM from langchain_core.outputs import Generation, GenerationChunk, LLMResult +from pydantic import ConfigDict DEFAULT_MODEL_ID = "gpt2" DEFAULT_TASK = "text-generation" @@ -69,8 +70,9 @@ class HuggingFacePipeline(BaseLLM): batch_size: int = DEFAULT_BATCH_SIZE """Batch size to use when passing multiple documents to generate.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @classmethod def from_model_id( diff --git a/libs/community/langchain_community/llms/huggingface_text_gen_inference.py b/libs/community/langchain_community/llms/huggingface_text_gen_inference.py index 56bcf69166ed0..97acb3ce78caf 100644 --- a/libs/community/langchain_community/llms/huggingface_text_gen_inference.py +++ b/libs/community/langchain_community/llms/huggingface_text_gen_inference.py @@ -8,8 +8,8 @@ ) from langchain_core.language_models.llms import LLM from langchain_core.outputs import GenerationChunk -from langchain_core.pydantic_v1 import Field, root_validator from langchain_core.utils import get_pydantic_field_names, pre_init +from pydantic import ConfigDict, Field, model_validator logger = logging.getLogger(__name__) @@ -103,11 +103,13 @@ class HuggingFaceTextGenInference(LLM): client: Any async_client: Any - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + 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", {}) diff --git a/libs/community/langchain_community/llms/human.py b/libs/community/langchain_community/llms/human.py index 39473c0ee1e0e..9a54b29aa10c2 100644 --- a/libs/community/langchain_community/llms/human.py +++ b/libs/community/langchain_community/llms/human.py @@ -2,7 +2,7 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import Field +from pydantic import Field from langchain_community.llms.utils import enforce_stop_tokens diff --git a/libs/community/langchain_community/llms/ipex_llm.py b/libs/community/langchain_community/llms/ipex_llm.py index c1f7f40c4e887..72d5afa342d25 100644 --- a/libs/community/langchain_community/llms/ipex_llm.py +++ b/libs/community/langchain_community/llms/ipex_llm.py @@ -3,6 +3,7 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM +from pydantic import ConfigDict DEFAULT_MODEL_ID = "gpt2" @@ -31,8 +32,9 @@ class IpexLLM(LLM): streaming: bool = True """Whether to stream the results, token by token.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @classmethod def from_model_id( diff --git a/libs/community/langchain_community/llms/javelin_ai_gateway.py b/libs/community/langchain_community/llms/javelin_ai_gateway.py index 3ba7fa3ccf9f3..156350308fac7 100644 --- a/libs/community/langchain_community/llms/javelin_ai_gateway.py +++ b/libs/community/langchain_community/llms/javelin_ai_gateway.py @@ -7,7 +7,7 @@ CallbackManagerForLLMRun, ) from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel # Ignoring type because below is valid pydantic code diff --git a/libs/community/langchain_community/llms/konko.py b/libs/community/langchain_community/llms/konko.py index 6cc422cd43c4b..0c2a62e92747f 100644 --- a/libs/community/langchain_community/llms/konko.py +++ b/libs/community/langchain_community/llms/konko.py @@ -9,7 +9,7 @@ CallbackManagerForLLMRun, ) from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import SecretStr, root_validator +from pydantic import ConfigDict, SecretStr, model_validator from langchain_community.utils.openai import is_openai_v1 @@ -60,11 +60,13 @@ class Konko(LLM): the response for each token generation step. """ - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict[str, Any]) -> Any: """Validate that python package exists in environment.""" try: import konko diff --git a/libs/community/langchain_community/llms/layerup_security.py b/libs/community/langchain_community/llms/layerup_security.py index dd8bb819284b4..c15626eff2318 100644 --- a/libs/community/langchain_community/llms/layerup_security.py +++ b/libs/community/langchain_community/llms/layerup_security.py @@ -3,7 +3,7 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import root_validator +from pydantic import model_validator logger = logging.getLogger(__name__) @@ -47,8 +47,9 @@ class LayerupSecurity(LLM): ) client: Any #: :meta private: - @root_validator(pre=True) - def validate_layerup_sdk(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def validate_layerup_sdk(cls, values: Dict[str, Any]) -> Any: try: from layerup_security import LayerupSecurity as LayerupSecuritySDK diff --git a/libs/community/langchain_community/llms/llamacpp.py b/libs/community/langchain_community/llms/llamacpp.py index 0bd6d7e010c0b..d206f138e6ba7 100644 --- a/libs/community/langchain_community/llms/llamacpp.py +++ b/libs/community/langchain_community/llms/llamacpp.py @@ -7,9 +7,9 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM from langchain_core.outputs import GenerationChunk -from langchain_core.pydantic_v1 import Field, root_validator from langchain_core.utils import get_pydantic_field_names, pre_init from langchain_core.utils.utils import build_extra_kwargs +from pydantic import Field, model_validator logger = logging.getLogger(__name__) @@ -194,8 +194,9 @@ def validate_environment(cls, values: Dict) -> Dict: pass return values - @root_validator(pre=True) - def build_model_kwargs(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def build_model_kwargs(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", {}) diff --git a/libs/community/langchain_community/llms/llamafile.py b/libs/community/langchain_community/llms/llamafile.py index 1f102bab52801..e168572c1b32a 100644 --- a/libs/community/langchain_community/llms/llamafile.py +++ b/libs/community/langchain_community/llms/llamafile.py @@ -9,6 +9,7 @@ from langchain_core.language_models.llms import LLM from langchain_core.outputs import GenerationChunk from langchain_core.utils import get_pydantic_field_names +from pydantic import ConfigDict class Llamafile(LLM): @@ -112,8 +113,9 @@ class Llamafile(LLM): mirostat_eta: float = 0.1 """Set the Mirostat learning rate, parameter eta. Default: 0.1.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @property def _llm_type(self) -> str: diff --git a/libs/community/langchain_community/llms/manifest.py b/libs/community/langchain_community/llms/manifest.py index 4806d8c219fe0..745de7e595401 100644 --- a/libs/community/langchain_community/llms/manifest.py +++ b/libs/community/langchain_community/llms/manifest.py @@ -3,6 +3,7 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM from langchain_core.utils import pre_init +from pydantic import ConfigDict class ManifestWrapper(LLM): @@ -11,8 +12,9 @@ class ManifestWrapper(LLM): client: Any #: :meta private: llm_kwargs: Optional[Dict] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/llms/minimax.py b/libs/community/langchain_community/llms/minimax.py index 18a71cfdab38a..e508eb4f959c8 100644 --- a/libs/community/langchain_community/llms/minimax.py +++ b/libs/community/langchain_community/llms/minimax.py @@ -15,8 +15,8 @@ CallbackManagerForLLMRun, ) from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import BaseModel, Field, SecretStr, root_validator from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import BaseModel, Field, SecretStr, model_validator from langchain_community.llms.utils import enforce_stop_tokens @@ -31,8 +31,9 @@ class _MinimaxEndpointClient(BaseModel): api_key: SecretStr api_url: str - @root_validator(pre=True) - def set_api_url(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def set_api_url(cls, values: Dict[str, Any]) -> Any: if "api_url" not in values: host = values["host"] group_id = values["group_id"] diff --git a/libs/community/langchain_community/llms/mlflow.py b/libs/community/langchain_community/llms/mlflow.py index 9b27ed57d8721..d8422629bcde3 100644 --- a/libs/community/langchain_community/llms/mlflow.py +++ b/libs/community/langchain_community/llms/mlflow.py @@ -5,7 +5,7 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models import LLM -from langchain_core.pydantic_v1 import Field, PrivateAttr +from pydantic import Field, PrivateAttr class Mlflow(LLM): diff --git a/libs/community/langchain_community/llms/mlflow_ai_gateway.py b/libs/community/langchain_community/llms/mlflow_ai_gateway.py index bd064c3dbff93..95196838e94fb 100644 --- a/libs/community/langchain_community/llms/mlflow_ai_gateway.py +++ b/libs/community/langchain_community/llms/mlflow_ai_gateway.py @@ -5,7 +5,7 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel # Ignoring type because below is valid pydantic code diff --git a/libs/community/langchain_community/llms/mlx_pipeline.py b/libs/community/langchain_community/llms/mlx_pipeline.py index 3486aa33e5895..0937c8f39d517 100644 --- a/libs/community/langchain_community/llms/mlx_pipeline.py +++ b/libs/community/langchain_community/llms/mlx_pipeline.py @@ -6,6 +6,7 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM from langchain_core.outputs import GenerationChunk +from pydantic import ConfigDict DEFAULT_MODEL_ID = "mlx-community/quantized-gemma-2b" @@ -74,8 +75,9 @@ class MLXPipeline(LLM): """ - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @classmethod def from_model_id( diff --git a/libs/community/langchain_community/llms/modal.py b/libs/community/langchain_community/llms/modal.py index 010127d4d5438..a68aa985d3d1d 100644 --- a/libs/community/langchain_community/llms/modal.py +++ b/libs/community/langchain_community/llms/modal.py @@ -4,8 +4,8 @@ import requests from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import Field, root_validator from langchain_core.utils.pydantic import get_fields +from pydantic import ConfigDict, Field, model_validator from langchain_community.llms.utils import enforce_stop_tokens @@ -35,11 +35,13 @@ class Modal(LLM): """Holds any model parameters valid for `create` call not explicitly specified.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def build_extra(cls, values: Dict[str, Any]) -> Any: """Build extra kwargs from additional params that were passed in.""" all_required_field_names = {field.alias for field in get_fields(cls).values()} diff --git a/libs/community/langchain_community/llms/moonshot.py b/libs/community/langchain_community/llms/moonshot.py index 390113c1f9c23..eedc482053fb6 100644 --- a/libs/community/langchain_community/llms/moonshot.py +++ b/libs/community/langchain_community/llms/moonshot.py @@ -3,8 +3,14 @@ import requests from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models import LLM -from langchain_core.pydantic_v1 import BaseModel, Field, SecretStr, root_validator from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import ( + BaseModel, + ConfigDict, + Field, + SecretStr, + model_validator, +) from langchain_community.llms.utils import enforce_stop_tokens @@ -44,8 +50,9 @@ class MoonshotCommon(BaseModel): temperature: float = 0.3 """Temperature parameter (higher values make the model more creative).""" - class Config: - allow_population_by_field_name = True + model_config = ConfigDict( + populate_by_name=True, + ) @property def lc_secrets(self) -> dict: @@ -69,8 +76,9 @@ def _default_params(self) -> Dict[str, Any]: def _invocation_params(self) -> Dict[str, Any]: return {**{"model": self.model_name}, **self._default_params} - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def build_extra(cls, values: Dict[str, Any]) -> Any: """Build extra parameters. Override the superclass method, prevent the model parameter from being overridden. @@ -112,8 +120,9 @@ class Moonshot(MoonshotCommon, LLM): moonshot = Moonshot(model="moonshot-v1-8k") """ - class Config: - allow_population_by_field_name = True + model_config = ConfigDict( + populate_by_name=True, + ) def _call( self, diff --git a/libs/community/langchain_community/llms/mosaicml.py b/libs/community/langchain_community/llms/mosaicml.py index a0a7eca2ed751..15464dc829d9a 100644 --- a/libs/community/langchain_community/llms/mosaicml.py +++ b/libs/community/langchain_community/llms/mosaicml.py @@ -4,6 +4,7 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM from langchain_core.utils import get_from_dict_or_env, pre_init +from pydantic import ConfigDict from langchain_community.llms.utils import enforce_stop_tokens @@ -58,8 +59,9 @@ class MosaicML(LLM): mosaicml_api_token: Optional[str] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/llms/nlpcloud.py b/libs/community/langchain_community/llms/nlpcloud.py index 31c5b0f8b25d3..cd014f8581f1f 100644 --- a/libs/community/langchain_community/llms/nlpcloud.py +++ b/libs/community/langchain_community/llms/nlpcloud.py @@ -2,8 +2,8 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import SecretStr from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import ConfigDict, SecretStr class NLPCloud(LLM): @@ -51,8 +51,9 @@ class NLPCloud(LLM): nlpcloud_api_key: Optional[SecretStr] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/llms/oci_data_science_model_deployment_endpoint.py b/libs/community/langchain_community/llms/oci_data_science_model_deployment_endpoint.py index 972d245302662..04f551ce27c42 100644 --- a/libs/community/langchain_community/llms/oci_data_science_model_deployment_endpoint.py +++ b/libs/community/langchain_community/llms/oci_data_science_model_deployment_endpoint.py @@ -4,8 +4,8 @@ import requests from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import Field from langchain_core.utils import get_from_dict_or_env, pre_init +from pydantic import Field logger = logging.getLogger(__name__) diff --git a/libs/community/langchain_community/llms/oci_generative_ai.py b/libs/community/langchain_community/llms/oci_generative_ai.py index 5c996fe3191dd..74a37c05972e5 100644 --- a/libs/community/langchain_community/llms/oci_generative_ai.py +++ b/libs/community/langchain_community/llms/oci_generative_ai.py @@ -8,8 +8,8 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM from langchain_core.outputs import GenerationChunk -from langchain_core.pydantic_v1 import BaseModel from langchain_core.utils import pre_init +from pydantic import BaseModel, ConfigDict, Field from langchain_community.llms.utils import enforce_stop_tokens @@ -61,7 +61,7 @@ class OCIAuthType(Enum): class OCIGenAIBase(BaseModel, ABC): """Base class for OCI GenAI models""" - client: Any #: :meta private: + client: Any = Field(default=None, exclude=True) #: :meta private: auth_type: Optional[str] = "API_KEY" """Authentication type, could be @@ -79,10 +79,10 @@ class OCIGenAIBase(BaseModel, ABC): If not specified , DEFAULT will be used """ - model_id: str = None # type: ignore[assignment] + model_id: Optional[str] = None """Id of the model to call, e.g., cohere.command""" - provider: str = None # type: ignore[assignment] + provider: Optional[str] = None """Provider name of the model. Default to None, will try to be derived from the model_id otherwise, requires user input @@ -91,15 +91,20 @@ class OCIGenAIBase(BaseModel, ABC): model_kwargs: Optional[Dict] = None """Keyword arguments to pass to the model""" - service_endpoint: str = None # type: ignore[assignment] + service_endpoint: Optional[str] = None """service endpoint url""" - compartment_id: str = None # type: ignore[assignment] + compartment_id: Optional[str] = None """OCID of compartment""" is_stream: bool = False """Whether to stream back partial progress""" + model_config = ConfigDict( + extra="forbid", + arbitrary_types_allowed=True, + ) + @pre_init def validate_environment(cls, values: Dict) -> Dict: """Validate that OCI config and python package exists in environment.""" @@ -189,6 +194,12 @@ def _get_provider(self, provider_map: Mapping[str, Any]) -> Any: if self.provider is not None: provider = self.provider else: + if self.model_id is None: + raise ValueError( + "model_id is required to derive the provider, " + "please provide the provider explicitly or specify " + "the model_id to derive the provider." + ) provider = self.model_id.split(".")[0].lower() if provider not in provider_map: @@ -230,8 +241,10 @@ class OCIGenAI(LLM, OCIGenAIBase): ) """ - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + arbitrary_types_allowed=True, + ) @property def _llm_type(self) -> str: @@ -260,6 +273,12 @@ def _prepare_invocation_object( if stop is not None: _model_kwargs[self._provider.stop_sequence_key] = stop + if self.model_id is None: + raise ValueError( + "model_id is required to call the model, " + "please provide the model_id." + ) + if self.model_id.startswith(CUSTOM_ENDPOINT_PREFIX): serving_mode = models.DedicatedServingMode(endpoint_id=self.model_id) else: diff --git a/libs/community/langchain_community/llms/octoai_endpoint.py b/libs/community/langchain_community/llms/octoai_endpoint.py index ff5302c3ff058..7a30c7c65b4e5 100644 --- a/libs/community/langchain_community/llms/octoai_endpoint.py +++ b/libs/community/langchain_community/llms/octoai_endpoint.py @@ -1,7 +1,7 @@ from typing import Any, Dict -from langchain_core.pydantic_v1 import Field, SecretStr from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import Field, SecretStr from langchain_community.llms.openai import BaseOpenAI from langchain_community.utils.openai import is_openai_v1 diff --git a/libs/community/langchain_community/llms/ollama.py b/libs/community/langchain_community/llms/ollama.py index afb540573dbb2..580ed68b330b6 100644 --- a/libs/community/langchain_community/llms/ollama.py +++ b/libs/community/langchain_community/llms/ollama.py @@ -23,6 +23,7 @@ from langchain_core.language_models import BaseLanguageModel from langchain_core.language_models.llms import BaseLLM from langchain_core.outputs import GenerationChunk, LLMResult +from pydantic import ConfigDict def _stream_response_to_generation_chunk( @@ -397,8 +398,9 @@ class Ollama(BaseLLM, _OllamaCommon): ollama = Ollama(model="llama2") """ - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @property def _llm_type(self) -> str: diff --git a/libs/community/langchain_community/llms/opaqueprompts.py b/libs/community/langchain_community/llms/opaqueprompts.py index 39c8d3ee88000..46a2a2b36fb71 100644 --- a/libs/community/langchain_community/llms/opaqueprompts.py +++ b/libs/community/langchain_community/llms/opaqueprompts.py @@ -6,6 +6,7 @@ from langchain_core.language_models.llms import LLM from langchain_core.messages import AIMessage from langchain_core.utils import get_from_dict_or_env, pre_init +from pydantic import ConfigDict logger = logging.getLogger(__name__) @@ -32,8 +33,9 @@ class OpaquePrompts(LLM): base_llm: BaseLanguageModel """The base LLM to use.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/llms/openai.py b/libs/community/langchain_community/llms/openai.py index c912f8e41500f..aeb16883fd434 100644 --- a/libs/community/langchain_community/llms/openai.py +++ b/libs/community/langchain_community/llms/openai.py @@ -28,7 +28,6 @@ ) from langchain_core.language_models.llms import BaseLLM, create_base_retry_decorator from langchain_core.outputs import Generation, GenerationChunk, LLMResult -from langchain_core.pydantic_v1 import Field, root_validator from langchain_core.utils import ( get_from_dict_or_env, get_pydantic_field_names, @@ -36,6 +35,7 @@ ) from langchain_core.utils.pydantic import get_fields from langchain_core.utils.utils import build_extra_kwargs +from pydantic import ConfigDict, Field, model_validator from langchain_community.utils.openai import is_openai_v1 @@ -259,11 +259,13 @@ def __new__(cls, **data: Any) -> Union[OpenAIChat, BaseOpenAI]: # type: ignore return OpenAIChat(**data) return super().__new__(cls) - class Config: - allow_population_by_field_name = True + model_config = ConfigDict( + populate_by_name=True, + ) - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + 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", {}) @@ -1012,8 +1014,9 @@ class OpenAIChat(BaseLLM): disallowed_special: Union[Literal["all"], Collection[str]] = "all" """Set of special tokens that are not allowed。""" - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def build_extra(cls, values: Dict[str, Any]) -> Any: """Build extra kwargs from additional params that were passed in.""" all_required_field_names = {field.alias for field in get_fields(cls).values()} diff --git a/libs/community/langchain_community/llms/openllm.py b/libs/community/langchain_community/llms/openllm.py index 7fa00b0d79122..21f4849538011 100644 --- a/libs/community/langchain_community/llms/openllm.py +++ b/libs/community/langchain_community/llms/openllm.py @@ -20,7 +20,7 @@ CallbackManagerForLLMRun, ) from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import PrivateAttr +from pydantic import ConfigDict, PrivateAttr if TYPE_CHECKING: import openllm @@ -97,8 +97,9 @@ class OpenLLM(LLM): PrivateAttr(default=None) ) - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @overload def __init__( diff --git a/libs/community/langchain_community/llms/petals.py b/libs/community/langchain_community/llms/petals.py index 7506e3bf5c802..3f8e4f87333a9 100644 --- a/libs/community/langchain_community/llms/petals.py +++ b/libs/community/langchain_community/llms/petals.py @@ -3,9 +3,9 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import Field, SecretStr, root_validator from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init from langchain_core.utils.pydantic import get_fields +from pydantic import ConfigDict, Field, SecretStr, model_validator from langchain_community.llms.utils import enforce_stop_tokens @@ -63,11 +63,13 @@ class Petals(LLM): huggingface_api_key: Optional[SecretStr] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def build_extra(cls, values: Dict[str, Any]) -> Any: """Build extra kwargs from additional params that were passed in.""" all_required_field_names = {field.alias for field in get_fields(cls).values()} diff --git a/libs/community/langchain_community/llms/pipelineai.py b/libs/community/langchain_community/llms/pipelineai.py index d3616a4f60dc2..8d0f6e1579de4 100644 --- a/libs/community/langchain_community/llms/pipelineai.py +++ b/libs/community/langchain_community/llms/pipelineai.py @@ -3,14 +3,14 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import ( +from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import ( BaseModel, + ConfigDict, Field, SecretStr, - root_validator, + model_validator, ) -from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init -from langchain_core.utils.pydantic import get_fields from langchain_community.llms.utils import enforce_stop_tokens @@ -42,13 +42,15 @@ class PipelineAI(LLM, BaseModel): pipeline_api_key: Optional[SecretStr] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def build_extra(cls, values: Dict[str, Any]) -> Any: """Build extra kwargs from additional params that were passed in.""" - all_required_field_names = {field.alias for field in get_fields(cls).values()} + all_required_field_names = set(list(cls.model_fields.keys())) extra = values.get("pipeline_kwargs", {}) for field_name in list(values): diff --git a/libs/community/langchain_community/llms/predibase.py b/libs/community/langchain_community/llms/predibase.py index 398c169b4a22e..fbabdc04e70a8 100644 --- a/libs/community/langchain_community/llms/predibase.py +++ b/libs/community/langchain_community/llms/predibase.py @@ -3,7 +3,7 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import Field, SecretStr +from pydantic import Field, SecretStr class Predibase(LLM): @@ -34,8 +34,7 @@ class Predibase(LLM): { "max_new_tokens": 256, "temperature": 0.1, - }, - const=True, + } ) @property diff --git a/libs/community/langchain_community/llms/predictionguard.py b/libs/community/langchain_community/llms/predictionguard.py index 65196476e5d7f..9e85daf4ba66e 100644 --- a/libs/community/langchain_community/llms/predictionguard.py +++ b/libs/community/langchain_community/llms/predictionguard.py @@ -4,6 +4,7 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM from langchain_core.utils import get_from_dict_or_env, pre_init +from pydantic import ConfigDict from langchain_community.llms.utils import enforce_stop_tokens @@ -47,8 +48,9 @@ class PredictionGuard(LLM): stop: Optional[List[str]] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/llms/replicate.py b/libs/community/langchain_community/llms/replicate.py index 7235e57d7f4b4..ad4dceaf8f301 100644 --- a/libs/community/langchain_community/llms/replicate.py +++ b/libs/community/langchain_community/llms/replicate.py @@ -6,9 +6,9 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM from langchain_core.outputs import GenerationChunk -from langchain_core.pydantic_v1 import Field, root_validator from langchain_core.utils import get_from_dict_or_env, pre_init from langchain_core.utils.pydantic import get_fields +from pydantic import ConfigDict, Field, model_validator if TYPE_CHECKING: from replicate.prediction import Prediction @@ -56,9 +56,10 @@ class Replicate(LLM): stop: List[str] = Field(default_factory=list) """Stop sequences to early-terminate generation.""" - class Config: - allow_population_by_field_name = True - extra = "forbid" + model_config = ConfigDict( + populate_by_name=True, + extra="forbid", + ) @property def lc_secrets(self) -> Dict[str, str]: @@ -73,8 +74,9 @@ def get_lc_namespace(cls) -> List[str]: """Get the namespace of the langchain object.""" return ["langchain", "llms", "replicate"] - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def build_extra(cls, values: Dict[str, Any]) -> Any: """Build extra kwargs from additional params that were passed in.""" all_required_field_names = {field.alias for field in get_fields(cls).values()} diff --git a/libs/community/langchain_community/llms/rwkv.py b/libs/community/langchain_community/llms/rwkv.py index 0f1aaf74777ea..9273467c7fb09 100644 --- a/libs/community/langchain_community/llms/rwkv.py +++ b/libs/community/langchain_community/llms/rwkv.py @@ -8,8 +8,8 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import BaseModel from langchain_core.utils import pre_init +from pydantic import BaseModel, ConfigDict from langchain_community.llms.utils import enforce_stop_tokens @@ -74,8 +74,9 @@ class RWKV(LLM, BaseModel): model_state: Any = None #: :meta private: - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @property def _default_params(self) -> Dict[str, Any]: diff --git a/libs/community/langchain_community/llms/sagemaker_endpoint.py b/libs/community/langchain_community/llms/sagemaker_endpoint.py index 80025eebb3841..db86beb9e0fa2 100644 --- a/libs/community/langchain_community/llms/sagemaker_endpoint.py +++ b/libs/community/langchain_community/llms/sagemaker_endpoint.py @@ -8,6 +8,7 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM from langchain_core.utils import pre_init +from pydantic import ConfigDict from langchain_community.llms.utils import enforce_stop_tokens @@ -244,8 +245,9 @@ def transform_output(self, output: bytes) -> str: .. _boto3: """ - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/llms/sambanova.py b/libs/community/langchain_community/llms/sambanova.py index 187c1708fe425..5441f53678d75 100644 --- a/libs/community/langchain_community/llms/sambanova.py +++ b/libs/community/langchain_community/llms/sambanova.py @@ -6,6 +6,7 @@ from langchain_core.language_models.llms import LLM from langchain_core.outputs import GenerationChunk from langchain_core.utils import get_from_dict_or_env, pre_init +from pydantic import ConfigDict class SVEndpointHandler: @@ -209,8 +210,9 @@ class Sambaverse(LLM): streaming: Optional[bool] = False """Streaming flag to get streamed response.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @classmethod def is_lc_serializable(cls) -> bool: @@ -727,8 +729,9 @@ class SambaStudio(LLM): streaming: Optional[bool] = False """Streaming flag to get streamed response.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @classmethod def is_lc_serializable(cls) -> bool: diff --git a/libs/community/langchain_community/llms/self_hosted.py b/libs/community/langchain_community/llms/self_hosted.py index 320a4a651e2d2..5026c2108a7db 100644 --- a/libs/community/langchain_community/llms/self_hosted.py +++ b/libs/community/langchain_community/llms/self_hosted.py @@ -5,6 +5,7 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM +from pydantic import ConfigDict from langchain_community.llms.utils import enforce_stop_tokens @@ -143,8 +144,9 @@ def inference_fn(pipeline, prompt, stop = None): loading compromised data. """ - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def __init__(self, **kwargs: Any): """Init the pipeline with an auxiliary function. diff --git a/libs/community/langchain_community/llms/self_hosted_hugging_face.py b/libs/community/langchain_community/llms/self_hosted_hugging_face.py index 7358d4c54822d..5003a86aeb332 100644 --- a/libs/community/langchain_community/llms/self_hosted_hugging_face.py +++ b/libs/community/langchain_community/llms/self_hosted_hugging_face.py @@ -3,6 +3,7 @@ from typing import Any, Callable, List, Mapping, Optional from langchain_core.callbacks import CallbackManagerForLLMRun +from pydantic import ConfigDict from langchain_community.llms.self_hosted import SelfHostedPipeline from langchain_community.llms.utils import enforce_stop_tokens @@ -168,8 +169,9 @@ def get_pipeline(): inference_fn: Callable = _generate_text #: :meta private: """Inference function to send to the remote hardware.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def __init__(self, **kwargs: Any): """Construct the pipeline remotely using an auxiliary function. diff --git a/libs/community/langchain_community/llms/solar.py b/libs/community/langchain_community/llms/solar.py index 4afc6bd97e225..32dc00b98b29b 100644 --- a/libs/community/langchain_community/llms/solar.py +++ b/libs/community/langchain_community/llms/solar.py @@ -3,8 +3,14 @@ import requests from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models import LLM -from langchain_core.pydantic_v1 import BaseModel, Field, SecretStr, root_validator from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import ( + BaseModel, + ConfigDict, + Field, + SecretStr, + model_validator, +) from langchain_community.llms.utils import enforce_stop_tokens @@ -43,10 +49,11 @@ class SolarCommon(BaseModel): max_tokens: int = Field(default=1024) temperature: float = 0.3 - class Config: - allow_population_by_field_name = True - arbitrary_types_allowed = True - extra = "ignore" + model_config = ConfigDict( + populate_by_name=True, + arbitrary_types_allowed=True, + extra="ignore", + ) @property def lc_secrets(self) -> dict: @@ -64,8 +71,9 @@ def _default_params(self) -> Dict[str, Any]: def _invocation_params(self) -> Dict[str, Any]: return {**{"model": self.model_name}, **self._default_params} - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def build_extra(cls, values: Dict[str, Any]) -> Any: return values @pre_init @@ -100,8 +108,9 @@ class Solar(SolarCommon, LLM): Referenced from https://console.upstage.ai/services/solar """ - class Config: - allow_population_by_field_name = True + model_config = ConfigDict( + populate_by_name=True, + ) def _call( self, diff --git a/libs/community/langchain_community/llms/sparkllm.py b/libs/community/langchain_community/llms/sparkllm.py index d3741aa93d9b3..8f0ead4d27d98 100644 --- a/libs/community/langchain_community/llms/sparkllm.py +++ b/libs/community/langchain_community/llms/sparkllm.py @@ -17,8 +17,8 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM from langchain_core.outputs import GenerationChunk -from langchain_core.pydantic_v1 import Field from langchain_core.utils import get_from_dict_or_env, pre_init +from pydantic import Field logger = logging.getLogger(__name__) diff --git a/libs/community/langchain_community/llms/stochasticai.py b/libs/community/langchain_community/llms/stochasticai.py index d97fd1e5ee2e2..0e999bcb3ae45 100644 --- a/libs/community/langchain_community/llms/stochasticai.py +++ b/libs/community/langchain_community/llms/stochasticai.py @@ -5,9 +5,8 @@ import requests from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import Field, SecretStr, root_validator from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init -from langchain_core.utils.pydantic import get_fields +from pydantic import ConfigDict, Field, SecretStr, model_validator from langchain_community.llms.utils import enforce_stop_tokens @@ -36,13 +35,15 @@ class StochasticAI(LLM): stochasticai_api_key: Optional[SecretStr] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def build_extra(cls, values: Dict[str, Any]) -> Any: """Build extra kwargs from additional params that were passed in.""" - all_required_field_names = {field.alias for field in get_fields(cls).values()} + all_required_field_names = set(list(cls.model_fields.keys())) extra = values.get("model_kwargs", {}) for field_name in list(values): diff --git a/libs/community/langchain_community/llms/symblai_nebula.py b/libs/community/langchain_community/llms/symblai_nebula.py index 1b8d07be8275a..63bb29a9a506a 100644 --- a/libs/community/langchain_community/llms/symblai_nebula.py +++ b/libs/community/langchain_community/llms/symblai_nebula.py @@ -5,8 +5,8 @@ import requests from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import SecretStr from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import ConfigDict, SecretStr from requests import ConnectTimeout, ReadTimeout, RequestException from tenacity import ( before_sleep_log, @@ -60,8 +60,9 @@ class Nebula(LLM): stop_sequences: Optional[List[str]] = None max_retries: Optional[int] = 10 - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/llms/textgen.py b/libs/community/langchain_community/llms/textgen.py index aa3a490fa2958..a9a100d402625 100644 --- a/libs/community/langchain_community/llms/textgen.py +++ b/libs/community/langchain_community/llms/textgen.py @@ -9,7 +9,7 @@ ) from langchain_core.language_models.llms import LLM from langchain_core.outputs import GenerationChunk -from langchain_core.pydantic_v1 import Field +from pydantic import Field logger = logging.getLogger(__name__) diff --git a/libs/community/langchain_community/llms/titan_takeoff.py b/libs/community/langchain_community/llms/titan_takeoff.py index 5ff1f20a568f4..f89a1f5e3ac76 100644 --- a/libs/community/langchain_community/llms/titan_takeoff.py +++ b/libs/community/langchain_community/llms/titan_takeoff.py @@ -4,7 +4,7 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM from langchain_core.outputs import GenerationChunk -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel, ConfigDict from langchain_community.llms.utils import enforce_stop_tokens @@ -19,8 +19,9 @@ class Device(str, Enum): class ReaderConfig(BaseModel): """Configuration for the reader to be deployed in Titan Takeoff API.""" - class Config: - protected_namespaces = () + model_config = ConfigDict( + protected_namespaces=(), + ) model_name: str """The name of the model to use""" diff --git a/libs/community/langchain_community/llms/together.py b/libs/community/langchain_community/llms/together.py index a0729c3723a42..e5e7b8d68bd85 100644 --- a/libs/community/langchain_community/llms/together.py +++ b/libs/community/langchain_community/llms/together.py @@ -10,8 +10,8 @@ CallbackManagerForLLMRun, ) from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import SecretStr, root_validator from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env +from pydantic import ConfigDict, SecretStr, model_validator from langchain_community.utilities.requests import Requests @@ -66,11 +66,13 @@ class Together(LLM): the response for each token generation step. """ - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key exists in environment.""" values["together_api_key"] = convert_to_secret_str( get_from_dict_or_env(values, "together_api_key", "TOGETHER_API_KEY") diff --git a/libs/community/langchain_community/llms/tongyi.py b/libs/community/langchain_community/llms/tongyi.py index b4d79a52c1c2c..65bdd47ceaed7 100644 --- a/libs/community/langchain_community/llms/tongyi.py +++ b/libs/community/langchain_community/llms/tongyi.py @@ -24,8 +24,8 @@ ) from langchain_core.language_models.llms import BaseLLM from langchain_core.outputs import Generation, GenerationChunk, LLMResult -from langchain_core.pydantic_v1 import Field from langchain_core.utils import get_from_dict_or_env, pre_init +from pydantic import Field from requests.exceptions import HTTPError from tenacity import ( before_sleep_log, diff --git a/libs/community/langchain_community/llms/vertexai.py b/libs/community/langchain_community/llms/vertexai.py index 19409c004dea8..35fd883fdd7b5 100644 --- a/libs/community/langchain_community/llms/vertexai.py +++ b/libs/community/langchain_community/llms/vertexai.py @@ -10,8 +10,8 @@ ) from langchain_core.language_models.llms import BaseLLM from langchain_core.outputs import Generation, GenerationChunk, LLMResult -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.utils import pre_init +from pydantic import BaseModel, Field from langchain_community.utilities.vertexai import ( create_retry_decorator, diff --git a/libs/community/langchain_community/llms/vllm.py b/libs/community/langchain_community/llms/vllm.py index f4eddf46350ef..83e28b7ac71d5 100644 --- a/libs/community/langchain_community/llms/vllm.py +++ b/libs/community/langchain_community/llms/vllm.py @@ -3,8 +3,8 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import BaseLLM from langchain_core.outputs import Generation, LLMResult -from langchain_core.pydantic_v1 import Field from langchain_core.utils import pre_init +from pydantic import Field from langchain_community.llms.openai import BaseOpenAI from langchain_community.utils.openai import is_openai_v1 diff --git a/libs/community/langchain_community/llms/volcengine_maas.py b/libs/community/langchain_community/llms/volcengine_maas.py index dab4989adf3af..1046eb4544b09 100644 --- a/libs/community/langchain_community/llms/volcengine_maas.py +++ b/libs/community/langchain_community/llms/volcengine_maas.py @@ -5,8 +5,8 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM from langchain_core.outputs import GenerationChunk -from langchain_core.pydantic_v1 import BaseModel, Field, SecretStr from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import BaseModel, Field, SecretStr class VolcEngineMaasBase(BaseModel): diff --git a/libs/community/langchain_community/llms/watsonxllm.py b/libs/community/langchain_community/llms/watsonxllm.py index 2e910b23323bb..a19909e2da2f9 100644 --- a/libs/community/langchain_community/llms/watsonxllm.py +++ b/libs/community/langchain_community/llms/watsonxllm.py @@ -6,8 +6,8 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import BaseLLM from langchain_core.outputs import Generation, GenerationChunk, LLMResult -from langchain_core.pydantic_v1 import SecretStr from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import ConfigDict, SecretStr logger = logging.getLogger(__name__) @@ -95,8 +95,9 @@ class WatsonxLLM(BaseLLM): watsonx_model: Any - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @classmethod def is_lc_serializable(cls) -> bool: diff --git a/libs/community/langchain_community/llms/weight_only_quantization.py b/libs/community/langchain_community/llms/weight_only_quantization.py index ddce10c976525..35302ff6fd578 100644 --- a/libs/community/langchain_community/llms/weight_only_quantization.py +++ b/libs/community/langchain_community/llms/weight_only_quantization.py @@ -3,6 +3,7 @@ from langchain_core.callbacks.manager import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM +from pydantic import ConfigDict from langchain_community.llms.utils import enforce_stop_tokens @@ -71,8 +72,9 @@ class WeightOnlyQuantPipeline(LLM): pipeline_kwargs: Optional[dict] = None """Key word arguments passed to the pipeline.""" - class Config: - extra = "allow" + model_config = ConfigDict( + extra="allow", + ) @classmethod def from_model_id( diff --git a/libs/community/langchain_community/llms/writer.py b/libs/community/langchain_community/llms/writer.py index feccd05a282a1..d82a346c43616 100644 --- a/libs/community/langchain_community/llms/writer.py +++ b/libs/community/langchain_community/llms/writer.py @@ -4,6 +4,7 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM from langchain_core.utils import get_from_dict_or_env, pre_init +from pydantic import ConfigDict from langchain_community.llms.utils import enforce_stop_tokens @@ -63,8 +64,9 @@ class Writer(LLM): base_url: Optional[str] = None """Base url to use, if None decides based on model name.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/llms/yandex.py b/libs/community/langchain_community/llms/yandex.py index b8761acd62b3a..cc615ac6d7390 100644 --- a/libs/community/langchain_community/llms/yandex.py +++ b/libs/community/langchain_community/llms/yandex.py @@ -9,8 +9,8 @@ ) from langchain_core.language_models.llms import LLM from langchain_core.load.serializable import Serializable -from langchain_core.pydantic_v1 import SecretStr from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import SecretStr from tenacity import ( before_sleep_log, retry, diff --git a/libs/community/langchain_community/llms/yi.py b/libs/community/langchain_community/llms/yi.py index 8a4557ab199f6..6b41e8d33789f 100644 --- a/libs/community/langchain_community/llms/yi.py +++ b/libs/community/langchain_community/llms/yi.py @@ -7,8 +7,8 @@ import requests from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import Field, SecretStr from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env +from pydantic import Field, SecretStr from langchain_community.llms.utils import enforce_stop_tokens diff --git a/libs/community/langchain_community/llms/you.py b/libs/community/langchain_community/llms/you.py index 54c1c31bfec9c..20ba6ca451bfe 100644 --- a/libs/community/langchain_community/llms/you.py +++ b/libs/community/langchain_community/llms/you.py @@ -5,7 +5,7 @@ from langchain_core.callbacks.manager import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM from langchain_core.outputs import GenerationChunk -from langchain_core.pydantic_v1 import Field +from pydantic import Field SMART_ENDPOINT = "https://chat-api.you.com/smart" RESEARCH_ENDPOINT = "https://chat-api.you.com/research" diff --git a/libs/community/langchain_community/llms/yuan2.py b/libs/community/langchain_community/llms/yuan2.py index 4373ba622e79f..1087d0cb006d5 100644 --- a/libs/community/langchain_community/llms/yuan2.py +++ b/libs/community/langchain_community/llms/yuan2.py @@ -5,7 +5,7 @@ import requests from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import Field +from pydantic import Field from langchain_community.llms.utils import enforce_stop_tokens diff --git a/libs/community/langchain_community/memory/kg.py b/libs/community/langchain_community/memory/kg.py index 09787d396c7cc..2a4828d81ff8d 100644 --- a/libs/community/langchain_community/memory/kg.py +++ b/libs/community/langchain_community/memory/kg.py @@ -3,7 +3,7 @@ from langchain_core.language_models import BaseLanguageModel from langchain_core.messages import BaseMessage, SystemMessage, get_buffer_string from langchain_core.prompts import BasePromptTemplate -from langchain_core.pydantic_v1 import Field +from pydantic import Field from langchain_community.graphs import NetworkxEntityGraph from langchain_community.graphs.networkx_graph import ( diff --git a/libs/community/langchain_community/output_parsers/ernie_functions.py b/libs/community/langchain_community/output_parsers/ernie_functions.py index 93fe9771cb2a6..8b80134af22df 100644 --- a/libs/community/langchain_community/output_parsers/ernie_functions.py +++ b/libs/community/langchain_community/output_parsers/ernie_functions.py @@ -13,7 +13,7 @@ ChatGeneration, Generation, ) -from langchain_core.pydantic_v1 import BaseModel, root_validator +from pydantic import BaseModel, model_validator class OutputFunctionsParser(BaseGenerationOutputParser[Any]): @@ -143,8 +143,9 @@ class PydanticOutputFunctionsParser(OutputFunctionsParser): pydantic_schema: Union[Type[BaseModel], Dict[str, Type[BaseModel]]] """The pydantic schema to parse the output with.""" - @root_validator(pre=True) - def validate_schema(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_schema(cls, values: Dict) -> Any: schema = values["pydantic_schema"] if "args_only" not in values: values["args_only"] = isinstance(schema, type) and issubclass( diff --git a/libs/community/langchain_community/retrievers/arcee.py b/libs/community/langchain_community/retrievers/arcee.py index 6d2b6f0b8009a..ebdc8301cfe71 100644 --- a/libs/community/langchain_community/retrievers/arcee.py +++ b/libs/community/langchain_community/retrievers/arcee.py @@ -2,9 +2,9 @@ from langchain_core.callbacks import CallbackManagerForRetrieverRun from langchain_core.documents import Document -from langchain_core.pydantic_v1 import SecretStr from langchain_core.retrievers import BaseRetriever from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import ConfigDict, SecretStr from langchain_community.utilities.arcee import ArceeWrapper, DALMFilter @@ -49,9 +49,9 @@ class ArceeRetriever(BaseRetriever): model_kwargs: Optional[Dict[str, Any]] = None """Keyword arguments to pass to the model.""" - class Config: - extra = "forbid" - underscore_attrs_are_private = True + model_config = ConfigDict( + extra="forbid", + ) def __init__(self, **data: Any) -> None: """Initializes private fields.""" diff --git a/libs/community/langchain_community/retrievers/azure_ai_search.py b/libs/community/langchain_community/retrievers/azure_ai_search.py index e2ab7f74f90b4..beb0dc05972ad 100644 --- a/libs/community/langchain_community/retrievers/azure_ai_search.py +++ b/libs/community/langchain_community/retrievers/azure_ai_search.py @@ -1,7 +1,7 @@ from __future__ import annotations import json -from typing import Dict, List, Optional +from typing import Any, Dict, List, Optional import aiohttp import requests @@ -10,9 +10,9 @@ CallbackManagerForRetrieverRun, ) from langchain_core.documents import Document -from langchain_core.pydantic_v1 import root_validator from langchain_core.retrievers import BaseRetriever from langchain_core.utils import get_from_dict_or_env, get_from_env +from pydantic import ConfigDict, model_validator DEFAULT_URL_SUFFIX = "search.windows.net" """Default URL Suffix for endpoint connection - commercial cloud""" @@ -103,12 +103,14 @@ def format_docs(docs): filter: Optional[str] = None """OData $filter expression to apply to the search query.""" - class Config: - arbitrary_types_allowed = True - extra = "forbid" + model_config = ConfigDict( + arbitrary_types_allowed=True, + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that service name, index name and api key exists in environment.""" values["service_name"] = get_from_dict_or_env( values, "service_name", "AZURE_AI_SEARCH_SERVICE_NAME" diff --git a/libs/community/langchain_community/retrievers/bedrock.py b/libs/community/langchain_community/retrievers/bedrock.py index a2a05f77496e5..6415bc9767833 100644 --- a/libs/community/langchain_community/retrievers/bedrock.py +++ b/libs/community/langchain_community/retrievers/bedrock.py @@ -2,8 +2,8 @@ from langchain_core.callbacks import CallbackManagerForRetrieverRun from langchain_core.documents import Document -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.retrievers import BaseRetriever +from pydantic import BaseModel, model_validator class VectorSearchConfig(BaseModel, extra="allow"): # type: ignore[call-arg] @@ -105,8 +105,9 @@ def format_docs(docs): client: Any retrieval_config: RetrievalConfig - @root_validator(pre=True) - def create_client(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def create_client(cls, values: Dict[str, Any]) -> Any: if values.get("client") is not None: return values diff --git a/libs/community/langchain_community/retrievers/bm25.py b/libs/community/langchain_community/retrievers/bm25.py index ae16ee2df3db6..a654d3626ab8c 100644 --- a/libs/community/langchain_community/retrievers/bm25.py +++ b/libs/community/langchain_community/retrievers/bm25.py @@ -4,8 +4,8 @@ from langchain_core.callbacks import CallbackManagerForRetrieverRun from langchain_core.documents import Document -from langchain_core.pydantic_v1 import Field from langchain_core.retrievers import BaseRetriever +from pydantic import ConfigDict, Field def default_preprocessing_func(text: str) -> List[str]: @@ -24,8 +24,9 @@ class BM25Retriever(BaseRetriever): preprocess_func: Callable[[str], List[str]] = default_preprocessing_func """ Preprocessing function to use on the text before BM25 vectorization.""" - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) @classmethod def from_texts( diff --git a/libs/community/langchain_community/retrievers/chatgpt_plugin_retriever.py b/libs/community/langchain_community/retrievers/chatgpt_plugin_retriever.py index 178d99c8a178d..08559110bca89 100644 --- a/libs/community/langchain_community/retrievers/chatgpt_plugin_retriever.py +++ b/libs/community/langchain_community/retrievers/chatgpt_plugin_retriever.py @@ -10,6 +10,7 @@ ) from langchain_core.documents import Document from langchain_core.retrievers import BaseRetriever +from pydantic import ConfigDict class ChatGPTPluginRetriever(BaseRetriever): @@ -26,8 +27,9 @@ class ChatGPTPluginRetriever(BaseRetriever): aiosession: Optional[aiohttp.ClientSession] = None """Aiohttp session to use for requests.""" - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def _get_relevant_documents( self, query: str, *, run_manager: CallbackManagerForRetrieverRun diff --git a/libs/community/langchain_community/retrievers/cohere_rag_retriever.py b/libs/community/langchain_community/retrievers/cohere_rag_retriever.py index dd0f8d21872d7..f76aafa2900f7 100644 --- a/libs/community/langchain_community/retrievers/cohere_rag_retriever.py +++ b/libs/community/langchain_community/retrievers/cohere_rag_retriever.py @@ -10,8 +10,8 @@ from langchain_core.documents import Document from langchain_core.language_models.chat_models import BaseChatModel from langchain_core.messages import HumanMessage -from langchain_core.pydantic_v1 import Field from langchain_core.retrievers import BaseRetriever +from pydantic import ConfigDict, Field if TYPE_CHECKING: from langchain_core.messages import BaseMessage @@ -61,8 +61,9 @@ class CohereRagRetriever(BaseRetriever): llm: BaseChatModel """Cohere ChatModel to use.""" - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def _get_relevant_documents( self, query: str, *, run_manager: CallbackManagerForRetrieverRun, **kwargs: Any diff --git a/libs/community/langchain_community/retrievers/docarray.py b/libs/community/langchain_community/retrievers/docarray.py index e258735be2c2a..4531028fb6ecf 100644 --- a/libs/community/langchain_community/retrievers/docarray.py +++ b/libs/community/langchain_community/retrievers/docarray.py @@ -7,6 +7,7 @@ from langchain_core.embeddings import Embeddings from langchain_core.retrievers import BaseRetriever from langchain_core.utils.pydantic import get_fields +from pydantic import ConfigDict from langchain_community.vectorstores.utils import maximal_marginal_relevance @@ -45,8 +46,9 @@ class DocArrayRetriever(BaseRetriever): top_k: int = 1 filters: Optional[Any] = None - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def _get_relevant_documents( self, diff --git a/libs/community/langchain_community/retrievers/google_vertex_ai_search.py b/libs/community/langchain_community/retrievers/google_vertex_ai_search.py index 6b836389a77ff..41625823567c7 100644 --- a/libs/community/langchain_community/retrievers/google_vertex_ai_search.py +++ b/libs/community/langchain_community/retrievers/google_vertex_ai_search.py @@ -7,9 +7,9 @@ from langchain_core._api.deprecation import deprecated from langchain_core.callbacks import CallbackManagerForRetrieverRun from langchain_core.documents import Document -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator from langchain_core.retrievers import BaseRetriever from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, Field, model_validator from langchain_community.utilities.vertexai import get_client_info @@ -46,8 +46,9 @@ class _BaseGoogleVertexAISearchRetriever(BaseModel): 3 - Blended search """ - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validates the environment.""" try: from google.cloud import discoveryengine_v1beta # noqa: F401 @@ -245,10 +246,10 @@ class GoogleVertexAISearchRetriever(BaseRetriever, _BaseGoogleVertexAISearchRetr _client: SearchServiceClient _serving_config: str - class Config: - arbitrary_types_allowed = True - extra = "ignore" - underscore_attrs_are_private = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + extra="ignore", + ) def __init__(self, **kwargs: Any) -> None: """Initializes private fields.""" @@ -410,10 +411,10 @@ class GoogleVertexAIMultiTurnSearchRetriever( _client: ConversationalSearchServiceClient _serving_config: str - class Config: - arbitrary_types_allowed = True - extra = "ignore" - underscore_attrs_are_private = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + extra="ignore", + ) def __init__(self, **kwargs: Any): super().__init__(**kwargs) diff --git a/libs/community/langchain_community/retrievers/kendra.py b/libs/community/langchain_community/retrievers/kendra.py index c5c3023ea78d7..7c824b0eacf9c 100644 --- a/libs/community/langchain_community/retrievers/kendra.py +++ b/libs/community/langchain_community/retrievers/kendra.py @@ -13,13 +13,13 @@ from langchain_core.callbacks import CallbackManagerForRetrieverRun from langchain_core.documents import Document -from langchain_core.pydantic_v1 import ( +from langchain_core.retrievers import BaseRetriever +from pydantic import ( BaseModel, Field, - root_validator, + model_validator, validator, ) -from langchain_core.retrievers import BaseRetriever from typing_extensions import Annotated @@ -382,8 +382,9 @@ def validate_top_k(cls, value: int) -> int: raise ValueError(f"top_k ({value}) cannot be negative.") return value - @root_validator(pre=True) - def create_client(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def create_client(cls, values: Dict[str, Any]) -> Any: if values.get("client") is not None: return values diff --git a/libs/community/langchain_community/retrievers/knn.py b/libs/community/langchain_community/retrievers/knn.py index 2266f98f20121..ed76aa41b7bdb 100644 --- a/libs/community/langchain_community/retrievers/knn.py +++ b/libs/community/langchain_community/retrievers/knn.py @@ -12,6 +12,7 @@ from langchain_core.documents import Document from langchain_core.embeddings import Embeddings from langchain_core.retrievers import BaseRetriever +from pydantic import ConfigDict def create_index(contexts: List[str], embeddings: Embeddings) -> np.ndarray: @@ -45,8 +46,9 @@ class KNNRetriever(BaseRetriever): relevancy_threshold: Optional[float] = None """Threshold for relevancy.""" - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) @classmethod def from_texts( diff --git a/libs/community/langchain_community/retrievers/llama_index.py b/libs/community/langchain_community/retrievers/llama_index.py index 9106c058d7548..433def090011f 100644 --- a/libs/community/langchain_community/retrievers/llama_index.py +++ b/libs/community/langchain_community/retrievers/llama_index.py @@ -2,8 +2,8 @@ from langchain_core.callbacks import CallbackManagerForRetrieverRun from langchain_core.documents import Document -from langchain_core.pydantic_v1 import Field from langchain_core.retrievers import BaseRetriever +from pydantic import Field class LlamaIndexRetriever(BaseRetriever): diff --git a/libs/community/langchain_community/retrievers/metal.py b/libs/community/langchain_community/retrievers/metal.py index 6eefd8312e3dc..df2f57f2357dc 100644 --- a/libs/community/langchain_community/retrievers/metal.py +++ b/libs/community/langchain_community/retrievers/metal.py @@ -2,8 +2,8 @@ from langchain_core.callbacks import CallbackManagerForRetrieverRun from langchain_core.documents import Document -from langchain_core.pydantic_v1 import root_validator from langchain_core.retrievers import BaseRetriever +from pydantic import model_validator class MetalRetriever(BaseRetriever): @@ -14,8 +14,9 @@ class MetalRetriever(BaseRetriever): params: Optional[dict] = None """The parameters to pass to the Metal client.""" - @root_validator(pre=True) - def validate_client(cls, values: dict) -> dict: + @model_validator(mode="before") + @classmethod + def validate_client(cls, values: dict) -> Any: """Validate that the client is of the correct type.""" from metal_sdk.metal import Metal diff --git a/libs/community/langchain_community/retrievers/milvus.py b/libs/community/langchain_community/retrievers/milvus.py index 7e8cd3fdb3ab0..23d8890341a5c 100644 --- a/libs/community/langchain_community/retrievers/milvus.py +++ b/libs/community/langchain_community/retrievers/milvus.py @@ -6,8 +6,8 @@ from langchain_core.callbacks import CallbackManagerForRetrieverRun from langchain_core.documents import Document from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import root_validator from langchain_core.retrievers import BaseRetriever +from pydantic import model_validator from langchain_community.vectorstores.milvus import Milvus @@ -93,8 +93,9 @@ def format_docs(docs): store: Milvus retriever: BaseRetriever - @root_validator(pre=True) - def create_retriever(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def create_retriever(cls, values: Dict) -> Any: """Create the Milvus store and retriever.""" values["store"] = Milvus( values["embedding_function"], diff --git a/libs/community/langchain_community/retrievers/nanopq.py b/libs/community/langchain_community/retrievers/nanopq.py index 1a52d1d7756b7..3ce6069327a42 100644 --- a/libs/community/langchain_community/retrievers/nanopq.py +++ b/libs/community/langchain_community/retrievers/nanopq.py @@ -8,6 +8,7 @@ from langchain_core.documents import Document from langchain_core.embeddings import Embeddings from langchain_core.retrievers import BaseRetriever +from pydantic import ConfigDict def create_index(contexts: List[str], embeddings: Embeddings) -> np.ndarray: @@ -45,8 +46,9 @@ class NanoPQRetriever(BaseRetriever): clusters: int = 128 """No of clusters to be created""" - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) @classmethod def from_texts( diff --git a/libs/community/langchain_community/retrievers/pinecone_hybrid_search.py b/libs/community/langchain_community/retrievers/pinecone_hybrid_search.py index dcbf35e38d7cc..b2e2d10d8121e 100644 --- a/libs/community/langchain_community/retrievers/pinecone_hybrid_search.py +++ b/libs/community/langchain_community/retrievers/pinecone_hybrid_search.py @@ -8,6 +8,7 @@ from langchain_core.embeddings import Embeddings from langchain_core.retrievers import BaseRetriever from langchain_core.utils import pre_init +from pydantic import ConfigDict def hash_text(text: str) -> str: @@ -114,9 +115,10 @@ class PineconeHybridSearchRetriever(BaseRetriever): namespace: Optional[str] = None """Namespace value for index partition.""" - class Config: - arbitrary_types_allowed = True - extra = "forbid" + model_config = ConfigDict( + arbitrary_types_allowed=True, + extra="forbid", + ) def add_texts( self, diff --git a/libs/community/langchain_community/retrievers/qdrant_sparse_vector_retriever.py b/libs/community/langchain_community/retrievers/qdrant_sparse_vector_retriever.py index 3226a3b6899a6..1e9fde744a267 100644 --- a/libs/community/langchain_community/retrievers/qdrant_sparse_vector_retriever.py +++ b/libs/community/langchain_community/retrievers/qdrant_sparse_vector_retriever.py @@ -18,6 +18,7 @@ from langchain_core.documents import Document from langchain_core.retrievers import BaseRetriever from langchain_core.utils import pre_init +from pydantic import ConfigDict from langchain_community.vectorstores.qdrant import Qdrant, QdrantException @@ -54,9 +55,10 @@ class QdrantSparseVectorRetriever(BaseRetriever): search_options: Dict[str, Any] = {} """Additional search options to pass to qdrant_client.QdrantClient.search().""" - class Config: - arbitrary_types_allowed = True - extra = "forbid" + model_config = ConfigDict( + arbitrary_types_allowed=True, + extra="forbid", + ) @pre_init def validate_environment(cls, values: Dict) -> Dict: diff --git a/libs/community/langchain_community/retrievers/svm.py b/libs/community/langchain_community/retrievers/svm.py index c19f83e58d38d..6b21e7fec5292 100644 --- a/libs/community/langchain_community/retrievers/svm.py +++ b/libs/community/langchain_community/retrievers/svm.py @@ -8,6 +8,7 @@ from langchain_core.documents import Document from langchain_core.embeddings import Embeddings from langchain_core.retrievers import BaseRetriever +from pydantic import ConfigDict def create_index(contexts: List[str], embeddings: Embeddings) -> np.ndarray: @@ -45,8 +46,9 @@ class SVMRetriever(BaseRetriever): relevancy_threshold: Optional[float] = None """Threshold for relevancy.""" - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) @classmethod def from_texts( diff --git a/libs/community/langchain_community/retrievers/tfidf.py b/libs/community/langchain_community/retrievers/tfidf.py index f556a8745ec2e..4101ac9af50f1 100644 --- a/libs/community/langchain_community/retrievers/tfidf.py +++ b/libs/community/langchain_community/retrievers/tfidf.py @@ -7,6 +7,7 @@ from langchain_core.callbacks import CallbackManagerForRetrieverRun from langchain_core.documents import Document from langchain_core.retrievers import BaseRetriever +from pydantic import ConfigDict class TFIDFRetriever(BaseRetriever): @@ -25,8 +26,9 @@ class TFIDFRetriever(BaseRetriever): k: int = 4 """Number of documents to return.""" - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) @classmethod def from_texts( diff --git a/libs/community/langchain_community/retrievers/thirdai_neuraldb.py b/libs/community/langchain_community/retrievers/thirdai_neuraldb.py index c1d9d140edc8c..2fde6d73d148c 100644 --- a/libs/community/langchain_community/retrievers/thirdai_neuraldb.py +++ b/libs/community/langchain_community/retrievers/thirdai_neuraldb.py @@ -7,9 +7,9 @@ from langchain_core.callbacks import CallbackManagerForRetrieverRun from langchain_core.documents import Document -from langchain_core.pydantic_v1 import SecretStr from langchain_core.retrievers import BaseRetriever from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init +from pydantic import ConfigDict, SecretStr class NeuralDBRetriever(BaseRetriever): @@ -21,9 +21,9 @@ class NeuralDBRetriever(BaseRetriever): db: Any = None #: :meta private: """NeuralDB instance""" - class Config: - extra = "forbid" - underscore_attrs_are_private = True + model_config = ConfigDict( + extra="forbid", + ) @staticmethod def _verify_thirdai_library(thirdai_key: Optional[str] = None) -> None: diff --git a/libs/community/langchain_community/retrievers/weaviate_hybrid_search.py b/libs/community/langchain_community/retrievers/weaviate_hybrid_search.py index 198091f4f1b05..6e98d076690da 100644 --- a/libs/community/langchain_community/retrievers/weaviate_hybrid_search.py +++ b/libs/community/langchain_community/retrievers/weaviate_hybrid_search.py @@ -5,8 +5,8 @@ from langchain_core.callbacks import CallbackManagerForRetrieverRun from langchain_core.documents import Document -from langchain_core.pydantic_v1 import root_validator from langchain_core.retrievers import BaseRetriever +from pydantic import ConfigDict, model_validator class WeaviateHybridSearchRetriever(BaseRetriever): @@ -31,11 +31,12 @@ class WeaviateHybridSearchRetriever(BaseRetriever): create_schema_if_missing: bool = True """Whether to create the schema if it doesn't exist.""" - @root_validator(pre=True) + @model_validator(mode="before") + @classmethod def validate_client( cls, values: Dict[str, Any], - ) -> Dict[str, Any]: + ) -> Any: try: import weaviate except ImportError: @@ -65,8 +66,9 @@ def validate_client( return values - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) # added text_key def add_documents(self, docs: List[Document], **kwargs: Any) -> List[str]: diff --git a/libs/community/langchain_community/retrievers/web_research.py b/libs/community/langchain_community/retrievers/web_research.py index 8dfadb49cf22f..aa604dd84132a 100644 --- a/libs/community/langchain_community/retrievers/web_research.py +++ b/libs/community/langchain_community/retrievers/web_research.py @@ -12,10 +12,10 @@ from langchain_core.language_models import BaseLLM from langchain_core.output_parsers import BaseOutputParser from langchain_core.prompts import BasePromptTemplate, PromptTemplate -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.retrievers import BaseRetriever from langchain_core.vectorstores import VectorStore from langchain_text_splitters import RecursiveCharacterTextSplitter, TextSplitter +from pydantic import BaseModel, Field from langchain_community.document_loaders import AsyncHtmlLoader from langchain_community.document_transformers import Html2TextTransformer diff --git a/libs/community/langchain_community/retrievers/zep.py b/libs/community/langchain_community/retrievers/zep.py index 2060788e4cd34..d59aa00781566 100644 --- a/libs/community/langchain_community/retrievers/zep.py +++ b/libs/community/langchain_community/retrievers/zep.py @@ -8,8 +8,8 @@ CallbackManagerForRetrieverRun, ) from langchain_core.documents import Document -from langchain_core.pydantic_v1 import root_validator from langchain_core.retrievers import BaseRetriever +from pydantic import model_validator if TYPE_CHECKING: from zep_python.memory import MemorySearchResult @@ -79,8 +79,9 @@ class ZepRetriever(BaseRetriever): mmr_lambda: Optional[float] = None """Lambda value for MMR search.""" - @root_validator(pre=True) - def create_client(cls, values: dict) -> dict: + @model_validator(mode="before") + @classmethod + def create_client(cls, values: dict) -> Any: try: from zep_python import ZepClient except ImportError: diff --git a/libs/community/langchain_community/retrievers/zep_cloud.py b/libs/community/langchain_community/retrievers/zep_cloud.py index 96758c71d9861..c4e3f11040cc5 100644 --- a/libs/community/langchain_community/retrievers/zep_cloud.py +++ b/libs/community/langchain_community/retrievers/zep_cloud.py @@ -7,8 +7,8 @@ CallbackManagerForRetrieverRun, ) from langchain_core.documents import Document -from langchain_core.pydantic_v1 import root_validator from langchain_core.retrievers import BaseRetriever +from pydantic import model_validator if TYPE_CHECKING: from zep_cloud import MemorySearchResult, SearchScope, SearchType @@ -61,8 +61,9 @@ class ZepCloudRetriever(BaseRetriever): mmr_lambda: Optional[float] = None """Lambda value for MMR search.""" - @root_validator(pre=True) - def create_client(cls, values: dict) -> dict: + @model_validator(mode="before") + @classmethod + def create_client(cls, values: dict) -> Any: try: from zep_cloud.client import AsyncZep, Zep except ImportError: diff --git a/libs/community/langchain_community/retrievers/zilliz.py b/libs/community/langchain_community/retrievers/zilliz.py index b273ab6e5766a..d7e614942010f 100644 --- a/libs/community/langchain_community/retrievers/zilliz.py +++ b/libs/community/langchain_community/retrievers/zilliz.py @@ -4,8 +4,8 @@ from langchain_core.callbacks import CallbackManagerForRetrieverRun from langchain_core.documents import Document from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import root_validator from langchain_core.retrievers import BaseRetriever +from pydantic import model_validator from langchain_community.vectorstores.zilliz import Zilliz @@ -30,8 +30,9 @@ class ZillizRetriever(BaseRetriever): retriever: BaseRetriever """The underlying retriever.""" - @root_validator(pre=True) - def create_client(cls, values: dict) -> dict: + @model_validator(mode="before") + @classmethod + def create_client(cls, values: dict) -> Any: values["store"] = Zilliz( values["embedding_function"], values["collection_name"], diff --git a/libs/community/langchain_community/tools/ainetwork/app.py b/libs/community/langchain_community/tools/ainetwork/app.py index faef6120f9f9b..8175a210b7067 100644 --- a/libs/community/langchain_community/tools/ainetwork/app.py +++ b/libs/community/langchain_community/tools/ainetwork/app.py @@ -4,7 +4,7 @@ from typing import List, Optional, Type, Union from langchain_core.callbacks import AsyncCallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field from langchain_community.tools.ainetwork.base import AINBaseTool diff --git a/libs/community/langchain_community/tools/ainetwork/base.py b/libs/community/langchain_community/tools/ainetwork/base.py index 2f94127576983..00e4fc7f7a298 100644 --- a/libs/community/langchain_community/tools/ainetwork/base.py +++ b/libs/community/langchain_community/tools/ainetwork/base.py @@ -6,8 +6,8 @@ from typing import TYPE_CHECKING, Any, Optional from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool +from pydantic import Field from langchain_community.tools.ainetwork.utils import authenticate diff --git a/libs/community/langchain_community/tools/ainetwork/owner.py b/libs/community/langchain_community/tools/ainetwork/owner.py index a89134f2a0597..13d41d9327350 100644 --- a/libs/community/langchain_community/tools/ainetwork/owner.py +++ b/libs/community/langchain_community/tools/ainetwork/owner.py @@ -3,7 +3,7 @@ from typing import List, Optional, Type, Union from langchain_core.callbacks import AsyncCallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field from langchain_community.tools.ainetwork.base import AINBaseTool, OperationType diff --git a/libs/community/langchain_community/tools/ainetwork/rule.py b/libs/community/langchain_community/tools/ainetwork/rule.py index 309010f5ba3ba..5a24c9e5aba79 100644 --- a/libs/community/langchain_community/tools/ainetwork/rule.py +++ b/libs/community/langchain_community/tools/ainetwork/rule.py @@ -3,7 +3,7 @@ from typing import Optional, Type from langchain_core.callbacks import AsyncCallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field from langchain_community.tools.ainetwork.base import AINBaseTool, OperationType diff --git a/libs/community/langchain_community/tools/ainetwork/transfer.py b/libs/community/langchain_community/tools/ainetwork/transfer.py index deab34ad9c2b5..81d630af2e3bd 100644 --- a/libs/community/langchain_community/tools/ainetwork/transfer.py +++ b/libs/community/langchain_community/tools/ainetwork/transfer.py @@ -2,7 +2,7 @@ from typing import Optional, Type from langchain_core.callbacks import AsyncCallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field from langchain_community.tools.ainetwork.base import AINBaseTool diff --git a/libs/community/langchain_community/tools/ainetwork/value.py b/libs/community/langchain_community/tools/ainetwork/value.py index 300e36c573c92..be6414727f53f 100644 --- a/libs/community/langchain_community/tools/ainetwork/value.py +++ b/libs/community/langchain_community/tools/ainetwork/value.py @@ -3,7 +3,7 @@ from typing import Optional, Type, Union from langchain_core.callbacks import AsyncCallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field from langchain_community.tools.ainetwork.base import AINBaseTool, OperationType diff --git a/libs/community/langchain_community/tools/amadeus/base.py b/libs/community/langchain_community/tools/amadeus/base.py index 96b4380f4e2e1..3fd3f377ce2dd 100644 --- a/libs/community/langchain_community/tools/amadeus/base.py +++ b/libs/community/langchain_community/tools/amadeus/base.py @@ -4,8 +4,8 @@ from typing import TYPE_CHECKING -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool +from pydantic import Field from langchain_community.tools.amadeus.utils import authenticate diff --git a/libs/community/langchain_community/tools/amadeus/closest_airport.py b/libs/community/langchain_community/tools/amadeus/closest_airport.py index 1a3f4c4da2cb2..9523f73afbb6d 100644 --- a/libs/community/langchain_community/tools/amadeus/closest_airport.py +++ b/libs/community/langchain_community/tools/amadeus/closest_airport.py @@ -2,7 +2,7 @@ from langchain_core.callbacks import CallbackManagerForToolRun from langchain_core.language_models import BaseLanguageModel -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator +from pydantic import BaseModel, Field, model_validator from langchain_community.chat_models import ChatOpenAI from langchain_community.tools.amadeus.base import AmadeusBaseTool @@ -39,8 +39,9 @@ class AmadeusClosestAirport(AmadeusBaseTool): llm: Optional[BaseLanguageModel] = Field(default=None) """Tool's llm used for calculating the closest airport. Defaults to `ChatOpenAI`.""" - @root_validator(pre=True) - def set_llm(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def set_llm(cls, values: Dict[str, Any]) -> Any: if not values.get("llm"): # For backward-compatibility values["llm"] = ChatOpenAI(temperature=0) diff --git a/libs/community/langchain_community/tools/amadeus/flight_search.py b/libs/community/langchain_community/tools/amadeus/flight_search.py index 708a53054dc0d..c3cd8fe7bb9d2 100644 --- a/libs/community/langchain_community/tools/amadeus/flight_search.py +++ b/libs/community/langchain_community/tools/amadeus/flight_search.py @@ -3,7 +3,7 @@ from typing import Dict, Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field from langchain_community.tools.amadeus.base import AmadeusBaseTool diff --git a/libs/community/langchain_community/tools/arxiv/tool.py b/libs/community/langchain_community/tools/arxiv/tool.py index 5c8ca77f1bb80..601022a1325c3 100644 --- a/libs/community/langchain_community/tools/arxiv/tool.py +++ b/libs/community/langchain_community/tools/arxiv/tool.py @@ -3,8 +3,8 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.utilities.arxiv import ArxivAPIWrapper diff --git a/libs/community/langchain_community/tools/asknews/tool.py b/libs/community/langchain_community/tools/asknews/tool.py index e3d39027aa701..7589d11078bd6 100644 --- a/libs/community/langchain_community/tools/asknews/tool.py +++ b/libs/community/langchain_community/tools/asknews/tool.py @@ -12,8 +12,8 @@ AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.utilities.asknews import AskNewsAPIWrapper diff --git a/libs/community/langchain_community/tools/audio/huggingface_text_to_speech_inference.py b/libs/community/langchain_community/tools/audio/huggingface_text_to_speech_inference.py index 62d73f9191b16..bbe5f7d47c295 100644 --- a/libs/community/langchain_community/tools/audio/huggingface_text_to_speech_inference.py +++ b/libs/community/langchain_community/tools/audio/huggingface_text_to_speech_inference.py @@ -6,8 +6,8 @@ import requests from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import SecretStr from langchain_core.tools import BaseTool +from pydantic import SecretStr logger = logging.getLogger(__name__) @@ -48,10 +48,12 @@ def __init__( destination_dir: str = "./tts", file_naming_func: Literal["uuid", "timestamp"] = "uuid", huggingface_api_key: Optional[SecretStr] = None, + _HUGGINGFACE_API_KEY_ENV_NAME: str = "HUGGINGFACE_API_KEY", + _HUGGINGFACE_API_URL_ROOT: str = "https://api-inference.huggingface.co/models", ) -> None: if not huggingface_api_key: huggingface_api_key = SecretStr( - os.getenv(self._HUGGINGFACE_API_KEY_ENV_NAME, "") + os.getenv(_HUGGINGFACE_API_KEY_ENV_NAME, "") ) if ( @@ -60,7 +62,7 @@ def __init__( or huggingface_api_key.get_secret_value() == "" ): raise ValueError( - f"'{self._HUGGINGFACE_API_KEY_ENV_NAME}' must be or set or passed" + f"'{_HUGGINGFACE_API_KEY_ENV_NAME}' must be or set or passed" ) if file_naming_func == "uuid": @@ -75,10 +77,12 @@ def __init__( super().__init__( # type: ignore[call-arg] model=model, file_extension=file_extension, - api_url=f"{self._HUGGINGFACE_API_URL_ROOT}/{model}", + api_url=f"{_HUGGINGFACE_API_URL_ROOT}/{model}", destination_dir=destination_dir, file_namer=file_namer, huggingface_api_key=huggingface_api_key, + _HUGGINGFACE_API_KEY_ENV_NAME=_HUGGINGFACE_API_KEY_ENV_NAME, + _HUGGINGFACE_API_URL_ROOT=_HUGGINGFACE_API_URL_ROOT, ) def _run( diff --git a/libs/community/langchain_community/tools/azure_ai_services/document_intelligence.py b/libs/community/langchain_community/tools/azure_ai_services/document_intelligence.py index a5e9e9e481ad9..cd0ac25018ee4 100644 --- a/libs/community/langchain_community/tools/azure_ai_services/document_intelligence.py +++ b/libs/community/langchain_community/tools/azure_ai_services/document_intelligence.py @@ -4,9 +4,9 @@ from typing import Any, Dict, List, Optional from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import root_validator from langchain_core.tools import BaseTool from langchain_core.utils import get_from_dict_or_env +from pydantic import model_validator from langchain_community.tools.azure_ai_services.utils import ( detect_file_src_type, @@ -34,8 +34,9 @@ class AzureAiServicesDocumentIntelligenceTool(BaseTool): "Input should be a url to a document." ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and endpoint exists in environment.""" azure_ai_services_key = get_from_dict_or_env( values, "azure_ai_services_key", "AZURE_AI_SERVICES_KEY" diff --git a/libs/community/langchain_community/tools/azure_ai_services/image_analysis.py b/libs/community/langchain_community/tools/azure_ai_services/image_analysis.py index ade4ef2bdbad6..0a1e206e45e7d 100644 --- a/libs/community/langchain_community/tools/azure_ai_services/image_analysis.py +++ b/libs/community/langchain_community/tools/azure_ai_services/image_analysis.py @@ -4,9 +4,9 @@ from typing import Any, Dict, Optional from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import root_validator from langchain_core.tools import BaseTool from langchain_core.utils import get_from_dict_or_env +from pydantic import model_validator from langchain_community.tools.azure_ai_services.utils import ( detect_file_src_type, @@ -34,8 +34,9 @@ class AzureAiServicesImageAnalysisTool(BaseTool): "Input should be a url to an image." ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and endpoint exists in environment.""" azure_ai_services_key = get_from_dict_or_env( values, "azure_ai_services_key", "AZURE_AI_SERVICES_KEY" diff --git a/libs/community/langchain_community/tools/azure_ai_services/speech_to_text.py b/libs/community/langchain_community/tools/azure_ai_services/speech_to_text.py index 28d490d7ab843..15e08d27222e1 100644 --- a/libs/community/langchain_community/tools/azure_ai_services/speech_to_text.py +++ b/libs/community/langchain_community/tools/azure_ai_services/speech_to_text.py @@ -5,9 +5,9 @@ from typing import Any, Dict, Optional from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import root_validator from langchain_core.tools import BaseTool from langchain_core.utils import get_from_dict_or_env +from pydantic import model_validator from langchain_community.tools.azure_ai_services.utils import ( detect_file_src_type, @@ -36,8 +36,9 @@ class AzureAiServicesSpeechToTextTool(BaseTool): "Input should be a url to an audio file." ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and endpoint exists in environment.""" azure_ai_services_key = get_from_dict_or_env( values, "azure_ai_services_key", "AZURE_AI_SERVICES_KEY" diff --git a/libs/community/langchain_community/tools/azure_ai_services/text_analytics_for_health.py b/libs/community/langchain_community/tools/azure_ai_services/text_analytics_for_health.py index 869c93657166d..6447c5de4185e 100644 --- a/libs/community/langchain_community/tools/azure_ai_services/text_analytics_for_health.py +++ b/libs/community/langchain_community/tools/azure_ai_services/text_analytics_for_health.py @@ -4,9 +4,9 @@ from typing import Any, Dict, Optional from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import root_validator from langchain_core.tools import BaseTool from langchain_core.utils import get_from_dict_or_env +from pydantic import model_validator logger = logging.getLogger(__name__) @@ -29,8 +29,9 @@ class AzureAiServicesTextAnalyticsForHealthTool(BaseTool): "Input should be text." ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and endpoint exists in environment.""" azure_ai_services_key = get_from_dict_or_env( values, "azure_ai_services_key", "AZURE_AI_SERVICES_KEY" diff --git a/libs/community/langchain_community/tools/azure_ai_services/text_to_speech.py b/libs/community/langchain_community/tools/azure_ai_services/text_to_speech.py index 67fde6bad8534..1291e2dac5641 100644 --- a/libs/community/langchain_community/tools/azure_ai_services/text_to_speech.py +++ b/libs/community/langchain_community/tools/azure_ai_services/text_to_speech.py @@ -5,9 +5,9 @@ from typing import Any, Dict, Optional from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import root_validator from langchain_core.tools import BaseTool from langchain_core.utils import get_from_dict_or_env +from pydantic import model_validator logger = logging.getLogger(__name__) @@ -31,8 +31,9 @@ class AzureAiServicesTextToSpeechTool(BaseTool): speech_language: str = "en-US" #: :meta private: speech_config: Any #: :meta private: - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and endpoint exists in environment.""" azure_ai_services_key = get_from_dict_or_env( values, "azure_ai_services_key", "AZURE_AI_SERVICES_KEY" diff --git a/libs/community/langchain_community/tools/azure_cognitive_services/form_recognizer.py b/libs/community/langchain_community/tools/azure_cognitive_services/form_recognizer.py index 42d11b4ac4ed7..937b1fc7930f3 100644 --- a/libs/community/langchain_community/tools/azure_cognitive_services/form_recognizer.py +++ b/libs/community/langchain_community/tools/azure_cognitive_services/form_recognizer.py @@ -4,9 +4,9 @@ from typing import Any, Dict, List, Optional from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import root_validator from langchain_core.tools import BaseTool from langchain_core.utils import get_from_dict_or_env +from pydantic import model_validator from langchain_community.tools.azure_cognitive_services.utils import ( detect_file_src_type, @@ -34,8 +34,9 @@ class AzureCogsFormRecognizerTool(BaseTool): "Input should be a url to a document." ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and endpoint exists in environment.""" azure_cogs_key = get_from_dict_or_env( values, "azure_cogs_key", "AZURE_COGS_KEY" diff --git a/libs/community/langchain_community/tools/azure_cognitive_services/image_analysis.py b/libs/community/langchain_community/tools/azure_cognitive_services/image_analysis.py index 801aa57bd2d7b..ce076243a7b14 100644 --- a/libs/community/langchain_community/tools/azure_cognitive_services/image_analysis.py +++ b/libs/community/langchain_community/tools/azure_cognitive_services/image_analysis.py @@ -4,9 +4,9 @@ from typing import Any, Dict, Optional from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import root_validator from langchain_core.tools import BaseTool from langchain_core.utils import get_from_dict_or_env +from pydantic import model_validator from langchain_community.tools.azure_cognitive_services.utils import ( detect_file_src_type, @@ -34,8 +34,9 @@ class AzureCogsImageAnalysisTool(BaseTool): "Input should be a url to an image." ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and endpoint exists in environment.""" azure_cogs_key = get_from_dict_or_env( values, "azure_cogs_key", "AZURE_COGS_KEY" diff --git a/libs/community/langchain_community/tools/azure_cognitive_services/speech2text.py b/libs/community/langchain_community/tools/azure_cognitive_services/speech2text.py index b1aa90cb7013d..125c910df1ba7 100644 --- a/libs/community/langchain_community/tools/azure_cognitive_services/speech2text.py +++ b/libs/community/langchain_community/tools/azure_cognitive_services/speech2text.py @@ -5,9 +5,9 @@ from typing import Any, Dict, Optional from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import root_validator from langchain_core.tools import BaseTool from langchain_core.utils import get_from_dict_or_env +from pydantic import model_validator from langchain_community.tools.azure_cognitive_services.utils import ( detect_file_src_type, @@ -36,8 +36,9 @@ class AzureCogsSpeech2TextTool(BaseTool): "Input should be a url to an audio file." ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and endpoint exists in environment.""" azure_cogs_key = get_from_dict_or_env( values, "azure_cogs_key", "AZURE_COGS_KEY" diff --git a/libs/community/langchain_community/tools/azure_cognitive_services/text2speech.py b/libs/community/langchain_community/tools/azure_cognitive_services/text2speech.py index f65049f13a966..343653fe9c3e1 100644 --- a/libs/community/langchain_community/tools/azure_cognitive_services/text2speech.py +++ b/libs/community/langchain_community/tools/azure_cognitive_services/text2speech.py @@ -5,9 +5,9 @@ from typing import Any, Dict, Optional from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import root_validator from langchain_core.tools import BaseTool from langchain_core.utils import get_from_dict_or_env +from pydantic import model_validator logger = logging.getLogger(__name__) @@ -30,8 +30,9 @@ class AzureCogsText2SpeechTool(BaseTool): "Useful for when you need to convert text to speech. " ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and endpoint exists in environment.""" azure_cogs_key = get_from_dict_or_env( values, "azure_cogs_key", "AZURE_COGS_KEY" diff --git a/libs/community/langchain_community/tools/azure_cognitive_services/text_analytics_health.py b/libs/community/langchain_community/tools/azure_cognitive_services/text_analytics_health.py index ce5332436c7ea..1de8566528fe4 100644 --- a/libs/community/langchain_community/tools/azure_cognitive_services/text_analytics_health.py +++ b/libs/community/langchain_community/tools/azure_cognitive_services/text_analytics_health.py @@ -4,9 +4,9 @@ from typing import Any, Dict, Optional from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import root_validator from langchain_core.tools import BaseTool from langchain_core.utils import get_from_dict_or_env +from pydantic import model_validator logger = logging.getLogger(__name__) @@ -29,8 +29,9 @@ class AzureCogsTextAnalyticsHealthTool(BaseTool): "Input should be text." ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and endpoint exists in environment.""" azure_cogs_key = get_from_dict_or_env( values, "azure_cogs_key", "AZURE_COGS_KEY" diff --git a/libs/community/langchain_community/tools/bearly/tool.py b/libs/community/langchain_community/tools/bearly/tool.py index 64a2625716053..eba71c0d7ff7e 100644 --- a/libs/community/langchain_community/tools/bearly/tool.py +++ b/libs/community/langchain_community/tools/bearly/tool.py @@ -6,8 +6,8 @@ from typing import Dict, List, Type import requests -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import Tool +from pydantic import BaseModel, Field def strip_markdown_code(md_string: str) -> str: @@ -37,7 +37,7 @@ class BearlyInterpreterToolArguments(BaseModel): python_code: str = Field( ..., - example="print('Hello World')", + examples=["print('Hello World')"], description=( "The pure python script to be evaluated. " "The contents will be in main.py. " diff --git a/libs/community/langchain_community/tools/cassandra_database/tool.py b/libs/community/langchain_community/tools/cassandra_database/tool.py index 10074ecabb0f5..b96da9973a4c5 100644 --- a/libs/community/langchain_community/tools/cassandra_database/tool.py +++ b/libs/community/langchain_community/tools/cassandra_database/tool.py @@ -6,8 +6,8 @@ from typing import TYPE_CHECKING, Any, Dict, Optional, Sequence, Type, Union from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.utilities.cassandra_database import CassandraDatabase @@ -20,9 +20,6 @@ class BaseCassandraDatabaseTool(BaseModel): db: CassandraDatabase = Field(exclude=True) - class Config(BaseTool.Config): - pass - class _QueryCassandraDatabaseToolInput(BaseModel): query: str = Field(..., description="A detailed and correct CQL query.") diff --git a/libs/community/langchain_community/tools/clickup/tool.py b/libs/community/langchain_community/tools/clickup/tool.py index 8e40c97c456b6..03b3fde586c18 100644 --- a/libs/community/langchain_community/tools/clickup/tool.py +++ b/libs/community/langchain_community/tools/clickup/tool.py @@ -20,8 +20,8 @@ from typing import Optional from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool +from pydantic import Field from langchain_community.utilities.clickup import ClickupAPIWrapper diff --git a/libs/community/langchain_community/tools/cogniswitch/tool.py b/libs/community/langchain_community/tools/cogniswitch/tool.py index d88120e5076f8..41c9663fb5eb0 100644 --- a/libs/community/langchain_community/tools/cogniswitch/tool.py +++ b/libs/community/langchain_community/tools/cogniswitch/tool.py @@ -98,7 +98,7 @@ class CogniswitchKnowledgeStatus(BaseTool): cs_token: str OAI_token: str apiKey: str - knowledge_status_url = ( + knowledge_status_url: str = ( "https://api.cogniswitch.ai:8243/cs-api/0.0.1/cs/knowledgeSource/status" ) @@ -200,7 +200,7 @@ class CogniswitchKnowledgeSourceFile(BaseTool): cs_token: str OAI_token: str apiKey: str - knowledgesource_file = ( + knowledgesource_file: str = ( "https://api.cogniswitch.ai:8243/cs-api/0.0.1/cs/knowledgeSource/file" ) @@ -312,7 +312,7 @@ class CogniswitchKnowledgeSourceURL(BaseTool): cs_token: str OAI_token: str apiKey: str - knowledgesource_url = ( + knowledgesource_url: str = ( "https://api.cogniswitch.ai:8243/cs-api/0.0.1/cs/knowledgeSource/url" ) diff --git a/libs/community/langchain_community/tools/connery/models.py b/libs/community/langchain_community/tools/connery/models.py index a8292e62b9e42..537f58bea133a 100644 --- a/libs/community/langchain_community/tools/connery/models.py +++ b/libs/community/langchain_community/tools/connery/models.py @@ -1,6 +1,6 @@ -from typing import List, Optional +from typing import Any, List, Optional -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel class Validation(BaseModel): @@ -15,7 +15,7 @@ class Parameter(BaseModel): key: str title: str description: Optional[str] = None - type: str + type: Any validation: Optional[Validation] = None diff --git a/libs/community/langchain_community/tools/connery/service.py b/libs/community/langchain_community/tools/connery/service.py index 11c921c1bc2cb..bbe4cc8c183bd 100644 --- a/libs/community/langchain_community/tools/connery/service.py +++ b/libs/community/langchain_community/tools/connery/service.py @@ -1,9 +1,9 @@ import json -from typing import Dict, List, Optional +from typing import Any, Dict, List, Optional import requests -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils.env import get_from_dict_or_env +from pydantic import BaseModel, model_validator from langchain_community.tools.connery.models import Action from langchain_community.tools.connery.tool import ConneryAction @@ -20,8 +20,9 @@ class ConneryService(BaseModel): runner_url: Optional[str] = None api_key: Optional[str] = None - @root_validator(pre=True) - def validate_attributes(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_attributes(cls, values: Dict) -> Any: """ Validate the attributes of the ConneryService class. Parameters: diff --git a/libs/community/langchain_community/tools/connery/tool.py b/libs/community/langchain_community/tools/connery/tool.py index da7f802696ed4..d74bfc1743e87 100644 --- a/libs/community/langchain_community/tools/connery/tool.py +++ b/libs/community/langchain_community/tools/connery/tool.py @@ -6,8 +6,8 @@ AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import BaseModel, Field, create_model, root_validator from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field, create_model, model_validator from langchain_community.tools.connery.models import Action, Parameter @@ -63,8 +63,9 @@ def get_schema_json(self) -> str: return self.args_schema.schema_json(indent=2) - @root_validator(pre=True) - def validate_attributes(cls, values: dict) -> dict: + @model_validator(mode="before") + @classmethod + def validate_attributes(cls, values: dict) -> Any: """ Validate the attributes of the ConneryAction class. Parameters: @@ -153,8 +154,8 @@ def _create_input_schema(cls, inputParameters: List[Parameter]) -> Type[BaseMode type = param.type dynamic_input_fields[param.key] = ( - str, - Field(default, title=title, description=description, type=type), + type, + Field(default, title=title, description=description), ) InputModel = create_model("InputSchema", **dynamic_input_fields) diff --git a/libs/community/langchain_community/tools/databricks/tool.py b/libs/community/langchain_community/tools/databricks/tool.py index 7bf30e2d20b3c..2255caf4d08a1 100644 --- a/libs/community/langchain_community/tools/databricks/tool.py +++ b/libs/community/langchain_community/tools/databricks/tool.py @@ -4,15 +4,17 @@ from hashlib import md5 from typing import TYPE_CHECKING, Any, Dict, List, Optional, Type, Union -from langchain_core.pydantic_v1 import BaseModel, Field, create_model from langchain_core.tools import BaseTool, StructuredTool from langchain_core.tools.base import BaseToolkit +from pydantic import BaseModel, Field, create_model from typing_extensions import Self if TYPE_CHECKING: from databricks.sdk import WorkspaceClient from databricks.sdk.service.catalog import FunctionInfo +from pydantic import ConfigDict + from langchain_community.tools.databricks._execution import execute_function @@ -142,8 +144,9 @@ class UCFunctionToolkit(BaseToolkit): tools: Dict[str, BaseTool] = Field(default_factory=dict) - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def include(self, *function_names: str, **kwargs: Any) -> Self: """ diff --git a/libs/community/langchain_community/tools/dataforseo_api_search/tool.py b/libs/community/langchain_community/tools/dataforseo_api_search/tool.py index 4d601df59adcf..65bf1ec22cd75 100644 --- a/libs/community/langchain_community/tools/dataforseo_api_search/tool.py +++ b/libs/community/langchain_community/tools/dataforseo_api_search/tool.py @@ -6,8 +6,8 @@ AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool +from pydantic import Field from langchain_community.utilities.dataforseo_api_search import DataForSeoAPIWrapper diff --git a/libs/community/langchain_community/tools/dataherald/tool.py b/libs/community/langchain_community/tools/dataherald/tool.py index 90c4cef7102d8..2a2546a328378 100644 --- a/libs/community/langchain_community/tools/dataherald/tool.py +++ b/libs/community/langchain_community/tools/dataherald/tool.py @@ -3,8 +3,8 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.utilities.dataherald import DataheraldAPIWrapper diff --git a/libs/community/langchain_community/tools/ddg_search/tool.py b/libs/community/langchain_community/tools/ddg_search/tool.py index fe707a1a201c6..f2889daa716b2 100644 --- a/libs/community/langchain_community/tools/ddg_search/tool.py +++ b/libs/community/langchain_community/tools/ddg_search/tool.py @@ -4,8 +4,8 @@ from typing import Any, List, Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.utilities.duckduckgo_search import DuckDuckGoSearchAPIWrapper diff --git a/libs/community/langchain_community/tools/e2b_data_analysis/tool.py b/libs/community/langchain_community/tools/e2b_data_analysis/tool.py index fee95b4af5bf9..f9705086a9800 100644 --- a/libs/community/langchain_community/tools/e2b_data_analysis/tool.py +++ b/libs/community/langchain_community/tools/e2b_data_analysis/tool.py @@ -12,8 +12,8 @@ CallbackManager, CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import BaseModel, Field, PrivateAttr from langchain_core.tools import BaseTool, Tool +from pydantic import BaseModel, Field, PrivateAttr from langchain_community.tools.e2b_data_analysis.unparse import Unparser @@ -84,7 +84,7 @@ class E2BDataAnalysisToolArguments(BaseModel): python_code: str = Field( ..., - example="print('Hello World')", + examples=["print('Hello World')"], description=( "The python script to be evaluated. " "The contents will be in main.py. " diff --git a/libs/community/langchain_community/tools/edenai/audio_speech_to_text.py b/libs/community/langchain_community/tools/edenai/audio_speech_to_text.py index 342f33d2b2a42..c0fc870ef2d94 100644 --- a/libs/community/langchain_community/tools/edenai/audio_speech_to_text.py +++ b/libs/community/langchain_community/tools/edenai/audio_speech_to_text.py @@ -7,7 +7,7 @@ import requests from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field, HttpUrl, validator +from pydantic import BaseModel, Field, HttpUrl, validator from langchain_community.tools.edenai.edenai_base_tool import EdenaiTool @@ -30,7 +30,7 @@ class EdenAiSpeechToTextTool(EdenaiTool): """ name: str = "edenai_speech_to_text" - description = ( + description: str = ( "A wrapper around edenai Services speech to text " "Useful for when you have to convert audio to text." "Input should be a url to an audio file." diff --git a/libs/community/langchain_community/tools/edenai/audio_text_to_speech.py b/libs/community/langchain_community/tools/edenai/audio_text_to_speech.py index e78cf35419b1e..4bf79180b39d0 100644 --- a/libs/community/langchain_community/tools/edenai/audio_text_to_speech.py +++ b/libs/community/langchain_community/tools/edenai/audio_text_to_speech.py @@ -1,11 +1,11 @@ from __future__ import annotations import logging -from typing import Dict, List, Literal, Optional, Type +from typing import Any, Dict, List, Literal, Optional, Type import requests from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator, validator +from pydantic import BaseModel, Field, model_validator, validator from langchain_community.tools.edenai.edenai_base_tool import EdenaiTool @@ -28,7 +28,7 @@ class EdenAiTextToSpeechTool(EdenaiTool): """ name: str = "edenai_text_to_speech" - description = ( + description: str = ( "A wrapper around edenai Services text to speech." "Useful for when you need to convert text to speech." """the output is a string representing the URL of the audio file, @@ -70,8 +70,9 @@ def check_only_one_provider_selected(cls, v: List[str]) -> List[str]: ) return v - @root_validator(pre=True) - def check_voice_models_key_is_provider_name(cls, values: dict) -> dict: + @model_validator(mode="before") + @classmethod + def check_voice_models_key_is_provider_name(cls, values: dict) -> Any: for key in values.get("voice_models", {}).keys(): if key not in values.get("providers", []): raise ValueError( diff --git a/libs/community/langchain_community/tools/edenai/edenai_base_tool.py b/libs/community/langchain_community/tools/edenai/edenai_base_tool.py index 2f83ef37bc487..c4f5547999234 100644 --- a/libs/community/langchain_community/tools/edenai/edenai_base_tool.py +++ b/libs/community/langchain_community/tools/edenai/edenai_base_tool.py @@ -6,9 +6,9 @@ import requests from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import Field, SecretStr from langchain_core.tools import BaseTool from langchain_core.utils import secret_from_env +from pydantic import Field, SecretStr logger = logging.getLogger(__name__) diff --git a/libs/community/langchain_community/tools/edenai/image_explicitcontent.py b/libs/community/langchain_community/tools/edenai/image_explicitcontent.py index 8ca1d7739cb7f..50f9f24338a3f 100644 --- a/libs/community/langchain_community/tools/edenai/image_explicitcontent.py +++ b/libs/community/langchain_community/tools/edenai/image_explicitcontent.py @@ -4,7 +4,7 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field, HttpUrl +from pydantic import BaseModel, Field, HttpUrl from langchain_community.tools.edenai.edenai_base_tool import EdenaiTool diff --git a/libs/community/langchain_community/tools/edenai/image_objectdetection.py b/libs/community/langchain_community/tools/edenai/image_objectdetection.py index 1098e8e37f6da..aedfe56b88389 100644 --- a/libs/community/langchain_community/tools/edenai/image_objectdetection.py +++ b/libs/community/langchain_community/tools/edenai/image_objectdetection.py @@ -4,7 +4,7 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field, HttpUrl +from pydantic import BaseModel, Field, HttpUrl from langchain_community.tools.edenai.edenai_base_tool import EdenaiTool diff --git a/libs/community/langchain_community/tools/edenai/ocr_identityparser.py b/libs/community/langchain_community/tools/edenai/ocr_identityparser.py index 2e208dbb54340..f2270345917b4 100644 --- a/libs/community/langchain_community/tools/edenai/ocr_identityparser.py +++ b/libs/community/langchain_community/tools/edenai/ocr_identityparser.py @@ -4,7 +4,7 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field, HttpUrl +from pydantic import BaseModel, Field, HttpUrl from langchain_community.tools.edenai.edenai_base_tool import EdenaiTool diff --git a/libs/community/langchain_community/tools/edenai/ocr_invoiceparser.py b/libs/community/langchain_community/tools/edenai/ocr_invoiceparser.py index 75c8425154aa2..d5266476784c9 100644 --- a/libs/community/langchain_community/tools/edenai/ocr_invoiceparser.py +++ b/libs/community/langchain_community/tools/edenai/ocr_invoiceparser.py @@ -4,7 +4,7 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field, HttpUrl +from pydantic import BaseModel, Field, HttpUrl from langchain_community.tools.edenai.edenai_base_tool import EdenaiTool diff --git a/libs/community/langchain_community/tools/edenai/text_moderation.py b/libs/community/langchain_community/tools/edenai/text_moderation.py index 9aed36f0b7343..f5f8497ff3ca6 100644 --- a/libs/community/langchain_community/tools/edenai/text_moderation.py +++ b/libs/community/langchain_community/tools/edenai/text_moderation.py @@ -4,7 +4,7 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field from langchain_community.tools.edenai.edenai_base_tool import EdenaiTool diff --git a/libs/community/langchain_community/tools/eleven_labs/text2speech.py b/libs/community/langchain_community/tools/eleven_labs/text2speech.py index d1a68ba7a5c6f..333dfb6bb6432 100644 --- a/libs/community/langchain_community/tools/eleven_labs/text2speech.py +++ b/libs/community/langchain_community/tools/eleven_labs/text2speech.py @@ -3,9 +3,9 @@ from typing import Any, Dict, Optional, Union from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import root_validator from langchain_core.tools import BaseTool from langchain_core.utils import get_from_dict_or_env +from pydantic import model_validator def _import_elevenlabs() -> Any: @@ -42,8 +42,9 @@ class ElevenLabsText2SpeechTool(BaseTool): "Spanish, Italian, French, Portuguese, and Hindi. " ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key exists in environment.""" _ = get_from_dict_or_env(values, "eleven_api_key", "ELEVEN_API_KEY") diff --git a/libs/community/langchain_community/tools/file_management/copy.py b/libs/community/langchain_community/tools/file_management/copy.py index f91081003d14c..7679e3c43bee0 100644 --- a/libs/community/langchain_community/tools/file_management/copy.py +++ b/libs/community/langchain_community/tools/file_management/copy.py @@ -2,8 +2,8 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.tools.file_management.utils import ( INVALID_PATH_TEMPLATE, diff --git a/libs/community/langchain_community/tools/file_management/delete.py b/libs/community/langchain_community/tools/file_management/delete.py index c2694762aede3..33f4b70b28dd6 100644 --- a/libs/community/langchain_community/tools/file_management/delete.py +++ b/libs/community/langchain_community/tools/file_management/delete.py @@ -2,8 +2,8 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.tools.file_management.utils import ( INVALID_PATH_TEMPLATE, diff --git a/libs/community/langchain_community/tools/file_management/file_search.py b/libs/community/langchain_community/tools/file_management/file_search.py index c77abd0bef95d..a00aee40b4ba7 100644 --- a/libs/community/langchain_community/tools/file_management/file_search.py +++ b/libs/community/langchain_community/tools/file_management/file_search.py @@ -3,8 +3,8 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.tools.file_management.utils import ( INVALID_PATH_TEMPLATE, diff --git a/libs/community/langchain_community/tools/file_management/list_dir.py b/libs/community/langchain_community/tools/file_management/list_dir.py index d8b700134ae9f..a8bfdc8e3abe1 100644 --- a/libs/community/langchain_community/tools/file_management/list_dir.py +++ b/libs/community/langchain_community/tools/file_management/list_dir.py @@ -2,8 +2,8 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.tools.file_management.utils import ( INVALID_PATH_TEMPLATE, diff --git a/libs/community/langchain_community/tools/file_management/move.py b/libs/community/langchain_community/tools/file_management/move.py index fc3bb778d8ef8..935625172e9a3 100644 --- a/libs/community/langchain_community/tools/file_management/move.py +++ b/libs/community/langchain_community/tools/file_management/move.py @@ -2,8 +2,8 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.tools.file_management.utils import ( INVALID_PATH_TEMPLATE, diff --git a/libs/community/langchain_community/tools/file_management/read.py b/libs/community/langchain_community/tools/file_management/read.py index 42cc1515e7e70..9f746ed16c105 100644 --- a/libs/community/langchain_community/tools/file_management/read.py +++ b/libs/community/langchain_community/tools/file_management/read.py @@ -1,8 +1,8 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.tools.file_management.utils import ( INVALID_PATH_TEMPLATE, diff --git a/libs/community/langchain_community/tools/file_management/utils.py b/libs/community/langchain_community/tools/file_management/utils.py index b2a3632ecaa88..788823fecd739 100644 --- a/libs/community/langchain_community/tools/file_management/utils.py +++ b/libs/community/langchain_community/tools/file_management/utils.py @@ -2,7 +2,7 @@ from pathlib import Path from typing import Optional -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel def is_relative_to(path: Path, root: Path) -> bool: diff --git a/libs/community/langchain_community/tools/file_management/write.py b/libs/community/langchain_community/tools/file_management/write.py index b42b70256b7fe..1d62065bb74e4 100644 --- a/libs/community/langchain_community/tools/file_management/write.py +++ b/libs/community/langchain_community/tools/file_management/write.py @@ -1,8 +1,8 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.tools.file_management.utils import ( INVALID_PATH_TEMPLATE, diff --git a/libs/community/langchain_community/tools/financial_datasets/balance_sheets.py b/libs/community/langchain_community/tools/financial_datasets/balance_sheets.py index 5bbc9093dc3be..a2374e35c7cbb 100644 --- a/libs/community/langchain_community/tools/financial_datasets/balance_sheets.py +++ b/libs/community/langchain_community/tools/financial_datasets/balance_sheets.py @@ -1,8 +1,8 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.utilities.financial_datasets import FinancialDatasetsAPIWrapper diff --git a/libs/community/langchain_community/tools/financial_datasets/cash_flow_statements.py b/libs/community/langchain_community/tools/financial_datasets/cash_flow_statements.py index 65f95a3e0c993..e8ac4a5b2bff6 100644 --- a/libs/community/langchain_community/tools/financial_datasets/cash_flow_statements.py +++ b/libs/community/langchain_community/tools/financial_datasets/cash_flow_statements.py @@ -1,8 +1,8 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.utilities.financial_datasets import FinancialDatasetsAPIWrapper diff --git a/libs/community/langchain_community/tools/financial_datasets/income_statements.py b/libs/community/langchain_community/tools/financial_datasets/income_statements.py index 8e4f197d529e3..e9db4e5cc1626 100644 --- a/libs/community/langchain_community/tools/financial_datasets/income_statements.py +++ b/libs/community/langchain_community/tools/financial_datasets/income_statements.py @@ -1,8 +1,8 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.utilities.financial_datasets import FinancialDatasetsAPIWrapper diff --git a/libs/community/langchain_community/tools/github/tool.py b/libs/community/langchain_community/tools/github/tool.py index cb1433b6551c9..af6933fe9b80e 100644 --- a/libs/community/langchain_community/tools/github/tool.py +++ b/libs/community/langchain_community/tools/github/tool.py @@ -11,8 +11,8 @@ from typing import Any, Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.utilities.github import GitHubAPIWrapper diff --git a/libs/community/langchain_community/tools/gitlab/tool.py b/libs/community/langchain_community/tools/gitlab/tool.py index bce7ca950cc13..8dfe2bb32fdbd 100644 --- a/libs/community/langchain_community/tools/gitlab/tool.py +++ b/libs/community/langchain_community/tools/gitlab/tool.py @@ -11,8 +11,8 @@ from typing import Optional from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool +from pydantic import Field from langchain_community.utilities.gitlab import GitLabAPIWrapper diff --git a/libs/community/langchain_community/tools/gmail/base.py b/libs/community/langchain_community/tools/gmail/base.py index 1bbecb252326f..d55b0d30f8a45 100644 --- a/libs/community/langchain_community/tools/gmail/base.py +++ b/libs/community/langchain_community/tools/gmail/base.py @@ -4,8 +4,8 @@ from typing import TYPE_CHECKING -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool +from pydantic import Field from langchain_community.tools.gmail.utils import build_resource_service diff --git a/libs/community/langchain_community/tools/gmail/create_draft.py b/libs/community/langchain_community/tools/gmail/create_draft.py index b8e6ac93c6178..2fdfcf37dd2c8 100644 --- a/libs/community/langchain_community/tools/gmail/create_draft.py +++ b/libs/community/langchain_community/tools/gmail/create_draft.py @@ -3,7 +3,7 @@ from typing import List, Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field from langchain_community.tools.gmail.base import GmailBaseTool diff --git a/libs/community/langchain_community/tools/gmail/get_message.py b/libs/community/langchain_community/tools/gmail/get_message.py index 3be2db0853312..6155cb499f4d9 100644 --- a/libs/community/langchain_community/tools/gmail/get_message.py +++ b/libs/community/langchain_community/tools/gmail/get_message.py @@ -3,7 +3,7 @@ from typing import Dict, Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field from langchain_community.tools.gmail.base import GmailBaseTool from langchain_community.tools.gmail.utils import clean_email_body diff --git a/libs/community/langchain_community/tools/gmail/get_thread.py b/libs/community/langchain_community/tools/gmail/get_thread.py index c42c90924b6dc..5e61bd8bb98ab 100644 --- a/libs/community/langchain_community/tools/gmail/get_thread.py +++ b/libs/community/langchain_community/tools/gmail/get_thread.py @@ -1,7 +1,7 @@ from typing import Dict, Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field from langchain_community.tools.gmail.base import GmailBaseTool diff --git a/libs/community/langchain_community/tools/gmail/search.py b/libs/community/langchain_community/tools/gmail/search.py index 72125762d5464..eb61968429520 100644 --- a/libs/community/langchain_community/tools/gmail/search.py +++ b/libs/community/langchain_community/tools/gmail/search.py @@ -4,7 +4,7 @@ from typing import Any, Dict, List, Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field from langchain_community.tools.gmail.base import GmailBaseTool from langchain_community.tools.gmail.utils import clean_email_body diff --git a/libs/community/langchain_community/tools/gmail/send_message.py b/libs/community/langchain_community/tools/gmail/send_message.py index c059b31f3f4b3..3ccd2185048e6 100644 --- a/libs/community/langchain_community/tools/gmail/send_message.py +++ b/libs/community/langchain_community/tools/gmail/send_message.py @@ -6,7 +6,7 @@ from typing import Any, Dict, List, Optional, Type, Union from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field from langchain_community.tools.gmail.base import GmailBaseTool diff --git a/libs/community/langchain_community/tools/google_places/tool.py b/libs/community/langchain_community/tools/google_places/tool.py index de33e0196a177..b97705c9742dd 100644 --- a/libs/community/langchain_community/tools/google_places/tool.py +++ b/libs/community/langchain_community/tools/google_places/tool.py @@ -4,8 +4,8 @@ from langchain_core._api.deprecation import deprecated from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.utilities.google_places_api import GooglePlacesAPIWrapper diff --git a/libs/community/langchain_community/tools/google_serper/tool.py b/libs/community/langchain_community/tools/google_serper/tool.py index b94771a894f38..562dd012a1ac6 100644 --- a/libs/community/langchain_community/tools/google_serper/tool.py +++ b/libs/community/langchain_community/tools/google_serper/tool.py @@ -6,8 +6,8 @@ AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool +from pydantic import Field from langchain_community.utilities.google_serper import GoogleSerperAPIWrapper diff --git a/libs/community/langchain_community/tools/graphql/tool.py b/libs/community/langchain_community/tools/graphql/tool.py index bca6211d5be8e..0530f8cae07fe 100644 --- a/libs/community/langchain_community/tools/graphql/tool.py +++ b/libs/community/langchain_community/tools/graphql/tool.py @@ -3,6 +3,7 @@ from langchain_core.callbacks import CallbackManagerForToolRun from langchain_core.tools import BaseTool +from pydantic import ConfigDict from langchain_community.utilities.graphql import GraphQLAPIWrapper @@ -22,8 +23,9 @@ class BaseGraphQLTool(BaseTool): Example Input: query {{ allUsers {{ id, name, email }} }}\ """ # noqa: E501 - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def _run( self, diff --git a/libs/community/langchain_community/tools/human/tool.py b/libs/community/langchain_community/tools/human/tool.py index e4987e44fa6c3..d9e238b93c97b 100644 --- a/libs/community/langchain_community/tools/human/tool.py +++ b/libs/community/langchain_community/tools/human/tool.py @@ -3,8 +3,8 @@ from typing import Callable, Optional from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool +from pydantic import Field def _print_func(text: str) -> None: diff --git a/libs/community/langchain_community/tools/jina_search/tool.py b/libs/community/langchain_community/tools/jina_search/tool.py index 4ddb098eebb4c..a6961e685b382 100644 --- a/libs/community/langchain_community/tools/jina_search/tool.py +++ b/libs/community/langchain_community/tools/jina_search/tool.py @@ -3,8 +3,8 @@ from typing import Optional from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.utilities.jina_search import JinaSearchAPIWrapper diff --git a/libs/community/langchain_community/tools/jira/tool.py b/libs/community/langchain_community/tools/jira/tool.py index a80044b2a10cd..70868947004ac 100644 --- a/libs/community/langchain_community/tools/jira/tool.py +++ b/libs/community/langchain_community/tools/jira/tool.py @@ -23,8 +23,8 @@ from typing import Optional from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool +from pydantic import Field from langchain_community.utilities.jira import JiraAPIWrapper diff --git a/libs/community/langchain_community/tools/json/tool.py b/libs/community/langchain_community/tools/json/tool.py index bcb04fbf1f2f2..6e7fddff6d7d6 100644 --- a/libs/community/langchain_community/tools/json/tool.py +++ b/libs/community/langchain_community/tools/json/tool.py @@ -8,7 +8,7 @@ from pathlib import Path from typing import Dict, List, Optional, Union -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel from langchain_core.callbacks import ( AsyncCallbackManagerForToolRun, diff --git a/libs/community/langchain_community/tools/memorize/tool.py b/libs/community/langchain_community/tools/memorize/tool.py index 30d416b1fd774..87badf9ac31f1 100644 --- a/libs/community/langchain_community/tools/memorize/tool.py +++ b/libs/community/langchain_community/tools/memorize/tool.py @@ -5,8 +5,8 @@ AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool +from pydantic import Field from langchain_community.llms.gradient_ai import TrainResult diff --git a/libs/community/langchain_community/tools/multion/close_session.py b/libs/community/langchain_community/tools/multion/close_session.py index 023c07539e0f1..28f0abd013ba7 100644 --- a/libs/community/langchain_community/tools/multion/close_session.py +++ b/libs/community/langchain_community/tools/multion/close_session.py @@ -3,8 +3,8 @@ from langchain_core.callbacks import ( CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field if TYPE_CHECKING: # This is for linting and IDE typehints diff --git a/libs/community/langchain_community/tools/multion/create_session.py b/libs/community/langchain_community/tools/multion/create_session.py index de6983cb4bc2d..53388a5a973f7 100644 --- a/libs/community/langchain_community/tools/multion/create_session.py +++ b/libs/community/langchain_community/tools/multion/create_session.py @@ -3,8 +3,8 @@ from langchain_core.callbacks import ( CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field if TYPE_CHECKING: # This is for linting and IDE typehints diff --git a/libs/community/langchain_community/tools/multion/update_session.py b/libs/community/langchain_community/tools/multion/update_session.py index 10fe4c9fa6384..b535861e2ac76 100644 --- a/libs/community/langchain_community/tools/multion/update_session.py +++ b/libs/community/langchain_community/tools/multion/update_session.py @@ -3,8 +3,8 @@ from langchain_core.callbacks import ( CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field if TYPE_CHECKING: # This is for linting and IDE typehints diff --git a/libs/community/langchain_community/tools/nasa/tool.py b/libs/community/langchain_community/tools/nasa/tool.py index 74c24c78449a4..b9f2caa455584 100644 --- a/libs/community/langchain_community/tools/nasa/tool.py +++ b/libs/community/langchain_community/tools/nasa/tool.py @@ -6,8 +6,8 @@ from typing import Optional from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool +from pydantic import Field from langchain_community.utilities.nasa import NasaAPIWrapper diff --git a/libs/community/langchain_community/tools/nuclia/tool.py b/libs/community/langchain_community/tools/nuclia/tool.py index fcba67a9618d4..a575f7d1d054d 100644 --- a/libs/community/langchain_community/tools/nuclia/tool.py +++ b/libs/community/langchain_community/tools/nuclia/tool.py @@ -20,8 +20,8 @@ AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field logger = logging.getLogger(__name__) diff --git a/libs/community/langchain_community/tools/office365/base.py b/libs/community/langchain_community/tools/office365/base.py index 24aed733a771b..55160bd5e509e 100644 --- a/libs/community/langchain_community/tools/office365/base.py +++ b/libs/community/langchain_community/tools/office365/base.py @@ -4,8 +4,8 @@ from typing import TYPE_CHECKING -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool +from pydantic import Field from langchain_community.tools.office365.utils import authenticate diff --git a/libs/community/langchain_community/tools/office365/create_draft_message.py b/libs/community/langchain_community/tools/office365/create_draft_message.py index 88c94578a83f6..02915ffedcf86 100644 --- a/libs/community/langchain_community/tools/office365/create_draft_message.py +++ b/libs/community/langchain_community/tools/office365/create_draft_message.py @@ -1,7 +1,7 @@ from typing import List, Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field from langchain_community.tools.office365.base import O365BaseTool diff --git a/libs/community/langchain_community/tools/office365/events_search.py b/libs/community/langchain_community/tools/office365/events_search.py index 514eb4025df1a..f23dd86b0870c 100644 --- a/libs/community/langchain_community/tools/office365/events_search.py +++ b/libs/community/langchain_community/tools/office365/events_search.py @@ -8,7 +8,7 @@ from typing import Any, Dict, List, Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, ConfigDict, Field from langchain_community.tools.office365.base import O365BaseTool from langchain_community.tools.office365.utils import UTC_FORMAT, clean_body @@ -70,8 +70,9 @@ class O365SearchEvents(O365BaseTool): "is busy during meetings. Any times without events are free for the user. " ) - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def _run( self, diff --git a/libs/community/langchain_community/tools/office365/messages_search.py b/libs/community/langchain_community/tools/office365/messages_search.py index dac0ceac30e3b..5b66dc63644b3 100644 --- a/libs/community/langchain_community/tools/office365/messages_search.py +++ b/libs/community/langchain_community/tools/office365/messages_search.py @@ -7,7 +7,7 @@ from typing import Any, Dict, List, Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, ConfigDict, Field from langchain_community.tools.office365.base import O365BaseTool from langchain_community.tools.office365.utils import UTC_FORMAT, clean_body @@ -66,8 +66,9 @@ class O365SearchEmails(O365BaseTool): " The output is a JSON list of the requested resource." ) - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def _run( self, diff --git a/libs/community/langchain_community/tools/office365/send_event.py b/libs/community/langchain_community/tools/office365/send_event.py index e8b9d023af7a5..6a824deef759d 100644 --- a/libs/community/langchain_community/tools/office365/send_event.py +++ b/libs/community/langchain_community/tools/office365/send_event.py @@ -8,7 +8,7 @@ from typing import List, Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field from langchain_community.tools.office365.base import O365BaseTool from langchain_community.tools.office365.utils import UTC_FORMAT diff --git a/libs/community/langchain_community/tools/office365/send_message.py b/libs/community/langchain_community/tools/office365/send_message.py index 828e9e4fa614d..6ebc8883714ed 100644 --- a/libs/community/langchain_community/tools/office365/send_message.py +++ b/libs/community/langchain_community/tools/office365/send_message.py @@ -1,7 +1,7 @@ from typing import List, Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field from langchain_community.tools.office365.base import O365BaseTool diff --git a/libs/community/langchain_community/tools/openapi/utils/api_models.py b/libs/community/langchain_community/tools/openapi/utils/api_models.py index 4c253e31e20a7..fef0b849ae37d 100644 --- a/libs/community/langchain_community/tools/openapi/utils/api_models.py +++ b/libs/community/langchain_community/tools/openapi/utils/api_models.py @@ -16,7 +16,7 @@ Union, ) -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field from langchain_community.tools.openapi.utils.openapi_utils import HTTPVerb, OpenAPISpec diff --git a/libs/community/langchain_community/tools/openweathermap/tool.py b/libs/community/langchain_community/tools/openweathermap/tool.py index d716b8098d840..f1e7758d2d0c2 100644 --- a/libs/community/langchain_community/tools/openweathermap/tool.py +++ b/libs/community/langchain_community/tools/openweathermap/tool.py @@ -3,8 +3,8 @@ from typing import Optional from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool +from pydantic import Field from langchain_community.utilities.openweathermap import OpenWeatherMapAPIWrapper diff --git a/libs/community/langchain_community/tools/passio_nutrition_ai/tool.py b/libs/community/langchain_community/tools/passio_nutrition_ai/tool.py index 275760d4cbca0..939e1a41bc9fe 100644 --- a/libs/community/langchain_community/tools/passio_nutrition_ai/tool.py +++ b/libs/community/langchain_community/tools/passio_nutrition_ai/tool.py @@ -3,8 +3,8 @@ from typing import Dict, Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.utilities.passio_nutrition_ai import NutritionAIAPI diff --git a/libs/community/langchain_community/tools/playwright/base.py b/libs/community/langchain_community/tools/playwright/base.py index f06b964e4d2d2..e85cc8479360c 100644 --- a/libs/community/langchain_community/tools/playwright/base.py +++ b/libs/community/langchain_community/tools/playwright/base.py @@ -1,10 +1,10 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Optional, Tuple, Type +from typing import TYPE_CHECKING, Any, Optional, Tuple, Type -from langchain_core.pydantic_v1 import root_validator from langchain_core.tools import BaseTool from langchain_core.utils import guard_import +from pydantic import model_validator if TYPE_CHECKING: from playwright.async_api import Browser as AsyncBrowser @@ -38,8 +38,9 @@ class BaseBrowserTool(BaseTool): sync_browser: Optional["SyncBrowser"] = None async_browser: Optional["AsyncBrowser"] = None - @root_validator(pre=True) - def validate_browser_provided(cls, values: dict) -> dict: + @model_validator(mode="before") + @classmethod + def validate_browser_provided(cls, values: dict) -> Any: """Check that the arguments are valid.""" lazy_import_playwright_browsers() if values.get("async_browser") is None and values.get("sync_browser") is None: diff --git a/libs/community/langchain_community/tools/playwright/click.py b/libs/community/langchain_community/tools/playwright/click.py index 5cf5cdda148ee..22c6a23bf9c33 100644 --- a/libs/community/langchain_community/tools/playwright/click.py +++ b/libs/community/langchain_community/tools/playwright/click.py @@ -6,7 +6,7 @@ AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field from langchain_community.tools.playwright.base import BaseBrowserTool from langchain_community.tools.playwright.utils import ( diff --git a/libs/community/langchain_community/tools/playwright/current_page.py b/libs/community/langchain_community/tools/playwright/current_page.py index 9ea5e0e1f9dd9..94abcaf1f1c25 100644 --- a/libs/community/langchain_community/tools/playwright/current_page.py +++ b/libs/community/langchain_community/tools/playwright/current_page.py @@ -6,7 +6,7 @@ AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel from langchain_community.tools.playwright.base import BaseBrowserTool from langchain_community.tools.playwright.utils import ( diff --git a/libs/community/langchain_community/tools/playwright/extract_hyperlinks.py b/libs/community/langchain_community/tools/playwright/extract_hyperlinks.py index 0dcd3fd2e06b6..00a5e290274b5 100644 --- a/libs/community/langchain_community/tools/playwright/extract_hyperlinks.py +++ b/libs/community/langchain_community/tools/playwright/extract_hyperlinks.py @@ -7,7 +7,7 @@ AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator +from pydantic import BaseModel, Field, model_validator from langchain_community.tools.playwright.base import BaseBrowserTool from langchain_community.tools.playwright.utils import ( @@ -35,8 +35,9 @@ class ExtractHyperlinksTool(BaseBrowserTool): description: str = "Extract all hyperlinks on the current webpage" args_schema: Type[BaseModel] = ExtractHyperlinksToolInput - @root_validator(pre=True) - def check_bs_import(cls, values: dict) -> dict: + @model_validator(mode="before") + @classmethod + def check_bs_import(cls, values: dict) -> Any: """Check that the arguments are valid.""" try: from bs4 import BeautifulSoup # noqa: F401 diff --git a/libs/community/langchain_community/tools/playwright/extract_text.py b/libs/community/langchain_community/tools/playwright/extract_text.py index 0cf112b0fb2e0..b04e367952afa 100644 --- a/libs/community/langchain_community/tools/playwright/extract_text.py +++ b/libs/community/langchain_community/tools/playwright/extract_text.py @@ -1,12 +1,12 @@ from __future__ import annotations -from typing import Optional, Type +from typing import Any, Optional, Type from langchain_core.callbacks import ( AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import BaseModel, root_validator +from pydantic import BaseModel, model_validator from langchain_community.tools.playwright.base import BaseBrowserTool from langchain_community.tools.playwright.utils import ( @@ -22,8 +22,9 @@ class ExtractTextTool(BaseBrowserTool): description: str = "Extract all the text on the current webpage" args_schema: Type[BaseModel] = BaseModel - @root_validator(pre=True) - def check_acheck_bs_importrgs(cls, values: dict) -> dict: + @model_validator(mode="before") + @classmethod + def check_acheck_bs_importrgs(cls, values: dict) -> Any: """Check that the arguments are valid.""" try: from bs4 import BeautifulSoup # noqa: F401 diff --git a/libs/community/langchain_community/tools/playwright/get_elements.py b/libs/community/langchain_community/tools/playwright/get_elements.py index 3b88529fc7588..11e43c01696aa 100644 --- a/libs/community/langchain_community/tools/playwright/get_elements.py +++ b/libs/community/langchain_community/tools/playwright/get_elements.py @@ -7,7 +7,7 @@ AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field from langchain_community.tools.playwright.base import BaseBrowserTool from langchain_community.tools.playwright.utils import ( diff --git a/libs/community/langchain_community/tools/playwright/navigate.py b/libs/community/langchain_community/tools/playwright/navigate.py index 82f6349ff064a..5c0de271ce9af 100644 --- a/libs/community/langchain_community/tools/playwright/navigate.py +++ b/libs/community/langchain_community/tools/playwright/navigate.py @@ -7,7 +7,7 @@ AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import BaseModel, Field, validator +from pydantic import BaseModel, Field, validator from langchain_community.tools.playwright.base import BaseBrowserTool from langchain_community.tools.playwright.utils import ( diff --git a/libs/community/langchain_community/tools/playwright/navigate_back.py b/libs/community/langchain_community/tools/playwright/navigate_back.py index 5988fa7fe894a..908e20bd57100 100644 --- a/libs/community/langchain_community/tools/playwright/navigate_back.py +++ b/libs/community/langchain_community/tools/playwright/navigate_back.py @@ -6,7 +6,7 @@ AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel from langchain_community.tools.playwright.base import BaseBrowserTool from langchain_community.tools.playwright.utils import ( diff --git a/libs/community/langchain_community/tools/plugin.py b/libs/community/langchain_community/tools/plugin.py index 0966ed5bedee2..102451e72dade 100644 --- a/libs/community/langchain_community/tools/plugin.py +++ b/libs/community/langchain_community/tools/plugin.py @@ -9,8 +9,8 @@ AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import BaseModel from langchain_core.tools import BaseTool +from pydantic import BaseModel class ApiConfig(BaseModel): diff --git a/libs/community/langchain_community/tools/polygon/aggregates.py b/libs/community/langchain_community/tools/polygon/aggregates.py index e4c07761727f9..26cb62d4677de 100644 --- a/libs/community/langchain_community/tools/polygon/aggregates.py +++ b/libs/community/langchain_community/tools/polygon/aggregates.py @@ -1,8 +1,8 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.utilities.polygon import PolygonAPIWrapper diff --git a/libs/community/langchain_community/tools/polygon/financials.py b/libs/community/langchain_community/tools/polygon/financials.py index aae19cad06ab7..8400e7498b469 100644 --- a/libs/community/langchain_community/tools/polygon/financials.py +++ b/libs/community/langchain_community/tools/polygon/financials.py @@ -1,8 +1,8 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel from langchain_core.tools import BaseTool +from pydantic import BaseModel from langchain_community.utilities.polygon import PolygonAPIWrapper diff --git a/libs/community/langchain_community/tools/polygon/last_quote.py b/libs/community/langchain_community/tools/polygon/last_quote.py index ce6ebe750ea57..76c768113b3b5 100644 --- a/libs/community/langchain_community/tools/polygon/last_quote.py +++ b/libs/community/langchain_community/tools/polygon/last_quote.py @@ -1,8 +1,8 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel from langchain_core.tools import BaseTool +from pydantic import BaseModel from langchain_community.utilities.polygon import PolygonAPIWrapper diff --git a/libs/community/langchain_community/tools/polygon/ticker_news.py b/libs/community/langchain_community/tools/polygon/ticker_news.py index 9d858ebc8f498..d4c4a2017abbd 100644 --- a/libs/community/langchain_community/tools/polygon/ticker_news.py +++ b/libs/community/langchain_community/tools/polygon/ticker_news.py @@ -1,8 +1,8 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel from langchain_core.tools import BaseTool +from pydantic import BaseModel from langchain_community.utilities.polygon import PolygonAPIWrapper diff --git a/libs/community/langchain_community/tools/powerbi/tool.py b/libs/community/langchain_community/tools/powerbi/tool.py index 2a2602f28ad9f..9d55f431c2fc5 100644 --- a/libs/community/langchain_community/tools/powerbi/tool.py +++ b/libs/community/langchain_community/tools/powerbi/tool.py @@ -8,8 +8,8 @@ AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import Field, validator from langchain_core.tools import BaseTool +from pydantic import ConfigDict, Field, validator from langchain_community.chat_models.openai import _import_tiktoken from langchain_community.tools.powerbi.prompt import ( @@ -39,8 +39,9 @@ class QueryPowerBITool(BaseTool): output_token_limit: int = 4000 tiktoken_model_name: Optional[str] = None # "cl100k_base" - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) @validator("llm_chain") def validate_llm_chain_input_variables( # pylint: disable=E0213 @@ -225,8 +226,9 @@ class InfoPowerBITool(BaseTool): """ # noqa: E501 powerbi: PowerBIDataset = Field(exclude=True) - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def _run( self, @@ -251,8 +253,9 @@ class ListPowerBITool(BaseTool): description: str = "Input is an empty string, output is a comma separated list of tables in the database." # noqa: E501 # pylint: disable=C0301 powerbi: PowerBIDataset = Field(exclude=True) - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def _run( self, diff --git a/libs/community/langchain_community/tools/pubmed/tool.py b/libs/community/langchain_community/tools/pubmed/tool.py index 7eda0fd3a5778..fe8338635449f 100644 --- a/libs/community/langchain_community/tools/pubmed/tool.py +++ b/libs/community/langchain_community/tools/pubmed/tool.py @@ -1,8 +1,8 @@ from typing import Optional from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool +from pydantic import Field from langchain_community.utilities.pubmed import PubMedAPIWrapper diff --git a/libs/community/langchain_community/tools/reddit_search/tool.py b/libs/community/langchain_community/tools/reddit_search/tool.py index e245286e840b4..fc823c2b23dcd 100644 --- a/libs/community/langchain_community/tools/reddit_search/tool.py +++ b/libs/community/langchain_community/tools/reddit_search/tool.py @@ -3,8 +3,8 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.utilities.reddit_search import RedditSearchAPIWrapper diff --git a/libs/community/langchain_community/tools/requests/tool.py b/libs/community/langchain_community/tools/requests/tool.py index 49c2b1734f514..c91b348053f0e 100644 --- a/libs/community/langchain_community/tools/requests/tool.py +++ b/libs/community/langchain_community/tools/requests/tool.py @@ -4,7 +4,7 @@ import json from typing import Any, Dict, Optional, Union -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel from langchain_core.callbacks import ( AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, diff --git a/libs/community/langchain_community/tools/riza/command.py b/libs/community/langchain_community/tools/riza/command.py index cfc36865768af..f0fbe382d46cd 100644 --- a/libs/community/langchain_community/tools/riza/command.py +++ b/libs/community/langchain_community/tools/riza/command.py @@ -10,8 +10,8 @@ from langchain_core.callbacks import ( CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool, ToolException +from pydantic import BaseModel, Field class ExecPythonInput(BaseModel): diff --git a/libs/community/langchain_community/tools/scenexplain/tool.py b/libs/community/langchain_community/tools/scenexplain/tool.py index 94ed70e699102..fdc085ab1c8fd 100644 --- a/libs/community/langchain_community/tools/scenexplain/tool.py +++ b/libs/community/langchain_community/tools/scenexplain/tool.py @@ -3,8 +3,8 @@ from typing import Optional from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.utilities.scenexplain import SceneXplainAPIWrapper diff --git a/libs/community/langchain_community/tools/searchapi/tool.py b/libs/community/langchain_community/tools/searchapi/tool.py index 5e7f5e8917cff..205d59880deaa 100644 --- a/libs/community/langchain_community/tools/searchapi/tool.py +++ b/libs/community/langchain_community/tools/searchapi/tool.py @@ -6,8 +6,8 @@ AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool +from pydantic import Field from langchain_community.utilities.searchapi import SearchApiAPIWrapper diff --git a/libs/community/langchain_community/tools/searx_search/tool.py b/libs/community/langchain_community/tools/searx_search/tool.py index db8bca859c3b4..d16739e88f2f1 100644 --- a/libs/community/langchain_community/tools/searx_search/tool.py +++ b/libs/community/langchain_community/tools/searx_search/tool.py @@ -6,8 +6,8 @@ AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, ConfigDict, Field from langchain_community.utilities.searx_search import SearxSearchWrapper @@ -62,8 +62,9 @@ class SearxSearchResults(BaseTool): kwargs: dict = Field(default_factory=dict) args_schema: Type[BaseModel] = SearxSearchQueryInput - class Config: - extra = "allow" + model_config = ConfigDict( + extra="allow", + ) def _run( self, diff --git a/libs/community/langchain_community/tools/semanticscholar/tool.py b/libs/community/langchain_community/tools/semanticscholar/tool.py index d9bf773ea9368..ce53fa4bab525 100644 --- a/libs/community/langchain_community/tools/semanticscholar/tool.py +++ b/libs/community/langchain_community/tools/semanticscholar/tool.py @@ -3,8 +3,8 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.utilities.semanticscholar import SemanticScholarAPIWrapper diff --git a/libs/community/langchain_community/tools/shell/tool.py b/libs/community/langchain_community/tools/shell/tool.py index cbc77ea16ffa6..c4ff4d1b60596 100644 --- a/libs/community/langchain_community/tools/shell/tool.py +++ b/libs/community/langchain_community/tools/shell/tool.py @@ -6,8 +6,8 @@ from langchain_core.callbacks import ( CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field, model_validator logger = logging.getLogger(__name__) @@ -21,8 +21,9 @@ class ShellInput(BaseModel): ) """List of shell commands to run.""" - @root_validator(pre=True) - def _validate_commands(cls, values: dict) -> dict: + @model_validator(mode="before") + @classmethod + def _validate_commands(cls, values: dict) -> Any: """Validate commands.""" # TODO: Add real validators commands = values.get("commands") diff --git a/libs/community/langchain_community/tools/slack/base.py b/libs/community/langchain_community/tools/slack/base.py index 1b87779490729..6579e6ce44e30 100644 --- a/libs/community/langchain_community/tools/slack/base.py +++ b/libs/community/langchain_community/tools/slack/base.py @@ -4,8 +4,8 @@ from typing import TYPE_CHECKING -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool +from pydantic import Field from langchain_community.tools.slack.utils import login diff --git a/libs/community/langchain_community/tools/slack/get_message.py b/libs/community/langchain_community/tools/slack/get_message.py index 06ec9a9f57031..733f16979e428 100644 --- a/libs/community/langchain_community/tools/slack/get_message.py +++ b/libs/community/langchain_community/tools/slack/get_message.py @@ -3,7 +3,7 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field from langchain_community.tools.slack.base import SlackBaseTool diff --git a/libs/community/langchain_community/tools/slack/schedule_message.py b/libs/community/langchain_community/tools/slack/schedule_message.py index 90edc9bcb9d71..c4a561f5aae17 100644 --- a/libs/community/langchain_community/tools/slack/schedule_message.py +++ b/libs/community/langchain_community/tools/slack/schedule_message.py @@ -3,7 +3,7 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field from langchain_community.tools.slack.base import SlackBaseTool from langchain_community.tools.slack.utils import UTC_FORMAT diff --git a/libs/community/langchain_community/tools/slack/send_message.py b/libs/community/langchain_community/tools/slack/send_message.py index c5e8a875ad0fe..87223830d431a 100644 --- a/libs/community/langchain_community/tools/slack/send_message.py +++ b/libs/community/langchain_community/tools/slack/send_message.py @@ -1,7 +1,7 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field from langchain_community.tools.slack.base import SlackBaseTool diff --git a/libs/community/langchain_community/tools/sleep/tool.py b/libs/community/langchain_community/tools/sleep/tool.py index 27ea267e6e930..e39a272a79f79 100644 --- a/libs/community/langchain_community/tools/sleep/tool.py +++ b/libs/community/langchain_community/tools/sleep/tool.py @@ -8,8 +8,8 @@ AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field class SleepInput(BaseModel): diff --git a/libs/community/langchain_community/tools/spark_sql/tool.py b/libs/community/langchain_community/tools/spark_sql/tool.py index 7c40bebf17b70..91bbc87f29549 100644 --- a/libs/community/langchain_community/tools/spark_sql/tool.py +++ b/libs/community/langchain_community/tools/spark_sql/tool.py @@ -3,7 +3,7 @@ from typing import Any, Dict, Optional -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator +from pydantic import BaseModel, Field, root_validator, model_validator, ConfigDict from langchain_core.language_models import BaseLanguageModel from langchain_core.callbacks import ( @@ -21,8 +21,9 @@ class BaseSparkSQLTool(BaseModel): db: SparkSQL = Field(exclude=True) - class Config(BaseTool.Config): - pass + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) class QuerySparkSQLTool(BaseSparkSQLTool, BaseTool): @@ -92,8 +93,9 @@ class QueryCheckerTool(BaseSparkSQLTool, BaseTool): Always use this tool before executing a query with query_sql_db! """ - @root_validator(pre=True) - def initialize_llm_chain(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def initialize_llm_chain(cls, values: Dict[str, Any]) -> Any: if "llm_chain" not in values: from langchain.chains.llm import LLMChain diff --git a/libs/community/langchain_community/tools/sql_database/tool.py b/libs/community/langchain_community/tools/sql_database/tool.py index be89eefbf0912..1bc119860d6ed 100644 --- a/libs/community/langchain_community/tools/sql_database/tool.py +++ b/libs/community/langchain_community/tools/sql_database/tool.py @@ -5,7 +5,7 @@ from sqlalchemy.engine import Result -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator +from pydantic import BaseModel, Field, root_validator, model_validator, ConfigDict from langchain_core.language_models import BaseLanguageModel from langchain_core.callbacks import ( @@ -23,8 +23,9 @@ class BaseSQLDatabaseTool(BaseModel): db: SQLDatabase = Field(exclude=True) - class Config(BaseTool.Config): - pass + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) class _QuerySQLDataBaseToolInput(BaseModel): @@ -117,8 +118,9 @@ class QuerySQLCheckerTool(BaseSQLDatabaseTool, BaseTool): """ args_schema: Type[BaseModel] = _QuerySQLCheckerToolInput - @root_validator(pre=True) - def initialize_llm_chain(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def initialize_llm_chain(cls, values: Dict[str, Any]) -> Any: if "llm_chain" not in values: from langchain.chains.llm import LLMChain diff --git a/libs/community/langchain_community/tools/steamship_image_generation/tool.py b/libs/community/langchain_community/tools/steamship_image_generation/tool.py index 20d3956209f7e..ea1c4b6860df0 100644 --- a/libs/community/langchain_community/tools/steamship_image_generation/tool.py +++ b/libs/community/langchain_community/tools/steamship_image_generation/tool.py @@ -15,12 +15,12 @@ from __future__ import annotations from enum import Enum -from typing import TYPE_CHECKING, Dict, Optional +from typing import TYPE_CHECKING, Any, Dict, Optional from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import root_validator from langchain_core.tools import BaseTool from langchain_core.utils import get_from_dict_or_env +from pydantic import model_validator from langchain_community.tools.steamship_image_generation.utils import make_image_public @@ -56,8 +56,9 @@ class SteamshipImageGenerationTool(BaseTool): "Output: the UUID of a generated image" ) - @root_validator(pre=True) - def validate_size(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_size(cls, values: Dict) -> Any: if "size" in values: size = values["size"] model_name = values["model_name"] @@ -66,8 +67,9 @@ def validate_size(cls, values: Dict) -> Dict: return values - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" steamship_api_key = get_from_dict_or_env( values, "steamship_api_key", "STEAMSHIP_API_KEY" diff --git a/libs/community/langchain_community/tools/tavily_search/tool.py b/libs/community/langchain_community/tools/tavily_search/tool.py index 6a5b246dd1e86..53164d9340698 100644 --- a/libs/community/langchain_community/tools/tavily_search/tool.py +++ b/libs/community/langchain_community/tools/tavily_search/tool.py @@ -6,8 +6,8 @@ AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.utilities.tavily_search import TavilySearchAPIWrapper diff --git a/libs/community/langchain_community/tools/vectorstore/tool.py b/libs/community/langchain_community/tools/vectorstore/tool.py index 7f9b965f08cc8..e2479929df6ba 100644 --- a/libs/community/langchain_community/tools/vectorstore/tool.py +++ b/libs/community/langchain_community/tools/vectorstore/tool.py @@ -8,9 +8,9 @@ CallbackManagerForToolRun, ) from langchain_core.language_models import BaseLanguageModel -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool from langchain_core.vectorstores import VectorStore +from pydantic import BaseModel, ConfigDict, Field from langchain_community.llms.openai import OpenAI @@ -21,8 +21,9 @@ class BaseVectorStoreTool(BaseModel): vectorstore: VectorStore = Field(exclude=True) llm: BaseLanguageModel = Field(default_factory=lambda: OpenAI(temperature=0)) - class Config(BaseTool.Config): - pass + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def _create_description_from_template(values: Dict[str, Any]) -> Dict[str, Any]: diff --git a/libs/community/langchain_community/tools/wikipedia/tool.py b/libs/community/langchain_community/tools/wikipedia/tool.py index 66af94e41a055..127117f89d642 100644 --- a/libs/community/langchain_community/tools/wikipedia/tool.py +++ b/libs/community/langchain_community/tools/wikipedia/tool.py @@ -3,8 +3,8 @@ from typing import Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.utilities.wikipedia import WikipediaAPIWrapper diff --git a/libs/community/langchain_community/tools/yahoo_finance_news.py b/libs/community/langchain_community/tools/yahoo_finance_news.py index 4e120e45ce41b..7c844f0c59052 100644 --- a/libs/community/langchain_community/tools/yahoo_finance_news.py +++ b/libs/community/langchain_community/tools/yahoo_finance_news.py @@ -2,8 +2,8 @@ from langchain_core.callbacks import CallbackManagerForToolRun from langchain_core.documents import Document -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from requests.exceptions import HTTPError, ReadTimeout from urllib3.exceptions import ConnectionError diff --git a/libs/community/langchain_community/tools/you/tool.py b/libs/community/langchain_community/tools/you/tool.py index 949eca08274d7..59990e71ff545 100644 --- a/libs/community/langchain_community/tools/you/tool.py +++ b/libs/community/langchain_community/tools/you/tool.py @@ -5,8 +5,8 @@ CallbackManagerForToolRun, ) from langchain_core.documents import Document -from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field from langchain_community.utilities.you import YouSearchAPIWrapper diff --git a/libs/community/langchain_community/tools/zapier/tool.py b/libs/community/langchain_community/tools/zapier/tool.py index 7cc97132a7c13..44d7fb04c0af7 100644 --- a/libs/community/langchain_community/tools/zapier/tool.py +++ b/libs/community/langchain_community/tools/zapier/tool.py @@ -75,9 +75,9 @@ AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) -from langchain_core.pydantic_v1 import Field from langchain_core.tools import BaseTool from langchain_core.utils import pre_init +from pydantic import Field from langchain_community.tools.zapier.prompt import BASE_ZAPIER_TOOL_PROMPT from langchain_community.utilities.zapier import ZapierNLAWrapper diff --git a/libs/community/langchain_community/tools/zenguard/tool.py b/libs/community/langchain_community/tools/zenguard/tool.py index f7ddbc4defd4b..368c3d273acaf 100644 --- a/libs/community/langchain_community/tools/zenguard/tool.py +++ b/libs/community/langchain_community/tools/zenguard/tool.py @@ -1,10 +1,10 @@ import os from enum import Enum -from typing import Any, Dict, List, Optional +from typing import Any, Dict, List, Optional, Type import requests -from langchain_core.pydantic_v1 import BaseModel, Field, ValidationError, validator from langchain_core.tools import BaseTool +from pydantic import BaseModel, Field, ValidationError, validator class Detector(str, Enum): @@ -30,13 +30,12 @@ class DetectorAPI(str, Enum): class ZenGuardInput(BaseModel): prompts: List[str] = Field( ..., - min_items=1, min_length=1, description="Prompt to check", ) detectors: List[Detector] = Field( ..., - min_items=1, + min_length=1, description="List of detectors by which you want to check the prompt", ) in_parallel: bool = Field( @@ -50,7 +49,7 @@ class ZenGuardTool(BaseTool): description: str = ( "ZenGuard AI integration package. ZenGuard AI - the fastest GenAI guardrails." ) - args_schema = ZenGuardInput + args_schema: Type[BaseModel] = ZenGuardInput return_direct: bool = True zenguard_api_key: Optional[str] = Field(default=None) diff --git a/libs/community/langchain_community/utilities/alpha_vantage.py b/libs/community/langchain_community/utilities/alpha_vantage.py index 59eb65ac910dc..e6354affbfae0 100644 --- a/libs/community/langchain_community/utilities/alpha_vantage.py +++ b/libs/community/langchain_community/utilities/alpha_vantage.py @@ -3,8 +3,8 @@ from typing import Any, Dict, List, Optional import requests -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator class AlphaVantageAPIWrapper(BaseModel): @@ -18,11 +18,13 @@ class AlphaVantageAPIWrapper(BaseModel): alphavantage_api_key: Optional[str] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key exists in environment.""" values["alphavantage_api_key"] = get_from_dict_or_env( values, "alphavantage_api_key", "ALPHAVANTAGE_API_KEY" diff --git a/libs/community/langchain_community/utilities/apify.py b/libs/community/langchain_community/utilities/apify.py index f80deb496d4d3..077bcc12b2985 100644 --- a/libs/community/langchain_community/utilities/apify.py +++ b/libs/community/langchain_community/utilities/apify.py @@ -1,8 +1,8 @@ from typing import TYPE_CHECKING, Any, Callable, Dict, Optional from langchain_core.documents import Document -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, model_validator if TYPE_CHECKING: from langchain_community.document_loaders import ApifyDatasetLoader @@ -19,8 +19,9 @@ class ApifyWrapper(BaseModel): apify_client_async: Any apify_api_token: Optional[str] = None - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate environment. Validate that an Apify API token is set and the apify-client Python package exists in the current environment. diff --git a/libs/community/langchain_community/utilities/arcee.py b/libs/community/langchain_community/utilities/arcee.py index 3064fd16b4d6d..badbf9cdc9139 100644 --- a/libs/community/langchain_community/utilities/arcee.py +++ b/libs/community/langchain_community/utilities/arcee.py @@ -6,8 +6,8 @@ from typing import Any, Dict, List, Literal, Mapping, Optional, Union import requests -from langchain_core.pydantic_v1 import BaseModel, SecretStr, root_validator from langchain_core.retrievers import Document +from pydantic import BaseModel, SecretStr, model_validator class ArceeRoute(str, Enum): @@ -51,8 +51,9 @@ class DALMFilter(BaseModel): value: str _is_metadata: bool = False - @root_validator(pre=True) - def set_meta(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def set_meta(cls, values: Dict) -> Any: """document and name are reserved arcee keys. Anything else is metadata""" values["_is_meta"] = values.get("field_name") not in ["document", "name"] return values diff --git a/libs/community/langchain_community/utilities/arxiv.py b/libs/community/langchain_community/utilities/arxiv.py index 55767a339b723..a4c6cf5aceaad 100644 --- a/libs/community/langchain_community/utilities/arxiv.py +++ b/libs/community/langchain_community/utilities/arxiv.py @@ -6,7 +6,7 @@ from typing import Any, Dict, Iterator, List, Optional from langchain_core.documents import Document -from langchain_core.pydantic_v1 import BaseModel, root_validator +from pydantic import BaseModel, model_validator logger = logging.getLogger(__name__) @@ -73,8 +73,9 @@ def is_arxiv_identifier(self, query: str) -> bool: return False return True - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that the python package exists in environment.""" try: import arxiv diff --git a/libs/community/langchain_community/utilities/asknews.py b/libs/community/langchain_community/utilities/asknews.py index 4f7051bbc1be5..e61a1e23f3d9c 100644 --- a/libs/community/langchain_community/utilities/asknews.py +++ b/libs/community/langchain_community/utilities/asknews.py @@ -5,8 +5,8 @@ from datetime import datetime, timedelta from typing import Any, Dict, Optional -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator class AskNewsAPIWrapper(BaseModel): @@ -19,11 +19,13 @@ class AskNewsAPIWrapper(BaseModel): asknews_client_secret: Optional[str] = None """Client Secret for the AskNews API.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api credentials and python package exists in environment.""" asknews_client_id = get_from_dict_or_env( diff --git a/libs/community/langchain_community/utilities/awslambda.py b/libs/community/langchain_community/utilities/awslambda.py index 1bc77295061b7..9abd0e7958f50 100644 --- a/libs/community/langchain_community/utilities/awslambda.py +++ b/libs/community/langchain_community/utilities/awslambda.py @@ -3,7 +3,7 @@ import json from typing import Any, Dict, Optional -from langchain_core.pydantic_v1 import BaseModel, root_validator +from pydantic import BaseModel, ConfigDict, model_validator class LambdaWrapper(BaseModel): @@ -30,11 +30,13 @@ class LambdaWrapper(BaseModel): awslambda_tool_description: Optional[str] = None """If passing to an agent as a tool, the description""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that python package exists in environment.""" try: diff --git a/libs/community/langchain_community/utilities/bibtex.py b/libs/community/langchain_community/utilities/bibtex.py index 101b88bfaac86..a3bf82ab73889 100644 --- a/libs/community/langchain_community/utilities/bibtex.py +++ b/libs/community/langchain_community/utilities/bibtex.py @@ -3,7 +3,7 @@ import logging from typing import Any, Dict, List, Mapping -from langchain_core.pydantic_v1 import BaseModel, root_validator +from pydantic import BaseModel, ConfigDict, model_validator logger = logging.getLogger(__name__) @@ -36,11 +36,13 @@ class BibtexparserWrapper(BaseModel): a bibtex file and fetch document summaries. """ - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that the python package exists in environment.""" try: import bibtexparser # noqa diff --git a/libs/community/langchain_community/utilities/bing_search.py b/libs/community/langchain_community/utilities/bing_search.py index ee85cbe216bb8..a3dc03fdea9b5 100644 --- a/libs/community/langchain_community/utilities/bing_search.py +++ b/libs/community/langchain_community/utilities/bing_search.py @@ -1,10 +1,10 @@ """Util that calls Bing Search.""" -from typing import Dict, List +from typing import Any, Dict, List import requests -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, Field, model_validator # BING_SEARCH_ENDPOINT is the default endpoint for Bing Web Search API. # Currently There are two web-based Bing Search services available on Azure, @@ -34,8 +34,9 @@ class BingSearchAPIWrapper(BaseModel): search_kwargs: dict = Field(default_factory=dict) """Additional keyword arguments to pass to the search request.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def _bing_search_results(self, search_term: str, count: int) -> List[dict]: headers = {"Ocp-Apim-Subscription-Key": self.bing_subscription_key} @@ -57,8 +58,9 @@ def _bing_search_results(self, search_term: str, count: int) -> List[dict]: return search_results["webPages"]["value"] return [] - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and endpoint exists in environment.""" bing_subscription_key = get_from_dict_or_env( values, "bing_subscription_key", "BING_SUBSCRIPTION_KEY" diff --git a/libs/community/langchain_community/utilities/brave_search.py b/libs/community/langchain_community/utilities/brave_search.py index cb822e55f43d1..41dd9015f2e40 100644 --- a/libs/community/langchain_community/utilities/brave_search.py +++ b/libs/community/langchain_community/utilities/brave_search.py @@ -3,7 +3,7 @@ import requests from langchain_core.documents import Document -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field class BraveSearchWrapper(BaseModel): diff --git a/libs/community/langchain_community/utilities/cassandra_database.py b/libs/community/langchain_community/utilities/cassandra_database.py index 90eba0cae1b16..4ec1973b1668e 100644 --- a/libs/community/langchain_community/utilities/cassandra_database.py +++ b/libs/community/langchain_community/utilities/cassandra_database.py @@ -5,7 +5,8 @@ import re from typing import TYPE_CHECKING, Any, Dict, List, Optional, Sequence, Tuple, Union -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator +from pydantic import BaseModel, ConfigDict, Field, model_validator +from typing_extensions import Self if TYPE_CHECKING: from cassandra.cluster import ResultSet, Session @@ -480,16 +481,17 @@ class Table(BaseModel): clustering: List[Tuple[str, str]] = Field(default_factory=list) indexes: List[Tuple[str, str, str]] = Field(default_factory=list) - class Config: - frozen = True + model_config = ConfigDict( + frozen=True, + ) - @root_validator(pre=False, skip_on_failure=True) - def check_required_fields(cls, class_values: dict) -> dict: - if not class_values["columns"]: + @model_validator(mode="after") + def check_required_fields(self) -> Self: + if not self.columns: raise ValueError("non-empty column list for must be provided") - if not class_values["partition"]: + if not self.partition: raise ValueError("non-empty partition list must be provided") - return class_values + return self @classmethod def from_database( diff --git a/libs/community/langchain_community/utilities/clickup.py b/libs/community/langchain_community/utilities/clickup.py index 2c099a8623a78..f5f00b22a0884 100644 --- a/libs/community/langchain_community/utilities/clickup.py +++ b/libs/community/langchain_community/utilities/clickup.py @@ -6,8 +6,8 @@ from typing import Any, Dict, List, Mapping, Optional, Tuple, Type, Union import requests -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator DEFAULT_URL = "https://api.clickup.com/api/v2" @@ -282,8 +282,9 @@ class ClickupAPIWrapper(BaseModel): folder_id: Optional[str] = None list_id: Optional[str] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) @classmethod def get_access_code_url( @@ -321,8 +322,9 @@ def get_access_token( return data["access_token"] - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" values["access_token"] = get_from_dict_or_env( values, "access_token", "CLICKUP_ACCESS_TOKEN" diff --git a/libs/community/langchain_community/utilities/dalle_image_generator.py b/libs/community/langchain_community/utilities/dalle_image_generator.py index 6ce9b3b5686b8..c0cbf00a437ba 100644 --- a/libs/community/langchain_community/utilities/dalle_image_generator.py +++ b/libs/community/langchain_community/utilities/dalle_image_generator.py @@ -1,14 +1,15 @@ """Utility that calls OpenAI's Dall-E Image Generator.""" import logging -import os from typing import Any, Dict, Mapping, Optional, Tuple, Union -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator from langchain_core.utils import ( - get_from_dict_or_env, + from_env, get_pydantic_field_names, + secret_from_env, ) +from pydantic import BaseModel, ConfigDict, Field, Secret, model_validator +from typing_extensions import Self from langchain_community.utils.openai import is_openai_v1 @@ -30,15 +31,28 @@ class DallEAPIWrapper(BaseModel): async_client: Any = Field(default=None, exclude=True) #: :meta private: model_name: str = Field(default="dall-e-2", alias="model") model_kwargs: Dict[str, Any] = Field(default_factory=dict) - openai_api_key: Optional[str] = Field(default=None, alias="api_key") + openai_api_key: Secret[str] = Field( + alias="api_key", + default_factory=secret_from_env( + "OPENAI_API_KEY", + default=None, + ), + ) """Automatically inferred from env var `OPENAI_API_KEY` if not provided.""" - openai_api_base: Optional[str] = Field(default=None, alias="base_url") + openai_api_base: Optional[str] = Field( + alias="base_url", default_factory=from_env("OPENAI_API_BASE", default=None) + ) """Base URL path for API requests, leave blank if not using a proxy or service emulator.""" - openai_organization: Optional[str] = Field(default=None, alias="organization") + openai_organization: Optional[str] = Field( + alias="organization", + default_factory=from_env( + ["OPENAI_ORG_ID", "OPENAI_ORGANIZATION"], default=None + ), + ) """Automatically inferred from env var `OPENAI_ORG_ID` if not provided.""" # to support explicit proxy for OpenAI - openai_proxy: Optional[str] = None + openai_proxy: str = Field(default_factory=from_env("OPENAI_PROXY", default="")) request_timeout: Union[float, Tuple[float, float], Any, None] = Field( default=None, alias="timeout" ) @@ -59,11 +73,13 @@ class DallEAPIWrapper(BaseModel): http_client: Union[Any, None] = None """Optional httpx.Client.""" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + 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", {}) @@ -88,29 +104,9 @@ def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: values["model_kwargs"] = extra return values - @root_validator(pre=False, skip_on_failure=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="after") + def validate_environment(self) -> Self: """Validate that api key and python package exists in environment.""" - values["openai_api_key"] = get_from_dict_or_env( - values, "openai_api_key", "OPENAI_API_KEY" - ) - # Check OPENAI_ORGANIZATION for backwards compatibility. - values["openai_organization"] = ( - values["openai_organization"] - or os.getenv("OPENAI_ORG_ID") - or os.getenv("OPENAI_ORGANIZATION") - or None - ) - values["openai_api_base"] = values["openai_api_base"] or os.getenv( - "OPENAI_API_BASE" - ) - values["openai_proxy"] = get_from_dict_or_env( - values, - "openai_proxy", - "OPENAI_PROXY", - default="", - ) - try: import openai @@ -122,25 +118,25 @@ def validate_environment(cls, values: Dict) -> Dict: if is_openai_v1(): client_params = { - "api_key": values["openai_api_key"], - "organization": values["openai_organization"], - "base_url": values["openai_api_base"], - "timeout": values["request_timeout"], - "max_retries": values["max_retries"], - "default_headers": values["default_headers"], - "default_query": values["default_query"], - "http_client": values["http_client"], + "api_key": self.openai_api_key, + "organization": self.openai_organization, + "base_url": self.openai_api_base, + "timeout": self.request_timeout, + "max_retries": self.max_retries, + "default_headers": self.default_headers, + "default_query": self.default_query, + "http_client": self.http_client, } - if not values.get("client"): - values["client"] = openai.OpenAI(**client_params).images - if not values.get("async_client"): - values["async_client"] = openai.AsyncOpenAI(**client_params).images - elif not values.get("client"): - values["client"] = openai.Image + if not self.client: + self.client = openai.OpenAI(**client_params).images + if not self.async_client: + self.async_client = openai.AsyncOpenAI(**client_params).images + elif not self.client: + self.client = openai.Image else: pass - return values + return self def run(self, query: str) -> str: """Run query through OpenAI and parse result.""" diff --git a/libs/community/langchain_community/utilities/dataforseo_api_search.py b/libs/community/langchain_community/utilities/dataforseo_api_search.py index 4c705dabcbf97..7e0c7e085db90 100644 --- a/libs/community/langchain_community/utilities/dataforseo_api_search.py +++ b/libs/community/langchain_community/utilities/dataforseo_api_search.py @@ -1,19 +1,20 @@ import base64 -from typing import Dict, Optional +from typing import Any, Dict, Optional from urllib.parse import quote import aiohttp import requests -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, Field, model_validator class DataForSeoAPIWrapper(BaseModel): """Wrapper around the DataForSeo API.""" - class Config: - arbitrary_types_allowed = True - extra = "forbid" + model_config = ConfigDict( + arbitrary_types_allowed=True, + extra="forbid", + ) default_params: dict = Field( default={ @@ -40,8 +41,9 @@ class Config: aiosession: Optional[aiohttp.ClientSession] = None """The aiohttp session to use for the DataForSEO SERP API.""" - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that login and password exists in environment.""" login = get_from_dict_or_env(values, "api_login", "DATAFORSEO_LOGIN") password = get_from_dict_or_env(values, "api_password", "DATAFORSEO_PASSWORD") diff --git a/libs/community/langchain_community/utilities/dataherald.py b/libs/community/langchain_community/utilities/dataherald.py index fd0c28aeff648..bf69d8813af19 100644 --- a/libs/community/langchain_community/utilities/dataherald.py +++ b/libs/community/langchain_community/utilities/dataherald.py @@ -2,8 +2,8 @@ from typing import Any, Dict, Optional -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator class DataheraldAPIWrapper(BaseModel): @@ -22,11 +22,13 @@ class DataheraldAPIWrapper(BaseModel): db_connection_id: str dataherald_api_key: Optional[str] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" dataherald_api_key = get_from_dict_or_env( values, "dataherald_api_key", "DATAHERALD_API_KEY" diff --git a/libs/community/langchain_community/utilities/duckduckgo_search.py b/libs/community/langchain_community/utilities/duckduckgo_search.py index 1d69f47c19c85..d8017c28ae661 100644 --- a/libs/community/langchain_community/utilities/duckduckgo_search.py +++ b/libs/community/langchain_community/utilities/duckduckgo_search.py @@ -4,9 +4,9 @@ https://pypi.org/project/duckduckgo-search/ """ -from typing import Dict, List, Optional +from typing import Any, Dict, List, Optional -from langchain_core.pydantic_v1 import BaseModel, root_validator +from pydantic import BaseModel, ConfigDict, model_validator class DuckDuckGoSearchAPIWrapper(BaseModel): @@ -37,11 +37,13 @@ class DuckDuckGoSearchAPIWrapper(BaseModel): Options: text, news """ - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that python package exists in environment.""" try: from duckduckgo_search import DDGS # noqa: F401 diff --git a/libs/community/langchain_community/utilities/financial_datasets.py b/libs/community/langchain_community/utilities/financial_datasets.py index d8e769442e1e3..b3bfd2165113b 100644 --- a/libs/community/langchain_community/utilities/financial_datasets.py +++ b/libs/community/langchain_community/utilities/financial_datasets.py @@ -7,8 +7,8 @@ from typing import Any, List, Optional import requests -from langchain_core.pydantic_v1 import BaseModel from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel FINANCIAL_DATASETS_BASE_URL = "https://api.financialdatasets.ai/" diff --git a/libs/community/langchain_community/utilities/github.py b/libs/community/langchain_community/utilities/github.py index f650fd2b46898..6fee6c27cf9a6 100644 --- a/libs/community/langchain_community/utilities/github.py +++ b/libs/community/langchain_community/utilities/github.py @@ -6,8 +6,8 @@ from typing import TYPE_CHECKING, Any, Dict, List, Optional import requests -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator if TYPE_CHECKING: from github.Issue import Issue @@ -37,11 +37,13 @@ class GitHubAPIWrapper(BaseModel): active_branch: Optional[str] = None github_base_branch: Optional[str] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" github_repository = get_from_dict_or_env( values, "github_repository", "GITHUB_REPOSITORY" diff --git a/libs/community/langchain_community/utilities/gitlab.py b/libs/community/langchain_community/utilities/gitlab.py index 4907958d8f77b..12e2e1ecd22ad 100644 --- a/libs/community/langchain_community/utilities/gitlab.py +++ b/libs/community/langchain_community/utilities/gitlab.py @@ -5,8 +5,8 @@ import json from typing import TYPE_CHECKING, Any, Dict, List, Optional -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator if TYPE_CHECKING: from gitlab.v4.objects import Issue @@ -30,11 +30,13 @@ class GitLabAPIWrapper(BaseModel): Usually 'main' or 'master'. Defaults to 'main'. """ - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" gitlab_url = get_from_dict_or_env( diff --git a/libs/community/langchain_community/utilities/golden_query.py b/libs/community/langchain_community/utilities/golden_query.py index 6e0204eef24f4..74cad57f64418 100644 --- a/libs/community/langchain_community/utilities/golden_query.py +++ b/libs/community/langchain_community/utilities/golden_query.py @@ -1,11 +1,11 @@ """Util that calls Golden.""" import json -from typing import Dict, Optional +from typing import Any, Dict, Optional import requests -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator GOLDEN_BASE_URL = "https://golden.com" GOLDEN_TIMEOUT = 5000 @@ -24,11 +24,13 @@ class GoldenQueryAPIWrapper(BaseModel): golden_api_key: Optional[str] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" golden_api_key = get_from_dict_or_env( values, "golden_api_key", "GOLDEN_API_KEY" diff --git a/libs/community/langchain_community/utilities/google_finance.py b/libs/community/langchain_community/utilities/google_finance.py index da35a2383edea..650bf3611aa85 100644 --- a/libs/community/langchain_community/utilities/google_finance.py +++ b/libs/community/langchain_community/utilities/google_finance.py @@ -2,8 +2,8 @@ from typing import Any, Dict, Optional, cast -from langchain_core.pydantic_v1 import BaseModel, SecretStr, root_validator from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, SecretStr, model_validator class GoogleFinanceAPIWrapper(BaseModel): @@ -25,11 +25,13 @@ class GoogleFinanceAPIWrapper(BaseModel): serp_search_engine: Any serp_api_key: Optional[SecretStr] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" values["serp_api_key"] = convert_to_secret_str( get_from_dict_or_env(values, "serp_api_key", "SERPAPI_API_KEY") diff --git a/libs/community/langchain_community/utilities/google_jobs.py b/libs/community/langchain_community/utilities/google_jobs.py index bd7be4c7fd220..d036ff38e8bae 100644 --- a/libs/community/langchain_community/utilities/google_jobs.py +++ b/libs/community/langchain_community/utilities/google_jobs.py @@ -2,8 +2,8 @@ from typing import Any, Dict, Optional, cast -from langchain_core.pydantic_v1 import BaseModel, SecretStr, root_validator from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, SecretStr, model_validator class GoogleJobsAPIWrapper(BaseModel): @@ -25,11 +25,13 @@ class GoogleJobsAPIWrapper(BaseModel): serp_search_engine: Any serp_api_key: Optional[SecretStr] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" values["serp_api_key"] = convert_to_secret_str( get_from_dict_or_env(values, "serp_api_key", "SERPAPI_API_KEY") diff --git a/libs/community/langchain_community/utilities/google_lens.py b/libs/community/langchain_community/utilities/google_lens.py index 34bab1def7988..2aad581c995d2 100644 --- a/libs/community/langchain_community/utilities/google_lens.py +++ b/libs/community/langchain_community/utilities/google_lens.py @@ -3,8 +3,8 @@ from typing import Any, Dict, Optional, cast import requests -from langchain_core.pydantic_v1 import BaseModel, SecretStr, root_validator from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, SecretStr, model_validator class GoogleLensAPIWrapper(BaseModel): @@ -30,11 +30,13 @@ class GoogleLensAPIWrapper(BaseModel): serp_search_engine: Any serp_api_key: Optional[SecretStr] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" values["serp_api_key"] = convert_to_secret_str( get_from_dict_or_env(values, "serp_api_key", "SERPAPI_API_KEY") diff --git a/libs/community/langchain_community/utilities/google_places_api.py b/libs/community/langchain_community/utilities/google_places_api.py index 5143aeb9e6095..e5e15898ab1b0 100644 --- a/libs/community/langchain_community/utilities/google_places_api.py +++ b/libs/community/langchain_community/utilities/google_places_api.py @@ -4,8 +4,8 @@ from typing import Any, Dict, Optional from langchain_core._api.deprecation import deprecated -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator @deprecated( @@ -37,12 +37,14 @@ class GooglePlacesAPIWrapper(BaseModel): google_map_client: Any #: :meta private: top_k_results: Optional[int] = None - class Config: - arbitrary_types_allowed = True - extra = "forbid" + model_config = ConfigDict( + arbitrary_types_allowed=True, + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key is in your environment variable.""" gplaces_api_key = get_from_dict_or_env( values, "gplaces_api_key", "GPLACES_API_KEY" diff --git a/libs/community/langchain_community/utilities/google_scholar.py b/libs/community/langchain_community/utilities/google_scholar.py index e2c0445fd2944..ffc94848a61a5 100644 --- a/libs/community/langchain_community/utilities/google_scholar.py +++ b/libs/community/langchain_community/utilities/google_scholar.py @@ -1,9 +1,9 @@ """Util that calls Google Scholar Search.""" -from typing import Dict, Optional +from typing import Any, Dict, Optional -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator class GoogleScholarAPIWrapper(BaseModel): @@ -46,11 +46,13 @@ class GoogleScholarAPIWrapper(BaseModel): lr: str = "lang_en" serp_api_key: Optional[str] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" serp_api_key = get_from_dict_or_env(values, "serp_api_key", "SERP_API_KEY") values["SERP_API_KEY"] = serp_api_key diff --git a/libs/community/langchain_community/utilities/google_search.py b/libs/community/langchain_community/utilities/google_search.py index 997df2f9acf88..13e1a41c3b3cd 100644 --- a/libs/community/langchain_community/utilities/google_search.py +++ b/libs/community/langchain_community/utilities/google_search.py @@ -3,8 +3,8 @@ from typing import Any, Dict, List, Optional from langchain_core._api.deprecation import deprecated -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator @deprecated( @@ -57,8 +57,9 @@ class GoogleSearchAPIWrapper(BaseModel): k: int = 10 siterestrict: bool = False - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def _google_search_results(self, search_term: str, **kwargs: Any) -> List[dict]: cse = self.search_engine.cse() @@ -67,8 +68,9 @@ def _google_search_results(self, search_term: str, **kwargs: Any) -> List[dict]: res = cse.list(q=search_term, cx=self.google_cse_id, **kwargs).execute() return res.get("items", []) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" google_api_key = get_from_dict_or_env( values, "google_api_key", "GOOGLE_API_KEY" diff --git a/libs/community/langchain_community/utilities/google_serper.py b/libs/community/langchain_community/utilities/google_serper.py index 442ad4bf44584..49f75eaf275b8 100644 --- a/libs/community/langchain_community/utilities/google_serper.py +++ b/libs/community/langchain_community/utilities/google_serper.py @@ -4,8 +4,8 @@ import aiohttp import requests -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator from typing_extensions import Literal @@ -31,7 +31,7 @@ class GoogleSerperAPIWrapper(BaseModel): # "places" and "images" is available from Serper but not implemented in the # parser of run(). They can be used in results() type: Literal["news", "search", "places", "images"] = "search" - result_key_for_type = { + result_key_for_type: dict = { "news": "news", "places": "places", "images": "images", @@ -42,11 +42,13 @@ class GoogleSerperAPIWrapper(BaseModel): serper_api_key: Optional[str] = None aiosession: Optional[aiohttp.ClientSession] = None - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key exists in environment.""" serper_api_key = get_from_dict_or_env( values, "serper_api_key", "SERPER_API_KEY" diff --git a/libs/community/langchain_community/utilities/google_trends.py b/libs/community/langchain_community/utilities/google_trends.py index 7ad121a09b59b..38163a43bbddf 100644 --- a/libs/community/langchain_community/utilities/google_trends.py +++ b/libs/community/langchain_community/utilities/google_trends.py @@ -2,8 +2,8 @@ from typing import Any, Dict, Optional, cast -from langchain_core.pydantic_v1 import BaseModel, SecretStr, root_validator from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, SecretStr, model_validator class GoogleTrendsAPIWrapper(BaseModel): @@ -29,11 +29,13 @@ class GoogleTrendsAPIWrapper(BaseModel): serp_search_engine: Any serp_api_key: Optional[SecretStr] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" values["serp_api_key"] = convert_to_secret_str( get_from_dict_or_env(values, "serp_api_key", "SERPAPI_API_KEY") diff --git a/libs/community/langchain_community/utilities/graphql.py b/libs/community/langchain_community/utilities/graphql.py index 2b27357305df6..2e1dcc181b70e 100644 --- a/libs/community/langchain_community/utilities/graphql.py +++ b/libs/community/langchain_community/utilities/graphql.py @@ -1,7 +1,7 @@ import json from typing import Any, Callable, Dict, Optional -from langchain_core.pydantic_v1 import BaseModel, root_validator +from pydantic import BaseModel, ConfigDict, model_validator class GraphQLAPIWrapper(BaseModel): @@ -17,11 +17,13 @@ class GraphQLAPIWrapper(BaseModel): gql_client: Any #: :meta private: gql_function: Callable[[str], Any] #: :meta private: - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that the python package exists in the environment.""" try: from gql import Client, gql diff --git a/libs/community/langchain_community/utilities/infobip.py b/libs/community/langchain_community/utilities/infobip.py index f73e73b81f46d..a036627daa46e 100644 --- a/libs/community/langchain_community/utilities/infobip.py +++ b/libs/community/langchain_community/utilities/infobip.py @@ -1,10 +1,10 @@ """Util that sends messages via Infobip.""" -from typing import Dict, List, Optional +from typing import Any, Dict, List, Optional import requests -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator from requests.adapters import HTTPAdapter from urllib3.util import Retry @@ -15,11 +15,13 @@ class InfobipAPIWrapper(BaseModel): infobip_api_key: Optional[str] = None infobip_base_url: Optional[str] = "https://api.infobip.com" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key exists in environment.""" values["infobip_api_key"] = get_from_dict_or_env( values, "infobip_api_key", "INFOBIP_API_KEY" diff --git a/libs/community/langchain_community/utilities/jina_search.py b/libs/community/langchain_community/utilities/jina_search.py index eba067a11b2c8..b35b31f6830ad 100644 --- a/libs/community/langchain_community/utilities/jina_search.py +++ b/libs/community/langchain_community/utilities/jina_search.py @@ -3,7 +3,7 @@ import requests from langchain_core.documents import Document -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel from yarl import URL diff --git a/libs/community/langchain_community/utilities/jira.py b/libs/community/langchain_community/utilities/jira.py index 1657f13234e1f..811e7136086a2 100644 --- a/libs/community/langchain_community/utilities/jira.py +++ b/libs/community/langchain_community/utilities/jira.py @@ -2,8 +2,8 @@ from typing import Any, Dict, List, Optional -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator # TODO: think about error handling, more specific api specs, and jql/project limits @@ -17,11 +17,13 @@ class JiraAPIWrapper(BaseModel): jira_instance_url: Optional[str] = None jira_cloud: Optional[bool] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" jira_username = get_from_dict_or_env( values, "jira_username", "JIRA_USERNAME", default="" diff --git a/libs/community/langchain_community/utilities/merriam_webster.py b/libs/community/langchain_community/utilities/merriam_webster.py index bf02a7ce1961a..8cf9e18a107e8 100644 --- a/libs/community/langchain_community/utilities/merriam_webster.py +++ b/libs/community/langchain_community/utilities/merriam_webster.py @@ -1,12 +1,12 @@ """Util that calls Merriam-Webster.""" import json -from typing import Dict, Iterator, List, Optional +from typing import Any, Dict, Iterator, List, Optional from urllib.parse import quote import requests -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator MERRIAM_WEBSTER_API_URL = ( "https://www.dictionaryapi.com/api/v3/references/collegiate/json" @@ -28,11 +28,13 @@ class MerriamWebsterAPIWrapper(BaseModel): merriam_webster_api_key: Optional[str] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key exists in environment.""" merriam_webster_api_key = get_from_dict_or_env( values, "merriam_webster_api_key", "MERRIAM_WEBSTER_API_KEY" diff --git a/libs/community/langchain_community/utilities/metaphor_search.py b/libs/community/langchain_community/utilities/metaphor_search.py index 4269d6c31c751..cbfab0d35e8ee 100644 --- a/libs/community/langchain_community/utilities/metaphor_search.py +++ b/libs/community/langchain_community/utilities/metaphor_search.py @@ -4,12 +4,12 @@ """ import json -from typing import Dict, List, Optional +from typing import Any, Dict, List, Optional import aiohttp import requests -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator METAPHOR_API_URL = "https://api.metaphor.systems" @@ -20,8 +20,9 @@ class MetaphorSearchAPIWrapper(BaseModel): metaphor_api_key: str k: int = 10 - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def _metaphor_search_results( self, @@ -58,8 +59,9 @@ def _metaphor_search_results( search_results = response.json() return search_results["results"] - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and endpoint exists in environment.""" metaphor_api_key = get_from_dict_or_env( values, "metaphor_api_key", "METAPHOR_API_KEY" diff --git a/libs/community/langchain_community/utilities/mojeek_search.py b/libs/community/langchain_community/utilities/mojeek_search.py index 8f48059dcb9c3..eb5e688cbb667 100644 --- a/libs/community/langchain_community/utilities/mojeek_search.py +++ b/libs/community/langchain_community/utilities/mojeek_search.py @@ -2,7 +2,7 @@ from typing import List import requests -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field class MojeekSearchAPIWrapper(BaseModel): diff --git a/libs/community/langchain_community/utilities/nasa.py b/libs/community/langchain_community/utilities/nasa.py index a0e2904f875ed..2726cae8c1dbe 100644 --- a/libs/community/langchain_community/utilities/nasa.py +++ b/libs/community/langchain_community/utilities/nasa.py @@ -3,7 +3,7 @@ import json import requests -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel IMAGE_AND_VIDEO_LIBRARY_URL = "https://images-api.nasa.gov" diff --git a/libs/community/langchain_community/utilities/nvidia_riva.py b/libs/community/langchain_community/utilities/nvidia_riva.py index c3c887b14f079..c798935e2a7e8 100644 --- a/libs/community/langchain_community/utilities/nvidia_riva.py +++ b/libs/community/langchain_community/utilities/nvidia_riva.py @@ -25,7 +25,8 @@ from langchain_core.messages import AnyMessage, BaseMessage from langchain_core.prompt_values import PromptValue -from langchain_core.pydantic_v1 import ( +from langchain_core.runnables import RunnableConfig, RunnableSerializable +from pydantic import ( AnyHttpUrl, BaseModel, Field, @@ -33,7 +34,6 @@ root_validator, validator, ) -from langchain_core.runnables import RunnableConfig, RunnableSerializable if TYPE_CHECKING: import riva.client @@ -110,7 +110,7 @@ class RivaAuthMixin(BaseModel): """Configuration for the authentication to a Riva service connection.""" url: Union[AnyHttpUrl, str] = Field( - AnyHttpUrl("http://localhost:50051", scheme="http"), + AnyHttpUrl("http://localhost:50051"), description="The full URL where the Riva service can be found.", examples=["http://localhost:50051", "https://user@pass:riva.example.com"], ) diff --git a/libs/community/langchain_community/utilities/openapi.py b/libs/community/langchain_community/utilities/openapi.py index 6bd9182713a8b..1d99f7e182301 100644 --- a/libs/community/langchain_community/utilities/openapi.py +++ b/libs/community/langchain_community/utilities/openapi.py @@ -12,7 +12,7 @@ import requests import yaml -from langchain_core.pydantic_v1 import ValidationError +from pydantic import ValidationError logger = logging.getLogger(__name__) diff --git a/libs/community/langchain_community/utilities/openweathermap.py b/libs/community/langchain_community/utilities/openweathermap.py index 07f4517038af5..42eb2674929a9 100644 --- a/libs/community/langchain_community/utilities/openweathermap.py +++ b/libs/community/langchain_community/utilities/openweathermap.py @@ -2,8 +2,8 @@ from typing import Any, Dict, Optional -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator class OpenWeatherMapAPIWrapper(BaseModel): @@ -19,11 +19,13 @@ class OpenWeatherMapAPIWrapper(BaseModel): owm: Any openweathermap_api_key: Optional[str] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key exists in environment.""" openweathermap_api_key = get_from_dict_or_env( values, "openweathermap_api_key", "OPENWEATHERMAP_API_KEY" diff --git a/libs/community/langchain_community/utilities/outline.py b/libs/community/langchain_community/utilities/outline.py index 152c69a8fe0c1..a1106bd4f29ed 100644 --- a/libs/community/langchain_community/utilities/outline.py +++ b/libs/community/langchain_community/utilities/outline.py @@ -5,8 +5,8 @@ import requests from langchain_core.documents import Document -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, model_validator logger = logging.getLogger(__name__) @@ -28,8 +28,9 @@ class OutlineAPIWrapper(BaseModel): outline_api_key: Optional[str] = None outline_search_endpoint: str = "/api/documents.search" - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that instance url and api key exists in environment.""" outline_instance_url = get_from_dict_or_env( values, "outline_instance_url", "OUTLINE_INSTANCE_URL" diff --git a/libs/community/langchain_community/utilities/passio_nutrition_ai.py b/libs/community/langchain_community/utilities/passio_nutrition_ai.py index d4854c348ba39..41caef5eb0079 100644 --- a/libs/community/langchain_community/utilities/passio_nutrition_ai.py +++ b/libs/community/langchain_community/utilities/passio_nutrition_ai.py @@ -4,8 +4,8 @@ from typing import Any, Callable, Dict, Optional, final import requests -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, Field, model_validator class NoDiskStorage: @@ -120,9 +120,10 @@ class NutritionAIAPI(BaseModel): more_kwargs: dict = Field(default_factory=dict) auth_: ManagedPassioLifeAuth - class Config: - arbitrary_types_allowed = True - extra = "forbid" + model_config = ConfigDict( + arbitrary_types_allowed=True, + extra="forbid", + ) @retry( retry=retry_if_result(is_http_retryable), @@ -144,8 +145,9 @@ def _api_call_results(self, search_term: str) -> dict: rsp.raise_for_status() return rsp.json() - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and endpoint exists in environment.""" nutritionai_subscription_key = get_from_dict_or_env( values, "nutritionai_subscription_key", "NUTRITIONAI_SUBSCRIPTION_KEY" diff --git a/libs/community/langchain_community/utilities/pebblo.py b/libs/community/langchain_community/utilities/pebblo.py index eacf90ed8aaff..d68ec6e9ff75c 100644 --- a/libs/community/langchain_community/utilities/pebblo.py +++ b/libs/community/langchain_community/utilities/pebblo.py @@ -11,8 +11,8 @@ from langchain_core.documents import Document from langchain_core.env import get_runtime_environment -from langchain_core.pydantic_v1 import BaseModel from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel from requests import Response, request from requests.exceptions import RequestException diff --git a/libs/community/langchain_community/utilities/polygon.py b/libs/community/langchain_community/utilities/polygon.py index 36e21abefcaad..c7ab49f405467 100644 --- a/libs/community/langchain_community/utilities/polygon.py +++ b/libs/community/langchain_community/utilities/polygon.py @@ -7,8 +7,8 @@ from typing import Any, Dict, Optional import requests -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, model_validator POLYGON_BASE_URL = "https://api.polygon.io/" @@ -18,8 +18,9 @@ class PolygonAPIWrapper(BaseModel): polygon_api_key: Optional[str] = None - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key in environment.""" polygon_api_key = get_from_dict_or_env( values, "polygon_api_key", "POLYGON_API_KEY" diff --git a/libs/community/langchain_community/utilities/powerbi.py b/libs/community/langchain_community/utilities/powerbi.py index 0390ffcab0897..88aee2ae08789 100644 --- a/libs/community/langchain_community/utilities/powerbi.py +++ b/libs/community/langchain_community/utilities/powerbi.py @@ -10,7 +10,13 @@ import aiohttp import requests from aiohttp import ClientTimeout, ServerTimeoutError -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator, validator +from pydantic import ( + BaseModel, + ConfigDict, + Field, + model_validator, + validator, +) from requests.exceptions import Timeout logger = logging.getLogger(__name__) @@ -40,16 +46,18 @@ class PowerBIDataset(BaseModel): schemas: Dict[str, str] = Field(default_factory=dict) aiosession: Optional[aiohttp.ClientSession] = None - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) @validator("table_names", allow_reuse=True) def fix_table_names(cls, table_names: List[str]) -> List[str]: """Fix the table names.""" return [fix_table_name(table) for table in table_names] - @root_validator(pre=True) - def token_or_credential_present(cls, values: Dict[str, Any]) -> Dict[str, Any]: + @model_validator(mode="before") + @classmethod + def token_or_credential_present(cls, values: Dict[str, Any]) -> Any: """Validate that at least one of token and credentials is present.""" if "token" in values or "credential" in values: return values diff --git a/libs/community/langchain_community/utilities/pubmed.py b/libs/community/langchain_community/utilities/pubmed.py index ab541b02b31f3..e3b23cfa0adfb 100644 --- a/libs/community/langchain_community/utilities/pubmed.py +++ b/libs/community/langchain_community/utilities/pubmed.py @@ -7,7 +7,7 @@ from typing import Any, Dict, Iterator, List from langchain_core.documents import Document -from langchain_core.pydantic_v1 import BaseModel, root_validator +from pydantic import BaseModel, model_validator logger = logging.getLogger(__name__) @@ -48,8 +48,9 @@ class PubMedAPIWrapper(BaseModel): doc_content_chars_max: int = 2000 email: str = "your_email@example.com" - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that the python package exists in environment.""" try: import xmltodict diff --git a/libs/community/langchain_community/utilities/reddit_search.py b/libs/community/langchain_community/utilities/reddit_search.py index 6192c033b3672..ae4300c5109f8 100644 --- a/libs/community/langchain_community/utilities/reddit_search.py +++ b/libs/community/langchain_community/utilities/reddit_search.py @@ -2,8 +2,8 @@ from typing import Any, Dict, List, Optional -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, model_validator class RedditSearchAPIWrapper(BaseModel): @@ -30,8 +30,9 @@ class RedditSearchAPIWrapper(BaseModel): reddit_client_secret: Optional[str] reddit_user_agent: Optional[str] - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that the API ID, secret and user agent exists in environment and check that praw module is present. """ diff --git a/libs/community/langchain_community/utilities/rememberizer.py b/libs/community/langchain_community/utilities/rememberizer.py index 03d7a3a40a906..402b76ee0126e 100644 --- a/libs/community/langchain_community/utilities/rememberizer.py +++ b/libs/community/langchain_community/utilities/rememberizer.py @@ -1,11 +1,11 @@ """Wrapper for Rememberizer APIs.""" -from typing import Dict, List, Optional, cast +from typing import Any, Dict, List, Optional, cast import requests from langchain_core.documents import Document -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, model_validator class RememberizerAPIWrapper(BaseModel): @@ -14,8 +14,9 @@ class RememberizerAPIWrapper(BaseModel): top_k_results: int = 10 rememberizer_api_key: Optional[str] = None - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key in environment.""" rememberizer_api_key = get_from_dict_or_env( values, "rememberizer_api_key", "REMEMBERIZER_API_KEY" diff --git a/libs/community/langchain_community/utilities/requests.py b/libs/community/langchain_community/utilities/requests.py index 59d37221b1880..d23218e116281 100644 --- a/libs/community/langchain_community/utilities/requests.py +++ b/libs/community/langchain_community/utilities/requests.py @@ -5,7 +5,7 @@ import aiohttp import requests -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel, ConfigDict from requests import Response @@ -21,9 +21,10 @@ class Requests(BaseModel): auth: Optional[Any] = None verify: Optional[bool] = True - class Config: - arbitrary_types_allowed = True - extra = "forbid" + model_config = ConfigDict( + arbitrary_types_allowed=True, + extra="forbid", + ) def get(self, url: str, **kwargs: Any) -> requests.Response: """GET the URL and return the text.""" @@ -145,9 +146,10 @@ class GenericRequestsWrapper(BaseModel): response_content_type: Literal["text", "json"] = "text" verify: bool = True - class Config: - arbitrary_types_allowed = True - extra = "forbid" + model_config = ConfigDict( + arbitrary_types_allowed=True, + extra="forbid", + ) @property def requests(self) -> Requests: diff --git a/libs/community/langchain_community/utilities/scenexplain.py b/libs/community/langchain_community/utilities/scenexplain.py index 82c35a06091c2..30eff00fdfb14 100644 --- a/libs/community/langchain_community/utilities/scenexplain.py +++ b/libs/community/langchain_community/utilities/scenexplain.py @@ -6,14 +6,14 @@ - Navigate to the API Access page (https://scenex.jina.ai/api) and create a new API key. """ -from typing import Dict +from typing import Any, Dict import requests -from langchain_core.pydantic_v1 import BaseModel, BaseSettings, Field, root_validator -from langchain_core.utils import get_from_dict_or_env +from langchain_core.utils import from_env, get_from_dict_or_env +from pydantic import BaseModel, Field, model_validator -class SceneXplainAPIWrapper(BaseSettings, BaseModel): +class SceneXplainAPIWrapper(BaseModel): """Wrapper for SceneXplain API. In order to set this up, you need API key for the SceneXplain API. @@ -23,7 +23,7 @@ class SceneXplainAPIWrapper(BaseSettings, BaseModel): and create a new API key. """ - scenex_api_key: str = Field(..., env="SCENEX_API_KEY") + scenex_api_key: str = Field(..., default_factory=from_env("SCENEX_API_KEY")) scenex_api_url: str = "https://api.scenex.jina.ai/v1/describe" def _describe_image(self, image: str) -> str: @@ -47,8 +47,9 @@ def _describe_image(self, image: str) -> str: return img.get("text", "") - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key exists in environment.""" scenex_api_key = get_from_dict_or_env( values, "scenex_api_key", "SCENEX_API_KEY" diff --git a/libs/community/langchain_community/utilities/searchapi.py b/libs/community/langchain_community/utilities/searchapi.py index c4edcd8fb0be4..9e08df68d94da 100644 --- a/libs/community/langchain_community/utilities/searchapi.py +++ b/libs/community/langchain_community/utilities/searchapi.py @@ -2,8 +2,8 @@ import aiohttp import requests -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator class SearchApiAPIWrapper(BaseModel): @@ -27,11 +27,13 @@ class SearchApiAPIWrapper(BaseModel): searchapi_api_key: Optional[str] = None aiosession: Optional[aiohttp.ClientSession] = None - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that API key exists in environment.""" searchapi_api_key = get_from_dict_or_env( values, "searchapi_api_key", "SEARCHAPI_API_KEY" diff --git a/libs/community/langchain_community/utilities/searx_search.py b/libs/community/langchain_community/utilities/searx_search.py index 5debde2dfef8d..7fdd54b52f375 100644 --- a/libs/community/langchain_community/utilities/searx_search.py +++ b/libs/community/langchain_community/utilities/searx_search.py @@ -132,14 +132,14 @@ import aiohttp import requests -from langchain_core.pydantic_v1 import ( +from langchain_core.utils import get_from_dict_or_env +from pydantic import ( BaseModel, + ConfigDict, Field, PrivateAttr, - root_validator, - validator, + model_validator, ) -from langchain_core.utils import get_from_dict_or_env def _get_default_params() -> dict: @@ -214,22 +214,9 @@ class SearxSearchWrapper(BaseModel): k: int = 10 aiosession: Optional[Any] = None - @validator("unsecure") - def disable_ssl_warnings(cls, v: bool) -> bool: - """Disable SSL warnings.""" - if v: - # requests.urllib3.disable_warnings() - try: - import urllib3 - - urllib3.disable_warnings() - except ImportError as e: - print(e) # noqa: T201 - - return v - - @root_validator(pre=True) - def validate_params(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_params(cls, values: Dict) -> Any: """Validate that custom searx params are merged with default ones.""" user_params = values.get("params", {}) default = _get_default_params() @@ -252,13 +239,13 @@ def validate_params(cls, values: Dict) -> Dict: searx_host = "https://" + searx_host elif searx_host.startswith("http://"): values["unsecure"] = True - cls.disable_ssl_warnings(True) values["searx_host"] = searx_host return values - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def _searx_api_query(self, params: dict) -> SearxResults: """Actual request to searx API.""" diff --git a/libs/community/langchain_community/utilities/semanticscholar.py b/libs/community/langchain_community/utilities/semanticscholar.py index 14a9333c6b85e..896b0e599c360 100644 --- a/libs/community/langchain_community/utilities/semanticscholar.py +++ b/libs/community/langchain_community/utilities/semanticscholar.py @@ -1,9 +1,9 @@ """Utils for interacting with the Semantic Scholar API.""" import logging -from typing import Any, Dict, Optional +from typing import Any, Dict, List, Optional -from langchain_core.pydantic_v1 import BaseModel, root_validator +from pydantic import BaseModel, model_validator logger = logging.getLogger(__name__) @@ -39,7 +39,7 @@ class SemanticScholarAPIWrapper(BaseModel): S2_MAX_QUERY_LENGTH: int = 300 load_max_docs: int = 100 doc_content_chars_max: Optional[int] = 4000 - returned_fields = [ + returned_fields: List[str] = [ "title", "abstract", "venue", @@ -51,8 +51,9 @@ class SemanticScholarAPIWrapper(BaseModel): "externalIds", ] - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that the python package exists in environment.""" try: from semanticscholar import SemanticScholar diff --git a/libs/community/langchain_community/utilities/serpapi.py b/libs/community/langchain_community/utilities/serpapi.py index c3152a06d0cbc..a9281d587e347 100644 --- a/libs/community/langchain_community/utilities/serpapi.py +++ b/libs/community/langchain_community/utilities/serpapi.py @@ -8,8 +8,8 @@ from typing import Any, Dict, Optional, Tuple import aiohttp -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, Field, model_validator class HiddenPrints: @@ -52,12 +52,14 @@ class SerpAPIWrapper(BaseModel): serpapi_api_key: Optional[str] = None aiosession: Optional[aiohttp.ClientSession] = None - class Config: - arbitrary_types_allowed = True - extra = "forbid" + model_config = ConfigDict( + arbitrary_types_allowed=True, + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" serpapi_api_key = get_from_dict_or_env( values, "serpapi_api_key", "SERPAPI_API_KEY" diff --git a/libs/community/langchain_community/utilities/stackexchange.py b/libs/community/langchain_community/utilities/stackexchange.py index 777022cdc828a..7d09a480f39ee 100644 --- a/libs/community/langchain_community/utilities/stackexchange.py +++ b/libs/community/langchain_community/utilities/stackexchange.py @@ -1,7 +1,7 @@ import html from typing import Any, Dict, Literal -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator +from pydantic import BaseModel, Field, model_validator class StackExchangeAPIWrapper(BaseModel): @@ -19,8 +19,9 @@ class StackExchangeAPIWrapper(BaseModel): result_separator: str = "\n\n" """Separator between question,answer pairs.""" - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that the required Python package exists.""" try: from stackapi import StackAPI diff --git a/libs/community/langchain_community/utilities/steam.py b/libs/community/langchain_community/utilities/steam.py index 304412d2bd4cd..e3197b5295427 100644 --- a/libs/community/langchain_community/utilities/steam.py +++ b/libs/community/langchain_community/utilities/steam.py @@ -2,7 +2,12 @@ from typing import Any, List -from langchain_core.pydantic_v1 import BaseModel, root_validator +from pydantic import BaseModel, ConfigDict, model_validator + +from langchain_community.tools.steam.prompt import ( + STEAM_GET_GAMES_DETAILS, + STEAM_GET_RECOMMENDED_GAMES, +) class SteamWebAPIWrapper(BaseModel): @@ -10,11 +15,6 @@ class SteamWebAPIWrapper(BaseModel): steam: Any # for python-steam-api - from langchain_community.tools.steam.prompt import ( - STEAM_GET_GAMES_DETAILS, - STEAM_GET_RECOMMENDED_GAMES, - ) - # operations: a list of dictionaries, each representing a specific operation that # can be performed with the API operations: List[dict] = [ @@ -30,15 +30,17 @@ class SteamWebAPIWrapper(BaseModel): }, ] - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def get_operations(self) -> List[dict]: """Return a list of operations.""" return self.operations - @root_validator(pre=True) - def validate_environment(cls, values: dict) -> dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: dict) -> Any: """Validate api key and python package has been configured.""" # check if the python package is installed diff --git a/libs/community/langchain_community/utilities/tavily_search.py b/libs/community/langchain_community/utilities/tavily_search.py index 3c5666d0257cd..84e9815ee9ea6 100644 --- a/libs/community/langchain_community/utilities/tavily_search.py +++ b/libs/community/langchain_community/utilities/tavily_search.py @@ -5,12 +5,12 @@ """ import json -from typing import Dict, List, Optional +from typing import Any, Dict, List, Optional import aiohttp import requests -from langchain_core.pydantic_v1 import BaseModel, SecretStr, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, SecretStr, model_validator TAVILY_API_URL = "https://api.tavily.com" @@ -20,11 +20,13 @@ class TavilySearchAPIWrapper(BaseModel): tavily_api_key: SecretStr - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and endpoint exists in environment.""" tavily_api_key = get_from_dict_or_env( values, "tavily_api_key", "TAVILY_API_KEY" diff --git a/libs/community/langchain_community/utilities/tensorflow_datasets.py b/libs/community/langchain_community/utilities/tensorflow_datasets.py index 197c2f4c0f8d1..6fe5abefcd0f6 100644 --- a/libs/community/langchain_community/utilities/tensorflow_datasets.py +++ b/libs/community/langchain_community/utilities/tensorflow_datasets.py @@ -2,7 +2,7 @@ from typing import Any, Callable, Dict, Iterator, List, Optional from langchain_core.documents import Document -from langchain_core.pydantic_v1 import BaseModel, root_validator +from pydantic import BaseModel, model_validator logger = logging.getLogger(__name__) @@ -60,8 +60,9 @@ def mlqaen_example_to_document(example: dict) -> Document: sample_to_document_function: Optional[Callable[[Dict], Document]] = None dataset: Any #: :meta private: - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that the python package exists in environment.""" try: import tensorflow # noqa: F401 diff --git a/libs/community/langchain_community/utilities/twilio.py b/libs/community/langchain_community/utilities/twilio.py index 5e7d6e6d8e776..ceef3b2f26a64 100644 --- a/libs/community/langchain_community/utilities/twilio.py +++ b/libs/community/langchain_community/utilities/twilio.py @@ -2,8 +2,8 @@ from typing import Any, Dict, Optional -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator class TwilioAPIWrapper(BaseModel): @@ -43,12 +43,14 @@ class TwilioAPIWrapper(BaseModel): must be empty. """ - class Config: - arbitrary_types_allowed = False - extra = "forbid" + model_config = ConfigDict( + arbitrary_types_allowed=False, + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" try: from twilio.rest import Client diff --git a/libs/community/langchain_community/utilities/wikidata.py b/libs/community/langchain_community/utilities/wikidata.py index 8ea4122d915ec..3b2d877766200 100644 --- a/libs/community/langchain_community/utilities/wikidata.py +++ b/libs/community/langchain_community/utilities/wikidata.py @@ -4,7 +4,7 @@ from typing import Any, Dict, List, Optional from langchain_core.documents import Document -from langchain_core.pydantic_v1 import BaseModel, root_validator +from pydantic import BaseModel, model_validator logger = logging.getLogger(__name__) @@ -92,8 +92,9 @@ class WikidataAPIWrapper(BaseModel): wikidata_props: List[str] = DEFAULT_PROPERTIES lang: str = DEFAULT_LANG_CODE - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that the python package exists in environment.""" try: from mediawikiapi import MediaWikiAPI diff --git a/libs/community/langchain_community/utilities/wikipedia.py b/libs/community/langchain_community/utilities/wikipedia.py index ede156656b119..271a165ebdeb0 100644 --- a/libs/community/langchain_community/utilities/wikipedia.py +++ b/libs/community/langchain_community/utilities/wikipedia.py @@ -4,7 +4,7 @@ from typing import Any, Dict, Iterator, List, Optional from langchain_core.documents import Document -from langchain_core.pydantic_v1 import BaseModel, root_validator +from pydantic import BaseModel, model_validator logger = logging.getLogger(__name__) @@ -27,8 +27,9 @@ class WikipediaAPIWrapper(BaseModel): load_all_available_meta: bool = False doc_content_chars_max: int = 4000 - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that the python package exists in environment.""" try: import wikipedia diff --git a/libs/community/langchain_community/utilities/wolfram_alpha.py b/libs/community/langchain_community/utilities/wolfram_alpha.py index 35fe64308e228..aed9b9ddc675b 100644 --- a/libs/community/langchain_community/utilities/wolfram_alpha.py +++ b/libs/community/langchain_community/utilities/wolfram_alpha.py @@ -2,8 +2,8 @@ from typing import Any, Dict, Optional -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator class WolframAlphaAPIWrapper(BaseModel): @@ -21,11 +21,13 @@ class WolframAlphaAPIWrapper(BaseModel): wolfram_client: Any #: :meta private: wolfram_alpha_appid: Optional[str] = None - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key and python package exists in environment.""" wolfram_alpha_appid = get_from_dict_or_env( values, "wolfram_alpha_appid", "WOLFRAM_ALPHA_APPID" diff --git a/libs/community/langchain_community/utilities/you.py b/libs/community/langchain_community/utilities/you.py index 1cd17bdc55643..dadb2309c2f8a 100644 --- a/libs/community/langchain_community/utilities/you.py +++ b/libs/community/langchain_community/utilities/you.py @@ -10,8 +10,9 @@ import aiohttp import requests from langchain_core.documents import Document -from langchain_core.pydantic_v1 import BaseModel, Field, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, Field, model_validator +from typing_extensions import Self YOU_API_URL = "https://api.ydc-index.io" @@ -106,30 +107,31 @@ class YouSearchAPIWrapper(BaseModel): # should deprecate n_hits n_hits: Optional[int] = None - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key exists in environment.""" ydc_api_key = get_from_dict_or_env(values, "ydc_api_key", "YDC_API_KEY") values["ydc_api_key"] = ydc_api_key return values - @root_validator(pre=False, skip_on_failure=True) - def warn_if_set_fields_have_no_effect(cls, values: Dict) -> Dict: - if values["endpoint_type"] != "news": + @model_validator(mode="after") + def warn_if_set_fields_have_no_effect(self) -> Self: + if self.endpoint_type != "news": news_api_fields = ("search_lang", "ui_lang", "spellcheck") for field in news_api_fields: - if values[field]: + if getattr(self, field): warnings.warn( ( f"News API-specific field '{field}' is set but " - f"`endpoint_type=\"{values['endpoint_type']}\"`. " + f'`endpoint_type="{self.endpoint_type}"`. ' "This will have no effect." ), UserWarning, ) - if values["endpoint_type"] not in ("search", "snippet"): - if values["n_snippets_per_hit"]: + if self.endpoint_type not in ("search", "snippet"): + if self.n_snippets_per_hit: warnings.warn( ( "Field 'n_snippets_per_hit' only has effect on " @@ -137,19 +139,19 @@ def warn_if_set_fields_have_no_effect(cls, values: Dict) -> Dict: ), UserWarning, ) - return values + return self - @root_validator(pre=False, skip_on_failure=True) - def warn_if_deprecated_endpoints_are_used(cls, values: Dict) -> Dict: - if values["endpoint_type"] == "snippets": + @model_validator(mode="after") + def warn_if_deprecated_endpoints_are_used(self) -> Self: + if self.endpoint_type == "snippets": warnings.warn( ( - f"`endpoint_type=\"{values['endpoint_type']}\"` is deprecated. " + f'`endpoint_type="{self.endpoint_type}"` is deprecated. ' 'Use `endpoint_type="search"` instead.' ), DeprecationWarning, ) - return values + return self def _generate_params(self, query: str, **kwargs: Any) -> Dict: """ diff --git a/libs/community/langchain_community/utilities/zapier.py b/libs/community/langchain_community/utilities/zapier.py index 38f3ba7ff8475..56e5dc5dda4c2 100644 --- a/libs/community/langchain_community/utilities/zapier.py +++ b/libs/community/langchain_community/utilities/zapier.py @@ -17,8 +17,8 @@ import aiohttp import requests -from langchain_core.pydantic_v1 import BaseModel, root_validator from langchain_core.utils import get_from_dict_or_env +from pydantic import BaseModel, ConfigDict, model_validator from requests import Request, Session @@ -45,8 +45,9 @@ class ZapierNLAWrapper(BaseModel): zapier_nla_oauth_access_token: str zapier_nla_api_base: str = "https://nla.zapier.com/api/v1/" - class Config: - extra = "forbid" + model_config = ConfigDict( + extra="forbid", + ) def _format_headers(self) -> Dict[str, str]: """Format headers for requests.""" @@ -108,8 +109,9 @@ def _create_action_request( # type: ignore[no-untyped-def] json=data, ) - @root_validator(pre=True) - def validate_environment(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_environment(cls, values: Dict) -> Any: """Validate that api key exists in environment.""" zapier_nla_api_key_default = None diff --git a/libs/community/langchain_community/utils/ernie_functions.py b/libs/community/langchain_community/utils/ernie_functions.py index 4166de1bfd383..fcbc705e33d42 100644 --- a/libs/community/langchain_community/utils/ernie_functions.py +++ b/libs/community/langchain_community/utils/ernie_functions.py @@ -1,7 +1,7 @@ from typing import Literal, Optional, Type, TypedDict -from langchain_core.pydantic_v1 import BaseModel from langchain_core.utils.json_schema import dereference_refs +from pydantic import BaseModel class FunctionDescription(TypedDict): diff --git a/libs/community/langchain_community/vectorstores/apache_doris.py b/libs/community/langchain_community/vectorstores/apache_doris.py index c6c929074c02c..07f72d1508dab 100644 --- a/libs/community/langchain_community/vectorstores/apache_doris.py +++ b/libs/community/langchain_community/vectorstores/apache_doris.py @@ -8,8 +8,8 @@ from langchain_core.documents import Document from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseSettings from langchain_core.vectorstores import VectorStore +from pydantic_settings import BaseSettings, SettingsConfigDict logger = logging.getLogger() DEBUG = False @@ -61,10 +61,9 @@ class ApacheDorisSettings(BaseSettings): def __getitem__(self, item: str) -> Any: return getattr(self, item) - class Config: - env_file = ".env" - env_file_encoding = "utf-8" - env_prefix = "apache_doris_" + model_config = SettingsConfigDict( + env_file=".env", env_file_encoding="utf-8", env_prefix="apache_doris_" + ) class ApacheDoris(VectorStore): diff --git a/libs/community/langchain_community/vectorstores/azuresearch.py b/libs/community/langchain_community/vectorstores/azuresearch.py index 4dd4eb8177038..fa2a989631106 100644 --- a/libs/community/langchain_community/vectorstores/azuresearch.py +++ b/libs/community/langchain_community/vectorstores/azuresearch.py @@ -32,10 +32,10 @@ from langchain_core.documents import Document from langchain_core.embeddings import Embeddings from langchain_core.exceptions import LangChainException -from langchain_core.pydantic_v1 import root_validator from langchain_core.retrievers import BaseRetriever from langchain_core.utils import get_from_env from langchain_core.vectorstores import VectorStore +from pydantic import ConfigDict, model_validator from langchain_community.vectorstores.utils import maximal_marginal_relevance @@ -1580,11 +1580,13 @@ class AzureSearchVectorStoreRetriever(BaseRetriever): "semantic_hybrid_score_threshold", ) - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) - @root_validator(pre=True) - def validate_search_type(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def validate_search_type(cls, values: Dict) -> Any: """Validate search type.""" if "search_type" in values: search_type = values["search_type"] diff --git a/libs/community/langchain_community/vectorstores/clickhouse.py b/libs/community/langchain_community/vectorstores/clickhouse.py index edea983b976e9..83f5af6698b7e 100644 --- a/libs/community/langchain_community/vectorstores/clickhouse.py +++ b/libs/community/langchain_community/vectorstores/clickhouse.py @@ -8,8 +8,8 @@ from langchain_core.documents import Document from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseSettings from langchain_core.vectorstores import VectorStore +from pydantic_settings import BaseSettings, SettingsConfigDict logger = logging.getLogger() @@ -95,10 +95,9 @@ class ClickhouseSettings(BaseSettings): def __getitem__(self, item: str) -> Any: return getattr(self, item) - class Config: - env_file = ".env" - env_file_encoding = "utf-8" - env_prefix = "clickhouse_" + model_config = SettingsConfigDict( + env_file=".env", env_file_encoding="utf-8", env_prefix="clickhouse_" + ) class Clickhouse(VectorStore): diff --git a/libs/community/langchain_community/vectorstores/docarray/base.py b/libs/community/langchain_community/vectorstores/docarray/base.py index 9f66e79bfb775..9e209ad61ebee 100644 --- a/libs/community/langchain_community/vectorstores/docarray/base.py +++ b/libs/community/langchain_community/vectorstores/docarray/base.py @@ -4,8 +4,8 @@ import numpy as np from langchain_core.documents import Document from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import Field from langchain_core.vectorstores import VectorStore +from pydantic import Field from langchain_community.vectorstores.utils import maximal_marginal_relevance @@ -51,9 +51,9 @@ def _get_doc_cls(**embeddings_params: Any) -> Type["BaseDoc"]: from docarray.typing import NdArray class DocArrayDoc(BaseDoc): - text: Optional[str] = Field(default=None, required=False) + text: Optional[str] = Field(default=None) embedding: Optional[NdArray] = Field(**embeddings_params) - metadata: Optional[dict] = Field(default=None, required=False) + metadata: Optional[dict] = Field(default=None) return DocArrayDoc diff --git a/libs/community/langchain_community/vectorstores/kinetica.py b/libs/community/langchain_community/vectorstores/kinetica.py index 2ecca3b65e8e4..464f71de2c547 100644 --- a/libs/community/langchain_community/vectorstores/kinetica.py +++ b/libs/community/langchain_community/vectorstores/kinetica.py @@ -14,8 +14,8 @@ import numpy as np from langchain_core.documents import Document from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseSettings from langchain_core.vectorstores import VectorStore +from pydantic_settings import BaseSettings, SettingsConfigDict from langchain_community.vectorstores.utils import maximal_marginal_relevance @@ -79,10 +79,9 @@ class KineticaSettings(BaseSettings): def __getitem__(self, item: str) -> Any: return getattr(self, item) - class Config: - env_file = ".env" - env_file_encoding = "utf-8" - env_prefix = "kinetica_" + model_config = SettingsConfigDict( + env_file=".env", env_file_encoding="utf-8", env_prefix="kinetica_" + ) class Kinetica(VectorStore): diff --git a/libs/community/langchain_community/vectorstores/llm_rails.py b/libs/community/langchain_community/vectorstores/llm_rails.py index 3684f3b3b581c..16277161280a0 100644 --- a/libs/community/langchain_community/vectorstores/llm_rails.py +++ b/libs/community/langchain_community/vectorstores/llm_rails.py @@ -11,8 +11,8 @@ import requests from langchain_core.documents import Document from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import Field from langchain_core.vectorstores import VectorStore, VectorStoreRetriever +from pydantic import Field class LLMRails(VectorStore): diff --git a/libs/community/langchain_community/vectorstores/manticore_search.py b/libs/community/langchain_community/vectorstores/manticore_search.py index 0a452deb32197..4566189388fdc 100644 --- a/libs/community/langchain_community/vectorstores/manticore_search.py +++ b/libs/community/langchain_community/vectorstores/manticore_search.py @@ -8,8 +8,8 @@ from langchain_core.documents import Document from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseSettings from langchain_core.vectorstores import VectorStore +from pydantic_settings import BaseSettings, SettingsConfigDict logger = logging.getLogger() DEFAULT_K = 4 # Number of Documents to return. @@ -56,10 +56,9 @@ def get_connection_string(self) -> str: def __getitem__(self, item: str) -> Any: return getattr(self, item) - class Config: - env_file = ".env" - env_file_encoding = "utf-8" - env_prefix = "manticore_" + model_config = SettingsConfigDict( + env_file=".env", env_file_encoding="utf-8", env_prefix="manticore_" + ) class ManticoreSearch(VectorStore): diff --git a/libs/community/langchain_community/vectorstores/myscale.py b/libs/community/langchain_community/vectorstores/myscale.py index b91962ed07bc2..8140840c6bbc6 100644 --- a/libs/community/langchain_community/vectorstores/myscale.py +++ b/libs/community/langchain_community/vectorstores/myscale.py @@ -8,8 +8,8 @@ from langchain_core.documents import Document from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseSettings from langchain_core.vectorstores import VectorStore +from pydantic_settings import BaseSettings, SettingsConfigDict logger = logging.getLogger() @@ -85,10 +85,9 @@ class MyScaleSettings(BaseSettings): def __getitem__(self, item: str) -> Any: return getattr(self, item) - class Config: - env_file = ".env" - env_file_encoding = "utf-8" - env_prefix = "myscale_" + model_config = SettingsConfigDict( + env_file=".env", env_file_encoding="utf-8", env_prefix="myscale_" + ) class MyScale(VectorStore): diff --git a/libs/community/langchain_community/vectorstores/redis/base.py b/libs/community/langchain_community/vectorstores/redis/base.py index 93d0fd5a28443..5a50b8dd310b2 100644 --- a/libs/community/langchain_community/vectorstores/redis/base.py +++ b/libs/community/langchain_community/vectorstores/redis/base.py @@ -31,6 +31,7 @@ from langchain_core.embeddings import Embeddings from langchain_core.utils import get_from_dict_or_env from langchain_core.vectorstores import VectorStore, VectorStoreRetriever +from pydantic import ConfigDict from langchain_community.utilities.redis import ( _array_to_buffer, @@ -1452,8 +1453,9 @@ class RedisVectorStoreRetriever(VectorStoreRetriever): ] """Allowed search types.""" - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def _get_relevant_documents( self, query: str, *, run_manager: CallbackManagerForRetrieverRun diff --git a/libs/community/langchain_community/vectorstores/redis/schema.py b/libs/community/langchain_community/vectorstores/redis/schema.py index 50b20245fe252..2cf1ee2d927cd 100644 --- a/libs/community/langchain_community/vectorstores/redis/schema.py +++ b/libs/community/langchain_community/vectorstores/redis/schema.py @@ -7,8 +7,8 @@ import numpy as np import yaml -from langchain_core.pydantic_v1 import BaseModel, Field, validator from langchain_core.utils.pydantic import get_fields +from pydantic import BaseModel, Field, field_validator, validator from typing_extensions import TYPE_CHECKING, Literal from langchain_community.vectorstores.redis.constants import REDIS_VECTOR_DTYPE_MAP @@ -100,7 +100,8 @@ class RedisVectorField(RedisField): distance_metric: RedisDistanceMetric = Field(default="COSINE") initial_cap: Optional[int] = None - @validator("algorithm", "datatype", "distance_metric", pre=True, each_item=True) + @field_validator("algorithm", "datatype", "distance_metric", mode="before") + @classmethod def uppercase_strings(cls, v: str) -> str: return v.upper() diff --git a/libs/community/langchain_community/vectorstores/starrocks.py b/libs/community/langchain_community/vectorstores/starrocks.py index 2360b75f4e036..f17d7b650fdaf 100644 --- a/libs/community/langchain_community/vectorstores/starrocks.py +++ b/libs/community/langchain_community/vectorstores/starrocks.py @@ -8,8 +8,8 @@ from langchain_core.documents import Document from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseSettings from langchain_core.vectorstores import VectorStore +from pydantic_settings import BaseSettings, SettingsConfigDict logger = logging.getLogger() DEBUG = False @@ -112,10 +112,9 @@ class StarRocksSettings(BaseSettings): def __getitem__(self, item: str) -> Any: return getattr(self, item) - class Config: - env_file = ".env" - env_file_encoding = "utf-8" - env_prefix = "starrocks_" + model_config = SettingsConfigDict( + env_file=".env", env_file_encoding="utf-8", env_prefix="starrocks_" + ) class StarRocks(VectorStore): diff --git a/libs/community/langchain_community/vectorstores/tencentvectordb.py b/libs/community/langchain_community/vectorstores/tencentvectordb.py index ffa2beb650146..c0dc41965fb84 100644 --- a/libs/community/langchain_community/vectorstores/tencentvectordb.py +++ b/libs/community/langchain_community/vectorstores/tencentvectordb.py @@ -11,9 +11,9 @@ import numpy as np from langchain_core.documents import Document from langchain_core.embeddings import Embeddings -from langchain_core.pydantic_v1 import BaseModel from langchain_core.utils import guard_import from langchain_core.vectorstores import VectorStore +from pydantic import BaseModel from langchain_community.vectorstores.utils import maximal_marginal_relevance diff --git a/libs/community/langchain_community/vectorstores/thirdai_neuraldb.py b/libs/community/langchain_community/vectorstores/thirdai_neuraldb.py index d30aeb1383b3b..17d0d6318a38e 100644 --- a/libs/community/langchain_community/vectorstores/thirdai_neuraldb.py +++ b/libs/community/langchain_community/vectorstores/thirdai_neuraldb.py @@ -7,6 +7,7 @@ from langchain_core.documents import Document from langchain_core.embeddings import Embeddings from langchain_core.vectorstores import VectorStore +from pydantic import ConfigDict class NeuralDBVectorStore(VectorStore): @@ -30,9 +31,9 @@ def __init__(self, db: Any) -> None: db: Any = None #: :meta private: """NeuralDB instance""" - class Config: - extra = "forbid" - underscore_attrs_are_private = True + model_config = ConfigDict( + extra="forbid", + ) @staticmethod def _verify_thirdai_library(thirdai_key: Optional[str] = None): # type: ignore[no-untyped-def] @@ -330,9 +331,9 @@ def __init__(self, db: Any) -> None: db: Any = None #: :meta private: """NeuralDB Client instance""" - class Config: - extra = "forbid" - underscore_attrs_are_private = True + model_config = ConfigDict( + extra="forbid", + ) def similarity_search( self, query: str, k: int = 10, **kwargs: Any diff --git a/libs/community/langchain_community/vectorstores/vectara.py b/libs/community/langchain_community/vectorstores/vectara.py index a83a220885ee6..2d217333f8394 100644 --- a/libs/community/langchain_community/vectorstores/vectara.py +++ b/libs/community/langchain_community/vectorstores/vectara.py @@ -16,6 +16,7 @@ from langchain_core.embeddings import Embeddings from langchain_core.runnables import Runnable, RunnableConfig from langchain_core.vectorstores import VectorStore, VectorStoreRetriever +from pydantic import ConfigDict logger = logging.getLogger(__name__) @@ -731,8 +732,9 @@ class VectaraRetriever(VectorStoreRetriever): config: VectaraQueryConfig """Configuration for this retriever.""" - class Config: - arbitrary_types_allowed = True + model_config = ConfigDict( + arbitrary_types_allowed=True, + ) def _get_relevant_documents( self, query: str, *, run_manager: CallbackManagerForRetrieverRun diff --git a/libs/community/poetry.lock b/libs/community/poetry.lock index a90e3edec89c1..34324f2ee6fe0 100644 --- a/libs/community/poetry.lock +++ b/libs/community/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.6.1 and should not be changed by hand. [[package]] name = "aiohappyeyeballs" @@ -2481,8 +2481,8 @@ files = [ [package.dependencies] numpy = [ {version = ">=1.20.3", markers = "python_version < \"3.10\""}, - {version = ">=1.21.0", markers = "python_version >= \"3.10\" and python_version < \"3.11\""}, {version = ">=1.23.2", markers = "python_version >= \"3.11\""}, + {version = ">=1.21.0", markers = "python_version >= \"3.10\" and python_version < \"3.11\""}, ] python-dateutil = ">=2.8.2" pytz = ">=2020.1" @@ -2840,6 +2840,26 @@ files = [ [package.dependencies] typing-extensions = ">=4.6.0,<4.7.0 || >4.7.0" +[[package]] +name = "pydantic-settings" +version = "2.4.0" +description = "Settings management using Pydantic" +optional = false +python-versions = ">=3.8" +files = [ + {file = "pydantic_settings-2.4.0-py3-none-any.whl", hash = "sha256:bb6849dc067f1687574c12a639e231f3a6feeed0a12d710c1382045c5db1c315"}, + {file = "pydantic_settings-2.4.0.tar.gz", hash = "sha256:ed81c3a0f46392b4d7c0a565c05884e6e54b3456e6f0fe4d8814981172dc9a88"}, +] + +[package.dependencies] +pydantic = ">=2.7.0" +python-dotenv = ">=0.21.0" + +[package.extras] +azure-key-vault = ["azure-identity (>=1.16.0)", "azure-keyvault-secrets (>=4.8.0)"] +toml = ["tomli (>=2.0.1)"] +yaml = ["pyyaml (>=6.0.1)"] + [[package]] name = "pygments" version = "2.18.0" @@ -3567,6 +3587,54 @@ description = "Database Abstraction Library" optional = false python-versions = ">=3.7" files = [ + {file = "SQLAlchemy-2.0.33-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:63b7d9890f7958dabd95cf98a3f48740fbe2bb0493523aef590e82164fa68194"}, + {file = "SQLAlchemy-2.0.33-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:32a4f38d2efca066ec793451ef6852cb0d9086dc3d5479d88a5a25529d1d1861"}, + {file = "SQLAlchemy-2.0.33-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a3926e4ed4a3e956c8b2b0f1140493378c8cd17cad123b4fc1e0f6ecd3e05b19"}, + {file = "SQLAlchemy-2.0.33-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:2415824ec658891ac38d13a2f36b4ceb2033f034dee1c226f83917589a65f072"}, + {file = "SQLAlchemy-2.0.33-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:92249ac94279b8e5f0c0c8420e09b804d0a49d2269f52f549d4cb536c8382434"}, + {file = "SQLAlchemy-2.0.33-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:c9f4f92eee7d06531cc6a5b814e603a0c7639876aab03638dcc70c420a3974f6"}, + {file = "SQLAlchemy-2.0.33-cp310-cp310-win32.whl", hash = "sha256:4f1c44c8d66101e6f627f330d8b5b3de5ad25eedb6df3ce39a2e6f92debbcf15"}, + {file = "SQLAlchemy-2.0.33-cp310-cp310-win_amd64.whl", hash = "sha256:3ad94634338d8c576b1d47a96c798be186650aa5282072053ce2d12c6f309f82"}, + {file = "SQLAlchemy-2.0.33-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:570ec43e8c3c020abac4f0720baa5fe5187334e3f1e8e1777183c041962b61cc"}, + {file = "SQLAlchemy-2.0.33-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:81759e77a4985abdbac068762a0eaf0f11860fe041ad6da170aae7615ea72531"}, + {file = "SQLAlchemy-2.0.33-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:49541a43828e273325c520fbacf786615bd974dad63ff60b8ea1e1216e914d1a"}, + {file = "SQLAlchemy-2.0.33-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:82c72da5be489c8d150deba70d5732398695418df5232bceb52ee323ddd9753b"}, + {file = "SQLAlchemy-2.0.33-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:31e56020832be602201fbf8189f379569cf5c3604cdc4ce79f10dbbfcbf8a0eb"}, + {file = "SQLAlchemy-2.0.33-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:30a3f55be76364b64c83788728faaba782ab282a24909e1994404c2146d39982"}, + {file = "SQLAlchemy-2.0.33-cp311-cp311-win32.whl", hash = "sha256:17d0c69f66392ad2db1609373a74d1f834b2e632f3f52d446747b8ec220aea53"}, + {file = "SQLAlchemy-2.0.33-cp311-cp311-win_amd64.whl", hash = "sha256:c5d5a733c6af7f392435e673d1b136f6bdf2366033abb35eed680400dc730840"}, + {file = "SQLAlchemy-2.0.33-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:1d81e3aeab456fe24c3f0dcfd4f952a3a5ee45e9c14fc66d34c1d7a60cf7b698"}, + {file = "SQLAlchemy-2.0.33-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:ca8788dc1baee100f09110f33a01d928cf9df4483d2bfb25a37be31a659d46bb"}, + {file = "SQLAlchemy-2.0.33-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:60c54b677d4f0a0b2df3b79e89e84d601fb931c720176641742efd66b50601f9"}, + {file = "SQLAlchemy-2.0.33-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:684aee5fd811091b2f48006fb3fe6c7f2de4a716ef8d294a2aab762099753133"}, + {file = "SQLAlchemy-2.0.33-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:ee2b82b170591ccd19d463c9798a9caeea0cad967a8d2f3264de459f582696d5"}, + {file = "SQLAlchemy-2.0.33-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:1109cc6dc5c9d1223c42186391e6a5509e6d4ab2c30fa629573c10184f742f2e"}, + {file = "SQLAlchemy-2.0.33-cp312-cp312-win32.whl", hash = "sha256:c633e2d2f8a7b88c06e276bbe16cb7e62fed815fcbeb69cd9752cea166ecb8e8"}, + {file = "SQLAlchemy-2.0.33-cp312-cp312-win_amd64.whl", hash = "sha256:77eaf8fdf305266b806a91ae4633edbf86ad37e13bd92ac85e305e7f654c19a5"}, + {file = "SQLAlchemy-2.0.33-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:67eb8e0ffbebd3d82ec5079ca5f807a661c574b482785483717857c2acab833a"}, + {file = "SQLAlchemy-2.0.33-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a3da2371628e28ef279f3f756f5e58858fad7820de08508138c9f5f9e4d8f4ac"}, + {file = "SQLAlchemy-2.0.33-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:f7c82a7930126bb5ccfbb73fc1562d52942fbffb2fda2791fab49de249fc202a"}, + {file = "SQLAlchemy-2.0.33-cp37-cp37m-musllinux_1_2_aarch64.whl", hash = "sha256:d004a623ad4aa8d2eb31b37e65b5e020c9f65a1852b8b9e6301f0e411aca5b9a"}, + {file = "SQLAlchemy-2.0.33-cp37-cp37m-musllinux_1_2_x86_64.whl", hash = "sha256:06b30bbc43c6dd8b7cdc509cd2e58f4f1dce867565642e1d1a65e32459c89bd0"}, + {file = "SQLAlchemy-2.0.33-cp37-cp37m-win32.whl", hash = "sha256:459099ab8dd43a5edbb99f58ba4730baec457df9c06ebc71434c6b4b78cc8cf9"}, + {file = "SQLAlchemy-2.0.33-cp37-cp37m-win_amd64.whl", hash = "sha256:3c64d58e83a68e228b1ae6ebac8721241e9d8cc5e0c0dd11ed5d89155477b243"}, + {file = "SQLAlchemy-2.0.33-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:9d035a672d5b3e4793a4a8865c3274a7bbbac7fac67a47b415023b5539105087"}, + {file = "SQLAlchemy-2.0.33-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:61e9a2d68a5a8ca6a84cbc79aa7f2e430ae854d3351d6e9ceb3edf6798797b63"}, + {file = "SQLAlchemy-2.0.33-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:93efa4b72f7cb70555b0f66ee5e113ae40073c57054a72887e50b05bfd97baa4"}, + {file = "SQLAlchemy-2.0.33-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ac252bafe8cbadfac7b1e8a74748ffd775e27325186d12b82600b652d9adcb86"}, + {file = "SQLAlchemy-2.0.33-cp38-cp38-musllinux_1_2_aarch64.whl", hash = "sha256:2b1e98507ec2aa200af980d592e936e9dac1c1ec50acc94330ae4b13c55d6fea"}, + {file = "SQLAlchemy-2.0.33-cp38-cp38-musllinux_1_2_x86_64.whl", hash = "sha256:523ae689c023cbf0fe1613101254824515193f85f806ba04611dee83302660b5"}, + {file = "SQLAlchemy-2.0.33-cp38-cp38-win32.whl", hash = "sha256:7fd0a28bc24a75326f13735a58272247f65c9e8ee16205eacb2431d6ee94f44a"}, + {file = "SQLAlchemy-2.0.33-cp38-cp38-win_amd64.whl", hash = "sha256:0ea64443a86c3b5a0fd7c93363ad2f9465cb3af61f9920b7c75d1a7bebbeef8a"}, + {file = "SQLAlchemy-2.0.33-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:9e5819822050e6e36e2aa41260d05074c026a1bbb9baa6869170b5ce64db7a4d"}, + {file = "SQLAlchemy-2.0.33-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:8bef11d31a1c48f5943e577d1ef81085ec1550c37552bfc9bf8e5d184ce47142"}, + {file = "SQLAlchemy-2.0.33-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:06504d9625e3ef114b39803ebca6f379133acad58a87c33117ddc5df66079915"}, + {file = "SQLAlchemy-2.0.33-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:454e9b4355f0051063daebc4060140251c19f33fc5d02151c347431860fd104b"}, + {file = "SQLAlchemy-2.0.33-cp39-cp39-musllinux_1_2_aarch64.whl", hash = "sha256:28c0800c851955f5bd11c0b904638c1343002650d0c071c6fbf0d157cc78627d"}, + {file = "SQLAlchemy-2.0.33-cp39-cp39-musllinux_1_2_x86_64.whl", hash = "sha256:816c927dd51e4951d6e79870c945340057a5d8e63543419dee0d247bd67a88f8"}, + {file = "SQLAlchemy-2.0.33-cp39-cp39-win32.whl", hash = "sha256:c40e0213beaf410a151e4329e30c73687838c251c998ba1b312975dbbcb2d05d"}, + {file = "SQLAlchemy-2.0.33-cp39-cp39-win_amd64.whl", hash = "sha256:751eaafa907a66dd9a328a9d15c3dcfdcba3ef8dd8f7f4a9771cdacdec45d9bf"}, + {file = "SQLAlchemy-2.0.33-py3-none-any.whl", hash = "sha256:ae294808afde1b14a1a69aa86a69cadfe391848bbb233a5332a8065e4081cabc"}, {file = "sqlalchemy-2.0.33.tar.gz", hash = "sha256:91c93333c2b37ff721dc83b37e28c29de4c502b5612f2d093468037b86aa2be0"}, ] @@ -4323,4 +4391,4 @@ type = ["pytest-mypy"] [metadata] lock-version = "2.0" python-versions = ">=3.8.1,<4.0" -content-hash = "b69b2186cc152c64945d8000e883e92d7872a38b7113f60b1db5c7e1c9aed7c6" +content-hash = "3d399069b8e59e6912a11435123974e0ab8a7c2fbf66af3e09fd982027ed2dbb" diff --git a/libs/community/pyproject.toml b/libs/community/pyproject.toml index 3a4758e5cf058..c943b0b7fcad2 100644 --- a/libs/community/pyproject.toml +++ b/libs/community/pyproject.toml @@ -39,6 +39,7 @@ aiohttp = "^3.8.3" tenacity = "^8.1.0,!=8.4.0" dataclasses-json = ">= 0.5.7, < 0.7" langsmith = "^0.1.0" +pydantic-settings = "^2.4.0" [[tool.poetry.dependencies.numpy]] version = "^1" python = "<3.12" diff --git a/libs/community/scripts/check_pydantic.sh b/libs/community/scripts/check_pydantic.sh deleted file mode 100755 index 1b091d0f4eb8a..0000000000000 --- a/libs/community/scripts/check_pydantic.sh +++ /dev/null @@ -1,68 +0,0 @@ -#!/bin/bash -# -# This script searches for lines starting with "import pydantic" or "from pydantic" -# in tracked files within a Git repository. -# -# Usage: ./scripts/check_pydantic.sh /path/to/repository - -# Check if a path argument is provided -if [ $# -ne 1 ]; then - echo "Usage: $0 /path/to/repository" - exit 1 -fi - -repository_path="$1" - -# Check that we are not using features that cannot be captured via init. -# pre-init is a custom decorator that we introduced to capture the same semantics -# as @root_validator(pre=False, skip_on_failure=False) available in pydantic 1. -count=$(git grep -E '(@root_validator)|(@validator)|(@pre_init)' -- "*.py" | wc -l) -# PRs that increase the current count will not be accepted. -# PRs that decrease update the code in the repository -# and allow decreasing the count of are welcome! -current_count=336 - -if [ "$count" -gt "$current_count" ]; then - echo "The PR seems to be introducing new usage of @root_validator and/or @field_validator." - echo "git grep -E '(@root_validator)|(@validator)' | wc -l returned $count" - echo "whereas the expected count should be equal or less than $current_count" - echo "Please update the code to instead use __init__" - echo "For examples, please see: " - echo "https://gist.github.com/eyurtsev/d1dcba10c2f35626e302f1b98a0f5a3c " - echo "This linter is here to make sure that its easier to upgrade pydantic in the future." - exit 1 -elif [ "$count" -lt "$current_count" ]; then - echo "Please update the $current_count variable in ./scripts/check_pydantic.sh to $count" - exit 1 -fi - - -# Search for lines matching the pattern within the specified repository -result=$(git -C "$repository_path" grep -En '^import pydantic|^from pydantic') - -# Check if any matching lines were found -if [ -n "$result" ]; then - echo "ERROR: The following lines need to be updated:" - echo "$result" - echo "Please replace the code with an import from langchain_core.pydantic_v1." - echo "For example, replace 'from pydantic import BaseModel'" - echo "with 'from langchain_core.pydantic_v1 import BaseModel'" - exit 1 -fi - -# Forbid vanilla usage of @root_validator -# This prevents the code from using either @root_validator or @root_validator() -# Search for lines matching the pattern within the specified repository -result=$(git -C "$repository_path" grep -En '(@root_validator\s*$)|(@root_validator\(\)|@root_validator\(pre=False\))' -- '*.py') - -# Check if any matching lines were found -if [ -n "$result" ]; then - echo "ERROR: The following lines need to be updated:" - echo - echo "$result" - echo - echo "Please replace @root_validator or @root_validator() with either:" - echo - echo "@root_validator(pre=True) or @root_validator(pre=False, skip_on_failure=True)" - exit 1 -fi diff --git a/libs/community/tests/integration_tests/chat_models/test_deepinfra.py b/libs/community/tests/integration_tests/chat_models/test_deepinfra.py index 0834d88476d4b..37fdc68ccb4c7 100644 --- a/libs/community/tests/integration_tests/chat_models/test_deepinfra.py +++ b/libs/community/tests/integration_tests/chat_models/test_deepinfra.py @@ -6,8 +6,8 @@ from langchain_core.messages.ai import AIMessage from langchain_core.messages.tool import ToolMessage from langchain_core.outputs import ChatGeneration, LLMResult -from langchain_core.pydantic_v1 import BaseModel from langchain_core.runnables.base import RunnableBinding +from pydantic import BaseModel from langchain_community.chat_models.deepinfra import ChatDeepInfra from tests.unit_tests.callbacks.fake_callback_handler import FakeCallbackHandler diff --git a/libs/community/tests/integration_tests/chat_models/test_gpt_router.py b/libs/community/tests/integration_tests/chat_models/test_gpt_router.py index 0b5230e98c993..a65515363f3b3 100644 --- a/libs/community/tests/integration_tests/chat_models/test_gpt_router.py +++ b/libs/community/tests/integration_tests/chat_models/test_gpt_router.py @@ -8,7 +8,7 @@ ) from langchain_core.messages import AIMessage, BaseMessage, HumanMessage from langchain_core.outputs import ChatGeneration, LLMResult -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture from langchain_community.chat_models.gpt_router import GPTRouter, GPTRouterModel diff --git a/libs/community/tests/integration_tests/chat_models/test_jinachat.py b/libs/community/tests/integration_tests/chat_models/test_jinachat.py index a43b955d7eabf..50b641311cf61 100644 --- a/libs/community/tests/integration_tests/chat_models/test_jinachat.py +++ b/libs/community/tests/integration_tests/chat_models/test_jinachat.py @@ -6,7 +6,7 @@ from langchain_core.callbacks import CallbackManager from langchain_core.messages import BaseMessage, HumanMessage, SystemMessage from langchain_core.outputs import ChatGeneration, LLMResult -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture, MonkeyPatch from langchain_community.chat_models.jinachat import JinaChat diff --git a/libs/community/tests/integration_tests/chat_models/test_konko.py b/libs/community/tests/integration_tests/chat_models/test_konko.py index a4b9977de39f4..1980aa9c6d030 100644 --- a/libs/community/tests/integration_tests/chat_models/test_konko.py +++ b/libs/community/tests/integration_tests/chat_models/test_konko.py @@ -6,7 +6,7 @@ from langchain_core.callbacks import CallbackManager from langchain_core.messages import BaseMessage, HumanMessage, SystemMessage from langchain_core.outputs import ChatGeneration, ChatResult, LLMResult -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture, MonkeyPatch from langchain_community.chat_models.konko import ChatKonko diff --git a/libs/community/tests/integration_tests/chat_models/test_minimax.py b/libs/community/tests/integration_tests/chat_models/test_minimax.py index 8826f7d64e6f3..88199f07535e9 100644 --- a/libs/community/tests/integration_tests/chat_models/test_minimax.py +++ b/libs/community/tests/integration_tests/chat_models/test_minimax.py @@ -1,8 +1,8 @@ import os from langchain_core.messages import AIMessage, HumanMessage, ToolMessage -from langchain_core.pydantic_v1 import BaseModel from langchain_core.tools import tool +from pydantic import BaseModel from langchain_community.chat_models import MiniMaxChat diff --git a/libs/community/tests/integration_tests/chat_models/test_moonshot.py b/libs/community/tests/integration_tests/chat_models/test_moonshot.py index bb29175a2d0a5..68d9f43b5d8f0 100644 --- a/libs/community/tests/integration_tests/chat_models/test_moonshot.py +++ b/libs/community/tests/integration_tests/chat_models/test_moonshot.py @@ -4,8 +4,8 @@ import pytest from langchain_core.language_models import BaseChatModel -from langchain_core.pydantic_v1 import SecretStr from langchain_standard_tests.integration_tests import ChatModelIntegrationTests +from pydantic import SecretStr from langchain_community.chat_models.moonshot import MoonshotChat diff --git a/libs/community/tests/integration_tests/chat_models/test_openai.py b/libs/community/tests/integration_tests/chat_models/test_openai.py index 4c1982f3923be..551cfdd7e84ca 100644 --- a/libs/community/tests/integration_tests/chat_models/test_openai.py +++ b/libs/community/tests/integration_tests/chat_models/test_openai.py @@ -11,7 +11,7 @@ LLMResult, ) from langchain_core.prompts import ChatPromptTemplate -from langchain_core.pydantic_v1 import BaseModel, Field +from pydantic import BaseModel, Field from langchain_community.chat_models.openai import ChatOpenAI from tests.unit_tests.callbacks.fake_callback_handler import FakeCallbackHandler diff --git a/libs/community/tests/integration_tests/chat_models/test_qianfan_endpoint.py b/libs/community/tests/integration_tests/chat_models/test_qianfan_endpoint.py index e34bee531b6bc..d286aca33b8b2 100644 --- a/libs/community/tests/integration_tests/chat_models/test_qianfan_endpoint.py +++ b/libs/community/tests/integration_tests/chat_models/test_qianfan_endpoint.py @@ -13,7 +13,7 @@ ) from langchain_core.outputs import ChatGeneration, LLMResult from langchain_core.prompts import ChatPromptTemplate, HumanMessagePromptTemplate -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture, MonkeyPatch from langchain_community.chat_models.baidu_qianfan_endpoint import ( diff --git a/libs/community/tests/integration_tests/chat_models/test_tongyi.py b/libs/community/tests/integration_tests/chat_models/test_tongyi.py index a395e800c9b16..e6884e6535346 100644 --- a/libs/community/tests/integration_tests/chat_models/test_tongyi.py +++ b/libs/community/tests/integration_tests/chat_models/test_tongyi.py @@ -8,7 +8,7 @@ from langchain_core.messages.tool import ToolCall, ToolMessage from langchain_core.outputs import ChatGeneration, LLMResult from langchain_core.prompts import ChatPromptTemplate, HumanMessagePromptTemplate -from langchain_core.pydantic_v1 import BaseModel, SecretStr +from pydantic import BaseModel, SecretStr from pytest import CaptureFixture from langchain_community.chat_models.tongyi import ChatTongyi diff --git a/libs/community/tests/integration_tests/document_loaders/test_tensorflow_datasets.py b/libs/community/tests/integration_tests/document_loaders/test_tensorflow_datasets.py index 5dae638e04646..9f6cb9190a411 100644 --- a/libs/community/tests/integration_tests/document_loaders/test_tensorflow_datasets.py +++ b/libs/community/tests/integration_tests/document_loaders/test_tensorflow_datasets.py @@ -6,7 +6,7 @@ import pytest from langchain_core.documents import Document -from langchain_core.pydantic_v1 import ValidationError +from pydantic import ValidationError from langchain_community.document_loaders.tensorflow_datasets import ( TensorflowDatasetLoader, diff --git a/libs/community/tests/integration_tests/embeddings/test_minimax.py b/libs/community/tests/integration_tests/embeddings/test_minimax.py index bd48277932b16..3bb949d953554 100644 --- a/libs/community/tests/integration_tests/embeddings/test_minimax.py +++ b/libs/community/tests/integration_tests/embeddings/test_minimax.py @@ -1,6 +1,6 @@ from typing import cast -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from langchain_community.embeddings import MiniMaxEmbeddings diff --git a/libs/community/tests/integration_tests/embeddings/test_qianfan_endpoint.py b/libs/community/tests/integration_tests/embeddings/test_qianfan_endpoint.py index a0edf46a6f946..6be6ecc5f8fba 100644 --- a/libs/community/tests/integration_tests/embeddings/test_qianfan_endpoint.py +++ b/libs/community/tests/integration_tests/embeddings/test_qianfan_endpoint.py @@ -2,7 +2,7 @@ from typing import cast -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from langchain_community.embeddings.baidu_qianfan_endpoint import ( QianfanEmbeddingsEndpoint, diff --git a/libs/community/tests/integration_tests/llms/test_arcee.py b/libs/community/tests/integration_tests/llms/test_arcee.py index e195a7818dbc7..d36a98b294126 100644 --- a/libs/community/tests/integration_tests/llms/test_arcee.py +++ b/libs/community/tests/integration_tests/llms/test_arcee.py @@ -1,6 +1,6 @@ from unittest.mock import MagicMock, patch -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture, MonkeyPatch from langchain_community.llms.arcee import Arcee diff --git a/libs/community/tests/integration_tests/llms/test_azureml_endpoint.py b/libs/community/tests/integration_tests/llms/test_azureml_endpoint.py index cda463d719c87..a02c5244cc01a 100644 --- a/libs/community/tests/integration_tests/llms/test_azureml_endpoint.py +++ b/libs/community/tests/integration_tests/llms/test_azureml_endpoint.py @@ -7,7 +7,7 @@ from urllib.request import HTTPError import pytest -from langchain_core.pydantic_v1 import ValidationError +from pydantic import ValidationError from langchain_community.llms.azureml_endpoint import ( AzureMLOnlineEndpoint, diff --git a/libs/community/tests/integration_tests/llms/test_cohere.py b/libs/community/tests/integration_tests/llms/test_cohere.py index 5fad016491804..02404d20665a3 100644 --- a/libs/community/tests/integration_tests/llms/test_cohere.py +++ b/libs/community/tests/integration_tests/llms/test_cohere.py @@ -2,7 +2,7 @@ from pathlib import Path -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import MonkeyPatch from langchain_community.llms.cohere import Cohere diff --git a/libs/community/tests/integration_tests/llms/test_edenai.py b/libs/community/tests/integration_tests/llms/test_edenai.py index 3d8e0b9e230da..233ee2bf8dc2b 100644 --- a/libs/community/tests/integration_tests/llms/test_edenai.py +++ b/libs/community/tests/integration_tests/llms/test_edenai.py @@ -9,7 +9,7 @@ You'll then need to set EDENAI_API_KEY environment variable to your api key. """ -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from langchain_community.llms import EdenAI diff --git a/libs/community/tests/integration_tests/llms/test_nlpcloud.py b/libs/community/tests/integration_tests/llms/test_nlpcloud.py index 85a67e3be6a6e..3645359e8563e 100644 --- a/libs/community/tests/integration_tests/llms/test_nlpcloud.py +++ b/libs/community/tests/integration_tests/llms/test_nlpcloud.py @@ -3,7 +3,7 @@ from pathlib import Path from typing import cast -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture, MonkeyPatch from langchain_community.llms.loading import load_llm diff --git a/libs/community/tests/integration_tests/llms/test_petals.py b/libs/community/tests/integration_tests/llms/test_petals.py index 82a7857ec0e11..1689efb26b3f9 100644 --- a/libs/community/tests/integration_tests/llms/test_petals.py +++ b/libs/community/tests/integration_tests/llms/test_petals.py @@ -1,6 +1,6 @@ """Test Petals API wrapper.""" -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture from langchain_community.llms.petals import Petals diff --git a/libs/community/tests/integration_tests/llms/test_qianfan_endpoint.py b/libs/community/tests/integration_tests/llms/test_qianfan_endpoint.py index afc654dfc6447..35c63e8d878db 100644 --- a/libs/community/tests/integration_tests/llms/test_qianfan_endpoint.py +++ b/libs/community/tests/integration_tests/llms/test_qianfan_endpoint.py @@ -3,7 +3,7 @@ from typing import Generator, cast from langchain_core.outputs import LLMResult -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from langchain_community.llms.baidu_qianfan_endpoint import QianfanLLMEndpoint diff --git a/libs/community/tests/integration_tests/llms/test_volcengine_maas.py b/libs/community/tests/integration_tests/llms/test_volcengine_maas.py index 7cf3e29081957..321e830eeb432 100644 --- a/libs/community/tests/integration_tests/llms/test_volcengine_maas.py +++ b/libs/community/tests/integration_tests/llms/test_volcengine_maas.py @@ -3,7 +3,7 @@ from typing import Generator from langchain_core.outputs import LLMResult -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture from langchain_community.llms.volcengine_maas import ( diff --git a/libs/community/tests/integration_tests/retrievers/docarray/fixtures.py b/libs/community/tests/integration_tests/retrievers/docarray/fixtures.py index d9e717e042934..ea3a5d4815a3c 100644 --- a/libs/community/tests/integration_tests/retrievers/docarray/fixtures.py +++ b/libs/community/tests/integration_tests/retrievers/docarray/fixtures.py @@ -5,7 +5,7 @@ import numpy as np import pytest -from langchain_core.pydantic_v1 import Field +from pydantic import Field if TYPE_CHECKING: from docarray.index import ( diff --git a/libs/community/tests/integration_tests/utilities/test_tensorflow_datasets.py b/libs/community/tests/integration_tests/utilities/test_tensorflow_datasets.py index 1b25fd522b3b9..973e7d0b9b696 100644 --- a/libs/community/tests/integration_tests/utilities/test_tensorflow_datasets.py +++ b/libs/community/tests/integration_tests/utilities/test_tensorflow_datasets.py @@ -6,7 +6,7 @@ import pytest from langchain_core.documents import Document -from langchain_core.pydantic_v1 import ValidationError +from pydantic import ValidationError from langchain_community.utilities.tensorflow_datasets import TensorflowDatasets diff --git a/libs/community/tests/unit_tests/agents/test_serialization.py b/libs/community/tests/unit_tests/agents/test_serialization.py deleted file mode 100644 index 338f33e093280..0000000000000 --- a/libs/community/tests/unit_tests/agents/test_serialization.py +++ /dev/null @@ -1,26 +0,0 @@ -from pathlib import Path -from tempfile import TemporaryDirectory - -from langchain.agents.agent_types import AgentType -from langchain.agents.initialize import initialize_agent, load_agent -from langchain_core.language_models import FakeListLLM -from langchain_core.tools import Tool - - -def test_mrkl_serialization() -> None: - agent = initialize_agent( - [ - Tool( - name="Test tool", - func=lambda x: x, - description="Test description", - ) - ], - FakeListLLM(responses=[]), - agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, - verbose=True, - ) - with TemporaryDirectory() as tempdir: - file = Path(tempdir) / "agent.json" - agent.save_agent(file) - load_agent(file) diff --git a/libs/community/tests/unit_tests/callbacks/fake_callback_handler.py b/libs/community/tests/unit_tests/callbacks/fake_callback_handler.py index 2b0889748a8f5..b6838b3c85cf6 100644 --- a/libs/community/tests/unit_tests/callbacks/fake_callback_handler.py +++ b/libs/community/tests/unit_tests/callbacks/fake_callback_handler.py @@ -6,7 +6,7 @@ from langchain_core.callbacks import AsyncCallbackHandler, BaseCallbackHandler from langchain_core.messages import BaseMessage -from langchain_core.pydantic_v1 import BaseModel +from pydantic import BaseModel class BaseFakeCallbackHandler(BaseModel): @@ -254,7 +254,7 @@ def on_retriever_error( ) -> Any: self.on_retriever_error_common() - def __deepcopy__(self, memo: dict) -> "FakeCallbackHandler": + def __deepcopy__(self, memo: dict) -> "FakeCallbackHandler": # type: ignore return self @@ -388,5 +388,5 @@ async def on_text( ) -> None: self.on_text_common() - def __deepcopy__(self, memo: dict) -> "FakeAsyncCallbackHandler": + def __deepcopy__(self, memo: dict) -> "FakeAsyncCallbackHandler": # type: ignore return self diff --git a/libs/community/tests/unit_tests/chains/test_llm.py b/libs/community/tests/unit_tests/chains/test_llm.py index cef101f8db242..d7f58f6a39de4 100644 --- a/libs/community/tests/unit_tests/chains/test_llm.py +++ b/libs/community/tests/unit_tests/chains/test_llm.py @@ -1,8 +1,6 @@ """Test LLM chain.""" -from tempfile import TemporaryDirectory from typing import Dict, List, Union -from unittest.mock import patch import pytest from langchain.chains.llm import LLMChain @@ -27,21 +25,6 @@ def fake_llm_chain() -> LLMChain: return LLMChain(prompt=prompt, llm=FakeLLM(), output_key="text1") -@patch( - "langchain_community.llms.loading.get_type_to_cls_dict", - lambda: {"fake": lambda: FakeLLM}, -) -def test_serialization(fake_llm_chain: LLMChain) -> None: - """Test serialization.""" - from langchain.chains.loading import load_chain - - with TemporaryDirectory() as temp_dir: - file = temp_dir + "/llm.json" - fake_llm_chain.save(file) - loaded_chain = load_chain(file) - assert loaded_chain == fake_llm_chain - - def test_missing_inputs(fake_llm_chain: LLMChain) -> None: """Test error is raised if inputs are missing.""" with pytest.raises(ValueError): diff --git a/libs/community/tests/unit_tests/chains/test_pebblo_retrieval.py b/libs/community/tests/unit_tests/chains/test_pebblo_retrieval.py index 3b49a0d5174c9..a2fb1dbd00920 100644 --- a/libs/community/tests/unit_tests/chains/test_pebblo_retrieval.py +++ b/libs/community/tests/unit_tests/chains/test_pebblo_retrieval.py @@ -45,18 +45,6 @@ async def _aget_relevant_documents( return [Document(page_content=query)] -@pytest.fixture -def unsupported_retriever() -> FakeRetriever: - """ - Create a FakeRetriever instance - """ - retriever = FakeRetriever() - retriever.search_kwargs = {} - # Set the class of vectorstore - retriever.vectorstore.__class__ = InMemoryVectorStore - return retriever - - @pytest.fixture def retriever() -> FakeRetriever: """ @@ -110,9 +98,7 @@ def test_invoke(pebblo_retrieval_qa: PebbloRetrievalQA) -> None: assert response is not None -def test_validate_vectorstore( - retriever: FakeRetriever, unsupported_retriever: FakeRetriever -) -> None: +def test_validate_vectorstore(retriever: FakeRetriever) -> None: """ Test vectorstore validation """ @@ -127,6 +113,11 @@ def test_validate_vectorstore( app_name="app_name", ) + unsupported_retriever = FakeRetriever() + unsupported_retriever.search_kwargs = {} + # Set the class of vectorstore + unsupported_retriever.vectorstore.__class__ = InMemoryVectorStore + # validate_vectorstore method should raise a ValueError for unsupported vectorstores with pytest.raises(ValueError) as exc_info: _ = PebbloRetrievalQA.from_chain_type( diff --git a/libs/community/tests/unit_tests/chat_models/test_azureml_endpoint.py b/libs/community/tests/unit_tests/chat_models/test_azureml_endpoint.py index bf01dcae2a968..20bb38f367e00 100644 --- a/libs/community/tests/unit_tests/chat_models/test_azureml_endpoint.py +++ b/libs/community/tests/unit_tests/chat_models/test_azureml_endpoint.py @@ -3,7 +3,7 @@ import os import pytest -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture, FixtureRequest from langchain_community.chat_models.azureml_endpoint import AzureMLChatOnlineEndpoint diff --git a/libs/community/tests/unit_tests/chat_models/test_baichuan.py b/libs/community/tests/unit_tests/chat_models/test_baichuan.py index 31d588274a70a..d869f0042bc0d 100644 --- a/libs/community/tests/unit_tests/chat_models/test_baichuan.py +++ b/libs/community/tests/unit_tests/chat_models/test_baichuan.py @@ -10,7 +10,7 @@ SystemMessage, ToolMessage, ) -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture, MonkeyPatch from langchain_community.chat_models.baichuan import ( diff --git a/libs/community/tests/unit_tests/chat_models/test_fireworks.py b/libs/community/tests/unit_tests/chat_models/test_fireworks.py index 5b9c69b4d0fea..61548fa52c442 100644 --- a/libs/community/tests/unit_tests/chat_models/test_fireworks.py +++ b/libs/community/tests/unit_tests/chat_models/test_fireworks.py @@ -3,7 +3,7 @@ import sys import pytest -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture from langchain_community.chat_models import ChatFireworks diff --git a/libs/community/tests/unit_tests/chat_models/test_friendli.py b/libs/community/tests/unit_tests/chat_models/test_friendli.py index e101533fb8731..c752171299122 100644 --- a/libs/community/tests/unit_tests/chat_models/test_friendli.py +++ b/libs/community/tests/unit_tests/chat_models/test_friendli.py @@ -3,7 +3,7 @@ from unittest.mock import AsyncMock, MagicMock, Mock import pytest -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture, MonkeyPatch from langchain_community.adapters.openai import aenumerate diff --git a/libs/community/tests/unit_tests/chat_models/test_javelin_ai_gateway.py b/libs/community/tests/unit_tests/chat_models/test_javelin_ai_gateway.py index c612747dd5dcc..f3e7e361917ca 100644 --- a/libs/community/tests/unit_tests/chat_models/test_javelin_ai_gateway.py +++ b/libs/community/tests/unit_tests/chat_models/test_javelin_ai_gateway.py @@ -1,7 +1,7 @@ """Test `Javelin AI Gateway` chat models""" import pytest -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from langchain_community.chat_models import ChatJavelinAIGateway diff --git a/libs/community/tests/unit_tests/chat_models/test_kinetica.py b/libs/community/tests/unit_tests/chat_models/test_kinetica.py index 87824c775a697..5d83e92cdc7d1 100644 --- a/libs/community/tests/unit_tests/chat_models/test_kinetica.py +++ b/libs/community/tests/unit_tests/chat_models/test_kinetica.py @@ -14,6 +14,7 @@ class TestChatKinetica: test_ctx_json: str = """ { "payload":{ + "question": "foo", "context":[ { "table":"demo.test_profiles", diff --git a/libs/community/tests/unit_tests/chat_models/test_mlflow.py b/libs/community/tests/unit_tests/chat_models/test_mlflow.py index d526086c490e0..af2efd5202dcf 100644 --- a/libs/community/tests/unit_tests/chat_models/test_mlflow.py +++ b/libs/community/tests/unit_tests/chat_models/test_mlflow.py @@ -19,8 +19,8 @@ ToolMessageChunk, ) from langchain_core.prompts import ChatPromptTemplate -from langchain_core.pydantic_v1 import _PYDANTIC_MAJOR_VERSION, BaseModel from langchain_core.tools import StructuredTool +from pydantic import BaseModel from langchain_community.chat_models.mlflow import ChatMlflow @@ -199,10 +199,6 @@ def mock_stream(*args: Any, **kwargs: Any) -> Any: @pytest.mark.requires("mlflow") -@pytest.mark.skipif( - _PYDANTIC_MAJOR_VERSION < 2, - reason="The tool mock is not compatible with pydantic 1.x", -) def test_chat_mlflow_bind_tools( llm: ChatMlflow, mock_predict_stream_result: List[dict] ) -> None: @@ -226,14 +222,18 @@ def mock_stream(*args: Any, **kwargs: Any) -> Any: ] ) - def mock_func(*args: Any, **kwargs: Any) -> str: + def mock_func(x: int, y: int) -> str: return "36939 x 8922.4 = 329,511,111.6" + class ArgsSchema(BaseModel): + x: int + y: int + tools = [ StructuredTool( name="name", description="description", - args_schema=BaseModel, + args_schema=ArgsSchema, func=mock_func, ) ] diff --git a/libs/community/tests/unit_tests/chat_models/test_oci_generative_ai.py b/libs/community/tests/unit_tests/chat_models/test_oci_generative_ai.py index a59893e8b2164..15ca91eea5864 100644 --- a/libs/community/tests/unit_tests/chat_models/test_oci_generative_ai.py +++ b/libs/community/tests/unit_tests/chat_models/test_oci_generative_ai.py @@ -23,7 +23,11 @@ def test_llm_chat(monkeypatch: MonkeyPatch, test_model_id: str) -> None: oci_gen_ai_client = MagicMock() llm = ChatOCIGenAI(model_id=test_model_id, client=oci_gen_ai_client) - provider = llm.model_id.split(".")[0].lower() + model_id = llm.model_id + if model_id is None: + raise ValueError("Model ID is required for OCI Generative AI LLM service.") + + provider = model_id.split(".")[0].lower() def mocked_response(*args): # type: ignore[no-untyped-def] response_text = "Assistant chat reply." diff --git a/libs/community/tests/unit_tests/chat_models/test_octoai.py b/libs/community/tests/unit_tests/chat_models/test_octoai.py index b0d66eae36d3b..fb639a7399b73 100644 --- a/libs/community/tests/unit_tests/chat_models/test_octoai.py +++ b/libs/community/tests/unit_tests/chat_models/test_octoai.py @@ -1,5 +1,5 @@ import pytest -from langchain_core.pydantic_v1 import SecretStr, ValidationError +from pydantic import SecretStr, ValidationError from langchain_community.chat_models.octoai import ChatOctoAI diff --git a/libs/community/tests/unit_tests/chat_models/test_ollama.py b/libs/community/tests/unit_tests/chat_models/test_ollama.py index 3d3cb6c32addf..96075dda33497 100644 --- a/libs/community/tests/unit_tests/chat_models/test_ollama.py +++ b/libs/community/tests/unit_tests/chat_models/test_ollama.py @@ -1,7 +1,7 @@ from typing import List, Literal, Optional import pytest -from langchain_core.pydantic_v1 import BaseModel, ValidationError +from pydantic import BaseModel, ValidationError from langchain_community.chat_models import ChatOllama @@ -12,8 +12,8 @@ class ExpectedParams(BaseModel): ls_model_name: str ls_model_type: Literal["chat", "llm"] ls_temperature: Optional[float] - ls_max_tokens: Optional[int] - ls_stop: Optional[List[str]] + ls_max_tokens: Optional[int] = None + ls_stop: Optional[List[str]] = None model = ChatOllama(model="llama3") ls_params = model._get_ls_params() diff --git a/libs/community/tests/unit_tests/chat_models/test_premai.py b/libs/community/tests/unit_tests/chat_models/test_premai.py index 22327e3aa2fb3..09118f48ea528 100644 --- a/libs/community/tests/unit_tests/chat_models/test_premai.py +++ b/libs/community/tests/unit_tests/chat_models/test_premai.py @@ -4,7 +4,7 @@ import pytest from langchain_core.messages import AIMessage, HumanMessage, SystemMessage, ToolMessage -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture from langchain_community.chat_models import ChatPremAI diff --git a/libs/community/tests/unit_tests/document_loaders/blob_loaders/test_schema.py b/libs/community/tests/unit_tests/document_loaders/blob_loaders/test_schema.py deleted file mode 100644 index 8829b227cf429..0000000000000 --- a/libs/community/tests/unit_tests/document_loaders/blob_loaders/test_schema.py +++ /dev/null @@ -1,154 +0,0 @@ -import os -from contextlib import contextmanager -from pathlib import Path -from tempfile import NamedTemporaryFile -from typing import Generator, Iterable, Optional - -import pytest - -from langchain_community.document_loaders.blob_loaders.schema import ( - Blob, - BlobLoader, - PathLike, -) - - -@contextmanager -def get_temp_file( - content: bytes, suffix: Optional[str] = None -) -> Generator[Path, None, None]: - """Yield a temporary field with some content.""" - with NamedTemporaryFile(suffix=suffix, delete=False) as temp_file: - temp_file.write(content) - path = Path(temp_file.name) - try: - yield path - finally: - os.remove(str(path)) - - -def test_blob_initialized_with_binary_data() -> None: - """Test reading blob IO if blob content hasn't been read yet.""" - data = b"Hello, World!" - blob = Blob(data=data) - assert blob.as_string() == "Hello, World!" - assert blob.as_bytes() == data - assert blob.source is None - with blob.as_bytes_io() as bytes_io: - assert bytes_io.read() == data - - -def test_blob_from_pure_path() -> None: - """Test reading blob from a file path.""" - content = b"Hello, World!" - - with get_temp_file(content, suffix=".html") as temp_path: - assert isinstance(temp_path, Path) - blob = Blob.from_path(temp_path) - assert blob.encoding == "utf-8" # Default encoding - assert blob.path == temp_path - assert blob.mimetype == "text/html" - assert blob.source == str(temp_path) - assert blob.data is None - assert blob.as_bytes() == content - assert blob.as_string() == "Hello, World!" - with blob.as_bytes_io() as bytes_io: - assert bytes_io.read() == content - - -def test_blob_from_str_path() -> None: - """Test reading blob from a file path.""" - content = b"Hello, World!" - - with get_temp_file(content) as temp_path: - str_path = str(temp_path) - assert isinstance(str_path, str) - blob = Blob.from_path(str_path) - assert blob.encoding == "utf-8" # Default encoding - assert blob.path == str(temp_path) - assert blob.source == str(temp_path) - assert blob.data is None - assert blob.as_bytes() == content - assert blob.as_string() == "Hello, World!" - with blob.as_bytes_io() as bytes_io: - assert bytes_io.read() == content - - -def test_blob_from_str_data() -> None: - """Test reading blob from a file path.""" - content = b"Hello, World!" - blob = Blob.from_data(content) - assert blob.encoding == "utf-8" # Default encoding - assert blob.path is None - assert blob.mimetype is None - assert blob.source is None - assert blob.data == b"Hello, World!" - assert blob.as_bytes() == content - assert blob.as_string() == "Hello, World!" - with blob.as_bytes_io() as bytes_io: - assert bytes_io.read() == content - - -def test_blob_mimetype_from_str_data() -> None: - """Test reading blob from a file path.""" - content = b"Hello, World!" - mimetype = "text/html" - blob = Blob.from_data(content, mime_type=mimetype) - assert blob.mimetype == mimetype - - -@pytest.mark.parametrize( - "path, mime_type, guess_type, expected_mime_type", - [ - ("test.txt", None, True, "text/plain"), - ("test.txt", None, False, None), - ("test.html", None, True, "text/html"), - ("test.html", None, False, None), - ("test.html", "user_forced_value", True, "user_forced_value"), - (Path("test.html"), "user_forced_value", True, "user_forced_value"), - (Path("test.html"), None, True, "text/html"), - ], -) -def test_mime_type_inference( - path: PathLike, mime_type: str, guess_type: bool, expected_mime_type: Optional[str] -) -> None: - """Tests mimetype inference based on options and path.""" - blob = Blob.from_path(path, mime_type=mime_type, guess_type=guess_type) - assert blob.mimetype == expected_mime_type - - -def test_blob_initialization_validator() -> None: - """Test that blob initialization validates the arguments.""" - with pytest.raises(ValueError, match="Either data or path must be provided"): - Blob() # type: ignore[call-arg] - - assert Blob(data=b"Hello, World!") is not None - assert Blob(path="some_path") is not None # type: ignore[call-arg] - - -def test_blob_loader() -> None: - """Simple test that verifies that we can implement a blob loader.""" - - class TestLoader(BlobLoader): - def yield_blobs(self) -> Iterable[Blob]: - """Yield blob implementation.""" - yield Blob(data=b"Hello, World!") - - assert list(TestLoader().yield_blobs()) == [Blob(data=b"Hello, World!")] - - -def test_metadata_and_source() -> None: - """Test metadata and source""" - blob = Blob(path="some_file", data="b") - assert blob.source == "some_file" - assert blob.metadata == {} - blob = Blob(data=b"", metadata={"source": "hello"}) - assert blob.source == "hello" - assert blob.metadata == {"source": "hello"} - - blob = Blob.from_data("data", metadata={"source": "somewhere"}) - assert blob.source == "somewhere" - - with get_temp_file(b"hello") as path: - blob = Blob.from_path(path, metadata={"source": "somewhere"}) - assert blob.source == "somewhere" diff --git a/libs/community/tests/unit_tests/document_loaders/loaders/vendors/test_docugami.py b/libs/community/tests/unit_tests/document_loaders/loaders/vendors/test_docugami.py index cfbf8ba82e105..7b107f32789c5 100644 --- a/libs/community/tests/unit_tests/document_loaders/loaders/vendors/test_docugami.py +++ b/libs/community/tests/unit_tests/document_loaders/loaders/vendors/test_docugami.py @@ -25,4 +25,6 @@ def test_docugami_loader_local() -> None: def test_docugami_initialization() -> None: """Test correct initialization in remote mode.""" - DocugamiLoader(access_token="test", docset_id="123") # type: ignore[call-arg] + DocugamiLoader( + access_token="test", docset_id="123", document_ids=None, file_paths=None + ) diff --git a/libs/community/tests/unit_tests/document_loaders/test_github.py b/libs/community/tests/unit_tests/document_loaders/test_github.py index 544681f0ae660..c37eb2db9ee7a 100644 --- a/libs/community/tests/unit_tests/document_loaders/test_github.py +++ b/libs/community/tests/unit_tests/document_loaders/test_github.py @@ -171,10 +171,11 @@ def test_github_file_content_get_file_paths(mocker: MockerFixture) -> None: assert files[0]["path"] == "readme.md" # case2: didn't add file_filter - loader = GithubFileLoader( # type: ignore[call-arg] + loader = GithubFileLoader( repo="shufanhao/langchain", access_token="access_token", github_api_url="https://github.com", + file_filter=None, ) # Call the load method @@ -220,10 +221,11 @@ def test_github_file_content_loader(mocker: MockerFixture) -> None: mocker.patch("requests.get", side_effect=[file_path_res, file_content_res]) # case1: file_extension=".md" - loader = GithubFileLoader( # type: ignore[call-arg] + loader = GithubFileLoader( repo="shufanhao/langchain", access_token="access_token", github_api_url="https://github.com", + file_filter=None, ) # Call the load method diff --git a/libs/community/tests/unit_tests/document_loaders/test_onenote.py b/libs/community/tests/unit_tests/document_loaders/test_onenote.py index 41a58b2ec0e19..40862ab26725d 100644 --- a/libs/community/tests/unit_tests/document_loaders/test_onenote.py +++ b/libs/community/tests/unit_tests/document_loaders/test_onenote.py @@ -54,7 +54,10 @@ def test_load(mocker: MockerFixture) -> None: "

Test Content

" ), ) - loader = OneNoteLoader(object_ids=["test_id"], access_token="access_token") + loader = OneNoteLoader( + object_ids=["test_id"], + access_token="access_token", + ) documents = loader.load() assert documents == [ Document( diff --git a/libs/community/tests/unit_tests/embeddings/test_baichuan.py b/libs/community/tests/unit_tests/embeddings/test_baichuan.py index 3c7961af2dc68..47fb9f032ecb6 100644 --- a/libs/community/tests/unit_tests/embeddings/test_baichuan.py +++ b/libs/community/tests/unit_tests/embeddings/test_baichuan.py @@ -1,15 +1,16 @@ from typing import cast -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from langchain_community.embeddings import BaichuanTextEmbeddings def test_sparkllm_initialization_by_alias() -> None: # Effective initialization - embeddings = BaichuanTextEmbeddings( # type: ignore[call-arg] + embeddings = BaichuanTextEmbeddings( model="embedding_model", api_key="your-api-key", # type: ignore[arg-type] + session=None, ) assert embeddings.model_name == "embedding_model" assert ( diff --git a/libs/community/tests/unit_tests/embeddings/test_edenai.py b/libs/community/tests/unit_tests/embeddings/test_edenai.py index d80c2279f8afc..164c27f1af10f 100644 --- a/libs/community/tests/unit_tests/embeddings/test_edenai.py +++ b/libs/community/tests/unit_tests/embeddings/test_edenai.py @@ -1,6 +1,6 @@ """Test EdenAiEmbeddings embeddings""" -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture from langchain_community.embeddings import EdenAiEmbeddings diff --git a/libs/community/tests/unit_tests/embeddings/test_embaas.py b/libs/community/tests/unit_tests/embeddings/test_embaas.py index d631be8036bc8..1297f80658c89 100644 --- a/libs/community/tests/unit_tests/embeddings/test_embaas.py +++ b/libs/community/tests/unit_tests/embeddings/test_embaas.py @@ -1,6 +1,6 @@ """Test EmbaasEmbeddings embeddings""" -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture from langchain_community.embeddings import EmbaasEmbeddings diff --git a/libs/community/tests/unit_tests/embeddings/test_llm_rails.py b/libs/community/tests/unit_tests/embeddings/test_llm_rails.py index ccc6ff0d77d6a..0ddaa79006bf7 100644 --- a/libs/community/tests/unit_tests/embeddings/test_llm_rails.py +++ b/libs/community/tests/unit_tests/embeddings/test_llm_rails.py @@ -1,6 +1,6 @@ """Test LLMRailsEmbeddings embeddings""" -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture from langchain_community.embeddings import LLMRailsEmbeddings diff --git a/libs/community/tests/unit_tests/embeddings/test_premai.py b/libs/community/tests/unit_tests/embeddings/test_premai.py index 3d1b2b77447fa..8c75a67d20c05 100644 --- a/libs/community/tests/unit_tests/embeddings/test_premai.py +++ b/libs/community/tests/unit_tests/embeddings/test_premai.py @@ -1,7 +1,7 @@ """Test EmbaasEmbeddings embeddings""" import pytest -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture from langchain_community.embeddings import PremAIEmbeddings diff --git a/libs/community/tests/unit_tests/embeddings/test_sparkllm.py b/libs/community/tests/unit_tests/embeddings/test_sparkllm.py index 41f8ca4bd2edd..faff133c5c057 100644 --- a/libs/community/tests/unit_tests/embeddings/test_sparkllm.py +++ b/libs/community/tests/unit_tests/embeddings/test_sparkllm.py @@ -2,7 +2,7 @@ from typing import cast import pytest -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from langchain_community.embeddings import SparkLLMTextEmbeddings diff --git a/libs/community/tests/unit_tests/llms/fake_llm.py b/libs/community/tests/unit_tests/llms/fake_llm.py index 6f457f00e63aa..a8e9eb67eacea 100644 --- a/libs/community/tests/unit_tests/llms/fake_llm.py +++ b/libs/community/tests/unit_tests/llms/fake_llm.py @@ -4,7 +4,7 @@ from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.language_models.llms import LLM -from langchain_core.pydantic_v1 import validator +from pydantic import validator class FakeLLM(LLM): diff --git a/libs/community/tests/unit_tests/llms/test_ai21.py b/libs/community/tests/unit_tests/llms/test_ai21.py index 788364da0c5a8..f743fe7503b2b 100644 --- a/libs/community/tests/unit_tests/llms/test_ai21.py +++ b/libs/community/tests/unit_tests/llms/test_ai21.py @@ -2,7 +2,7 @@ from typing import cast -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture, MonkeyPatch from langchain_community.llms.ai21 import AI21 diff --git a/libs/community/tests/unit_tests/llms/test_aleph_alpha.py b/libs/community/tests/unit_tests/llms/test_aleph_alpha.py index 57cd544d7eb58..24b2d0433dc7e 100644 --- a/libs/community/tests/unit_tests/llms/test_aleph_alpha.py +++ b/libs/community/tests/unit_tests/llms/test_aleph_alpha.py @@ -1,7 +1,7 @@ """Test Aleph Alpha specific stuff.""" import pytest -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture, MonkeyPatch from langchain_community.llms.aleph_alpha import AlephAlpha diff --git a/libs/community/tests/unit_tests/llms/test_anyscale.py b/libs/community/tests/unit_tests/llms/test_anyscale.py index c5896f540cae2..481738de81d23 100644 --- a/libs/community/tests/unit_tests/llms/test_anyscale.py +++ b/libs/community/tests/unit_tests/llms/test_anyscale.py @@ -1,7 +1,7 @@ """Test Anyscale llm""" import pytest -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture, MonkeyPatch from langchain_community.llms.anyscale import Anyscale diff --git a/libs/community/tests/unit_tests/llms/test_bananadev.py b/libs/community/tests/unit_tests/llms/test_bananadev.py index 026cf134d3a6e..caa5f49a3cdd7 100644 --- a/libs/community/tests/unit_tests/llms/test_bananadev.py +++ b/libs/community/tests/unit_tests/llms/test_bananadev.py @@ -2,7 +2,7 @@ from typing import cast -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture, MonkeyPatch from langchain_community.llms.bananadev import Banana diff --git a/libs/community/tests/unit_tests/llms/test_cerebriumai.py b/libs/community/tests/unit_tests/llms/test_cerebriumai.py index 87e0439870e88..6d57408378aed 100644 --- a/libs/community/tests/unit_tests/llms/test_cerebriumai.py +++ b/libs/community/tests/unit_tests/llms/test_cerebriumai.py @@ -1,6 +1,6 @@ """Test CerebriumAI llm""" -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture, MonkeyPatch from langchain_community.llms.cerebriumai import CerebriumAI diff --git a/libs/community/tests/unit_tests/llms/test_fireworks.py b/libs/community/tests/unit_tests/llms/test_fireworks.py index 1487fdc0fba19..a5030d3ad53ca 100644 --- a/libs/community/tests/unit_tests/llms/test_fireworks.py +++ b/libs/community/tests/unit_tests/llms/test_fireworks.py @@ -3,7 +3,7 @@ import sys import pytest -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture from langchain_community.llms import Fireworks diff --git a/libs/community/tests/unit_tests/llms/test_forefrontai.py b/libs/community/tests/unit_tests/llms/test_forefrontai.py index 711cb2186be71..52a2c578387a7 100644 --- a/libs/community/tests/unit_tests/llms/test_forefrontai.py +++ b/libs/community/tests/unit_tests/llms/test_forefrontai.py @@ -2,7 +2,7 @@ from typing import cast -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture, MonkeyPatch from langchain_community.llms.forefrontai import ForefrontAI diff --git a/libs/community/tests/unit_tests/llms/test_friendli.py b/libs/community/tests/unit_tests/llms/test_friendli.py index d55d6d14fa812..6fd4593d93d3b 100644 --- a/libs/community/tests/unit_tests/llms/test_friendli.py +++ b/libs/community/tests/unit_tests/llms/test_friendli.py @@ -3,7 +3,7 @@ from unittest.mock import AsyncMock, MagicMock, Mock import pytest -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture, MonkeyPatch from langchain_community.adapters.openai import aenumerate diff --git a/libs/community/tests/unit_tests/llms/test_gooseai.py b/libs/community/tests/unit_tests/llms/test_gooseai.py index be467b61758a9..6ec2f0aadc905 100644 --- a/libs/community/tests/unit_tests/llms/test_gooseai.py +++ b/libs/community/tests/unit_tests/llms/test_gooseai.py @@ -1,7 +1,7 @@ """Test GooseAI""" import pytest -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import MonkeyPatch from langchain_community.llms.gooseai import GooseAI diff --git a/libs/community/tests/unit_tests/llms/test_minimax.py b/libs/community/tests/unit_tests/llms/test_minimax.py index ef128ea1d5192..a244cf0b2492a 100644 --- a/libs/community/tests/unit_tests/llms/test_minimax.py +++ b/libs/community/tests/unit_tests/llms/test_minimax.py @@ -2,7 +2,7 @@ from typing import cast -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture, MonkeyPatch from langchain_community.llms.minimax import Minimax diff --git a/libs/community/tests/unit_tests/llms/test_oci_generative_ai.py b/libs/community/tests/unit_tests/llms/test_oci_generative_ai.py index df600d4d775bb..d0472abf86c36 100644 --- a/libs/community/tests/unit_tests/llms/test_oci_generative_ai.py +++ b/libs/community/tests/unit_tests/llms/test_oci_generative_ai.py @@ -22,7 +22,12 @@ def test_llm_complete(monkeypatch: MonkeyPatch, test_model_id: str) -> None: oci_gen_ai_client = MagicMock() llm = OCIGenAI(model_id=test_model_id, client=oci_gen_ai_client) - provider = llm.model_id.split(".")[0].lower() + model_id = llm.model_id + + if model_id is None: + raise ValueError("Model ID is required for OCI Generative AI LLM service.") + + provider = model_id.split(".")[0].lower() def mocked_response(*args): # type: ignore[no-untyped-def] response_text = "This is the completion." diff --git a/libs/community/tests/unit_tests/llms/test_pipelineai.py b/libs/community/tests/unit_tests/llms/test_pipelineai.py index 3a8513650fc04..ea56cd322278d 100644 --- a/libs/community/tests/unit_tests/llms/test_pipelineai.py +++ b/libs/community/tests/unit_tests/llms/test_pipelineai.py @@ -1,4 +1,4 @@ -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture from langchain_community.llms.pipelineai import PipelineAI diff --git a/libs/community/tests/unit_tests/llms/test_predibase.py b/libs/community/tests/unit_tests/llms/test_predibase.py index bb8245f1ef32b..4812492776a35 100644 --- a/libs/community/tests/unit_tests/llms/test_predibase.py +++ b/libs/community/tests/unit_tests/llms/test_predibase.py @@ -1,4 +1,4 @@ -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture from langchain_community.llms.predibase import Predibase diff --git a/libs/community/tests/unit_tests/llms/test_stochasticai.py b/libs/community/tests/unit_tests/llms/test_stochasticai.py index 285d4368f2a72..894b1df5a5693 100644 --- a/libs/community/tests/unit_tests/llms/test_stochasticai.py +++ b/libs/community/tests/unit_tests/llms/test_stochasticai.py @@ -1,4 +1,4 @@ -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture from langchain_community.llms.stochasticai import StochasticAI diff --git a/libs/community/tests/unit_tests/llms/test_symblai_nebula.py b/libs/community/tests/unit_tests/llms/test_symblai_nebula.py index 9109ee3c45be7..07654cdcd6b08 100644 --- a/libs/community/tests/unit_tests/llms/test_symblai_nebula.py +++ b/libs/community/tests/unit_tests/llms/test_symblai_nebula.py @@ -1,6 +1,6 @@ """Test the Nebula model by Symbl.ai""" -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture, MonkeyPatch from langchain_community.llms.symblai_nebula import Nebula diff --git a/libs/community/tests/unit_tests/llms/test_together.py b/libs/community/tests/unit_tests/llms/test_together.py index 0d6cf975035c0..2012a36590d7e 100644 --- a/libs/community/tests/unit_tests/llms/test_together.py +++ b/libs/community/tests/unit_tests/llms/test_together.py @@ -2,7 +2,7 @@ from typing import cast -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from pytest import CaptureFixture, MonkeyPatch from langchain_community.llms.together import Together diff --git a/libs/community/tests/unit_tests/load/__snapshots__/test_dump.ambr b/libs/community/tests/unit_tests/load/__snapshots__/test_dump.ambr index 2789472be2212..0c30949ab703e 100644 --- a/libs/community/tests/unit_tests/load/__snapshots__/test_dump.ambr +++ b/libs/community/tests/unit_tests/load/__snapshots__/test_dump.ambr @@ -71,6 +71,7 @@ "LLMChain" ], "kwargs": { + "verbose": false, "prompt": { "lc": 1, "type": "constructor", @@ -298,6 +299,7 @@ "LLMChain" ], "kwargs": { + "verbose": false, "prompt": { "lc": 1, "type": "constructor", @@ -587,6 +589,7 @@ "LLMChain" ], "kwargs": { + "verbose": false, "prompt": { "lc": 1, "type": "constructor", diff --git a/libs/community/tests/unit_tests/load/test_dump.py b/libs/community/tests/unit_tests/load/test_dump.py index c46233a874a72..ef86c654575de 100644 --- a/libs/community/tests/unit_tests/load/test_dump.py +++ b/libs/community/tests/unit_tests/load/test_dump.py @@ -11,8 +11,8 @@ from langchain_core.load.serializable import Serializable from langchain_core.prompts.chat import ChatPromptTemplate, HumanMessagePromptTemplate from langchain_core.prompts.prompt import PromptTemplate -from langchain_core.pydantic_v1 import Field, root_validator from langchain_core.tracers.langchain import LangChainTracer +from pydantic import ConfigDict, Field, model_validator class Person(Serializable): @@ -182,11 +182,13 @@ class TestClass(Serializable): my_favorite_secret: str = Field(alias="my_favorite_secret_alias") my_other_secret: str = Field() - class Config: - allow_population_by_field_name = True + model_config = ConfigDict( + populate_by_name=True, + ) - @root_validator(pre=True) - def get_from_env(cls, values: Dict) -> Dict: + @model_validator(mode="before") + @classmethod + def get_from_env(cls, values: Dict) -> Any: """Get the values from the environment.""" if "my_favorite_secret" not in values: values["my_favorite_secret"] = os.getenv("MY_FAVORITE_SECRET") diff --git a/libs/community/tests/unit_tests/load/test_serializable.py b/libs/community/tests/unit_tests/load/test_serializable.py index 9b6dcc536d4fa..f86d2f707396b 100644 --- a/libs/community/tests/unit_tests/load/test_serializable.py +++ b/libs/community/tests/unit_tests/load/test_serializable.py @@ -112,6 +112,14 @@ def test_serializable_mapping() -> None: "chat_models", "ChatGroq", ), + # TODO(0.3): For now we're skipping this test. Need to fix + # so that it only runs when langchain-aws is installed. + ("langchain", "chat_models", "bedrock", "ChatBedrock"): ( + "langchain_aws", + "chat_models", + "bedrock", + "ChatBedrock", + ), } serializable_modules = import_all_modules("langchain") diff --git a/libs/community/tests/unit_tests/retrievers/test_bedrock.py b/libs/community/tests/unit_tests/retrievers/test_bedrock.py index ff72d193e4ad6..6f49cd7bd2b8f 100644 --- a/libs/community/tests/unit_tests/retrievers/test_bedrock.py +++ b/libs/community/tests/unit_tests/retrievers/test_bedrock.py @@ -28,9 +28,11 @@ def amazon_retriever( ) -def test_create_client(amazon_retriever: AmazonKnowledgeBasesRetriever) -> None: - with pytest.raises(ImportError): - amazon_retriever.create_client({}) +def test_create_client() -> None: + # Import error if boto3 is not installed + # Value error if credentials are not supplied. + with pytest.raises((ImportError, ValueError)): + AmazonKnowledgeBasesRetriever() # type: ignore def test_standard_params(amazon_retriever: AmazonKnowledgeBasesRetriever) -> None: diff --git a/libs/community/tests/unit_tests/test_dependencies.py b/libs/community/tests/unit_tests/test_dependencies.py index e68f5e4fa6009..47239c945d2ad 100644 --- a/libs/community/tests/unit_tests/test_dependencies.py +++ b/libs/community/tests/unit_tests/test_dependencies.py @@ -48,6 +48,7 @@ def test_required_dependencies(poetry_conf: Mapping[str, Any]) -> None: "numpy", "python", "requests", + "pydantic-settings", "tenacity", "langchain", ] diff --git a/libs/community/tests/unit_tests/tools/audio/test_tools.py b/libs/community/tests/unit_tests/tools/audio/test_tools.py index 03055ad36c25d..30cacb7b7b0b5 100644 --- a/libs/community/tests/unit_tests/tools/audio/test_tools.py +++ b/libs/community/tests/unit_tests/tools/audio/test_tools.py @@ -6,7 +6,7 @@ from unittest.mock import Mock, mock_open, patch import pytest -from langchain_core.pydantic_v1 import SecretStr +from pydantic import SecretStr from langchain_community.tools.audio import HuggingFaceTextToSpeechModelInference diff --git a/libs/community/tests/unit_tests/tools/openapi/__init__.py b/libs/community/tests/unit_tests/tools/openapi/__init__.py deleted file mode 100644 index e69de29bb2d1d..0000000000000 diff --git a/libs/community/tests/unit_tests/tools/openapi/test_api_models.py b/libs/community/tests/unit_tests/tools/openapi/test_api_models.py deleted file mode 100644 index e871349a7bc59..0000000000000 --- a/libs/community/tests/unit_tests/tools/openapi/test_api_models.py +++ /dev/null @@ -1,224 +0,0 @@ -"""Test the APIOperation class.""" - -import json -import os -from pathlib import Path -from typing import Iterable, List, Tuple - -import pytest - -# Keep at top of file to ensure that pydantic test can be skipped before -# pydantic v1 related imports are attempted by openapi_pydantic. -from langchain_core.pydantic_v1 import _PYDANTIC_MAJOR_VERSION - -if _PYDANTIC_MAJOR_VERSION != 1: - pytest.skip( - f"Pydantic major version {_PYDANTIC_MAJOR_VERSION} is not supported.", - allow_module_level=True, - ) - -import pytest -import yaml - -from langchain_community.tools.openapi.utils.api_models import ( - APIOperation, - APIRequestBody, - APIRequestBodyProperty, -) -from langchain_community.tools.openapi.utils.openapi_utils import HTTPVerb, OpenAPISpec - -SPECS_DIR = Path(__file__).parents[2] / "examples" / "test_specs" - - -def _get_test_specs() -> Iterable[Path]: - """Walk the test_specs directory and collect all files with the name 'apispec' - in them. - """ - if not SPECS_DIR.exists(): - raise ValueError - return ( - Path(root) / file - for root, _, files in os.walk(SPECS_DIR) - for file in files - if file.startswith("apispec") - ) - - -def _get_paths_and_methods_from_spec_dictionary( - spec: dict, -) -> Iterable[Tuple[str, str]]: - """Return a tuple (paths, methods) for every path in spec.""" - valid_methods = [verb.value for verb in HTTPVerb] - for path_name, path_item in spec["paths"].items(): - for method in valid_methods: - if method in path_item: - yield (path_name, method) - - -def http_paths_and_methods() -> List[Tuple[str, OpenAPISpec, str, str]]: - """Return a args for every method in cached OpenAPI spec in test_specs.""" - http_paths_and_methods = [] - for test_spec in _get_test_specs(): - spec_name = test_spec.parent.name - if test_spec.suffix == ".json": - with test_spec.open("r") as f: - spec = json.load(f) - else: - with test_spec.open("r") as f: - spec = yaml.safe_load(f.read()) - parsed_spec = OpenAPISpec.from_file(test_spec) - for path, method in _get_paths_and_methods_from_spec_dictionary(spec): - http_paths_and_methods.append( - ( - spec_name, - parsed_spec, - path, - method, - ) - ) - return http_paths_and_methods - - -@pytest.mark.requires("openapi_pydantic") -def test_parse_api_operations() -> None: - """Test the APIOperation class.""" - for spec_name, spec, path, method in http_paths_and_methods(): - try: - APIOperation.from_openapi_spec(spec, path, method) - except Exception as e: - raise AssertionError(f"Error processing {spec_name}: {e} ") from e - - -@pytest.mark.requires("openapi_pydantic") -@pytest.fixture -def raw_spec() -> OpenAPISpec: - """Return a raw OpenAPI spec.""" - from openapi_pydantic import Info - - return OpenAPISpec( - info=Info(title="Test API", version="1.0.0"), - ) - - -@pytest.mark.requires("openapi_pydantic") -def test_api_request_body_from_request_body_with_ref(raw_spec: OpenAPISpec) -> None: - """Test instantiating APIRequestBody from RequestBody with a reference.""" - from openapi_pydantic import ( - Components, - MediaType, - Reference, - RequestBody, - Schema, - ) - - raw_spec.components = Components( - schemas={ - "Foo": Schema( - type="object", - properties={ - "foo": Schema(type="string"), - "bar": Schema(type="number"), - }, - required=["foo"], - ) - } - ) - media_type = MediaType( - schema=Reference( - ref="#/components/schemas/Foo", - ) - ) - request_body = RequestBody(content={"application/json": media_type}) - api_request_body = APIRequestBody.from_request_body(request_body, raw_spec) - assert api_request_body.description is None - assert len(api_request_body.properties) == 2 - foo_prop = api_request_body.properties[0] - assert foo_prop.name == "foo" - assert foo_prop.required is True - bar_prop = api_request_body.properties[1] - assert bar_prop.name == "bar" - assert bar_prop.required is False - assert api_request_body.media_type == "application/json" - - -@pytest.mark.requires("openapi_pydantic") -def test_api_request_body_from_request_body_with_schema(raw_spec: OpenAPISpec) -> None: - """Test instantiating APIRequestBody from RequestBody with a schema.""" - from openapi_pydantic import ( - MediaType, - RequestBody, - Schema, - ) - - request_body = RequestBody( - content={ - "application/json": MediaType( - schema=Schema(type="object", properties={"foo": Schema(type="string")}) - ) - } - ) - api_request_body = APIRequestBody.from_request_body(request_body, raw_spec) - assert api_request_body.properties == [ - APIRequestBodyProperty( - name="foo", - required=False, - type="string", - default=None, - description=None, - properties=[], - references_used=[], - ) - ] - assert api_request_body.media_type == "application/json" - - -@pytest.mark.requires("openapi_pydantic") -def test_api_request_body_property_from_schema(raw_spec: OpenAPISpec) -> None: - from openapi_pydantic import ( - Components, - Reference, - Schema, - ) - - raw_spec.components = Components( - schemas={ - "Bar": Schema( - type="number", - ) - } - ) - schema = Schema( - type="object", - properties={ - "foo": Schema(type="string"), - "bar": Reference(ref="#/components/schemas/Bar"), - }, - required=["bar"], - ) - api_request_body_property = APIRequestBodyProperty.from_schema( - schema, "test", required=True, spec=raw_spec - ) - expected_sub_properties = [ - APIRequestBodyProperty( - name="foo", - required=False, - type="string", - default=None, - description=None, - properties=[], - references_used=[], - ), - APIRequestBodyProperty( - name="bar", - required=True, - type="number", - default=None, - description=None, - properties=[], - references_used=["Bar"], - ), - ] - assert api_request_body_property.properties[0] == expected_sub_properties[0] - assert api_request_body_property.properties[1] == expected_sub_properties[1] - assert api_request_body_property.type == "object" - assert api_request_body_property.properties[1].references_used == ["Bar"] diff --git a/libs/community/tests/unit_tests/tools/test_exported.py b/libs/community/tests/unit_tests/tools/test_exported.py index 6dd98bd0d7790..e5503369873ed 100644 --- a/libs/community/tests/unit_tests/tools/test_exported.py +++ b/libs/community/tests/unit_tests/tools/test_exported.py @@ -23,13 +23,15 @@ def _get_tool_classes(skip_tools_without_default_names: bool) -> List[Type[BaseT if isinstance(tool_class, type) and issubclass(tool_class, BaseTool): if tool_class in _EXCLUDE: continue - if skip_tools_without_default_names and get_fields(tool_class)[ - "name" - ].default in [ # type: ignore + default_name = get_fields(tool_class)["name"].default + if skip_tools_without_default_names and default_name in [ # type: ignore None, "", ]: continue + if not isinstance(default_name, str): + continue + results.append(tool_class) return results @@ -37,6 +39,6 @@ def _get_tool_classes(skip_tools_without_default_names: bool) -> List[Type[BaseT def test_tool_names_unique() -> None: """Test that the default names for our core tools are unique.""" tool_classes = _get_tool_classes(skip_tools_without_default_names=True) - names = sorted([get_fields(tool_cls)["name"].default for tool_cls in tool_classes]) + names = sorted([tool_cls.model_fields["name"].default for tool_cls in tool_classes]) duplicated_names = [name for name in names if names.count(name) > 1] assert not duplicated_names diff --git a/libs/community/tests/unit_tests/tools/test_zapier.py b/libs/community/tests/unit_tests/tools/test_zapier.py index d2b0661943506..8df158a765c16 100644 --- a/libs/community/tests/unit_tests/tools/test_zapier.py +++ b/libs/community/tests/unit_tests/tools/test_zapier.py @@ -16,7 +16,9 @@ def test_default_base_prompt() -> None: action_id="test", zapier_description="test", params_schema={"test": "test"}, - api_wrapper=ZapierNLAWrapper(zapier_nla_api_key="test"), # type: ignore[call-arg] + api_wrapper=ZapierNLAWrapper( + zapier_nla_api_key="test", zapier_nla_oauth_access_token="" + ), ) # Test that the base prompt was successfully assigned to the default prompt diff --git a/libs/community/tests/unit_tests/utilities/test_nvidia_riva_asr.py b/libs/community/tests/unit_tests/utilities/test_nvidia_riva_asr.py index e95e41dc4f8a6..c5ffdacabf41f 100644 --- a/libs/community/tests/unit_tests/utilities/test_nvidia_riva_asr.py +++ b/libs/community/tests/unit_tests/utilities/test_nvidia_riva_asr.py @@ -4,6 +4,7 @@ from unittest.mock import patch import pytest +from pydantic import AnyHttpUrl from langchain_community.utilities.nvidia_riva import ( AudioStream, @@ -126,7 +127,10 @@ def stream() -> AudioStream: def test_init(asr: RivaASR) -> None: """Test that ASR accepts valid arguments.""" for key, expected_val in CONFIG.items(): - assert getattr(asr, key, None) == expected_val + if key == "url": + assert asr.url == AnyHttpUrl(expected_val) # type: ignore + else: + assert getattr(asr, key, None) == expected_val @pytest.mark.requires("riva.client") @@ -162,7 +166,7 @@ def test_get_service(asr: RivaASR) -> None: svc = asr._get_service() assert str(svc.auth.ssl_cert) == CONFIG["ssl_cert"] assert svc.auth.use_ssl == SVC_USE_SSL - assert svc.auth.uri == SVC_URI + assert str(svc.auth.uri) == SVC_URI @pytest.mark.requires("riva.client") diff --git a/libs/community/tests/unit_tests/utilities/test_nvidia_riva_tts.py b/libs/community/tests/unit_tests/utilities/test_nvidia_riva_tts.py index 3024ff5885a8a..fe584f2723f71 100644 --- a/libs/community/tests/unit_tests/utilities/test_nvidia_riva_tts.py +++ b/libs/community/tests/unit_tests/utilities/test_nvidia_riva_tts.py @@ -57,7 +57,10 @@ def tts() -> RivaTTS: def test_init(tts: RivaTTS) -> None: """Test that ASR accepts valid arguments.""" for key, expected_val in CONFIG.items(): - assert getattr(tts, key, None) == expected_val + if key == "url": + assert str(tts.url) == expected_val + "/" # type: ignore + else: + assert getattr(tts, key, None) == expected_val @pytest.mark.requires("riva.client") diff --git a/libs/community/tests/unit_tests/vectorstores/redis/test_redis_schema.py b/libs/community/tests/unit_tests/vectorstores/redis/test_redis_schema.py index 90196a7f3e661..a3afbfc5ad5cf 100644 --- a/libs/community/tests/unit_tests/vectorstores/redis/test_redis_schema.py +++ b/libs/community/tests/unit_tests/vectorstores/redis/test_redis_schema.py @@ -36,7 +36,7 @@ def test_numeric_field_schema_creation() -> None: def test_redis_vector_field_validation() -> None: """Test validation for RedisVectorField's datatype.""" - from langchain_core.pydantic_v1 import ValidationError + from pydantic import ValidationError with pytest.raises(ValidationError): RedisVectorField( diff --git a/libs/community/tests/unit_tests/vectorstores/test_inmemory.py b/libs/community/tests/unit_tests/vectorstores/test_inmemory.py index 7381335103a0a..6facca3429b2a 100644 --- a/libs/community/tests/unit_tests/vectorstores/test_inmemory.py +++ b/libs/community/tests/unit_tests/vectorstores/test_inmemory.py @@ -19,6 +19,13 @@ def __eq__(self, other: Any) -> bool: return isinstance(other, str) +def _AnyDocument(**kwargs: Any) -> Document: + """Create a Document with an any id field.""" + doc = Document(**kwargs) + doc.id = AnyStr() + return doc + + class TestInMemoryReadWriteTestSuite(ReadWriteTestSuite): @pytest.fixture def vectorstore(self) -> InMemoryVectorStore: @@ -37,12 +44,12 @@ async def test_inmemory() -> None: ["foo", "bar", "baz"], ConsistentFakeEmbeddings() ) output = await store.asimilarity_search("foo", k=1) - assert output == [Document(page_content="foo", id=AnyStr())] + assert output == [_AnyDocument(page_content="foo")] output = await store.asimilarity_search("bar", k=2) assert output == [ - Document(page_content="bar", id=AnyStr()), - Document(page_content="baz", id=AnyStr()), + _AnyDocument(page_content="bar"), + _AnyDocument(page_content="baz"), ] output2 = await store.asimilarity_search_with_score("bar", k=2) @@ -70,8 +77,8 @@ async def test_inmemory_mmr() -> None: "foo", k=10, lambda_mult=0.1 ) assert len(output) == len(texts) - assert output[0] == Document(page_content="foo", id=AnyStr()) - assert output[1] == Document(page_content="foy", id=AnyStr()) + assert output[0] == _AnyDocument(page_content="foo") + assert output[1] == _AnyDocument(page_content="foy") async def test_inmemory_dump_load(tmp_path: Path) -> None: @@ -99,4 +106,4 @@ async def test_inmemory_filter() -> None: output = await store.asimilarity_search( "baz", filter=lambda doc: doc.metadata["id"] == 1 ) - assert output == [Document(page_content="foo", metadata={"id": 1}, id=AnyStr())] + assert output == [_AnyDocument(page_content="foo", metadata={"id": 1})] diff --git a/libs/core/langchain_core/_api/deprecation.py b/libs/core/langchain_core/_api/deprecation.py index cf5dbd608a93e..7f46ff2802f90 100644 --- a/libs/core/langchain_core/_api/deprecation.py +++ b/libs/core/langchain_core/_api/deprecation.py @@ -144,7 +144,7 @@ def deprecate( _package: str = package, ) -> T: """Implementation of the decorator returned by `deprecated`.""" - from langchain_core.utils.pydantic import FieldInfoV1 + from langchain_core.utils.pydantic import FieldInfoV1, FieldInfoV2 def emit_warning() -> None: """Emit the warning.""" @@ -238,6 +238,25 @@ def finalize(wrapper: Callable[..., Any], new_doc: str) -> T: exclude=obj.exclude, ), ) + elif isinstance(obj, FieldInfoV2): + wrapped = None + if not _obj_type: + _obj_type = "attribute" + if not _name: + raise ValueError(f"Field {obj} must have a name to be deprecated.") + old_doc = obj.description + + def finalize(wrapper: Callable[..., Any], new_doc: str) -> T: + return cast( + T, + FieldInfoV2( + 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: diff --git a/libs/core/langchain_core/language_models/base.py b/libs/core/langchain_core/language_models/base.py index f2f0d47a251b4..6832b5fc5f948 100644 --- a/libs/core/langchain_core/language_models/base.py +++ b/libs/core/langchain_core/language_models/base.py @@ -18,7 +18,7 @@ Union, ) -from pydantic import BaseModel, ConfigDict, Field, validator +from pydantic import BaseModel, ConfigDict, Field, field_validator from typing_extensions import TypeAlias, TypedDict from langchain_core._api import deprecated @@ -134,7 +134,7 @@ class BaseLanguageModel( arbitrary_types_allowed=True, ) - @validator("verbose", pre=True, always=True, allow_reuse=True) + @field_validator("verbose", mode="before") def set_verbose(cls, verbose: Optional[bool]) -> bool: """If verbose is None, set it. diff --git a/libs/core/langchain_core/output_parsers/json.py b/libs/core/langchain_core/output_parsers/json.py index 5f2732273475b..b076e65e85335 100644 --- a/libs/core/langchain_core/output_parsers/json.py +++ b/libs/core/langchain_core/output_parsers/json.py @@ -54,8 +54,8 @@ def _get_schema(self, pydantic_object: Type[TBaseModel]) -> dict[str, Any]: if issubclass(pydantic_object, pydantic.BaseModel): return pydantic_object.model_json_schema() elif issubclass(pydantic_object, pydantic.v1.BaseModel): - return pydantic_object.schema() - return pydantic_object.schema() + return pydantic_object.model_json_schema() + return pydantic_object.model_json_schema() def parse_result(self, result: List[Generation], *, partial: bool = False) -> Any: """Parse the result of an LLM call to a JSON object. diff --git a/libs/core/langchain_core/output_parsers/pydantic.py b/libs/core/langchain_core/output_parsers/pydantic.py index e7ddae3ac5aef..8d48e98b2b349 100644 --- a/libs/core/langchain_core/output_parsers/pydantic.py +++ b/libs/core/langchain_core/output_parsers/pydantic.py @@ -90,7 +90,7 @@ def get_format_instructions(self) -> str: The format instructions for the JSON output. """ # Copy schema to avoid altering original Pydantic schema. - schema = {k: v for k, v in self.pydantic_object.schema().items()} + schema = {k: v for k, v in self.pydantic_object.model_json_schema().items()} # Remove extraneous fields. reduced_schema = schema diff --git a/libs/core/langchain_core/runnables/base.py b/libs/core/langchain_core/runnables/base.py index 444b191c2f55d..cc9656d5e75dc 100644 --- a/libs/core/langchain_core/runnables/base.py +++ b/libs/core/langchain_core/runnables/base.py @@ -204,8 +204,8 @@ def buggy_double(y: int) -> int: ) ) - print(sequence.input_schema.schema()) # Show inferred input schema - print(sequence.output_schema.schema()) # Show inferred output schema + print(sequence.input_schema.model_json_schema()) # Show inferred input schema + print(sequence.output_schema.model_json_schema()) # Show inferred output schema print(sequence.invoke(2)) # invoke the sequence (note the retry above!!) Debugging and tracing @@ -683,10 +683,10 @@ def assign( chain_with_assign = chain.assign(hello=itemgetter("str") | llm) - print(chain_with_assign.input_schema.schema()) + print(chain_with_assign.input_schema.model_json_schema()) # {'title': 'PromptInput', 'type': 'object', 'properties': {'question': {'title': 'Question', 'type': 'string'}}} - print(chain_with_assign.output_schema.schema()) # + print(chain_with_assign.output_schema.model_json_schema()) # {'title': 'RunnableSequenceOutput', 'type': 'object', 'properties': {'str': {'title': 'Str', 'type': 'string'}, 'hello': {'title': 'Hello', 'type': 'string'}}} @@ -3562,7 +3562,8 @@ def get_input_schema( The input schema of the Runnable. """ if all( - s.get_input_schema(config).schema().get("type", "object") == "object" + s.get_input_schema(config).model_json_schema().get("type", "object") + == "object" for s in self.steps__.values() ): # This is correct, but pydantic typings/mypy don't think so. diff --git a/libs/core/langchain_core/runnables/branch.py b/libs/core/langchain_core/runnables/branch.py index 6aba17437ca7b..60b8382fcf454 100644 --- a/libs/core/langchain_core/runnables/branch.py +++ b/libs/core/langchain_core/runnables/branch.py @@ -171,7 +171,10 @@ def get_input_schema( ) for runnable in runnables: - if runnable.get_input_schema(config).schema().get("type") is not None: + if ( + runnable.get_input_schema(config).model_json_schema().get("type") + is not None + ): return runnable.get_input_schema(config) return super().get_input_schema(config) diff --git a/libs/core/langchain_core/runnables/configurable.py b/libs/core/langchain_core/runnables/configurable.py index bc332475d63a1..112a70efca2cc 100644 --- a/libs/core/langchain_core/runnables/configurable.py +++ b/libs/core/langchain_core/runnables/configurable.py @@ -18,6 +18,7 @@ Union, cast, ) +from typing import Mapping as Mapping from weakref import WeakValueDictionary from pydantic import BaseModel, ConfigDict @@ -453,6 +454,9 @@ def _prepare( return (self.default, config) +RunnableConfigurableFields.model_rebuild() + + # Before Python 3.11 native StrEnum is not available class StrEnum(str, enum.Enum): """String enum.""" diff --git a/libs/core/langchain_core/tools/base.py b/libs/core/langchain_core/tools/base.py index c150a0b35d522..b9d23ae9206be 100644 --- a/libs/core/langchain_core/tools/base.py +++ b/libs/core/langchain_core/tools/base.py @@ -30,7 +30,6 @@ from pydantic import ( BaseModel, ConfigDict, - Extra, Field, SkipValidation, ValidationError, @@ -97,7 +96,7 @@ def _get_filtered_args( include_injected: bool = True, ) -> dict: """Get the arguments from a function's signature.""" - schema = inferred_model.schema()["properties"] + schema = inferred_model.model_json_schema()["properties"] valid_keys = signature(func).parameters return { k: schema[k] @@ -175,7 +174,7 @@ class _SchemaConfig: Defaults to True. """ - extra: Any = Extra.forbid + extra: str = "forbid" arbitrary_types_allowed: bool = True @@ -409,7 +408,7 @@ def is_single_input(self) -> bool: @property def args(self) -> dict: - return self.get_input_schema().schema()["properties"] + return self.get_input_schema().model_json_schema()["properties"] @property def tool_call_schema(self) -> Type[BaseModel]: diff --git a/libs/core/langchain_core/tools/convert.py b/libs/core/langchain_core/tools/convert.py index c0c2a214a529f..cac88769250b6 100644 --- a/libs/core/langchain_core/tools/convert.py +++ b/libs/core/langchain_core/tools/convert.py @@ -82,7 +82,7 @@ def foo(bar: str, baz: int) -> str: \"\"\" return bar - foo.args_schema.schema() + foo.args_schema.model_json_schema() .. code-block:: python @@ -145,7 +145,7 @@ def _make_tool(dec_func: Union[Callable, Runnable]) -> BaseTool: if isinstance(dec_func, Runnable): runnable = dec_func - if runnable.input_schema.schema().get("type") != "object": + if runnable.input_schema.model_json_schema().get("type") != "object": raise ValueError("Runnable must have an object schema.") async def ainvoke_wrapper( @@ -227,7 +227,7 @@ def _partial(func: Callable[[str], str]) -> BaseTool: def _get_description_from_runnable(runnable: Runnable) -> str: """Generate a placeholder description of a runnable.""" - input_schema = runnable.input_schema.schema() + input_schema = runnable.input_schema.model_json_schema() return f"Takes {input_schema}." @@ -275,7 +275,7 @@ def convert_runnable_to_tool( description = description or _get_description_from_runnable(runnable) name = name or runnable.get_name() - schema = runnable.input_schema.schema() + schema = runnable.input_schema.model_json_schema() if schema.get("type") == "string": return Tool( name=name, diff --git a/libs/core/langchain_core/tools/simple.py b/libs/core/langchain_core/tools/simple.py index b80b264ea590e..94be27bb895f7 100644 --- a/libs/core/langchain_core/tools/simple.py +++ b/libs/core/langchain_core/tools/simple.py @@ -60,7 +60,7 @@ def args(self) -> dict: The input arguments for the tool. """ if self.args_schema is not None: - return self.args_schema.schema()["properties"] + return self.args_schema.model_json_schema()["properties"] # For backwards compatibility, if the function signature is ambiguous, # assume it takes a single string input. return {"tool_input": {"type": "string"}} diff --git a/libs/core/langchain_core/tools/structured.py b/libs/core/langchain_core/tools/structured.py index 96d6e4ca494cb..248678d380162 100644 --- a/libs/core/langchain_core/tools/structured.py +++ b/libs/core/langchain_core/tools/structured.py @@ -65,7 +65,7 @@ async def ainvoke( @property def args(self) -> dict: """The tool's input arguments.""" - return self.args_schema.schema()["properties"] + return self.args_schema.model_json_schema()["properties"] def _run( self, diff --git a/libs/core/langchain_core/utils/function_calling.py b/libs/core/langchain_core/utils/function_calling.py index f94dfca75c1da..675725367b8c1 100644 --- a/libs/core/langchain_core/utils/function_calling.py +++ b/libs/core/langchain_core/utils/function_calling.py @@ -520,7 +520,7 @@ class Person(BaseModel): # of the pydantic model. This is implicit in the API right now, # and will be improved over time. "name": tool_call.__class__.__name__, - "arguments": tool_call.json(), + "arguments": tool_call.model_dump_json(), }, } ) diff --git a/libs/core/tests/unit_tests/output_parsers/test_pydantic_parser.py b/libs/core/tests/unit_tests/output_parsers/test_pydantic_parser.py index bdf8799dc145e..6a7adff650b12 100644 --- a/libs/core/tests/unit_tests/output_parsers/test_pydantic_parser.py +++ b/libs/core/tests/unit_tests/output_parsers/test_pydantic_parser.py @@ -174,7 +174,7 @@ class SampleModel(BaseModel): # Ignoring mypy error that appears in python 3.8, but not 3.11. # This seems to be functionally correct, so we'll ignore the error. pydantic_parser = PydanticOutputParser(pydantic_object=SampleModel) # type: ignore - schema = pydantic_parser.get_output_schema().schema() + schema = pydantic_parser.get_output_schema().model_json_schema() assert schema == { "properties": { diff --git a/libs/core/tests/unit_tests/pydantic_utils.py b/libs/core/tests/unit_tests/pydantic_utils.py index 438eb9d0be11e..05bc658251cf6 100644 --- a/libs/core/tests/unit_tests/pydantic_utils.py +++ b/libs/core/tests/unit_tests/pydantic_utils.py @@ -76,7 +76,7 @@ def _schema(obj: Any) -> dict: f"Object must be a Pydantic BaseModel subclass. Got {type(obj)}" ) if not hasattr(obj, "model_json_schema"): # V1 model - return obj.schema() + return obj.model_json_schema() schema_ = obj.model_json_schema(ref_template="#/definitions/{model}") if "$defs" in schema_: diff --git a/libs/core/tests/unit_tests/runnables/test_graph.py b/libs/core/tests/unit_tests/runnables/test_graph.py index c681db36365ac..f31d7d9299d27 100644 --- a/libs/core/tests/unit_tests/runnables/test_graph.py +++ b/libs/core/tests/unit_tests/runnables/test_graph.py @@ -18,10 +18,10 @@ def test_graph_single_runnable(snapshot: SnapshotAssertion) -> None: graph = StrOutputParser().get_graph() first_node = graph.first_node() assert first_node is not None - assert first_node.data.schema() == runnable.get_input_jsonschema() # type: ignore[union-attr] + assert first_node.data.model_json_schema() == runnable.get_input_jsonschema() # type: ignore[union-attr] last_node = graph.last_node() assert last_node is not None - assert last_node.data.schema() == runnable.get_output_jsonschema() # type: ignore[union-attr] + assert last_node.data.model_json_schema() == runnable.get_output_jsonschema() # type: ignore[union-attr] assert len(graph.nodes) == 3 assert len(graph.edges) == 2 assert graph.edges[0].source == first_node.id diff --git a/libs/core/tests/unit_tests/runnables/test_runnable.py b/libs/core/tests/unit_tests/runnables/test_runnable.py index 2ad80e95a43a7..d6d1a3445bca5 100644 --- a/libs/core/tests/unit_tests/runnables/test_runnable.py +++ b/libs/core/tests/unit_tests/runnables/test_runnable.py @@ -495,7 +495,7 @@ def foo(x: int) -> int: foo_ = RunnableLambda(foo) - assert foo_.assign(bar=lambda x: "foo").get_output_schema().schema() == { + assert foo_.assign(bar=lambda x: "foo").get_output_schema().model_json_schema() == { "properties": {"bar": {"title": "Bar"}, "root": {"title": "Root"}}, "required": ["root", "bar"], "title": "RunnableAssignOutput", diff --git a/libs/core/tests/unit_tests/test_tools.py b/libs/core/tests/unit_tests/test_tools.py index f1afba566aac5..2eff4dd791693 100644 --- a/libs/core/tests/unit_tests/test_tools.py +++ b/libs/core/tests/unit_tests/test_tools.py @@ -1492,7 +1492,7 @@ def _run(self, x: int, y: str) -> Any: return y tool_ = InheritedInjectedArgTool() - assert tool_.get_input_schema().schema() == { + assert tool_.get_input_schema().model_json_schema() == { "title": "fooSchema", "description": "foo.", "type": "object", @@ -1502,7 +1502,7 @@ def _run(self, x: int, y: str) -> Any: }, "required": ["y", "x"], } - assert tool_.tool_call_schema.schema() == { + assert tool_.tool_call_schema.model_json_schema() == { "title": "foo", "description": "foo.", "type": "object", @@ -1646,7 +1646,7 @@ def _run(self, *args: Any, **kwargs: Any) -> str: tool = SomeTool(name="some_tool", description="some description") - assert tool.get_input_schema().schema() == { + assert tool.get_input_schema().model_json_schema() == { "properties": { "a": {"title": "A", "type": "integer"}, "b": {"title": "B", "type": "string"}, @@ -1656,7 +1656,7 @@ def _run(self, *args: Any, **kwargs: Any) -> str: "type": "object", } - assert tool.tool_call_schema.schema() == { + assert tool.tool_call_schema.model_json_schema() == { "description": "some description", "properties": { "a": {"title": "A", "type": "integer"}, @@ -1911,7 +1911,7 @@ def foo(x): # type: ignore[no-untyped-def] """foo""" return x - assert foo.tool_call_schema.schema() == { + assert foo.tool_call_schema.model_json_schema() == { "description": "foo", "properties": { "x": { diff --git a/libs/core/tests/unit_tests/utils/test_function_calling.py b/libs/core/tests/unit_tests/utils/test_function_calling.py index f8a3b50076959..6c8eb773db190 100644 --- a/libs/core/tests/unit_tests/utils/test_function_calling.py +++ b/libs/core/tests/unit_tests/utils/test_function_calling.py @@ -391,7 +391,9 @@ def my_function(arg1: Nested) -> None: assert actual == expected -@pytest.mark.xfail(reason="Pydantic converts Optional[str] to str in .schema()") +@pytest.mark.xfail( + reason="Pydantic converts Optional[str] to str in .model_json_schema()" +) def test_function_optional_param() -> None: @tool def func5(