I believe your goal is as follows.
- You want to convert your top script with google-generativeai.
In this case, how about the following modification?
Modified script:
import google.generativeai as genai
import google.ai.generativelanguage as glm
apiKey = "###" # Please set your API key.
genai.configure(api_key=apiKey)
tool = glm.Tool(
function_declarations=[
glm.FunctionDeclaration(
name="find_movies",
description="find movie titles currently playing in theaters based on any description, genre, title words, etc.",
parameters=glm.Schema(
type=glm.Type.OBJECT,
properties={
"location": glm.Schema(
type=glm.Type.STRING,
description="The city and state, e.g. San Francisco, CA or a zip code e.g. 95616",
),
"description": glm.Schema(
type=glm.Type.STRING,
description="Any kind of description including category or genre, title words, attributes, etc.",
),
},
required=["description"],
),
),
glm.FunctionDeclaration(
name="find_theaters",
description="find theaters based on location and optionally movie title which is currently playing in theaters",
parameters=glm.Schema(
type=glm.Type.OBJECT,
properties={
"location": glm.Schema(
type=glm.Type.STRING,
description="The city and state, e.g. San Francisco, CA or a zip code e.g. 95616",
),
"movie": glm.Schema(
type=glm.Type.STRING,
description="Any movie title",
),
},
required=["location"],
),
),
glm.FunctionDeclaration(
name="get_showtimes",
description="Find the start times for movies playing in a specific theater",
parameters=glm.Schema(
type=glm.Type.OBJECT,
properties={
"location": glm.Schema(
type=glm.Type.STRING,
description="The city and state, e.g. San Francisco, CA or a zip code e.g. 95616",
),
"movie": glm.Schema(
type=glm.Type.STRING,
description="Any movie title",
),
"theater": glm.Schema(
type=glm.Type.STRING,
description="Name of the theater",
),
"date": glm.Schema(
type=glm.Type.STRING,
description="Date for requested showtime",
),
},
required=["location", "movie", "theater", "date"],
),
),
]
)
model = genai.GenerativeModel("gemini-pro", tools=[tool])
messages = [
glm.Part(text="Which theaters in Mountain View show Barbie movie?"),
glm.Part(
function_call=glm.FunctionCall(
name="find_theaters",
args={"location": "Mountain View, CA", "movie": "Barbie"},
)
),
glm.Part(
function_response=glm.FunctionResponse(
name="find_theaters",
response={
"name": "find_theaters",
"content": {
"movie": "Barbie",
"theaters": [
{
"name": "AMC Mountain View 16",
"address": "2000 W El Camino Real, Mountain View, CA 94040",
},
{
"name": "Regal Edwards 14",
"address": "245 Castro St, Mountain View, CA 94040",
},
],
},
},
)
),
]
response = model.generate_content(messages)
print(response.text)
Testing:
When this script is run, the following result is obtained.
There are two theaters in Mountain View showing Barbie movie: AMC Mountain View 16 and Regal Edwards 14.
Answer from Tanaike on Stack OverflowGoogle gemini generate_content is not working in python API using function calling function_response - Stack Overflow
How to set a timeout on Google Gemini generate content request with the Vertex AI SDK for Python - Stack Overflow
I want to `generate_content` through the Gemini API with Gemma models (but not instruction tuned) in python but I can't figure it out?
python - How to Use Gemini API to Process and Extract Data from an Image? - Stack Overflow
Q3: What's the safest API Key management approach? How to handle different environments (dev/staging/prod)?
Q1: What's the difference between `google-generativeai` and `vertexai` SDKs? Which should I use?
Q5: For long documents (100-page PDFs, 1-hour videos), can Gemini's context really handle it?
Videos
For anyone (or bot) that arrives here looking for how to set a timeout when using the modern-as-of-late-2024 Google GenAI SDK that can also use the Vertex API (more info at the repository), setting a timeout using HttpOptions works.
Here is an example with a 30-second timeout:
from google import genai
from google.genai import types
client = genai.Client(
http_options=types.HttpOptions(timeout=30_000) # timeout is in milliseconds
)
response = client.models.generate_content(
model='gemini-2.5-flash', contents='Why is the sky blue?'
)
print(response.text)
If you would use the async version of generate_content, you can utilize the native Python's asyncio function wait_for. If a timeout occurs, it cancels the task and raises TimeoutError. The script could look like:
import asyncio
model = GenerativeModel('gemini-pro')
timeout = 5 # seconds
try:
response = await asyncio.wait_for(
model.generate_content_async("Pick a number"),
timeout=timeout,
)
except asyncio.exceptions.TimeoutError:
pass
I am working on a small RAG pipeline and while testing it, I was using `gemma3:4b` locally on my computer with ollama to generate responses, but this was not the fastest (though the quality of responses was good). So I thought I would try to use the free tier of the gemini API and get faster responses and maybe even be able to use the 12b model (so I thought). But I am unable to find any documentation on using these...
From playing around in AI Studio, I can see that I could use the `gemma-3-Xb-it` (i.e., instruction-tuned) models but the responses I am getting from these for my conversational RAG experience are much worse. Trying to replace the `-it` with `-pt` (pre-trained) simply threw an error that the model does not exist.
Would appreciate any help on trying to figure out if its even possible to use gemma models through the API (because although `gemini-2.X-flash-lite` works fine, I just somehow preferred the tone and responses of the gemma model even with the same system prompt etc.)
anyways, thanks!
For Python, it is easier to work with the Python unified SDK than playing directly with the endpoints using libs like requests.
Using the Python SDK, you have two ways to send images within your prompts:
- you can send them inline, very straightforward:
image = Image.open(img_path)
response = client.models.generate_content(
model=MODEL_ID,
contents=[
image,
"ask something about the image here"
]
)
Or you can use the (File API)[https://googleapis.github.io/python-genai/genai.html#genai.files.AsyncFiles.upload] for payloads that may be higher than 20MB. Using that you will first upload the media using the SDK:
file_ref = client.files.upload(path=img_path)
And then you will reference the file API object file_ref within your prompt:
response = client.models.generate_content(
model=MODEL_ID,
contents=[
file_ref,
"ask something about the image here"
]
)
The response for your requests using those example requests will be text only and will can handle the results in the same way you do for text only prompts - like exploring the response object with structures like response.text for the model answer in text. more details about the response object structure can be found at the SDK reference doc.
This Gemini API get started notebook may be useful as well.
hope that helps.
i have built web tool for image editing and also mobile app you can test the web tool live here https://gemma-ai.vercel.app/
Gemini take image input as base64 and also its return image in base64 format
To perform image editing, add an image as input. The following example demonstrats uploading base64 encoded images
from google import genai
from google.genai import types
from PIL import Image
from io import BytesIO
import PIL.Image
image = PIL.Image.open('/path/to/image.png')
client = genai.Client()
text_input = ('Hi, This is a picture of me.'
'Can you add a llama next to me?',)
response = client.models.generate_content(
model="gemini-2.0-flash-exp-image-generation",
contents=[text_input, image],
config=types.GenerateContentConfig(
response_modalities=['Text', 'Image']
)
)
for part in response.candidates[0].content.parts:
if part.text is not None:
print(part.text)
elif part.inline_data is not None:
image = Image.open(BytesIO(part.inline_data.data))
image.show()
for more information you can visit :https://ai.google.dev/gemini-api/docs/image-generation#python