-
Notifications
You must be signed in to change notification settings - Fork 165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Import of ChatVertexAI fails with "name 'SafetySetting' is not defined" #610
Comments
same here. Here is my stack trace when running unit tests on CI:
|
Our docker:
|
As a workaround setting pydantic version to 2.9.0 seems to work. 2.10.0 seems to break backwards compatibility somehow. We have pydantic version bounded between 2 and 3 so new install might be pulling the new version. This works for me
|
We also received the same error. Weirdly enough not on our local tests but in the CI/CD pipelines tests. virtualenv .venv
source .venv/bin/activate
pip install langchain-google-vertexai
pip install pydantic==2.9.1
python -c "from langchain_google_vertexai import ChatVertexAI" But It still doesn't work. |
It fails with the same error? |
|
Can you |
[tool.poetry.dependencies] [tool.poetry.group.dev.dependencies] [tool.poetry.group.test.dependencies] [build-system] This is our toml files dependancies |
You have pydantic ^2.9.1, this can be installing 2.10.0 where the problem seems to be. Fix this version to 2.9.2 and try again |
FYI: The workaround works in our case both on AWS lambda & locally |
Thank you @jzaldi, pinning the pydantic version works! |
I don't think this ticket should be closed already, please reopen Of course everyone can pin their pydantic to <2.10, but some more things need to happen IMO.
|
In Pydantic 2.10, we introduced a complete refactor of the forward annotations evaluation. As a reminder, a forward annotation is a type hint surrounded by quotes: class Model(BaseModel):
a: 'MyInt'
# Or, if the `from __future__ import annotations` is included:
b: MyInt # the import will automatically surround the annotation with quotes
MyInt = int # defined _after_ `Model` Because Pydantic uses type hints to build the model schema, it needs to evaluate these annotations, which can be really challenging. Debugging issues related to forward annotations (like this one) is also complicated. Explanation of what should happenThe 2.10 refactor revealed what seems to be a genuine bug in this library. The langchain-google/libs/vertexai/langchain_google_vertexai/embeddings.py Lines 103 to 112 in 0b5d16a
langchain-google/libs/vertexai/langchain_google_vertexai/_base.py Lines 176 to 197 in 0b5d16a
SafetySettingsType = Union[
List["SafetySetting"],
Dict[
gapic_content_types.HarmCategory,
gapic_content_types.SafetySetting.HarmBlockThreshold,
],
]
class SafetySetting:
"""Parameters for the generation."""
HarmCategory = gapic_content_types.HarmCategory
HarmBlockMethod = gapic_content_types.SafetySetting.HarmBlockMethod
HarmBlockThreshold = gapic_content_types.SafetySetting.HarmBlockThreshold
... So if everything is working as expected, the class _VertexAICommon(_VertexAIBase):
# other fields defined ...
...
safety_settings: Optional[
Union[
List[vertexai.generative_models._generative_models.SafetySetting],
Dict[
gapic_content_types.HarmCategory,
gapic_content_types.SafetySetting.HarmBlockThreshold,
],
]
] What really happens in Pydantic 2.9However, in Pydantic 2.9, # pydantic==2.9.2
from langchain_google_vertexai.embeddings import VertexAIEmbeddings
VertexAIEmbeddings.model_fields['safety_settings'].annotation
>>> Optional[
>>> Union[
>>> List[google.cloud.aiplatform_v1beta1.types.content.SafetySetting], # different type!
>>> Dict[
>>> gapic_content_types.HarmCategory,
>>> gapic_content_types.SafetySetting.HarmBlockThreshold,
>>> ],
>>> ]
>>> ] The reason we get a different type for Now two questions can be raised from this:
# module1.py
from typing import List
Alias = List['SomeType']
SomeType = int
# module2.py
from module1 import Alias
from pydantic import BaseModel
class Model(BaseModel):
a: Alias
# Model failed to build (calling `model_rebuild()` will raise) Here, A workaround is to either avoid using forward references in such aliases (not always possible), or to use the new Alias = TypeAliasType('Alias', List['SomeType'])
# or, when 3.12 becomes the lowest supported version:
type Alias = List[SomeType] Why the wrong type was usedThis goes deeper in the runtime implementation of the typing constructs. When defining such an alias: Alias = List['SomeRef'] Any argument passed to the This means that Coming back to our Later on, Footnotes
|
Thank you for the detailed explanation, created a new issue for it in #613 |
Upon creation of a new virtual environment, the import of the ChatVertexAI now fails with "'SafetySetting' is not defined"
Steps to reproduce:
Error reported:
Full stacktrace
The text was updated successfully, but these errors were encountered: