langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
95.31k stars 15.46k forks source link

PydanticUndefinedAnnotation: name 'SafetySetting' is not defined using ChatVertexAI #28271

Open k3ybladewielder opened 4 days ago

k3ybladewielder commented 4 days ago

Checked other resources

Example Code


# Ensure your VertexAI credentials are configured

from langchain_google_vertexai import ChatVertexAI

model = ChatVertexAI(model="gemini-1.5-flash")

model.invoke("Hello, world!")

Error Message and Stack Trace (if applicable)

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
File /opt/conda/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py:815, in GenerateSchema._resolve_forward_ref(self, obj)
    814 try:
--> 815     obj = _typing_extra.eval_type_backport(obj, *self._types_namespace)
    816 except NameError as e:

File /opt/conda/lib/python3.10/site-packages/pydantic/_internal/_typing_extra.py:534, in eval_type_backport(value, globalns, localns, type_params)
    533 try:
--> 534     return _eval_type_backport(value, globalns, localns, type_params)
    535 except TypeError as e:

File /opt/conda/lib/python3.10/site-packages/pydantic/_internal/_typing_extra.py:558, in _eval_type_backport(value, globalns, localns, type_params)
    557 try:
--> 558     return _eval_type(value, globalns, localns, type_params)
    559 except TypeError as e:

File /opt/conda/lib/python3.10/site-packages/pydantic/_internal/_typing_extra.py:592, in _eval_type(value, globalns, localns, type_params)
    591 else:
--> 592     return typing._eval_type(  # type: ignore
    593         value, globalns, localns
    594     )

File /opt/conda/lib/python3.10/typing.py:327, in _eval_type(t, globalns, localns, recursive_guard)
    326 if isinstance(t, ForwardRef):
--> 327     return t._evaluate(globalns, localns, recursive_guard)
    328 if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):

File /opt/conda/lib/python3.10/typing.py:699, in ForwardRef._evaluate(self, globalns, localns, recursive_guard)
    693 type_ = _type_check(
    694     eval(self.__forward_code__, globalns, localns),
    695     "Forward references must evaluate to types.",
    696     is_argument=self.__forward_is_argument__,
    697     allow_special_forms=self.__forward_is_class__,
    698 )
--> 699 self.__forward_value__ = _eval_type(
    700     type_, globalns, localns, recursive_guard | {self.__forward_arg__}
    701 )
    702 self.__forward_evaluated__ = True

File /opt/conda/lib/python3.10/typing.py:329, in _eval_type(t, globalns, localns, recursive_guard)
    328 if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):
--> 329     ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
    330     if ev_args == t.__args__:

File /opt/conda/lib/python3.10/typing.py:329, in <genexpr>(.0)
    328 if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):
--> 329     ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
    330     if ev_args == t.__args__:

File /opt/conda/lib/python3.10/typing.py:327, in _eval_type(t, globalns, localns, recursive_guard)
    326 if isinstance(t, ForwardRef):
--> 327     return t._evaluate(globalns, localns, recursive_guard)
    328 if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):

File /opt/conda/lib/python3.10/typing.py:699, in ForwardRef._evaluate(self, globalns, localns, recursive_guard)
    693 type_ = _type_check(
    694     eval(self.__forward_code__, globalns, localns),
    695     "Forward references must evaluate to types.",
    696     is_argument=self.__forward_is_argument__,
    697     allow_special_forms=self.__forward_is_class__,
    698 )
--> 699 self.__forward_value__ = _eval_type(
    700     type_, globalns, localns, recursive_guard | {self.__forward_arg__}
    701 )
    702 self.__forward_evaluated__ = True

File /opt/conda/lib/python3.10/typing.py:329, in _eval_type(t, globalns, localns, recursive_guard)
    328 if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):
--> 329     ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
    330     if ev_args == t.__args__:

File /opt/conda/lib/python3.10/typing.py:329, in <genexpr>(.0)
    328 if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):
--> 329     ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
    330     if ev_args == t.__args__:

File /opt/conda/lib/python3.10/typing.py:329, in _eval_type(t, globalns, localns, recursive_guard)
    328 if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):
--> 329     ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
    330     if ev_args == t.__args__:

File /opt/conda/lib/python3.10/typing.py:329, in <genexpr>(.0)
    328 if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):
--> 329     ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
    330     if ev_args == t.__args__:

File /opt/conda/lib/python3.10/typing.py:327, in _eval_type(t, globalns, localns, recursive_guard)
    326 if isinstance(t, ForwardRef):
--> 327     return t._evaluate(globalns, localns, recursive_guard)
    328 if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):

File /opt/conda/lib/python3.10/typing.py:694, in ForwardRef._evaluate(self, globalns, localns, recursive_guard)
    690     globalns = getattr(
    691         sys.modules.get(self.__forward_module__, None), '__dict__', globalns
    692     )
    693 type_ = _type_check(
--> 694     eval(self.__forward_code__, globalns, localns),
    695     "Forward references must evaluate to types.",
    696     is_argument=self.__forward_is_argument__,
    697     allow_special_forms=self.__forward_is_class__,
    698 )
    699 self.__forward_value__ = _eval_type(
    700     type_, globalns, localns, recursive_guard | {self.__forward_arg__}
    701 )

File <string>:1

NameError: name 'SafetySetting' is not defined

The above exception was the direct cause of the following exception:

PydanticUndefinedAnnotation               Traceback (most recent call last)
Cell In[15], line 1
----> 1 from langchain_google_vertexai import ChatVertexAI
      3 model = ChatVertexAI(model="gemini-1.5-flash")
      5 model.invoke("Hello, world!")

File ~/.local/lib/python3.10/site-packages/langchain_google_vertexai/__init__.py:16
     14 from langchain_google_vertexai.chains import create_structured_runnable
     15 from langchain_google_vertexai.chat_models import ChatVertexAI
---> 16 from langchain_google_vertexai.embeddings import VertexAIEmbeddings
     17 from langchain_google_vertexai.evaluators.evaluation import (
     18     VertexPairWiseStringEvaluator,
     19     VertexStringEvaluator,
     20 )
     21 from langchain_google_vertexai.functions_utils import (
     22     PydanticFunctionsOutputParser,
     23 )

File ~/.local/lib/python3.10/site-packages/langchain_google_vertexai/embeddings.py:544
    540             embeddings.append(result.image_embedding)
    541         return embeddings
--> 544 VertexAIEmbeddings.model_rebuild()

File /opt/conda/lib/python3.10/site-packages/pydantic/main.py:589, in BaseModel.model_rebuild(cls, force, raise_errors, _parent_namespace_depth, _types_namespace)
    587 # manually override defer_build so complete_model_class doesn't skip building the model again
    588 config = {**cls.model_config, 'defer_build': False}
--> 589 return _model_construction.complete_model_class(
    590     cls,
    591     cls.__name__,
    592     _config.ConfigWrapper(config, check=False),
    593     raise_errors=raise_errors,
    594     ns_resolver=ns_resolver,
    595 )

File /opt/conda/lib/python3.10/site-packages/pydantic/_internal/_model_construction.py:658, in complete_model_class(cls, cls_name, config_wrapper, raise_errors, ns_resolver, create_model_module)
    651 handler = CallbackGetCoreSchemaHandler(
    652     partial(gen_schema.generate_schema, from_dunder_get_core_schema=False),
    653     gen_schema,
    654     ref_mode='unpack',
    655 )
    657 try:
--> 658     schema = cls.__get_pydantic_core_schema__(cls, handler)
    659 except PydanticUndefinedAnnotation as e:
    660     if raise_errors:

File /opt/conda/lib/python3.10/site-packages/pydantic/main.py:697, in BaseModel.__get_pydantic_core_schema__(cls, source, handler)
    694     if not cls.__pydantic_generic_metadata__['origin']:
    695         return cls.__pydantic_core_schema__
--> 697 return handler(source)

File /opt/conda/lib/python3.10/site-packages/pydantic/_internal/_schema_generation_shared.py:84, in CallbackGetCoreSchemaHandler.__call__(self, source_type)
     83 def __call__(self, source_type: Any, /) -> core_schema.CoreSchema:
---> 84     schema = self._handler(source_type)
     85     ref = schema.get('ref')
     86     if self._ref_mode == 'to-def':

File /opt/conda/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py:612, in GenerateSchema.generate_schema(self, obj, from_dunder_get_core_schema)
    609         schema = from_property
    611 if schema is None:
--> 612     schema = self._generate_schema_inner(obj)
    614 metadata_js_function = _extract_get_pydantic_json_schema(obj, schema)
    615 if metadata_js_function is not None:

File /opt/conda/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py:881, in GenerateSchema._generate_schema_inner(self, obj)
    879 if lenient_issubclass(obj, BaseModel):
    880     with self.model_type_stack.push(obj):
--> 881         return self._model_schema(obj)
    883 if isinstance(obj, PydanticRecursiveRef):
    884     return core_schema.definition_reference_schema(schema_ref=obj.type_ref)

File /opt/conda/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py:693, in GenerateSchema._model_schema(self, cls)
    681     model_schema = core_schema.model_schema(
    682         cls,
    683         inner_schema,
   (...)
    689         ref=model_ref,
    690     )
    691 else:
    692     fields_schema: core_schema.CoreSchema = core_schema.model_fields_schema(
--> 693         {k: self._generate_md_field_schema(k, v, decorators) for k, v in fields.items()},
    694         computed_fields=[
    695             self._computed_field_schema(d, decorators.field_serializers)
    696             for d in computed_fields.values()
    697         ],
    698         extras_schema=extras_schema,
    699         model_name=cls.__name__,
    700     )
    701     inner_schema = apply_validators(fields_schema, decorators.root_validators.values(), None)
    702     new_inner_schema = define_expected_missing_refs(inner_schema, recursively_defined_type_refs())

File /opt/conda/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py:693, in <dictcomp>(.0)
    681     model_schema = core_schema.model_schema(
    682         cls,
    683         inner_schema,
   (...)
    689         ref=model_ref,
    690     )
    691 else:
    692     fields_schema: core_schema.CoreSchema = core_schema.model_fields_schema(
--> 693         {k: self._generate_md_field_schema(k, v, decorators) for k, v in fields.items()},
    694         computed_fields=[
    695             self._computed_field_schema(d, decorators.field_serializers)
    696             for d in computed_fields.values()
    697         ],
    698         extras_schema=extras_schema,
    699         model_name=cls.__name__,
    700     )
    701     inner_schema = apply_validators(fields_schema, decorators.root_validators.values(), None)
    702     new_inner_schema = define_expected_missing_refs(inner_schema, recursively_defined_type_refs())

File /opt/conda/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py:1073, in GenerateSchema._generate_md_field_schema(self, name, field_info, decorators)
   1066 def _generate_md_field_schema(
   1067     self,
   1068     name: str,
   1069     field_info: FieldInfo,
   1070     decorators: DecoratorInfos,
   1071 ) -> core_schema.ModelField:
   1072     """Prepare a ModelField to represent a model field."""
-> 1073     common_field = self._common_field_schema(name, field_info, decorators)
   1074     return core_schema.model_field(
   1075         common_field['schema'],
   1076         serialization_exclude=common_field['serialization_exclude'],
   (...)
   1080         metadata=common_field['metadata'],
   1081     )

File /opt/conda/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py:1261, in GenerateSchema._common_field_schema(self, name, field_info, decorators)
   1257         schema = self._apply_annotations(
   1258             source_type, annotations + validators_from_decorators, transform_inner_schema=set_discriminator
   1259         )
   1260     else:
-> 1261         schema = self._apply_annotations(
   1262             source_type,
   1263             annotations + validators_from_decorators,
   1264         )
   1266 # This V1 compatibility shim should eventually be removed
   1267 # push down any `each_item=True` validators
   1268 # note that this won't work for any Annotated types that get wrapped by a function validator
   1269 # but that's okay because that didn't exist in V1
   1270 this_field_validators = filter_field_decorator_info_by_field(decorators.validators.values(), name)

File /opt/conda/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py:2051, in GenerateSchema._apply_annotations(self, source_type, annotations, transform_inner_schema)
   2046         continue
   2047     get_inner_schema = self._get_wrapped_inner_schema(
   2048         get_inner_schema, annotation, pydantic_js_annotation_functions
   2049     )
-> 2051 schema = get_inner_schema(source_type)
   2052 if pydantic_js_annotation_functions:
   2053     core_metadata = schema.setdefault('metadata', {})

File /opt/conda/lib/python3.10/site-packages/pydantic/_internal/_schema_generation_shared.py:84, in CallbackGetCoreSchemaHandler.__call__(self, source_type)
     83 def __call__(self, source_type: Any, /) -> core_schema.CoreSchema:
---> 84     schema = self._handler(source_type)
     85     ref = schema.get('ref')
     86     if self._ref_mode == 'to-def':

File /opt/conda/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py:2032, in GenerateSchema._apply_annotations.<locals>.inner_handler(obj)
   2030 from_property = self._generate_schema_from_property(obj, source_type)
   2031 if from_property is None:
-> 2032     schema = self._generate_schema_inner(obj)
   2033 else:
   2034     schema = from_property

File /opt/conda/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py:875, in GenerateSchema._generate_schema_inner(self, obj)
    872     obj = ForwardRef(obj)
    874 if isinstance(obj, ForwardRef):
--> 875     return self.generate_schema(self._resolve_forward_ref(obj))
    877 BaseModel = import_cached_base_model()
    879 if lenient_issubclass(obj, BaseModel):

File /opt/conda/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py:817, in GenerateSchema._resolve_forward_ref(self, obj)
    815     obj = _typing_extra.eval_type_backport(obj, *self._types_namespace)
    816 except NameError as e:
--> 817     raise PydanticUndefinedAnnotation.from_name_error(e) from e
    819 # if obj is still a ForwardRef, it means we can't evaluate it, raise PydanticUndefinedAnnotation
    820 if isinstance(obj, ForwardRef):

PydanticUndefinedAnnotation: name 'SafetySetting' is not defined

For further information visit https://errors.pydantic.dev/2.10/u/undefined-annotation

Description

Using the exact example from documentation the following error occur

System Info

System Information

OS: Linux OS Version: #1 SMP Debian 5.10.223-1 (2024-08-10) Python Version: 3.10.15 | packaged by conda-forge | (main, Sep 20 2024, 16:37:05) [GCC 13.3.0]

Package Information

langchain_core: 0.3.19 langchain: 0.3.7 langchain_community: 0.3.5 langsmith: 0.1.144 langchain_google_genai: 2.0.5 langchain_google_vertexai: 2.0.7 langchain_openai: 0.2.5 langchain_text_splitters: 0.3.2

Optional packages not installed

langgraph langserve

Other Dependencies

aiohttp: 3.10.5 anthropic[vertexai]: Installed. No version info available. async-timeout: 4.0.3 dataclasses-json: 0.6.7 google-cloud-aiplatform: 1.73.0 google-cloud-storage: 2.18.2 google-generativeai: 0.8.3 httpx: 0.27.2 httpx-sse: 0.4.0 jsonpatch: 1.33 langchain-mistralai: Installed. No version info available. numpy: 1.25.2 openai: 1.55.0 orjson: 3.10.11 packaging: 24.1 pydantic: 2.10.0 pydantic-settings: 2.6.1 PyYAML: 6.0.2 requests: 2.32.3 requests-toolbelt: 1.0.0 SQLAlchemy: 2.0.35 tenacity: 9.0.0 tiktoken: 0.8.0 typing-extensions: 4.12.2

OlaPietka commented 3 days ago

I have the same issue. No resolution whatsoever.

twiedijk commented 3 days ago

It can be resolved by downgrading to pydantic==2.9.0

Luc2357 commented 3 days ago

See https://github.com/langchain-ai/langchain-google/issues/610

andrewginns commented 3 days ago

I am also having this problem, no changes in pinned dependencies but suddenly my container is failing.

I can replicate this consistenly on both langchain-google-vertexai==2.0.5 and langchain-google-vertexai==2.0.7, I've not tested other versions.

This minimal Dockerfile demonstrates the issue:

FROM python:3.11-slim

WORKDIR /app

# Install Poetry
RUN pip install langchain-google-vertexai==2.0.7

CMD ["python", "-c", "from langchain_google_vertexai import ChatVertexAI"]
k3ybladewielder commented 3 days ago

I encountered the same issue and resolved it by downgrading to pydantic==2.9.0

See langchain-ai/langchain-google#610

Thanks for the answer @twiedijk @Luc2357. I tried and works well. I'll wait for the bug fix in the meantime

Viicos commented 3 days ago

See https://github.com/langchain-ai/langchain-google/issues/610#issuecomment-2493831993 for more details. TL;DR:

Upgrade back to 2.9 to avoid the issue. But do not that the 2.10 release revealed what could be a genuine bug in the langchain-google library, as the field annotations are not properly resolved to the correct type.

daniel-deychakiwsky commented 3 days ago

@k3ybladewielder @Viicos - we experienced the same issue. Would you happen to the have the bug link open within the langchain-google? I notcied https://github.com/langchain-ai/langchain-google/issues/610 is closed.

Viicos commented 3 days ago

The original author closed it but it was mentioned that it should be kept open. I do not maintain the library so we'll have to wait for them to reopen it again. In the meanwhile you can probably subscribe to the closed issue. I'm currently working on a fix.

DanielAvdar commented 1 day ago

same