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.6k stars 321 forks source link

Internal error with protobufs and tool use #626

Open duccdev opened 4 days ago

duccdev commented 4 days ago

Description of the bug:

I was playing around with tool use and streaming, this was my code:

import google.generativeai as genai
import config

genai.configure(api_key=config.API_KEY)

def set_brightness(value: float) -> None:
    """Controls the brightness of all house lights. `value` is a `float` between 0 (off) and 1 (max)."""
    print("Brightness changed:", value)

model = genai.GenerativeModel(
    "gemini-1.5-flash-002",
    tools=[set_brightness],
)

chat = model.start_chat()
while True:
    stream = chat.send_message(input("User: "), stream=True)
    print("Model: ", end="", flush=True)
    for chunk in stream:
        print(chunk.text, end="", flush=True)
    print()

I got this peculiar error when I prompted it like this:

❯ python3 main.py
User: hey can you turn off the lights
Model: Traceback (most recent call last):
  File "/home/ducc/Projects/sketchbooks/apithing/main.py", line 29, in <module>
    print(chunk.text, end="", flush=True)
          ^^^^^^^^^^
  File "/home/ducc/Projects/sketchbooks/apithing/.venv/lib/python3.13/site-packages/google/generativeai/types/generation_types.py", line 536, in text
    part_type = protos.Part.pb(part).whichOneof("data")
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: whichOneof. Did you mean: 'WhichOneof'?

I am currently using Python 3.13, latest version of the SDK. This is not an issue on my part as this seems to be a typo in the library.

Actual vs expected behavior:

Actual:

❯ python3 main.py
User: hey can you turn off the lights
Model: Traceback (most recent call last):
  File "/home/ducc/Projects/sketchbooks/apithing/main.py", line 29, in <module>
    print(chunk.text, end="", flush=True)
          ^^^^^^^^^^
  File "/home/ducc/Projects/sketchbooks/apithing/.venv/lib/python3.13/site-packages/google/generativeai/types/generation_types.py", line 536, in text
    part_type = protos.Part.pb(part).whichOneof("data")
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: whichOneof. Did you mean: 'WhichOneof'?

Expected (roughly):

❯ python3 main.py
User: hey can you turn off the lights
Brightness changed: 0.0
Model: Done!

Any other information you'd like to share?

Simply change the typo at types/generation_types.py, line 536, from .whichOneof to .WhichOneof.

Gunand3043 commented 3 days ago

Hi @duccdev

You need to set enable_automatic_function_calling to True to make it work.

def set_brightness(value: float) -> None:
    """Controls the brightness of all house lights. `value` is a `float` between 0 (off) and 1 (max)."""
    print("Brightness changed:", value)

model = genai.GenerativeModel(
    "gemini-1.5-flash-002",
    tools=[set_brightness],
)

chat = model.start_chat(enable_automatic_function_calling=True)
while True:
    stream = chat.send_message(input("User: "))
    print("Model: ", end="", flush=True)
    print(stream.text, end="", flush=True)
    print()
MarkDaoust commented 3 days ago

Yes. Thanks for reporting!