google-gemini / generative-ai-python

The official Python library for the Google Gemini API
https://pypi.org/project/google-generativeai/
Apache License 2.0
1.62k stars 322 forks source link

Unknown field for Candidate: finish_message #559

Closed gabrielmdesidera closed 1 month ago

gabrielmdesidera commented 2 months ago

Description of the bug:

During regular use, some requests to Gemini are returning this error instead of the response (Python project running in Docker):

chat-app-1  | 2024-09-17 14:34:44.282 Uncaught app exception
chat-app-1  | Traceback (most recent call last):
chat-app-1  |   File "/usr/local/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/exec_code.py", line 88, in exec_func_with_error_handling
chat-app-1  |     result = func()
chat-app-1  |   File "/usr/local/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 590, in code_to_exec
chat-app-1  |     exec(code, module.__dict__)
chat-app-1  |   File "/app/chat.py", line 135, in <module>
chat-app-1  |     if item.text:
chat-app-1  |   File "/usr/local/lib/python3.10/site-packages/google/generativeai/types/generation_types.py", line 454, in text
chat-app-1  |     if candidate.finish_message:
chat-app-1  |   File "/usr/local/lib/python3.10/site-packages/proto/message.py", line 906, in __getattr__
chat-app-1  |     raise AttributeError(
chat-app-1  | AttributeError: Unknown field for Candidate: finish_message

Actual vs expected behavior:

By reading the code from #527, I assume the expected behaviour would be to raise another error with the intended message

Any other information you'd like to share?

No response

gabrielmdesidera commented 2 months ago

This is the partial response that was returned that triggered this bug:

GenerateContentResponse(
    done=True,
    iterator=None,
    result=protos.GenerateContentResponse({
      "candidates": [
        {
          "finish_reason": "SAFETY",
          "index": 0,
          "safety_ratings": [
            {
              "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
              "probability": "NEGLIGIBLE"
            },
            {
              "category": "HARM_CATEGORY_HATE_SPEECH",
              "probability": "NEGLIGIBLE"
            },
            {
              "category": "HARM_CATEGORY_HARASSMENT",
              "probability": "NEGLIGIBLE"
            },
            {
              "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
              "probability": "HIGH"
            }
          ]
        }
      ],
      "usage_metadata": {
        "prompt_token_count": 121385,
        "candidates_token_count": 1,
        "total_token_count": 121386
      }
    }),
)

It looks like the safety violation should have triggered an error with a finish message, but the finish_message attribute is not present in the candidate instance, thus leading to the AttributeError: Unknown field for Candidate: finish_message bug

Majid-cloud commented 1 month ago

I faced the similar issue. Turns out that I was giving a prompt which was triggering a safety mechanism in the Gemini model. You have observed "finish_reason": "SAFETY" in response, indicating that the content generation was blocked due to safety concerns.

can you try simple prompt and send request again.

tianhuih commented 1 month ago

Hi @gabrielmdesidera, I'm encountering the same error. May I know how you get partial response when an exception occurs?

lingxiao216 commented 1 month ago

Hi, I encountered the same issue. I attempted to resolve it by adding the following parameters to my code:

safety_settings={
                HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
                HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
                HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
                HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE
            }   

I still receive the same error, and this message :

GenerateContentResponse(
    done=True,
    iterator=None,
    result=protos.GenerateContentResponse({
      "candidates": [
        {
          "finish_reason": "OTHER",
          "index": 0
        }
      ],
      "usage_metadata": {
        "prompt_token_count": 328,
        "total_token_count": 328
      }
    }),
)
MarkDaoust commented 1 month ago

https://github.com/google-gemini/generative-ai-python/pull/571