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

Missing stream=True Parameter in _StreamGenerateContent Class of GenerativeServiceRestTransport Causes Non-Streaming Requests #492

Closed
jbndr opened this issue Jul 25, 2024 · 7 comments

Comments

@jbndr
Copy link

jbndr commented Jul 25, 2024

Description of the bug:

In the _StreamGenerateContent class of the GenerativeServiceRestTransport implementation for the Google AI Generative Language service, the HTTP request intended for streaming does not include the stream=True parameter. This omission results in non-streaming behavior, contrary to the expected functionality.

Actual vs expected behavior:

Actual Behavior:
The _StreamGenerateContent class makes a non-streaming HTTP request using the REST transport.

Expected Behavior:
The _StreamGenerateContent class should make a streaming HTTP request using the REST transport.

Any other information you'd like to share?

Code Location:
/google/ai/generativelanguage_v1beta/services/generative_service/transports/rest.py

Current Code Snippet:

# Send the request
headers = dict(metadata)
headers["Content-Type"] = "application/json"
response = getattr(self._session, method)(
    "{host}{uri}".format(host=self._host, uri=uri),
    timeout=timeout,
    headers=headers,
    params=rest_helpers.flatten_query_params(query_params, strict=True),
    data=body,
    # Missing stream=True parameter
)

Proposed Fix:
Add the stream=True parameter to the request call:

# Send the request
headers = dict(metadata)
headers["Content-Type"] = "application/json"
response = getattr(self._session, method)(
    "{host}{uri}".format(host=self._host, uri=uri),
    timeout=timeout,
    headers=headers,
    params=rest_helpers.flatten_query_params(query_params, strict=True),
    data=body,
    stream=True # Added stream parameter
)
@jbndr
Copy link
Author

jbndr commented Jul 25, 2024

Please let me know if there is another repository that would be more appropriate for suggesting this fix.

@MarkDaoust
Copy link
Collaborator

MarkDaoust commented Jul 25, 2024

Hi,

Maybe you're misunderstanding this code. The stream=True parameter only exists in the google.generativeai SDK. It doesn't exist in the underlying client library, or in the API itself.

The SDK converts generate_content(..., stream=True) calls into stream_generate_content(...) calls.

This is working as expected:

for chunk in model.generate_content("Tell me a story abut a cat that can talk.", stream=True):
    print(chunk.text)
    print('-'*80)

If you raised this error because in Colab it waits till all the chunks are available before yielding the first one, that's an issue with Colab, not these libraries.

@jbndr
Copy link
Author

jbndr commented Jul 26, 2024

Hi @MarkDaoust ,

thank you for your quick reply!

I am aware of your example from: https://github.com/google-gemini/cookbook/blob/main/quickstarts/Streaming.ipynb
This also works for me locally with the SDK, but the GenerativeModel class defaults to using gRPC as the transport instead of the REST I need to use.

I am referring to the stream parameter of the requests library that is used to make the call within the REST transport implementation GenerativeServiceRestTransport, more specifically the _StreamGenerateContent class within it. I understand that this is not technically part of this repository, but I have not found the corresponding repository publicly.

getattr(self._session, method) calls this method https://requests.readthedocs.io/en/latest/api/#requests.Session.request without the stream parameter, which means that the chunks are only output after everything has been loaded.

@MarkDaoust MarkDaoust reopened this Jul 26, 2024
@MarkDaoust
Copy link
Collaborator

MarkDaoust commented Jul 26, 2024

Oh, I got it now. Thanks for clarifying.

Yeah, you're right and that totally fixes it, thanks!

The required change is in the generated client library, google.ai.generativelanguage to really fix this we'll need to update the code-generator.

@MarkDaoust
Copy link
Collaborator

I can't fix this here, so I've reported the problem over on the gapic-generator-python repository that generates this code:

googleapis/gapic-generator-python#2076

@jbndr
Copy link
Author

jbndr commented Jul 27, 2024

Thanks!

@MarkDaoust
Copy link
Collaborator

Okay, they've merged that into the client library generator. So this will actually be fixed with the next release of google-ai-generativelanguage, (whenever that is).

https://pypi.org/project/google-ai-generativelanguage/#history

It should work in anything after 6.10

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

No branches or pull requests

2 participants