Question: Function Response Reuse Capabilities of Gemini?
Hi all!
I am a first-time user of the Gemini API (long-time user of the OpenAI API). I am trying to migrate some use cases using tool calling to Gemini, but I see some behavior that is not working well when using tool calling.
Example Use Case: Perform a Booking. Gemini has access to two tools: getOffers
and bookOffer
. The getOffers
method produces some output and some UUIDs (the offer IDs). The UUIDs are not something I want to communicate to the user (and Gemini also chooses not to communicate them when showing me the offers). But when asking Gemini to book the first offer, it explicitly asks for the booking_id
that the previous function call returned and is still in the chat history—and I never saw.
Here is the source code if my explanation was insufficient—it should work with a given GOOGLE_API_KEY
in .envrc
and proper included libs (google-genai
, python-dotenv
):
import os, datetime, json
from textwrap import dedent
from dotenv import load_dotenv
load_dotenv(".envrc")
from google import genai
from google.genai import types
def getOffers(date: str, city: str):
""" Perform a search for the given date. Return the available offers.
Args:
date: The date for which the options should be searched. Format is "YYYY-MM-DD".
Returns:
A json object containing all available offers
"""
offer1 = dict()
offer1["offer_id"] = "A4ABDD9F-9AC8-4D85-A78B-BDD16544CDF3"
offer1["offer_name"] = "Hotel Hamilton, New York, 1st class suite. Child care and Breakfast included"
offer1["price"] = "$1098.00"
offer2 = dict()
offer2["offer_id"] = "3CF05DF5-8F65-41BD-994B-6AC049385037"
offer2["offer_name"] = "Hotel Grand, New York, Executive suite. Ocean view and one coffee included"
offer2["price"] = "$1298.00"
resultDict = dict()
resultDict["offers"] = [offer1, offer2]
# resultDict["system-instruction"] = "Do not expose the offer_id to the user. They are only for internal use."
return json.dumps(resultDict)
def bookOffer(offer_id: str):
""" Book the offer with the given offer_id.
Args:
offer_id: The id of the offer to be booked.
Returns:
A json object containing the booking status and the booking id.
"""
resultDict = dict()
resultDict["status"] = "Booking successful, email will arrive shortly."
resultDict["booking_id"] = "1234567890"
resultDict["system-instruction"] = "Inform the user about succeeded booking, give him the booking id and communicate that an email will be sent shortly."
return json.dumps(resultDict)
sysprompt = dedent("""
You are 'Bot', a helpful and polite assistant. You have access to some APIs and tools to help users.
Always respond succinctly, clearly, courteously, and respectfully, but also competently and informatively, and always in the language requested.
The format of your response should be in Markdown. Please avoid using smileys. Appropriate emojis are allowed when you have completed a task.
Today is the DATE and it is now TIME o'clock.
""")
config = types.GenerateContentConfig(
temperature=0,
tools=[getOffers, bookOffer],
system_instruction=sysprompt.replace("DATE", datetime.datetime.now().strftime("%d/%m/%Y")).replace("TIME", datetime.datetime.now().strftime("%H:%M")),
automatic_function_calling=types.AutomaticFunctionCallingConfig(maximum_remote_calls=20),
)
client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
chat = client.chats.create(model='gemini-2.0-flash', config=config, history=[])
query = "What offers are available for 2022-12-24 in New York?"
print("Query: ", query)
response = chat.send_message(query)
print("Response: ", response.text)
query = "Book the first offer."
print("Query: ", query)
response = chat.send_message(query)
print("Response: ", response.text)
Results:
Query: What offers are available for 2022-12-24 in New York?
Response: OK. For 2022-12-24 in New York, I found two offers: Hotel Hamilton, New York, 1st class suite. Child care and Breakfast included for $1098.00, and Hotel Grand, New York, Executive suite. Ocean view and one coffee included for $1298.00.
Query: Book the first offer.
Response: What is the offer ID of the offer you would like to book?
The function call of Query 1 and the function response are still in the chat’s _curated_history
field:
It seems that Gemini is not able to reuse previous function responses for the user (unlike the OpenAI API, which is perfectly capable of this task). Am I right, or am I missing something?
Thank you in advance and kind regards,
Stefan