Skip to content

Gemini : Streaming example not working on Android #249

Closed
@ImadSaddik

Description

@ImadSaddik

Hello, I was following the quick-start guide that shows how to use the Gemini API in Android apps. So far I have been able to replicate most of the examples, but I was facing some problems because I have been getting errors while trying to run the code samples.

In this issue that I opened, I will focus on the streaming example. The getResponse method is a method that I created to get the model's response with streaming support.

public void getResponse(String query, ResponseCallback callback) {
        GenerativeModelFutures model = getModel();

        Content.Builder contentBuilder = new Content.Builder();
        contentBuilder.setRole("user");
        contentBuilder.addText(query);
        Content content = contentBuilder.build();

        Log.d("GeminiPro", "getResponse: " + query);
        Log.d("GeminiPro", "getResponse: " + content);

        final String[] fullResponse = {""};
        Publisher<GenerateContentResponse> streamingResponse = model.generateContentStream(content);
        Log.d("GeminiPro", "getResponse: " + streamingResponse);

        streamingResponse.subscribe(new Subscriber<GenerateContentResponse>() {
            @Override
            public void onSubscribe(Subscription s) {
            }

            @Override
            public void onNext(GenerateContentResponse generateContentResponse) {
                String chunk = generateContentResponse.getText();
                fullResponse[0] += chunk;
                Log.d("GeminiPro", "onNext: " + chunk);
            }

            @Override
            public void onComplete() {
                System.out.println(fullResponse[0]);
                Log.d("GeminiPro", "onComplete: " + fullResponse[0]);
                callback.onResponse(fullResponse[0]);
            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                Log.d("GeminiPro", "onError: " + t.getMessage());
                callback.onError(t);
            }
        });
    }

First, in the code sample. The onSubscribe was not used, when I did the same nothing, I got nothing. The code kept running without giving a response. So, I added s.request(1) to request something from the publisher. This addition threw an exception that didn't make sense : contents is not specified why ?

FATAL EXCEPTION: DefaultDispatcher-worker-2
Process: com.example.streaminggeminiapp, PID: 2433
com.google.ai.client.generativeai.type.ServerException: * GenerateContentRequest.contents: contents is not specified
...

The content variable is already defined. I don't know if I am missing something or this is a bug that should be solved.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions