Hi everyone,
I’m developing an application using the Google Gen AI Python SDK, and I’ve encountered a change in the way function calling is handled between the old and new versions of the library.
In my old implementation, function calling appears to be handled automatically in a single call. For example, my old code looked like this:
# Old Code Snippet
response = client.models.generate_content(
model='gemini-2.0-flash',
contents='What is the weather like in Boston?',
config={
"temperature": 0,
"max_output_tokens": 100,
# other configurations...
}
)
print(response.text)
In this version, the model would automatically call the function and integrate the result without me explicitly handling two separate calls.
However, in the new version of the SDK, the process is split into two steps. The new code snippet looks like this:
# New Code Snippet for Function Calling
response = client.models.generate_content(
model='gemini-2.0-flash',
contents=contents, # contents includes user messages and possibly system instructions
config=generation_config # generation_config now includes system_instruction and tool settings
)
# Check if the model has returned a function call
if response.function_calls:
function_call = response.function_calls[0]
# Execute the function (e.g., get_weather) externally
weather_data = get_weather(function_call.args.get('city_name'))
function_response_part = types.Part.from_function_response(
name=function_call.name,
response=weather_data
)
function_response_content = types.Content(
role="tool",
parts=[function_response_part]
)
# Re-call the model to integrate the function result
response = client.models.generate_content(
model='gemini-2.0-flash',
contents=contents + [response.candidates[0].content, function_response_content],
config=generation_config
)
In the new implementation, the model is invoked twice: the first call returns a response that may include a function call, and then I explicitly execute the function and send the result back to the model in a second call.
My questions are:
- Is it correct that with the new SDK we are required to perform two separate calls for function calling (one to obtain the function call and another to integrate its result)?
- Where in the documentation is it stated that function calling is handled in these two stages?
- Are there any recommendations or best practices from the community for implementing function calling with this new SDK?
Thanks in advance for your help and clarifications!