Invalid argument error 400 while using function calling gemini-1.5-flash-latest api

Hey, I am facing this issue where this program is working properly without function calling
but showing an error when using function calling.
I have attached the stack trace after the code.

import google.generativeai as genai
import google.ai.generativelanguage as glm
from dotenv import load_dotenv
from FunctionsHandler import FunctionsProcessor
import os

load_dotenv()


# def save_context(title, content):
#     """ This function saves the context to the memory. """
#     return FunctionsProcessor.save_context(title, content)

# def make_query(query):
#     """ This function queries information from the memory. """
#     return FunctionsProcessor.make_query(query)

# def service_query(query):
#     """ This function queries information about the services from the memory to make it easy to use call_service. """
#     return FunctionsProcessor.service_query(query)

# def call_service(service_name, task_type, extra):
#     """ This function calls services as per requirements."""
#     return FunctionsProcessor.call_service(service_name, task_type, extra)

def print_hello_world():
    print("Hello World!")
    return "Done"


genai.configure(api_key=os.getenv('GEMINI_API_KEY'))

# funcs = [save_context, make_query, call_service, service_query]

model = genai.GenerativeModel(
    model_name = 'gemini-1.5-flash-latest',
    tools = print_hello_world)

chat = model.start_chat(enable_automatic_function_calling = True)


response = chat.send_message("HELLLOOOOOOO!")    
print(response.text)

Here’s the error:

Traceback (most recent call last):
  File "/home/disguisedg/Documents/DITOS/tryingout.py", line 42, in <module>
    response = chat.send_message("HELLLOOOOOOO!")    
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/disguisedg/Documents/DITOS/DITOS_VENV/lib/python3.11/site-packages/google/generativeai/generative_models.py", line 578, in send_message
    response = self.model.generate_content(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/disguisedg/Documents/DITOS/DITOS_VENV/lib/python3.11/site-packages/google/generativeai/generative_models.py", line 331, in generate_content
    response = self._client.generate_content(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/disguisedg/Documents/DITOS/DITOS_VENV/lib/python3.11/site-packages/google/ai/generativelanguage_v1beta/services/generative_service/client.py", line 827, in generate_content
    response = rpc(
               ^^^^
  File "/home/disguisedg/Documents/DITOS/DITOS_VENV/lib/python3.11/site-packages/google/api_core/gapic_v1/method.py", line 131, in __call__
    return wrapped_func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/disguisedg/Documents/DITOS/DITOS_VENV/lib/python3.11/site-packages/google/api_core/retry/retry_unary.py", line 293, in retry_wrapped_func
    return retry_target(
           ^^^^^^^^^^^^^
  File "/home/disguisedg/Documents/DITOS/DITOS_VENV/lib/python3.11/site-packages/google/api_core/retry/retry_unary.py", line 153, in retry_target
    _retry_error_helper(
  File "/home/disguisedg/Documents/DITOS/DITOS_VENV/lib/python3.11/site-packages/google/api_core/retry/retry_base.py", line 212, in _retry_error_helper
    raise final_exc from source_exc
  File "/home/disguisedg/Documents/DITOS/DITOS_VENV/lib/python3.11/site-packages/google/api_core/retry/retry_unary.py", line 144, in retry_target
    result = target()
             ^^^^^^^^
  File "/home/disguisedg/Documents/DITOS/DITOS_VENV/lib/python3.11/site-packages/google/api_core/timeout.py", line 120, in func_with_timeout
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/disguisedg/Documents/DITOS/DITOS_VENV/lib/python3.11/site-packages/google/api_core/grpc_helpers.py", line 78, in error_remapped_callable
    raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.InvalidArgument: 400 Request contains an invalid argument.

Hi @Dishant_Kapoor,

If you check the state diagram when enable_automatic_function_calling is enabled model is called again with function call response for final output and it appears in text response.

import os
import google.generativeai as genai
import google.ai.generativelanguage as glm

from google.colab import userdata
GOOGLE_API_KEY = userdata.get('GOOGLE_API_KEY')
genai.configure(api_key=GOOGLE_API_KEY)

def set_light_values(brightness: int, color_temp: str):
    """Set the brightness and color temperature of a room light. (mock API).

    Args:
        brightness: Light level from 0 to 100. Zero is off and 100 is full brightness
        color_temp: Color temperature of the light fixture, which can be `daylight`, `cool` or `warm`.

    Returns:
        A dictionary containing the set brightness and color temperature.
    """
    return {
        "brightness": brightness,
        "colorTemperature": color_temp
    }

model = genai.GenerativeModel(
    model_name = 'gemini-1.5-flash-latest',
    tools = [set_light_values])

chat = model.start_chat(enable_automatic_function_calling=True)

response = chat.send_message("Dim the lights so the room feels cozy and warm.!")   

for content in chat.history:
    print(content.role, "->", [type(part).to_dict(part) for part in content.parts])
    print("-" * 80)
user -> [{'text': 'Dim the lights so the room feels cozy and warm.!'}]
--------------------------------------------------------------------------------
model -> [{'function_call': {'name': 'set_light_values', 'args': {'color_temp': 'warm', 'brightness': 50.0}}}]
--------------------------------------------------------------------------------
user -> [{'function_response': {'name': 'set_light_values', 'response': {'brightness': 50.0, 'colorTemperature': 'warm'}}}]
--------------------------------------------------------------------------------
model -> [{'text': "OK. I've dimmed the lights to 50% and set the color temperature to warm.  Let me know if you'd like to adjust it further. \n"}]
--------------------------------------------------------------------------------

But if you can see for the with enable_automatic_function_calling disable you get back as a function call and you can retrieve the values from the function call

model = genai.GenerativeModel(
    model_name = 'gemini-1.5-flash-latest',
    tools = [set_light_values])

chat = model.start_chat()
response = chat.send_message("Dim the lights so the room feels cozy and warm.!") 

for content in chat.history:
    print(content.role, "->", [type(part).to_dict(part) for part in content.parts])
    print("-" * 80)
     
user -> [{'text': 'Dim the lights so the room feels cozy and warm.!'}]
--------------------------------------------------------------------------------
model -> [{'function_call': {'name': 'set_light_values', 'args': {'brightness': 50.0, 'color_temp': 'warm'}}}]
--------------------------------------------------------------------------------
def call_function(function_call, functions):
    function_name = function_call.name
    function_args = function_call.args
    return functions[function_name](**function_args)

functions = {
    "set_light_values": set_light_values,
}


part = response.candidates[0].content.parts[0]

# Check if it's a function call; in real use you'd need to also handle text
# responses as you won't know what the model will respond with.
if part.function_call:
    result = call_function(part.function_call, functions)

print(result)
{'brightness': 50.0, 'colorTemperature': 'warm'}
1 Like