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.18k stars 223 forks source link

The `response.text` quick accessor only works when the response contains a valid `Part`, but none was returned. Check the `candidate.safety_ratings` to see if the response was blocked. #373

Open 141forever opened 1 month ago

141forever commented 1 month ago

Description of the bug:

The response.text quick accessor only works when the response contains a valid Part, but none was returned. Check the candidate.safety_ratings to see if the response was blocked.

Actual vs expected behavior:

Traceback (most recent call last): File "D:\Study\codes\Gemini_score.py", line 64, in strr = str(id) + ":" + response.text File "D:\Study\codes\venv\lib\site-packages\google\generativeai\types\generation_types.py", line 401, in text raise ValueError( ValueError: The response.text quick accessor only works when the response contains a valid Part, but none was returned. Check the candidate.safety_ratings to see if the response was blocked.

Any other information you'd like to share?

THIS IS MY CODES:

import pandas as pd import pdb import google.generativeai as genai import os

genai.configure(api_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")

model = genai.GenerativeModel('gemini-1.5-pro')

response = model.generate_content(system_prompt) strr = str(id) + ":" + response.text

system_prompt is a string

singhniraj08 commented 1 month ago

@141forever, Similar feature request #282 is already filled. Requesting you to please follow and +1 the similar issue for updates and close this thread. Thank you!

trunglebka commented 4 weeks ago

@singhniraj08 I'm facing similar problem as the OP, since the finish_reason is always OTHER and the issue you linked is talking about safety I'm not sure it is related problem. My prompt is as simple as translating a JSON object with the content "Hi Peter, how's it going?" or "Chief Technology Officer" along with related metadata. Using the same prompt, I can easily get the result from ChatGPT, but Gemini (both Pro and Flash) just responds with finish_reason=OTHER so I don't even know what the problem is with the prompt.

trunglebka commented 4 weeks ago

Additional information: With the same prompt I always get the expected result via the public gemini website https://gemini.google.com With https://aistudio.google.com/app/prompts/new_chat , it is failed every time. I can share the prompt via email if you guys are interested

singhniraj08 commented 4 weeks ago

@trunglebka, #282 is for implementation of more helpful error messages when the response is blocked by Gemini because of safety or other reasons. The SDK doesn't control the service's responses. If you feel the response is blocked which shouldn't be the normal case, we would suggest you to use "Send Feedback" option in Gemini docs. Ref: Screenshot below. You can also post this issue on Discourse forum. image

trunglebka commented 4 weeks ago

@singhniraj08 Thanks for pointing that out, I wasn't paying close attention

baptvit commented 2 weeks ago

Im having the same issue, Im doing some rewriting text about healthcare USML dataset. And in a specific question I got this error.

github-actions[bot] commented 1 day ago

Marking this issue as stale since it has been open for 14 days with no activity. This issue will be closed if no further activity occurs.