Aidoc
Aidoc
Source: https://ai.google.dev/gemini-api/docs/openai#javascript
1
Speech generation → https://ai.google.dev/gemini-api/docs/speech-genera
tion
Music generation → https://ai.google.dev/gemini-api/docs/music-generatio
n
Long context → https://ai.google.dev/gemini-api/docs/long-context
Structured output → https://ai.google.dev/gemini-api/docs/structured-out
put
Thinking → https://ai.google.dev/gemini-api/docs/thinking
Function calling → https://ai.google.dev/gemini-api/docs/function-calling
Document understanding → https://ai.google.dev/gemini-api/docs/docu
ment-processing
Image understanding → https://ai.google.dev/gemini-api/docs/image-und
erstanding
Video understanding → https://ai.google.dev/gemini-api/docs/video-unde
rstanding
Audio understanding → https://ai.google.dev/gemini-api/docs/audio
Code execution → https://ai.google.dev/gemini-api/docs/code-execution
URL context → https://ai.google.dev/gemini-api/docs/url-context
Google Search → https://ai.google.dev/gemini-api/docs/google-search
Guides
Prompt engineering → https://ai.google.dev/gemini-api/docs/prompting-s
trategies
Resources
Migrate to Gen AI SDK → https://ai.google.dev/gemini-api/docs/migrate
Release notes → https://ai.google.dev/gemini-api/docs/changelog
2
API troubleshooting → https://ai.google.dev/gemini-api/docs/troubleshoo
ting
Fine-tuning → https://ai.google.dev/gemini-api/docs/model-tuning
Policies
Terms of service → https://ai.google.dev/gemini-api/terms
Available regions → https://ai.google.dev/gemini-api/docs/available-region
s
Additional usage polices → https://ai.google.dev/gemini-api/docs/usage-
policies
Gemini models are accessible using the OpenAI libraries (Python
and TypeScript / Javascript) along with the REST API, by updating
three lines of code and using your Gemini API key → https://aistudio.go
ogle.com/apikey. If you aren't already using the OpenAI libraries, we
recommend that you call the Gemini API directly → https://ai.google.de
v/gemini-api/docs/quickstart.
3
Python
from ope
nai import Ope
nAI
client = Ope
nAI(
api_key="GEM
INI_API_KEY",
base_url="https://gen
er
a
tivelan
‐
guage.googleapis.com/v1beta/ope
nai/"
)
response = client.chat.completions.cre
ate(
model="gemini-2.5-
flash",
mes
sages=[
{"role": "sys
tem", "content": "You are a help
ful
assis
tant."},
{
"role": "user",
"con
tent": "Explain to me how AI works"
}
]
)
print(response.choices[0].mes
sage)
4
JavaScript
import Ope
nAI from "ope
nai";
const ope
nai = new Ope
nAI({
apiKey: "GEM
INI_API_KEY",
baseURL: "https://gen
er
a
tivelan
‐
guage.googleapis.com/v1beta/ope
nai/"
});
con
sole.log(response.choices[0].mes
sage);
5
REST
curl "https://gener
a
tive
lan‐
guage.googleapis.com/v1beta/ope nai/chat/com
ple
tions" \
-H "Content-Type: appli
cation/json" \
-H "Authoriza
tion: Bearer GEMINI_API_KEY" \
-d '{
"model": "gemini-2.0-
flash",
"mes
sages": [
{"role": "user", "con tent": "Explain to me how AI
works"}
]
}'
Thinking
Gemini 2.5 models are trained to think through complex problems,
leading to significantly improved reasoning. The Gemini API comes
with a "thinking budget" parameter → https://ai.google.dev/gemini-api/doc
s/thinking#set-budget which gives fine grain control over how much the
model will think.
6
Unlike the Gemini API, the OpenAI API offers three levels of
thinking control: "low", "medium", and "high", which map to 1,024,
8,192, and 24,576 tokens, respectively.
If you want to disable thinking, you can set reasoning_effort to
"none" (note that reasoning cannot be turned off for 2.5 Pro models).
Python
from ope
nai import Ope
nAI
client = Ope
nAI(
api_key="GEM
INI_API_KEY",
base_url="https://gen
er
a
tivelan
‐
guage.googleapis.com/v1beta/ope
nai/"
)
response = client.chat.completions.cre
ate(
model="gemini-2.5-
flash",
rea
soning_effort="low",
mes
sages=[
{"role": "sys
tem", "content": "You are a help
ful
assis
tant."},
{
"role": "user",
"con
tent": "Explain to me how AI works"
}
]
)
print(response.choices[0].mes
sage)
7
JavaScript
import Ope
nAI from "ope
nai";
const ope
nai = new Ope
nAI({
apiKey: "GEM
INI_API_KEY",
baseURL: "https://gen
er
a
tivelan
‐
guage.googleapis.com/v1beta/ope
nai/"
});
con
sole.log(response.choices[0].mes
sage);
8
REST
curl "https://gener
a
tive
lan‐
guage.googleapis.com/v1beta/ope nai/chat/com
ple
tions" \
-H "Content-Type: appli
cation/json" \
-H "Authoriza
tion: Bearer GEMINI_API_KEY" \
-d '{
"model": "gemini-2.5-
flash",
"rea
soning_effort": "low",
"mes
sages": [
{"role": "user", "con tent": "Explain to me how AI
works"}
]
}'
9
Python
from ope
nai import Ope
nAI
client = Ope
nAI(
api_key="GEM
INI_API_KEY",
base_url="https://gen
er
a
tivelan
‐
guage.googleapis.com/v1beta/ope
nai/"
)
print(response.choices[0].mes
sage)
10
JavaScript
import Ope
nAI from "ope
nai";
const ope
nai = new Ope
nAI({
apiKey: "GEM
INI_API_KEY",
baseURL: "https://gen
er
a
tivelan
‐
guage.googleapis.com/v1beta/ope
nai/"
});
con
sole.log(response.choices[0].mes
sage);
11
REST
Streaming
The Gemini API supports streaming responses → https://ai.google.dev/ge
mini-api/docs/text-generation?lang=python#generate-a-text-stream.
12
Python
from ope
nai import Ope
nAI
client = Ope
nAI(
api_key="GEM
INI_API_KEY",
base_url="https://gen
er
a
tivelan
‐
guage.googleapis.com/v1beta/ope
nai/"
)
response = client.chat.completions.cre
ate(
model="gemini-
2.0-flash",
mes
sages=[
{"role": "system", "content": "You are a help
ful
assis
tant."},
{"role": "user", "content": "Hello!"}
],
stream=True
)
13
JavaScript
import Ope
nAI from "ope
nai";
const ope
nai = new Ope
nAI({
apiKey: "GEM
INI_API_KEY",
baseURL: "https://gen
er
a
tivelan
‐
guage.googleapis.com/v1beta/ope
nai/"
});
main();
14
REST
curl "https://gener
a
tive
lan‐
guage.googleapis.com/v1beta/ope nai/chat/com
ple
tions" \
-H "Content-Type: appli
cation/json" \
-H "Authoriza
tion: Bearer GEMINI_API_KEY" \
-d '{
"model": "gemini-2.0-
flash",
"mes
sages": [
{"role": "user", "con tent": "Explain to me how AI
works"}
],
"stream": true
}'
Function calling
Function calling makes it easier for you to get structured data out‐
puts from generative models and is supported in the Gemini API → h
ttps://ai.google.dev/gemini-api/docs/function-calling/tutorial.
15
Python
from ope
nai import Ope
nAI
client = Ope
nAI(
api_key="GEM
INI_API_KEY",
base_url="https://gen
er
a
tivelan
‐
guage.googleapis.com/v1beta/ope
nai/"
)
tools = [
{
"type": "func tion",
"function": {
"name": "get_weather",
"description": "Get the weather in a given loca
‐
tion",
"para
meters": {
"type": "object",
"proper
ties": {
"location": {
"type": "string",
"descrip
tion": "The city and state, e.g.
Chicago, IL",
},
"unit": {"type": "string", "enum": ["celsius",
"fahrenheit"]},
},
"required": ["location"],
},
}
}
]
mes
sages = [{"role": "user", "con
tent": "What's the
16
weather like in Chicago today?"}]
response = client.chat.comple
tions.cre
ate(
model="gemini-
2.0-flash",
mes
sages=mes
sages,
tools=tools,
tool_choice="auto"
)
print(response)
17
JavaScript
import Ope
nAI from "ope
nai";
const ope
nai = new Ope
nAI({
apiKey: "GEM
INI_API_KEY",
baseURL: "https://gen
er
a
tivelan
‐
guage.googleapis.com/v1beta/ope
nai/"
});
18
];
con
sole.log(response);
}
main();
19
REST
curl "https://gener
a
tivelan
‐
guage.googleapis.com/v1beta/ope nai/chat/com
ple
tions" \
-H "Content-Type: applica
tion/json" \
-H "Autho
rization: Bearer GEMINI_API_KEY" \
-d '{
"model": "gemini-2.0-
flash",
"mes
sages": [
{
"role": "user",
"con
tent": "What'\''s the weather like in Chicago
today?"
}
],
"tools": [
{
"type": "function",
"func
tion": {
"name": "get_weather",
"description": "Get the current weather in a given
loca
tion",
"parame
ters": {
"type": "object",
"proper
ties": {
"location": {
"type": "string",
"descrip
tion": "The city and state, e.g.
Chicago, IL"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahren
heit"]
}
},
20
"required": ["loca
tion"]
}
}
}
],
"tool_choice": "auto"
}'
Image understanding
Gemini models are natively multimodal and provide best in class
performance on many common vision tasks → https://ai.google.dev/gemi
ni-api/docs/vision.
21
Python
import base64
from ope
nai import Ope
nAI
client = Ope
nAI(
api_key="GEM
INI_API_KEY",
base_url="https://gen
er
a
tivelan
‐
guage.googleapis.com/v1beta/ope
nai/"
)
# Func
tion to encode the image
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return
base64.b64encode(image_file.read()).decode('utf-8')
# Get
ting the base64 string
base64_image = encode_image("Path/to/agi/image.jpeg")
22
},
},
],
}
],
)
print(response.choices[0])
23
JavaScript
import Ope
nAI from "ope
nai";
import fs from 'fs/promises';
const ope
nai = new Ope
nAI({
apiKey: "GEM
INI_API_KEY",
baseURL: "https://gen
er
a
tive
lan
‐
guage.googleapis.com/v1beta/ope
nai/"
});
async func
tion encodeImage(imagePath) {
try {
const image
Buffer = await fs.readFile(imagePath);
return imageBuffer.toString('base64');
} catch (error) {
con
sole.error("Error encoding image:", error);
return null;
}
}
async func
tion main() {
const imagePath = "Path/to/agi/image.jpeg";
const base64Image = await encodeIm
age(imagePath);
const messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is in this image?",
},
{
"type": "image_url",
24
"image_url": {
"url": `data:image/jpeg;base64,${base64Image}`
},
},
],
}
];
try {
const response = await ope
nai.chat.com
ple
tions.cre
‐
ate({
model: "gemini-
2.0-
flash",
mes
sages: mes
sages,
});
con
sole.log(response.choices[0]);
} catch (error) {
con
sole.error("Error call
ing Gem
ini API:", error);
}
}
main();
25
REST
bash -c '
base64_image=$(base64 -i "Path/to/agi/image.jpeg");
curl "https://gen
er
a
tive
lan‐
guage.googleapis.com/v1beta/openai/chat/com
ple
tions" \
-H "Content-
Type: appli
ca
tion/json" \
-H "Autho
riza
tion: Bearer GEMINI_API_KEY" \
-d "{
\"model\": \"gemini-
2.0-flash\",
\"mes
sages\": [
{
\"role\": \"user\",
\"con
tent\": [
{ \"type\": \"text\", \"text\": \"What is in
this image?\" },
{
\"type\": \"image_url\",
\"image_url\": { \"url\":
\"data:image/jpeg;base64,${base64_image}\" }
}
]
}
]
}"
'
Generate an image
Generate an image:
26
Python
import base64
from ope
nai import OpenAI
from PIL import Image
from io import Byte
sIO
client = Ope
nAI(
api_key="GEM
INI_API_KEY",
base_url="https://gen
er
a
tivelan
‐
guage.googleapis.com/v1beta/ope
nai/",
)
response = client.images.gener
ate(
model="imagen-3.0-
generate-002",
prompt="a portrait of a sheepadoo
dle wear
ing a cape",
response_for
mat='b64_json',
n=1,
)
27
JavaScript
import Ope
nAI from "ope
nai";
const ope
nai = new Ope
nAI({
apiKey: "GEM
INI_API_KEY",
baseURL: "https://gen
er
a
tive
lan
‐
guage.googleapis.com/v1beta/ope
nai/",
});
con
sole.log(image.data);
}
main();
28
REST
curl "https://gener
a
tive
lan‐
guage.googleapis.com/v1beta/ope nai/images/gen
er
a
tions" \
-H "Content-
Type: appli ca
tion/json" \
-H "Autho
rization: Bearer GEMINI_API_KEY" \
-d '{
"model": "imagen-3.0-generate-002",
"prompt": "a portrait of a sheepadoodle wear
ing a
cape",
"response_format": "b64_json",
"n": 1,
}'
Audio understanding
Analyze audio input:
29
Python
import base64
from ope
nai import Ope
nAI
client = Ope
nAI(
api_key="GEM
INI_API_KEY",
base_url="https://gen
er
a
tivelan
‐
guage.googleapis.com/v1beta/ope
nai/"
)
30
],
)
print(response.choices[0].mes
sage.con
tent)
31
JavaScript
32
},
],
});
con
sole.log(response.choices[0].mes
sage.con
tent);
}
main();
33
REST
bash -c '
base64_audio=$(base64 -i
"/path/to/your/audio/file.wav");
curl "https://gen
er
a
tive
lan‐
guage.googleapis.com/v1beta/openai/chat/com
ple
tions" \
-H "Content-
Type: appli
ca
tion/json" \
-H "Autho
riza
tion: Bearer GEMINI_API_KEY" \
-d "{
\"model\": \"gemini-
2.0-flash\",
\"mes
sages\": [
{
\"role\": \"user\",
\"con
tent\": [
{ \"type\": \"text\", \"text\": \"Transcribe
this audio file.\" },
{
\"type\": \"input_audio\",
\"input_audio\": {
\"data\": \"${base64_audio}\",
\"for
mat\": \"wav\"
}
}
]
}
]
}"
'
34
Structured output
Gemini models can output JSON objects in any structure you define
→ https://ai.google.dev/gemini-api/docs/structured-output.
Python
from pydan
tic import Base
Model
from ope
nai import Ope
nAI
client = Ope
nAI(
api_key="GEM
INI_API_KEY",
base_url="https://gen
er
a
tivelan
‐
guage.googleapis.com/v1beta/ope
nai/"
)
class Cal
endarEvent(Base
Model):
name: str
date: str
par
tic
i
pants: list[str]
com
pletion = client.beta.chat.com ple
tions.parse(
model="gemini-2.0-flash",
mes
sages=[
{"role": "sys
tem", "con tent": "Extract the event
informa
tion."},
{"role": "user", "con tent": "John and Susan are
going to an AI con fer
ence on Fri day."},
],
response_format=Calen
darEvent,
)
print(com
ple
tion.choices[0].mes
sage.parsed)
35
JavaScript
import Ope
nAI from "ope
nai";
import { zodRespon
se
For
mat } from "ope
nai/helpers/zod";
import { z } from "zod";
const ope
nai = new Ope
nAI({
apiKey: "GEM
INI_API_KEY",
baseURL: "https://gen
er
a
tivelan
‐
guage.googleapis.com/v1beta/ope
nai"
});
const Calen
darEvent = z.object({
name: z.string(),
date: z.string(),
par
tic
i
pants: z.array(z.string()),
});
const comple
tion = await openai.beta.chat.com
ple
‐
tions.parse({
model: "gemini-2.0-
flash",
mes
sages: [
{ role: "system", con
tent: "Extract the event infor
ma‐
tion." },
{ role: "user", content: "John and Susan are going to
an AI con
ference on Fri
day" },
],
response_format: zodRespon
seFor
mat(Cal
en
darEvent,
"event"),
});
36
Embeddings
Text embeddings measure the relatedness of text strings and can be
generated using the Gemini API → https://ai.google.dev/gemini-api/docs/em
beddings.
Python
from ope
nai import Ope
nAI
client = Ope
nAI(
api_key="GEM
INI_API_KEY",
base_url="https://gen
er
a
tivelan
‐
guage.googleapis.com/v1beta/ope
nai/"
)
response = client.embed
dings.cre
ate(
input="Your text string goes here",
model="text-
embedding-004"
)
print(response.data[0].embed
ding)
37
JavaScript
import Ope
nAI from "ope
nai";
const ope
nai = new Ope
nAI({
apiKey: "GEM
INI_API_KEY",
baseURL: "https://gen
er
a
tivelan
‐
guage.googleapis.com/v1beta/ope
nai/"
});
async func
tion main() {
const embed
ding = await ope
nai.embed
dings.cre
ate({
model: "text-
embedding-004",
input: "Your text string goes here",
});
con
sole.log(embed
ding);
}
main();
REST
curl "https://gen
er
ative
lan‐
guage.googleapis.com/v1beta/ope nai/embed
dings" \
-H "Content-
Type: application/json" \
-H "Autho
riza
tion: Bearer GEM INI_API_KEY" \
-d '{
"input": "Your text string goes here",
"model": "text-
embedding-004"
}'
38
extra_body
There are several features supported by Gemini that are not avail‐
able in OpenAI models but can be enabled using the extra_body
field.
extra_body features
safety_set
‐ Corresponds to Gemini's SafetySetting.
tings
cached_con
tent Corresponds to Gemini's GenerateContentRe‐
quest.cached_con tent.
think
ing_con
‐ Corresponds to Gemini's ThinkingConfig.
fig
cached_con
tent
Here's an example of using extra_body to set cached_content:
39
Python
from ope
nai import Ope
nAI
client = Ope
nAI(
api_key=MY_API_KEY,
base_url="https://gen
er
a
tive
lan
‐
guage.googleapis.com/v1beta/"
)
stream = client.chat.completions.cre
ate(
model="gemini-2.5-pro",
n=1,
mes
sages=[
{
"role": "user",
"con
tent": "Summa
rize the video"
}
],
stream=True,
stream_options={'include_usage': True},
extra_body={
'extra_body':
{
'google': {
'cached_con
tent': "cachedCon
‐
tents/0000aaaa1111bbbb2222cccc3333dddd4444eeee"
}
}
}
)
40
print(chunk.usage.to_dict())
List models
Get a list of available Gemini models:
Python
from ope
nai import Ope
nAI
client = Ope
nAI(
api_key="GEM
INI_API_KEY",
base_url="https://gen
er
a
tive
lan
‐
guage.googleapis.com/v1beta/ope
nai/"
)
mod
els = client.mod
els.list()
for model in mod
els:
print(model.id)
41
JavaScript
import Ope
nAI from "ope
nai";
const ope
nai = new Ope
nAI({
apiKey: "GEM
INI_API_KEY",
baseURL: "https://gen
er
a
tive
lan
‐
guage.googleapis.com/v1beta/ope
nai/",
});
async func
tion main() {
const list = await ope
nai.mod
els.list();
REST
curl https://gen
er
a
tive
lan
guage.googleapis.com/v1beta/ope
‐
nai/mod
els \
-H "Autho
riza
tion: Bearer GEM
INI_API_KEY"
Retrieve a model
Retrieve a Gemini model:
42
Python
from ope
nai import Ope
nAI
client = Ope
nAI(
api_key="GEM
INI_API_KEY",
base_url="https://gen
er
a
tive
lan
‐
guage.googleapis.com/v1beta/ope
nai/"
)
model = client.mod
els.retrieve("gemini-
2.0-
flash")
print(model.id)
JavaScript
import Ope
nAI from "ope
nai";
const ope
nai = new Ope
nAI({
apiKey: "GEM
INI_API_KEY",
baseURL: "https://gen
er
a
tive
lan
‐
guage.googleapis.com/v1beta/ope
nai/",
});
async func
tion main() {
const model = await ope
nai.mod
els.retrieve("gemini-
2.0-
flash");
con
sole.log(model.id);
}
main();
43
REST
curl https://gen
er
ative
lan
guage.googleapis.com/v1beta/ope
‐
nai/mod
els/gemini-
2.0-flash \
-H "Autho
riza
tion: Bearer GEM INI_API_KEY"
What's next
Try our OpenAI Compatib
ility Colab → https://colab.sandbox.google.com/
github/google-gemini/cookbook/blob/main/quickstarts/Get_started_OpenAI_Compat
ibility.ipynbto work through more detailed examples.
Except as otherwise noted, the content of this page is licensed under
the Creative Commons Attribution 4.0 License → https://creativecommon
s.org/licenses/by/4.0/, and code samples are licensed under the Apache
2.0 License → https://www.apache.org/licenses/LICENSE-2.0. For details, see
the Google Developers Site Policies → https://developers.google.com/site-p
olicies. Java is a registered trademark of Oracle and/or its affilia
tes.
Last updated 2025-06-18 UTC.
44