How to translate "finish_reason": 4 from GenerateContentResponse into something meaningful?

I have been using the gemini-1.5-flash-001 model (free tier) to extract information from scientific publications; so there is a prompt describing the task (what to extract and how to format the reply) appended to the plain text of the scientific paper. Most of these have worked well, but 5 out of 30 return the following:

response:
GenerateContentResponse(
    done=True,
    iterator=None,
    result=glm.GenerateContentResponse({
      "candidates": [
        {
          "finish_reason": 4,
          "index": 0,
          "safety_ratings": [],
          "token_count": 0,
          "grounding_attributions": []
        }
      ]
    }),
)

All of the documentation says the finish reasons are from this list which are not listed by number. It is not clear which of these the “4” represents (or even if that is the right way to interpret it!).

Also, just to be clear, I was hitting the API well below the limits published and these errors persisted on another day when I tried several of these files individually (so I don’t think I broke the rate limit).

The call was:

model=genai.GenerativeModel(
    model_name="gemini-1.5-flash-001",
    system_instruction=system_prompt_text)

temp = 0.2
max_reply_tokens = 4096

model_config = {
  "temperature": temp,
  "max_output_tokens": max_reply_tokens,
  'response_mime_type':'application/json'
}

completion = model.generate_content(submit_prompt,
  generation_config=model_config,
  safety_settings={
    HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_ONLY_HIGH
  }
)

Note that the stuff about harm categories was due to the fact that some of the scientific papers discussed drug abuse and some were coming back with dangerous content warnings of “medium.”

Although it would take up a lot of space, I could upload a example prompt/paper. But these are on the order of 6000-8000 tokens, so not gigantic things. The prompt asks for JSON responses and for the other papers this worked well.

But I really just want to know how to translate this to the actual finish reasons. Thanks if you can help!

A finish reason response would not be because of quota limits.

The list of finish reason numbers is documented at generative-ai-python/google/generativeai/types/answer_types.py at f08c789741f30e49ecfb822540fd749920d62bcc · google-gemini/generative-ai-python · GitHub
From there, you can see that 4 is “RECITATION”, which broadly means that the reply it would have given draws from copyrighted or licensed material.

3 Likes

This is an odd behavior, I went ahead and opened a bug to make sure we just show the raw text and not a number: Finish reason should be text not a number · Issue #371 · google-gemini/generative-ai-python · GitHub stay tuned!

2 Likes