'finish_reason': 'SAFETY'

When I call the API interface to generate a document, the streaming output will suddenly terminate due to security reasons, presenting a problematic feature to the user. How can we provide users with a better experience in this situation? Try again? Reminder to users that the termination was due to security reasons?

Welcome to the forums!

How you handle a SAFETY error is up to you and dependent on your specific needs.

Some chat applications, like the Gemini chat app, will do something like erase the entire response up to that point and say that they can’t answer that. You may choose to do something like interrupt it at that point and say that you can’t say more. You may wish to remove everything so far and start again with different prompting or a different model. If you see it a lot, you may wish to adjust the safety settings (tho this won’t work for all possible cases, and isn’t necessarily a great idea if you don’t have other counter-abuse methods in place).

But a great question that isn’t really discussed enough.

I used the API of the Gemini called by Langchain and set the safey_settings parameter according to the instructions on the Langchain official website.
safety_settings = {
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,

HarmCategory. HARM_CATEGORY_CIVIC_INTEGRITY: HarmBlockThreshold.BLOCK_NONE

}
llm = ChatGoogleGenerativeAI(
model=“gemini-1.5-pro”,
temperature=0,
max_tokens=None,
timeout=None,
max_retries=2,
safety_settings = safety_settings
)
However, it is not effective and there is still a high probability of ‘finish-reason’: ‘SAFETY’ occurring.
I directly request the HTTPS interface of Gemini, and there is also a high probability of ‘finish.rason’: ‘SAFETY’ appearing.
But today the situation has changed again. Regardless of whether I set safety_dettings or not, it will not display ‘finish_rason’: ‘SAFETY’. Directly requesting the HTTPS interface is the same. This issue seems to have disappeared. Could it be that Gemini-1.5-pro has been officially updated by Gemini? Because I found that Gemini-1.5-Flash still has this problem.