I’m exploring whether two specific features of Gemini can be used together:
Using OpenAI’s Client with Gemini
Gemini supports OpenAI’s client library and OpenAI-style completion format, allowing it to be used similarly to OpenAI’s models. For example:
from openai import OpenAI
client = OpenAI(
api_key="GEMINI_API_KEY",
base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)
response = client.chat.completions.create(
model="gemini-2.0-flash",
n=1,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain to me how AI works"}
]
)
print(response.choices[0].message)
Uploading Files & Referencing Them in Completions
Gemini also supports file uploads, allowing models to analyze images or documents. This is done using Google’s genai client:
from google import genai
client = genai.Client()
myfile = client.files.upload(file=media / "Cajun_instruments.jpg")
print(f"{myfile=}")
result = client.models.generate_content(
model="gemini-2.0-flash",
contents=[
myfile,
"\n\n",
"Can you tell me about the instruments in this photo?",
],
)
print(f"{result.text=}")
My Question
Can these two features be used together? Specifically, is it possible to upload a file via genai.Client() and then reference it in a completion request using the OpenAI-compatible client format? If anyone has tested this or found workarounds, I’d love to hear your insights!
Please see the docs I am referring to for Files and OpenAI Format.