langchain-ai / langchain-google

MIT License
119 stars 150 forks source link

vertexai: postpone `_VertexAIBase.project` validation #514

Closed wbadart closed 2 months ago

wbadart commented 2 months ago

PR Description

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).

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 the project field a chance to be set by downstream defaults.

Type

🐛 Bug Fix