flutterfire icon indicating copy to clipboard operation
flutterfire copied to clipboard

[firebase_vertexai]: role with null value added to the history on content blocked due to Safety reasons

Open Flucadetena opened this issue 1 year ago • 8 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues.

Which plugins are affected?

Other

Which platforms are affected?

No response

Description

This issue is caused by generative-ai-dart. When a response from the model is blocked due to Safety reasons, the response is added to the history with a null role. This throws an exception and breaks the ChatSession as multi-turn forces you to alternate between user and model roles.

As a null role was added, the history breaks and no further messages can be sent.

This seems to happen only using the method sendMessage and not the sendMessageStream.

There is an already a closed issue in the package and a pull request awaiting review in case someone need to use it temporarily.

Reproducing the issue

Create a new chat instance and force a message to be blocked for safety reasons. Then try to send a new message as a user or model.

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 Jul 26 '24 16:07 Flucadetena

Hey @Flucadetena. Does this issue affect the firebase_vertexai package directly? From what I can gather, this is an issue upstream that needs to be fixed

exaby73 avatar Jul 28 '24 23:07 exaby73

Hi @exaby73 yes It is an issue that needs to be fixed in the generative ai package, not in the vertex ai. But until it is solved, the vertex ai package suffers this issue as a consequence, as the "send message" function is really a wrapper over the generative ai package.

I created the issue mainly so that people that suffer it can know what the reason is and can track the state.

Flucadetena avatar Jul 29 '24 06:07 Flucadetena

Do you have a reliable reproduction case?

I'm curious why you chose to drop the messages. Was there some reason the reply did not seem useful to keep in the cases where it had no role?

natebosch avatar Aug 16 '24 23:08 natebosch

Hi @natebosch,

As a reproducible code, I don't have anything right now, but if it is necessary I can work something out. If on the other hand you want to try, simply create any basic chat, set the filters to "Low" and send inappropriate emojis, it will fire in 1-3 tries. The next time you call the "sendMessage" function, after a block, it will break with the error of the title of the issue.

The reason why you would not want the response in the "history" is that there is really no content, the "text" value is not present, as the response was blocked. The response is returned by the "sendMessage" function, which is correct, to access the blocking reason, but it should not be added to the history as there is no real response.

At least this is the way blocked responses are handled in the "sendMessageStream" and the other versions of the vertex AI package in other languages. (At least that I could see)

But I would say the main problem is that the responses is added with a "null" role, which breaks the chat session completely, as "multi-turn" forces the roles to be either: user, model, functionCall or functionResponse.

If the blocked responses were to be keep in the history, then a role should be added before saving it to the history (There is actually a TODO in the package asking that specific question).

This case maybe a good chance to add a method that cleans up the history before submitting the generation. (This is something the JS version of the package does, I implemented something similar in my end to ensure the conversation always follows the order required by the model in multi-turn and has the benefit of joining multiple messages of the same "role" so that wont break the generation).

Hope this clarifies the reason of the issue. Let me know If I can help in anyway, I'll be happy to do so.

Flucadetena avatar Aug 17 '24 02:08 Flucadetena

@Flucadetena, a full sample would go a long way to help solve this.

Lyokone avatar Aug 20 '24 14:08 Lyokone

Hi @Lyokone here is a test app i created for you to reproduce the problem. Repo - vertex_test

I was not able to run the vertex AI sample app, so I simply copy and pasted the code into this brand new flutter project.

You just need to change the bundle Id in android and run flutterfire configure.

  • To force the issue right in the input "What is 🍑🍆" and you will get a prompt that the answer was blocked. (Sometimes you need to try a couple times, in my test almost always was the first)
  • Then ask or write anything else on the input and send it, no matter what, and you will get the error.

Hope this helps.

Flucadetena avatar Aug 20 '24 17:08 Flucadetena

The issue is not resolve with the fix as the content is not added to the history when it is blocked. Because there is no content. I updated the repo on the top and tried and the problem persists.

I gave two possible solutions I can think of in the main repo: https://github.com/google-gemini/generative-ai-dart/issues/197

Happy to help in any way.

Flucadetena avatar Oct 04 '24 17:10 Flucadetena

Hey there, are you still having this issue?

MichaelVerdon avatar May 02 '25 12:05 MichaelVerdon

Hi there, I will be closing this issue as this ticket is going stale, please feel free to reopen if this issue is persisting, especially on the new Firebase_AI package.

MichaelVerdon avatar Jul 03 '25 14:07 MichaelVerdon