community[patch]: update @root_validator in utilities namespace (#23768)

Update all utilities to use `pre=True` or `pre=False`

https://github.com/langchain-ai/langchain/issues/22819
This commit is contained in:
Eugene Yurtsev 2024-07-02 10:33:01 -04:00 committed by GitHub
parent ee5eedfa04
commit 338cef35b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
35 changed files with 38 additions and 37 deletions

View File

@ -40,7 +40,7 @@ class BibtexparserWrapper(BaseModel):
extra = Extra.forbid extra = Extra.forbid
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that the python package exists in environment.""" """Validate that the python package exists in environment."""
try: try:

View File

@ -482,7 +482,7 @@ class Table(BaseModel):
class Config: class Config:
frozen = True frozen = True
@root_validator() @root_validator(pre=False, skip_on_failure=True)
def check_required_fields(cls, class_values: dict) -> dict: def check_required_fields(cls, class_values: dict) -> dict:
if not class_values["columns"]: if not class_values["columns"]:
raise ValueError("non-empty column list for must be provided") raise ValueError("non-empty column list for must be provided")

View File

@ -323,7 +323,7 @@ class ClickupAPIWrapper(BaseModel):
return data["access_token"] return data["access_token"]
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment.""" """Validate that api key and python package exists in environment."""
values["access_token"] = get_from_dict_or_env( values["access_token"] = get_from_dict_or_env(

View File

@ -89,7 +89,7 @@ class DallEAPIWrapper(BaseModel):
values["model_kwargs"] = extra values["model_kwargs"] = extra
return values return values
@root_validator() @root_validator(pre=False, skip_on_failure=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment.""" """Validate that api key and python package exists in environment."""
values["openai_api_key"] = get_from_dict_or_env( values["openai_api_key"] = get_from_dict_or_env(

View File

@ -42,7 +42,7 @@ class DataForSeoAPIWrapper(BaseModel):
aiosession: Optional[aiohttp.ClientSession] = None aiosession: Optional[aiohttp.ClientSession] = None
"""The aiohttp session to use for the DataForSEO SERP API.""" """The aiohttp session to use for the DataForSEO SERP API."""
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that login and password exists in environment.""" """Validate that login and password exists in environment."""
login = get_from_dict_or_env(values, "api_login", "DATAFORSEO_LOGIN") login = get_from_dict_or_env(values, "api_login", "DATAFORSEO_LOGIN")

View File

@ -26,7 +26,7 @@ class DataheraldAPIWrapper(BaseModel):
extra = Extra.forbid extra = Extra.forbid
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment.""" """Validate that api key and python package exists in environment."""
dataherald_api_key = get_from_dict_or_env( dataherald_api_key = get_from_dict_or_env(

View File

@ -41,7 +41,7 @@ class DuckDuckGoSearchAPIWrapper(BaseModel):
extra = Extra.forbid extra = Extra.forbid
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that python package exists in environment.""" """Validate that python package exists in environment."""
try: try:

View File

@ -42,7 +42,7 @@ class GitHubAPIWrapper(BaseModel):
extra = Extra.forbid extra = Extra.forbid
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment.""" """Validate that api key and python package exists in environment."""
github_repository = get_from_dict_or_env( github_repository = get_from_dict_or_env(

View File

@ -34,7 +34,7 @@ class GitLabAPIWrapper(BaseModel):
extra = Extra.forbid extra = Extra.forbid
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment.""" """Validate that api key and python package exists in environment."""

View File

@ -28,7 +28,7 @@ class GoldenQueryAPIWrapper(BaseModel):
extra = Extra.forbid extra = Extra.forbid
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment.""" """Validate that api key and python package exists in environment."""
golden_api_key = get_from_dict_or_env( golden_api_key = get_from_dict_or_env(

View File

@ -29,7 +29,7 @@ class GoogleFinanceAPIWrapper(BaseModel):
extra = Extra.forbid extra = Extra.forbid
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment.""" """Validate that api key and python package exists in environment."""
values["serp_api_key"] = convert_to_secret_str( values["serp_api_key"] = convert_to_secret_str(

View File

@ -29,7 +29,7 @@ class GoogleJobsAPIWrapper(BaseModel):
extra = Extra.forbid extra = Extra.forbid
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment.""" """Validate that api key and python package exists in environment."""
values["serp_api_key"] = convert_to_secret_str( values["serp_api_key"] = convert_to_secret_str(

View File

@ -35,7 +35,7 @@ class GoogleLensAPIWrapper(BaseModel):
extra = Extra.forbid extra = Extra.forbid
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment.""" """Validate that api key and python package exists in environment."""
values["serp_api_key"] = convert_to_secret_str( values["serp_api_key"] = convert_to_secret_str(

View File

@ -43,7 +43,7 @@ class GooglePlacesAPIWrapper(BaseModel):
extra = Extra.forbid extra = Extra.forbid
arbitrary_types_allowed = True arbitrary_types_allowed = True
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key is in your environment variable.""" """Validate that api key is in your environment variable."""
gplaces_api_key = get_from_dict_or_env( gplaces_api_key = get_from_dict_or_env(

View File

@ -50,7 +50,7 @@ class GoogleScholarAPIWrapper(BaseModel):
extra = Extra.forbid extra = Extra.forbid
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment.""" """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") serp_api_key = get_from_dict_or_env(values, "serp_api_key", "SERP_API_KEY")

View File

@ -69,7 +69,7 @@ class GoogleSearchAPIWrapper(BaseModel):
res = cse.list(q=search_term, cx=self.google_cse_id, **kwargs).execute() res = cse.list(q=search_term, cx=self.google_cse_id, **kwargs).execute()
return res.get("items", []) return res.get("items", [])
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment.""" """Validate that api key and python package exists in environment."""
google_api_key = get_from_dict_or_env( google_api_key = get_from_dict_or_env(

View File

@ -46,7 +46,7 @@ class GoogleSerperAPIWrapper(BaseModel):
arbitrary_types_allowed = True arbitrary_types_allowed = True
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key exists in environment.""" """Validate that api key exists in environment."""
serper_api_key = get_from_dict_or_env( serper_api_key = get_from_dict_or_env(

View File

@ -33,7 +33,7 @@ class GoogleTrendsAPIWrapper(BaseModel):
extra = Extra.forbid extra = Extra.forbid
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment.""" """Validate that api key and python package exists in environment."""
values["serp_api_key"] = convert_to_secret_str( values["serp_api_key"] = convert_to_secret_str(

View File

@ -21,7 +21,7 @@ class JiraAPIWrapper(BaseModel):
extra = Extra.forbid extra = Extra.forbid
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment.""" """Validate that api key and python package exists in environment."""
jira_username = get_from_dict_or_env( jira_username = get_from_dict_or_env(

View File

@ -32,7 +32,7 @@ class MerriamWebsterAPIWrapper(BaseModel):
extra = Extra.forbid extra = Extra.forbid
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key exists in environment.""" """Validate that api key exists in environment."""
merriam_webster_api_key = get_from_dict_or_env( merriam_webster_api_key = get_from_dict_or_env(

View File

@ -27,7 +27,7 @@ class OutlineAPIWrapper(BaseModel):
outline_api_key: Optional[str] = None outline_api_key: Optional[str] = None
outline_search_endpoint: str = "/api/documents.search" outline_search_endpoint: str = "/api/documents.search"
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that instance url and api key exists in environment.""" """Validate that instance url and api key exists in environment."""
outline_instance_url = get_from_dict_or_env( outline_instance_url = get_from_dict_or_env(

View File

@ -17,7 +17,7 @@ class PolygonAPIWrapper(BaseModel):
polygon_api_key: Optional[str] = None polygon_api_key: Optional[str] = None
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key in environment.""" """Validate that api key in environment."""
polygon_api_key = get_from_dict_or_env( polygon_api_key = get_from_dict_or_env(

View File

@ -48,7 +48,7 @@ class PubMedAPIWrapper(BaseModel):
doc_content_chars_max: int = 2000 doc_content_chars_max: int = 2000
email: str = "your_email@example.com" email: str = "your_email@example.com"
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that the python package exists in environment.""" """Validate that the python package exists in environment."""
try: try:

View File

@ -30,7 +30,7 @@ class RedditSearchAPIWrapper(BaseModel):
reddit_client_secret: Optional[str] reddit_client_secret: Optional[str]
reddit_user_agent: Optional[str] reddit_user_agent: Optional[str]
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that the API ID, secret and user agent exists in environment """Validate that the API ID, secret and user agent exists in environment
and check that praw module is present. and check that praw module is present.

View File

@ -13,7 +13,7 @@ class RememberizerAPIWrapper(BaseModel):
top_k_results: int = 10 top_k_results: int = 10
rememberizer_api_key: Optional[str] = None rememberizer_api_key: Optional[str] = None
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key in environment.""" """Validate that api key in environment."""
rememberizer_api_key = get_from_dict_or_env( rememberizer_api_key = get_from_dict_or_env(

View File

@ -32,7 +32,7 @@ class SearchApiAPIWrapper(BaseModel):
arbitrary_types_allowed = True arbitrary_types_allowed = True
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that API key exists in environment.""" """Validate that API key exists in environment."""
searchapi_api_key = get_from_dict_or_env( searchapi_api_key = get_from_dict_or_env(

View File

@ -229,10 +229,10 @@ class SearxSearchWrapper(BaseModel):
return v return v
@root_validator() @root_validator(pre=True)
def validate_params(cls, values: Dict) -> Dict: def validate_params(cls, values: Dict) -> Dict:
"""Validate that custom searx params are merged with default ones.""" """Validate that custom searx params are merged with default ones."""
user_params = values["params"] user_params = values.get("params", {})
default = _get_default_params() default = _get_default_params()
values["params"] = {**default, **user_params} values["params"] = {**default, **user_params}

View File

@ -50,7 +50,7 @@ class SemanticScholarAPIWrapper(BaseModel):
"externalIds", "externalIds",
] ]
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that the python package exists in environment.""" """Validate that the python package exists in environment."""
try: try:

View File

@ -57,7 +57,7 @@ class SerpAPIWrapper(BaseModel):
extra = Extra.forbid extra = Extra.forbid
arbitrary_types_allowed = True arbitrary_types_allowed = True
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment.""" """Validate that api key and python package exists in environment."""
serpapi_api_key = get_from_dict_or_env( serpapi_api_key = get_from_dict_or_env(

View File

@ -19,7 +19,7 @@ class StackExchangeAPIWrapper(BaseModel):
result_separator: str = "\n\n" result_separator: str = "\n\n"
"""Separator between question,answer pairs.""" """Separator between question,answer pairs."""
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that the required Python package exists.""" """Validate that the required Python package exists."""
try: try:

View File

@ -60,7 +60,7 @@ class TensorflowDatasets(BaseModel):
sample_to_document_function: Optional[Callable[[Dict], Document]] = None sample_to_document_function: Optional[Callable[[Dict], Document]] = None
dataset: Any #: :meta private: dataset: Any #: :meta private:
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that the python package exists in environment.""" """Validate that the python package exists in environment."""
try: try:

View File

@ -48,7 +48,7 @@ class TwilioAPIWrapper(BaseModel):
extra = Extra.forbid extra = Extra.forbid
arbitrary_types_allowed = False arbitrary_types_allowed = False
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment.""" """Validate that api key and python package exists in environment."""
try: try:

View File

@ -92,7 +92,7 @@ class WikidataAPIWrapper(BaseModel):
wikidata_props: List[str] = DEFAULT_PROPERTIES wikidata_props: List[str] = DEFAULT_PROPERTIES
lang: str = DEFAULT_LANG_CODE lang: str = DEFAULT_LANG_CODE
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that the python package exists in environment.""" """Validate that the python package exists in environment."""
try: try:

View File

@ -26,13 +26,14 @@ class WikipediaAPIWrapper(BaseModel):
load_all_available_meta: bool = False load_all_available_meta: bool = False
doc_content_chars_max: int = 4000 doc_content_chars_max: int = 4000
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that the python package exists in environment.""" """Validate that the python package exists in environment."""
try: try:
import wikipedia import wikipedia
wikipedia.set_lang(values["lang"]) lang = values.get("lang", "en")
wikipedia.set_lang(lang)
values["wiki_client"] = wikipedia values["wiki_client"] = wikipedia
except ImportError: except ImportError:
raise ImportError( raise ImportError(

View File

@ -25,7 +25,7 @@ class WolframAlphaAPIWrapper(BaseModel):
extra = Extra.forbid extra = Extra.forbid
@root_validator() @root_validator(pre=True)
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment.""" """Validate that api key and python package exists in environment."""
wolfram_alpha_appid = get_from_dict_or_env( wolfram_alpha_appid = get_from_dict_or_env(