Before this PR, subclasses of _VertexAIBase could have problems setting a default value (or default_factory) for the project attribute. For example,
import pydantic as pd
from langchain_core.utils import from_env
from langchain_google_vertexai.chat_models import ChatVertexAI
class MyVertexChat(ChatVertexAI):
project: pd.Field(default_factory=from_env("MY_GOOGLE_PROJ"))
>>> MyVertexChat()
...
GoogleAuthError: Unable to find your project. Please provide a project ID by: ...
This is because not passing a project to the constructor means that values["project"] is unset in the before model validator, triggering the project lookup in the global config (which can fail).
PR Description
Before this PR, subclasses of
_VertexAIBase
could have problems setting a default value (ordefault_factory
) for theproject
attribute. For example,This is because not passing a
project
to the constructor means thatvalues["project"]
is unset in thebefore
model validator, triggering the project lookup in the global config (which can fail).https://github.com/langchain-ai/langchain-google/blob/af2ef994ee5429bce18899b2c0c327470eba7032/libs/vertexai/langchain_google_vertexai/_base.py#L106-L107
This PR moves that global config fallback to an
after
validator, giving theproject
field a chance to be set by downstream defaults.Type
🐛 Bug Fix