flutterfire icon indicating copy to clipboard operation
flutterfire copied to clipboard

[firebase_vertexai]: Server Error not handle - JSON error unexpected character at (0)

Open Flucadetena opened this issue 1 year ago • 2 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues.

Which plugins are affected?

Other

Which platforms are affected?

Android, iOS, macOS, Web, Windows

Description

This is a problem in the "google-generative-ai" package, already reported in the issue: https://github.com/google-gemini/generative-ai-dart/issues/206

And already created a pull request to handle the error: https://github.com/google-gemini/generative-ai-dart/pull/207

I'm reporting the error here as I came across the problem while using the Vertex AI package, and to help other facing this issue while using it.

Description: When calling generateContent and the server responses with an error like +500, instead of throwing and giving a clear error, the makeRequest method tries to decode de result and breaks with a "JSON error unexpected character at (0)".

This is very confusing and not clear about the real problem.

Reproducing the issue

In this case we are going to try and process and image, pdf or other file stored in "Cloud Storage/Firebase Storage" through a "gs://" url. But the server won't have access to the files and response with a 502 error.

Other types of generation will work perfectly, thus the confusion when the generating content responses with "Json parse" error.

The replication case is rare, but happened to me when enabling the Vertex AI API in Google Cloud, for some reason it did not setup the permission for the bucket in Firebase right.

*This considers that you are using Firebase with "VertexAI" and have already enable the API in the project and have it working.

  • Go to the connected Firebase project in Google Cloud, under the section "Cloud Storage".
  • Select the bucket "{ProjectId}.appspot.com
  • Go to "permissions" tab on the top
  • Under "View By Principals" delete the "service agent for Vertext AI" automatically generated when enabling the API Google Cloud. Normally with a "principal" like "[email protected]"
  • After this, the API won't have access to the bucket, so when you try to generate content with a file stored in it, the server will response with a 502 error and prompt the error described above.

Firebase Core version

3.1.1

Flutter Version

3.19.6

Relevant Log Output

No response

Flutter dependencies

No response

Additional context and comments

No response

Flucadetena avatar Aug 26 '24 21:08 Flucadetena

Hi @Flucadetena, Kindly provide a sample minimal code reproducing this issue.

SelaseKay avatar Aug 27 '24 08:08 SelaseKay

Hi @SelaseKay I've already provided instructions to run this issue. By simply running the sample app and adding a file via "gs://" url without the permissions it will break.

As you have to configure this on your end with your firebase project, there is not much code I can provide.

If you are more specific i'll be happy to help.

Flucadetena avatar Aug 27 '24 10:08 Flucadetena

Hello @Flucadetena, I've pinged internally so someone would review your PR . Since there is nothing to fix directly on the FlutterFire repository, I'll go ahead and close this issue.

Lyokone avatar Sep 02 '24 14:09 Lyokone

Awesome @Lyokone completely agree.

If I can help any further let me know.

Hope you have a nice day ;)

Flucadetena avatar Sep 02 '24 14:09 Flucadetena