Skip to content

Commit

Permalink
community[patch]: fix errors introduced by pydantic 2.10 (#28297)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccurme authored Nov 22, 2024
1 parent aa7fa80 commit 203d20c
Show file tree
Hide file tree
Showing 23 changed files with 164 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from typing import List

from langchain_core.caches import BaseCache as BaseCache
from langchain_core.callbacks import Callbacks as Callbacks
from langchain_core.language_models import BaseLanguageModel
from langchain_core.tools import BaseTool
from langchain_core.tools.base import BaseToolkit
Expand Down Expand Up @@ -129,3 +131,6 @@ def get_tools(self) -> List[BaseTool]:
def get_context(self) -> dict:
"""Return db context that you may want in agent prompt."""
return self.db.get_context()


SQLDatabaseToolkit.model_rebuild()
2 changes: 1 addition & 1 deletion libs/community/langchain_community/chat_models/anyscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def lc_secrets(self) -> Dict[str, str]:
def is_lc_serializable(cls) -> bool:
return False

anyscale_api_key: SecretStr = Field(default=None)
anyscale_api_key: SecretStr = Field(default=SecretStr(""))
"""AnyScale Endpoints API keys."""
model_name: str = Field(default=DEFAULT_MODEL, alias="model")
"""Model name to use."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class GPTRouter(BaseChatModel):

client: Any = Field(default=None, exclude=True) #: :meta private:
models_priority_list: List[GPTRouterModel] = Field(min_length=1)
gpt_router_api_base: str = Field(default=None)
gpt_router_api_base: str = Field(default="")
"""WriteSonic GPTRouter custom endpoint"""
gpt_router_api_key: Optional[SecretStr] = None
"""WriteSonic GPTRouter API Key"""
Expand Down
15 changes: 7 additions & 8 deletions libs/community/langchain_community/chat_models/kinetica.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class _KdtSuggestContext(BaseModel):

table: Optional[str] = Field(default=None, title="Name of table")
description: Optional[str] = Field(default=None, title="Table description")
columns: List[str] = Field(default=None, title="Table columns list")
columns: List[str] = Field(default=[], title="Table columns list")
rules: Optional[List[str]] = Field(
default=None, title="Rules that apply to the table."
)
Expand Down Expand Up @@ -121,15 +121,15 @@ class _KdtoSuggestRequest(BaseModel):
class _KdtMessage(BaseModel):
"""pydantic API response type"""

role: str = Field(default=None, title="One of [user|assistant|system]")
role: str = Field(default="", title="One of [user|assistant|system]")
content: str


class _KdtChoice(BaseModel):
"""pydantic API response type"""

index: int
message: _KdtMessage = Field(default=None, title="The generated SQL")
message: Optional[_KdtMessage] = Field(default=None, title="The generated SQL")
finish_reason: str


Expand All @@ -150,7 +150,7 @@ class _KdtSqlResponse(BaseModel):
model: str
choices: List[_KdtChoice]
usage: _KdtUsage
prompt: str = Field(default=None, title="The input question")
prompt: str = Field(default="", title="The input question")


class _KdtCompletionResponse(BaseModel):
Expand Down Expand Up @@ -376,9 +376,8 @@ def _generate(
dict_messages = [self._convert_message_to_dict(m) for m in messages]
sql_response = self._submit_completion(dict_messages)

response_message = sql_response.choices[0].message
# generated_dict = response_message.model_dump() # pydantic v2
generated_dict = response_message.dict()
response_message = cast(_KdtMessage, sql_response.choices[0].message)
generated_dict = response_message.model_dump()

generated_message = self._convert_message_from_dict(generated_dict)

Expand Down Expand Up @@ -539,7 +538,7 @@ class KineticaSqlResponse(BaseModel):
the generated SQL and related Pandas Dataframe fetched from the database.
"""

sql: str = Field(default=None)
sql: str = Field(default="")
"""The generated SQL."""

# dataframe: "pd.DataFrame" = Field(default=None)
Expand Down
16 changes: 9 additions & 7 deletions libs/community/langchain_community/chat_models/naver.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ class ChatClovaX(BaseChatModel):
model.invoke([HumanMessage(content="Come up with 10 names for a song about parrots.")])
""" # noqa: E501

client: httpx.Client = Field(default=None) #: :meta private:
async_client: httpx.AsyncClient = Field(default=None) #: :meta private:
client: Optional[httpx.Client] = Field(default=None) #: :meta private:
async_client: Optional[httpx.AsyncClient] = Field(default=None) #: :meta private:

model_name: str = Field(
default="HCX-003",
Expand All @@ -197,7 +197,7 @@ class ChatClovaX(BaseChatModel):
ncp_apigw_api_key: Optional[SecretStr] = Field(default=None, alias="apigw_api_key")
"""Automatically inferred from env are `NCP_APIGW_API_KEY` if not provided."""

base_url: str = Field(default=None, alias="base_url")
base_url: str = Field(default="", alias="base_url")
"""
Automatically inferred from env are `NCP_CLOVASTUDIO_API_BASE_URL` if not provided.
"""
Expand Down Expand Up @@ -356,11 +356,12 @@ def _completion_with_retry(self, **kwargs: Any) -> Any:
kwargs["stream"] = False

stream = kwargs["stream"]
client = cast(httpx.Client, self.client)
if stream:

def iter_sse() -> Iterator[ServerSentEvent]:
with connect_sse(
self.client, "POST", self._api_url, json=kwargs
client, "POST", self._api_url, json=kwargs
) as event_source:
_raise_on_error(event_source.response)
for sse in event_source.iter_sse():
Expand All @@ -376,7 +377,7 @@ def iter_sse() -> Iterator[ServerSentEvent]:

return iter_sse()
else:
response = self.client.post(url=self._api_url, json=kwargs)
response = client.post(url=self._api_url, json=kwargs)
_raise_on_error(response)
return response.json()

Expand All @@ -395,13 +396,14 @@ async def _completion_with_retry(**kwargs: Any) -> Any:
if "stream" not in kwargs:
kwargs["stream"] = False
stream = kwargs["stream"]
async_client = cast(httpx.AsyncClient, self.async_client)
if stream:
event_source = aconnect_sse(
self.async_client, "POST", self._api_url, json=kwargs
async_client, "POST", self._api_url, json=kwargs
)
return _aiter_sse(event_source)
else:
response = await self.async_client.post(url=self._api_url, json=kwargs)
response = await async_client.post(url=self._api_url, json=kwargs)
await _araise_on_error(response)
return response.json()

Expand Down
2 changes: 1 addition & 1 deletion libs/community/langchain_community/chat_models/octoai.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ChatOctoAI(ChatOpenAI):
"""

octoai_api_base: str = Field(default=DEFAULT_API_BASE)
octoai_api_token: SecretStr = Field(default=None, alias="api_key")
octoai_api_token: SecretStr = Field(default=SecretStr(""), alias="api_key")
model_name: str = Field(default=DEFAULT_MODEL, alias="model")

@property
Expand Down
4 changes: 2 additions & 2 deletions libs/community/langchain_community/chat_models/sambanova.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ class Joke(BaseModel):
sambanova_url: str = Field(default="")
"""SambaNova Cloud Url"""

sambanova_api_key: SecretStr = Field(default="")
sambanova_api_key: SecretStr = Field(default=SecretStr(""))
"""SambaNova Cloud api key"""

model: str = Field(default="Meta-Llama-3.1-8B-Instruct")
Expand Down Expand Up @@ -1072,7 +1072,7 @@ class ChatSambaStudio(BaseChatModel):
sambastudio_url: str = Field(default="")
"""SambaStudio Url"""

sambastudio_api_key: SecretStr = Field(default="")
sambastudio_api_key: SecretStr = Field(default=SecretStr(""))
"""SambaStudio api key"""

base_url: str = Field(default="", exclude=True)
Expand Down
4 changes: 2 additions & 2 deletions libs/community/langchain_community/embeddings/anyscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from typing import Dict
from typing import Dict, Optional

from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init
from pydantic import Field, SecretStr
Expand All @@ -17,7 +17,7 @@
class AnyscaleEmbeddings(OpenAIEmbeddings):
"""`Anyscale` Embeddings API."""

anyscale_api_key: SecretStr = Field(default=None)
anyscale_api_key: Optional[SecretStr] = Field(default=None)
"""AnyScale Endpoints API keys."""
model: str = Field(default=DEFAULT_MODEL)
"""Model name to use."""
Expand Down
14 changes: 8 additions & 6 deletions libs/community/langchain_community/embeddings/naver.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import Any, Dict, List, Optional
from typing import Any, Dict, List, Optional, cast

import httpx
from langchain_core.embeddings import Embeddings
Expand Down Expand Up @@ -60,16 +60,16 @@ class ClovaXEmbeddings(BaseModel, Embeddings):
output = embedding.embed_documents(documents)
""" # noqa: E501

client: httpx.Client = Field(default=None) #: :meta private:
async_client: httpx.AsyncClient = Field(default=None) #: :meta private:
client: Optional[httpx.Client] = Field(default=None) #: :meta private:
async_client: Optional[httpx.AsyncClient] = Field(default=None) #: :meta private:

ncp_clovastudio_api_key: Optional[SecretStr] = Field(default=None, alias="api_key")
"""Automatically inferred from env are `NCP_CLOVASTUDIO_API_KEY` if not provided."""

ncp_apigw_api_key: Optional[SecretStr] = Field(default=None, alias="apigw_api_key")
"""Automatically inferred from env are `NCP_APIGW_API_KEY` if not provided."""

base_url: str = Field(default=None, alias="base_url")
base_url: Optional[str] = Field(default=None, alias="base_url")
"""
Automatically inferred from env are `NCP_CLOVASTUDIO_API_BASE_URL` if not provided.
"""
Expand Down Expand Up @@ -168,13 +168,15 @@ def default_headers(self) -> Dict[str, Any]:

def _embed_text(self, text: str) -> List[float]:
payload = {"text": text}
response = self.client.post(url=self._api_url, json=payload)
client = cast(httpx.Client, self.client)
response = client.post(url=self._api_url, json=payload)
_raise_on_error(response)
return response.json()["result"]["embedding"]

async def _aembed_text(self, text: str) -> List[float]:
payload = {"text": text}
response = await self.async_client.post(url=self._api_url, json=payload)
async_client = cast(httpx.AsyncClient, self.client)
response = await async_client.post(url=self._api_url, json=payload)
await _araise_on_error(response)
return response.json()["result"]["embedding"]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict
from typing import Dict, Optional

from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env, pre_init
from pydantic import Field, SecretStr
Expand All @@ -20,7 +20,7 @@ class OctoAIEmbeddings(OpenAIEmbeddings):
Alternatively, you can use the octoai_api_token keyword argument.
"""

octoai_api_token: SecretStr = Field(default=None)
octoai_api_token: Optional[SecretStr] = Field(default=None)
"""OctoAI Endpoints API keys."""
endpoint_url: str = Field(default=DEFAULT_API_BASE)
"""Base URL path for API requests."""
Expand Down
2 changes: 1 addition & 1 deletion libs/community/langchain_community/llms/anyscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def send_query(llm, text):

"""Key word arguments to pass to the model."""
anyscale_api_base: str = Field(default=DEFAULT_BASE_URL)
anyscale_api_key: SecretStr = Field(default=None)
anyscale_api_key: SecretStr = Field(default=SecretStr(""))
model_name: str = Field(default=DEFAULT_MODEL)

prefix_messages: List = Field(default_factory=list)
Expand Down
4 changes: 2 additions & 2 deletions libs/community/langchain_community/llms/exllamav2.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ExLlamaV2(LLM):
# Langchain parameters
logfunc: Callable = print

stop_sequences: List[str] = Field("")
stop_sequences: List[str] = Field([])
"""Sequences that immediately will stop the generator."""

max_new_tokens: int = Field(150)
Expand All @@ -56,7 +56,7 @@ class ExLlamaV2(LLM):
"""Whether to print debug information."""

# Generator parameters
disallowed_tokens: List[int] = Field(None)
disallowed_tokens: Optional[List[int]] = Field(None)
"""List of tokens to disallow during generation."""

@pre_init
Expand Down
2 changes: 1 addition & 1 deletion libs/community/langchain_community/llms/gooseai.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class GooseAI(LLM):
model_kwargs: Dict[str, Any] = Field(default_factory=dict)
"""Holds any model parameters valid for `create` call not explicitly specified."""

logit_bias: Optional[Dict[str, float]] = Field(default_factory=dict)
logit_bias: Optional[Dict[str, float]] = Field(default_factory=dict) # type: ignore[arg-type]
"""Adjust the probability of specific tokens being generated."""

gooseai_api_key: Optional[SecretStr] = None
Expand Down
2 changes: 1 addition & 1 deletion libs/community/langchain_community/llms/octoai_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class OctoAIEndpoint(BaseOpenAI): # type: ignore[override]

"""Key word arguments to pass to the model."""
octoai_api_base: str = Field(default=DEFAULT_BASE_URL)
octoai_api_token: SecretStr = Field(default=None)
octoai_api_token: SecretStr = Field(default=SecretStr(""))
model_name: str = Field(default=DEFAULT_MODEL)

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion libs/community/langchain_community/llms/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def is_lc_serializable(cls) -> bool:
)
"""Timeout for requests to OpenAI completion API. Can be float, httpx.Timeout or
None."""
logit_bias: Optional[Dict[str, float]] = Field(default_factory=dict)
logit_bias: Optional[Dict[str, float]] = Field(default_factory=dict) # type: ignore[arg-type]
"""Adjust the probability of specific tokens being generated."""
max_retries: int = 2
"""Maximum number of retries to make when generating."""
Expand Down
4 changes: 2 additions & 2 deletions libs/community/langchain_community/llms/sambanova.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class SambaStudio(LLM):
sambastudio_url: str = Field(default="")
"""SambaStudio Url"""

sambastudio_api_key: SecretStr = Field(default="")
sambastudio_api_key: SecretStr = Field(default=SecretStr(""))
"""SambaStudio api key"""

base_url: str = Field(default="", exclude=True)
Expand Down Expand Up @@ -607,7 +607,7 @@ class SambaNovaCloud(LLM):
sambanova_url: str = Field(default="")
"""SambaNova Cloud Url"""

sambanova_api_key: SecretStr = Field(default="")
sambanova_api_key: SecretStr = Field(default=SecretStr(""))
"""SambaNova Cloud api key"""

model: str = Field(default="Meta-Llama-3.1-8B-Instruct")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class EdenaiTool(BaseTool): # type: ignore[override]

feature: str
subfeature: str
edenai_api_key: SecretStr = Field(
edenai_api_key: Optional[SecretStr] = Field(
default_factory=secret_from_env("EDENAI_API_KEY", default=None)
)
is_async: bool = False
Expand All @@ -48,8 +48,9 @@ def _call_eden_ai(self, query_params: Dict[str, Any]) -> str:
requests.Response: The response from the EdenAI API call.
"""
api_key = self.edenai_api_key.get_secret_value() if self.edenai_api_key else ""
headers = {
"Authorization": f"Bearer {self.edenai_api_key.get_secret_value()}",
"Authorization": f"Bearer {api_key}",
"User-Agent": self.get_user_agent(),
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SearchEmailsInput(BaseModel):
"""From https://learn.microsoft.com/en-us/graph/search-query-parameter"""

folder: str = Field(
default=None,
default="",
description=(
" If the user wants to search in only one folder, the name of the folder. "
'Default folders are "inbox", "drafts", "sent items", "deleted ttems", but '
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ 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: SecretStr = Field(
openai_api_key: Optional[SecretStr] = Field(
alias="api_key",
default_factory=secret_from_env(
"OPENAI_API_KEY",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SceneXplainAPIWrapper(BaseModel):
and create a new API key.
"""

scenex_api_key: str = Field(..., default_factory=from_env("SCENEX_API_KEY"))
scenex_api_key: str = Field(..., default_factory=from_env("SCENEX_API_KEY")) # type: ignore[call-overload]
scenex_api_url: str = "https://api.scenex.jina.ai/v1/describe"

def _describe_image(self, image: str) -> str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class RedisVectorField(RedisField):
dims: int = Field(...)
algorithm: object = Field(...)
datatype: str = Field(default="FLOAT32")
distance_metric: RedisDistanceMetric = Field(default="COSINE")
distance_metric: RedisDistanceMetric = Field(default="COSINE") # type: ignore[assignment]
initial_cap: Optional[int] = None

@field_validator("algorithm", "datatype", "distance_metric", mode="before")
Expand Down
Loading

0 comments on commit 203d20c

Please sign in to comment.