Skip to content
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

Methods astream and astream_event not working for ChatVertexAI #733

Open
tanukiki opened this issue Feb 13, 2025 · 3 comments
Open

Methods astream and astream_event not working for ChatVertexAI #733

tanukiki opened this issue Feb 13, 2025 · 3 comments

Comments

@tanukiki
Copy link

tanukiki commented Feb 13, 2025

When using:

from langchain_google_vertexai import ChatVertexAI, VertexAI
llmodel = ChatVertexAI(model_name="gemini-2.0-flash", max_tokens=1024)

async for chunk in llmodel.astream('Say some interesting facts about chinchillas'):
    print(chunk, flush=True)

I get

TypeError: object ResponseIterator can't be used in 'await' expression

It works for base VertexAI model and for ChatGoogleGenerativeAI from langchain_google_genai but not for ChatVertexAI. The same with .astream_events

@langcarl langcarl bot added the investigate label Feb 13, 2025
@tanukiki
Copy link
Author

tanukiki commented Feb 14, 2025

When using:

from langchain_google_vertexai import ChatVertexAI, VertexAI
llmodel = ChatVertexAI(model_name="gemini-2.0-flash", max_tokens=1024)

async for chunk in llmodel.astream('Say some interesting facts about chinchillas'):
print(chunk, flush=True)
I get

TypeError: object ResponseIterator can't be used in 'await' expression

It works for base VertexAI model and for ChatGoogleGenerativeAI from langchain_google_genai but not for ChatVertexAI. The same with .astream_events

Ok it turns out that this error only occurs if we use vertexai.init() before running the model:

from langchain_google_vertexai import ChatVertexAI, VertexAI
import vertexai

vertexai.init()
llmodel = ChatVertexAI(model_name="gemini-2.0-flash", max_tokens=1024)

async for chunk in llmodel.astream('Say some interesting facts about chinchillas'):
    print(chunk, flush=True)

It may be related to incorrect async rest credentials https://www.googlecloudcommunity.com/gc/AI-ML/Async-REST-credentials-for-Vertex-AI/m-p/839712

@SauravP97
Copy link
Contributor

Looks like the error is being originated from here:

https://github.com/langchain-ai/langchain-google/blob/main/libs/vertexai/langchain_google_vertexai/chat_models.py#L639 #

i.e the last line in this code snippet.

 @retry_decorator
    async def _completion_with_retry_inner(
        generation_method: Callable, **kwargs: Any
    ) -> Any:
       return await generation_method(**kwargs)

I further looked at the generation_method. It seems this comes from the async_prediction_client.stream_generate_content.

Ref: https://github.com/langchain-ai/langchain-google/blob/main/libs/vertexai/langchain_google_vertexai/chat_models.py#L1699

response_iter = _acompletion_with_retry(
    self.async_prediction_client.stream_generate_content,
    max_retries=self.max_retries,
    request=request,
    is_gemini=True,
    **kwargs,
)

This is being created from this method async_prediction_client

Ref: https://github.com/langchain-ai/langchain-google/blob/main/libs/vertexai/langchain_google_vertexai/_base.py#L147C9-L147C32

    @property
    def async_prediction_client(self) -> v1beta1PredictionServiceAsyncClient:
        """Returns PredictionServiceClient."""
        if self.async_client is None:
            async_client_kwargs: dict[str, Any] = dict(
                client_options=self.client_options,
                client_info=get_client_info(module=self._user_agent),
                credentials=self.credentials,
            )

            if self.api_transport is not None:
                async_client_kwargs["transport"] = self.api_transport

            self.async_client = v1beta1PredictionServiceAsyncClient(
                **async_client_kwargs
            )

        return self.async_client

I am guessing the error is getting originated from the Vertex AI cloud library since the v1beta1PredictionServiceAsyncClient is a part of that library.

@lkuligin
Copy link
Collaborator

@tanukiki could you share a full trace, please? and is the error consistently reproducible on your side?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants