Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response.text raises an exception when only a function call is returned #532

Closed
JeremyEastham opened this issue Aug 29, 2024 · 3 comments
Closed
Assignees
Labels
component:python sdk Issue/PR related to Python SDK status:awaiting user response Awaiting a response from the author status:stale Issue/PR will be closed automatically if there's no further activity type:bug Something isn't working

Comments

@JeremyEastham
Copy link

Description of the bug:

The GenerateContentResponse.text property sometimes raises an exception when no text is returned from the API. The documentation states that this property is equivalent to response.candidates[0].content.parts[0].text, but accessing that property does not always raise an exception when response.text does. One of these cases is when the model responds by calling a function without any text, which leads to an error when parsing the parts list.

Actual vs expected behavior:

Minimal example:

import google.generativeai as genai

def my_tool():
    pass

genai.configure( api_key = ... )
model = genai.GenerativeModel( model_name = "gemini-1.5-flash", tools = [ my_tool ] )
chat = model.start_chat()
response = chat.send_message( "I'm trying to test my tool. Please call it for me." )
assert response.candidates[0].content.parts[0].text == "" # Succeeds
print( response.text ) # Expected: "" or None

# Traceback (most recent call last):
#   File "C:\Users\...\bug.py", line 13, in <module>
#     print( response.text ) # Expected: "" or None
#            ^^^^^^^^^^^^^
#   File "C:\Users\...\venv\Lib\site-packages\google\generativeai\types\generation_types.py", line 465, in text
#     part_type = protos.Part.pb(part).whichOneof("data")
#                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# AttributeError: whichOneof. Did you mean: 'WhichOneof'?

Any other information you'd like to share?

Related Issues:

@Gunand3043 Gunand3043 self-assigned this Sep 2, 2024
@Gunand3043 Gunand3043 added type:bug Something isn't working status:triaged Issue/PR triaged to the corresponding sub-team component:python sdk Issue/PR related to Python SDK labels Sep 2, 2024
@Gunand3043
Copy link

Hi @JeremyEastham , Since you haven't set the parameter 'enable_automatic_function_calling=True', you will need to manually call the function by yourself to get the generated text response.
For reference please check the link.

@Gunand3043 Gunand3043 added status:awaiting user response Awaiting a response from the author and removed status:triaged Issue/PR triaged to the corresponding sub-team labels Sep 2, 2024
Copy link

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.

@github-actions github-actions bot added the status:stale Issue/PR will be closed automatically if there's no further activity label Sep 17, 2024
Copy link

github-actions bot commented Oct 1, 2024

This issue was closed because it has been inactive for 28 days. Please post a new issue if you need further assistance. Thanks!

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component:python sdk Issue/PR related to Python SDK status:awaiting user response Awaiting a response from the author status:stale Issue/PR will be closed automatically if there's no further activity type:bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants