KEMBAR78
Aidoc | PDF | Java Script | Computer Data
0% found this document useful (0 votes)
46 views44 pages

Aidoc

The document provides guidance on using the Gemini API with OpenAI libraries, specifically for JavaScript and Python. It outlines how to update code to access Gemini models, including the necessary API key and endpoint adjustments. Additionally, it discusses the thinking capabilities of Gemini models and provides examples for implementing various features like reasoning effort and streaming responses.

Uploaded by

Tech Flyer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views44 pages

Aidoc

The document provides guidance on using the Gemini API with OpenAI libraries, specifically for JavaScript and Python. It outlines how to update code to access Gemini models, including the necessary API key and endpoint adjustments. Additionally, it discusses the thinking capabilities of Gemini models and provides examples for implementing various features like reasoning effort and streaming responses.

Uploaded by

Tech Flyer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

OpenAI compatibility

Source: https://ai.google.dev/gemini-api/docs/openai#javascript

Skip to main con­tent


Mod­els → https://ai.google.dev/gemini-api/docs

Gem­ini API docs → https://ai.google.dev/gemini-api/docs


API Ref­er­ence → https://ai.google.dev/api
Cook­book → https://github.com/google-gemini/cookbook
Com­mu­nity → https://discuss.ai.google.dev/c/gemini-api/
Solu­tions
Code assis­tance
Show­case
Com­mu­nity
Get started
Overview → https://ai.google.dev/gemini-api/docs
Quick­start → https://ai.google.dev/gemini-api/docs/quickstart
API keys → https://ai.google.dev/gemini-api/docs/api-key
Libraries → https://ai.google.dev/gemini-api/docs/libraries
Ope­nAI com­pat­ib ­ il­ity → https://ai.google.dev/gemini-api/docs/openai
Mod­els
All mod­els → https://ai.google.dev/gemini-api/docs/models
Pric­ing → https://ai.google.dev/gemini-api/docs/pricing
Rate lim­its → https://ai.google.dev/gemini-api/docs/rate-limits
Billing info → https://ai.google.dev/gemini-api/docs/billing
Model Capa­bil­it­ ies
Text gen­er­a­tion → https://ai.google.dev/gemini-api/docs/text-generation
Image gen­er­a­tion → https://ai.google.dev/gemini-api/docs/image-generati
on
Video gen­er­a­tion → https://ai.google.dev/gemini-api/docs/video

1
Speech gen­er­a­tion → https://ai.google.dev/gemini-api/docs/speech-genera
tion
Music gen­er­a­tion → https://ai.google.dev/gemini-api/docs/music-generatio
n
Long con­text → https://ai.google.dev/gemini-api/docs/long-context
Struc­tured out­put → https://ai.google.dev/gemini-api/docs/structured-out
put
Think­ing → https://ai.google.dev/gemini-api/docs/thinking
Func­tion call­ing → https://ai.google.dev/gemini-api/docs/function-calling
Doc­u­ment under­stand­ing → https://ai.google.dev/gemini-api/docs/docu
ment-processing
Image under­stand­ing → https://ai.google.dev/gemini-api/docs/image-und
erstanding
Video under­stand­ing → https://ai.google.dev/gemini-api/docs/video-unde
rstanding
Audio under­stand­ing → https://ai.google.dev/gemini-api/docs/audio
Code exe­cu­tion → https://ai.google.dev/gemini-api/docs/code-execution
URL con­text → https://ai.google.dev/gemini-api/docs/url-context
Google Search → https://ai.google.dev/gemini-api/docs/google-search
Guides
Prompt engi­neer­ing → https://ai.google.dev/gemini-api/docs/prompting-s
trategies

Con­text caching → https://ai.google.dev/gemini-api/docs/caching


Files API → https://ai.google.dev/gemini-api/docs/files
Token count­ing → https://ai.google.dev/gemini-api/docs/tokens
Embed­dings → https://ai.google.dev/gemini-api/docs/embeddings

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 trou­bleshoot­ing → https://ai.google.dev/gemini-api/docs/troubleshoo
ting
Fine-​tuning → https://ai.google.dev/gemini-api/docs/model-tuning

Poli­cies
Terms of ser­vice → https://ai.google.dev/gemini-api/terms
Avail­able regions → https://ai.google.dev/gemini-api/docs/available-region
s
Addi­tional usage polices → https://ai.google.dev/gemini-api/docs/usage-
policies
Gem­ini mod­els are acces­si­ble using the Ope­nAI libraries (Python
and Type­Script / Javascript) along with the REST API, by updat­ing
three lines of code and using your Gem­ini API key → https://aistudio.go
ogle.com/apikey. If you aren't already using the Ope­nAI libraries, we
rec­om­mend that you call the Gem­ini 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­

tive­lan­

guage.googleapis.com/v1beta/ope­
nai/"
)

response = client.chat.com­ple­tions.cre­
ate(
model="gemini-​2.5-​
flash",
mes­
sages=[
{"role": "sys­
tem", "con­tent": "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­

tive­lan­

guage.googleapis.com/v1beta/ope­
nai/"
});

const response = await ope­nai.chat.com­


ple­
tions.cre­ate({
model: "gemini-​2.0-​
flash",
mes­
sages: [
{ role: "sys­
tem", con­
tent: "You are a help­ful
assis­
tant." },
{
role: "user",
con­
tent: "Explain to me how AI works",
},
],
});

con­
sole.log(response.choices[0].mes­
sage);

5
REST

curl "https://gen­er­

tive­
lan­‐
guage.googleapis.com/v1beta/ope­ nai/chat/com­
ple­
tions" \
-H "Content-​Type: appli­
ca­tion/json" \
-H "Autho­riza­
tion: Bearer GEM­INI_API_KEY" \
-d '{
"model": "gemini-​2.0-​
flash",
"mes­
sages": [
{"role": "user", "con­ tent": "Explain to me how AI
works"}
]
}'

What changed? Just three lines!


INI_API_KEY": Replace "GEM­
api_key="GEM­ INI_API_KEY" with your
actual Gem­ini API key, which you can get in Google AI Stu­dio
→ https://aistudio.google.com/.
base_url="https://gen­
er­

tive­
lan­

nai/": This tells the Ope­nAI
guage.googleapis.com/v1beta/ope­
library to send requests to the Gem­ini API end­point instead of
the default URL.
model="gemini-​ flash": Choose a com­pat­ib
2.0-​ ­ le Gem­ini model

Think­ing
Gem­ini 2.5 mod­els are trained to think through com­plex prob­lems,
lead­ing to sig­nif­i­cantly improved rea­son­ing. The Gem­ini API comes
with a "think­ing bud­get" para­me­ter → https://ai.google.dev/gemini-api/doc
s/thinking#set-budget which gives fine grain con­trol over how much the
model will think.

6
Unlike the Gem­ini API, the Ope­nAI API offers three lev­els of
think­ing con­trol: "low", "medium", and "high", which map to 1,024,
8,192, and 24,576 tokens, respec­tively.
If you want to dis­able think­ing, you can set rea­son­ing_effort to
"none" (note that rea­son­ing can­not be turned off for 2.5 Pro mod­els).

Python

from ope­
nai import Ope­
nAI

client = Ope­
nAI(
api_key="GEM­
INI_API_KEY",
base_url="https://gen­
er­

tive­lan­

guage.googleapis.com/v1beta/ope­
nai/"
)

response = client.chat.com­ple­tions.cre­
ate(
model="gemini-​2.5-​
flash",
rea­
son­ing_effort="low",
mes­
sages=[
{"role": "sys­
tem", "con­tent": "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­

tive­lan­

guage.googleapis.com/v1beta/ope­
nai/"
});

const response = await ope­nai.chat.com­


ple­
tions.cre­ate({
model: "gemini-​2.5-​
flash",
rea­
son­ing_effort: "low",
mes­
sages: [
{ role: "sys­
tem", con­
tent: "You are a help­ful
assis­
tant." },
{
role: "user",
con­
tent: "Explain to me how AI works",
},
],
});

con­
sole.log(response.choices[0].mes­
sage);

8
REST

curl "https://gen­er­

tive­
lan­‐
guage.googleapis.com/v1beta/ope­ nai/chat/com­
ple­
tions" \
-H "Content-​Type: appli­
ca­tion/json" \
-H "Autho­riza­
tion: Bearer GEM­INI_API_KEY" \
-d '{
"model": "gemini-​2.5-​
flash",
"rea­
son­ing_effort": "low",
"mes­
sages": [
{"role": "user", "con­ tent": "Explain to me how AI
works"}
]
}'

Gem­ini think­ing mod­els also pro­duce thought sum­maries → https://ai.


google.dev/gemini-api/docs/thinking#summaries and can use exact think­ing
bud­gets → https://ai.google.dev/gemini-api/docs/thinking#set-budget. You can
use the extra_body field to include these fields in your request.
Note that rea­son­ing_effort and think­ing_bud­get over­lap func­‐
tion­al­ity, so they can't be used at the same time.

9
Python

from ope­
nai import Ope­
nAI

client = Ope­
nAI(
api_key="GEM­
INI_API_KEY",
base_url="https://gen­
er­

tive­lan­

guage.googleapis.com/v1beta/ope­
nai/"
)

response = client.chat.com­ ple­


tions.cre­
ate(
model="gemini-​2.5-​
flash",
mes­
sages=[{"role": "user", "con­ tent": "Explain to me
how AI works"}],
extra_body={
'extra_body': {
"google": {
"think­
ing_con­
fig": {
"think­
ing_bud­
get": 800,
"include_thoughts": True
}
}
}
}
)

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­

tive­lan­

guage.googleapis.com/v1beta/ope­
nai/"
});

const response = await ope­nai.chat.com­ple­


tions.cre­
ate({
model: "gemini-​2.5-​
flash",
mes­
sages: [{role: "user", con­ tent: "Explain to me how
AI works",}],
extra_body: {
"google": {
"think­
ing_con­
fig": {
"think­
ing_bud­
get": 800,
"include_thoughts": true
}
}
}
});

con­
sole.log(response.choices[0].mes­
sage);

11
REST

curl "https://gen­ er­



tive­
lan­‐
guage.googleapis.com/v1beta/ope­ nai/chat/com­
ple­
tions" \
-H "Content-​ Type: appli­
ca­tion/json" \
-H "Autho­riza­tion: Bearer GEM­INI_API_KEY" \
-d '{
"model": "gemini-​ 2.5-​
flash",
"mes­sages": [{"role": "user", "con­ tent": "Explain to
me how AI works"}],
"extra_body": {
"google": {
"think­ing_con­
fig": {
"include_thoughts": true
}
}
}
}'

Stream­ing
The Gem­ini API sup­ports stream­ing 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­

tive­lan­

guage.googleapis.com/v1beta/ope­
nai/"
)

response = client.chat.com­ple­tions.cre­
ate(
model="gemini-​
2.0-​flash",
mes­
sages=[
{"role": "sys­tem", "con­tent": "You are a help­
ful
assis­
tant."},
{"role": "user", "con­tent": "Hello!"}
],
stream=True
)

for chunk in response:


print(chunk.choices[0].delta)

13
JavaScript

import Ope­
nAI from "ope­
nai";

const ope­
nai = new Ope­
nAI({
apiKey: "GEM­
INI_API_KEY",
baseURL: "https://gen­
er­

tive­lan­

guage.googleapis.com/v1beta/ope­
nai/"
});

async func­tion main() {


const com­ple­
tion = await ope­nai.chat.com­
ple­
tions.cre­‐
ate({
model: "gemini-​2.0-​
flash",
mes­sages: [
{"role": "sys­
tem", "con­
tent": "You are a help­ful
assis­
tant."},
{"role": "user", "con­
tent": "Hello!"}
],
stream: true,
});

for await (const chunk of com­


ple­
tion) {
con­
sole.log(chunk.choices[0].delta.con­
tent);
}
}

main();

14
REST

curl "https://gen­er­

tive­
lan­‐
guage.googleapis.com/v1beta/ope­ nai/chat/com­
ple­
tions" \
-H "Content-​Type: appli­
ca­tion/json" \
-H "Autho­riza­
tion: Bearer GEM­INI_API_KEY" \
-d '{
"model": "gemini-​2.0-​
flash",
"mes­
sages": [
{"role": "user", "con­ tent": "Explain to me how AI
works"}
],
"stream": true
}'

Func­tion call­ing
Func­tion call­ing makes it eas­ier for you to get struc­tured data out­‐
puts from gen­er­a­tive mod­els and is sup­ported in the Gem­ini 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­

tive­lan­

guage.googleapis.com/v1beta/ope­
nai/"
)

tools = [
{
"type": "func­ tion",
"func­tion": {
"name": "get_weather",
"descrip­tion": "Get the weather in a given loca­

tion",
"para­
me­ters": {
"type": "object",
"prop­er­
ties": {
"loca­tion": {
"type": "string",
"descrip­
tion": "The city and state, e.g.
Chicago, IL",
},
"unit": {"type": "string", "enum": ["cel­sius",
"fahren­heit"]},
},
"required": ["loca­tion"],
},
}
}
]

mes­
sages = [{"role": "user", "con­
tent": "What's the

16
weather like in Chicago today?"}]
response = client.chat.com­ple­
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­

tive­lan­

guage.googleapis.com/v1beta/ope­
nai/"
});

async func­tion main() {


const mes­sages = [{"role": "user", "con­ tent": "What's
the weather like in Chicago today?"}];
const tools = [
{
"type": "func­ tion",
"func­tion": {
"name": "get_weather",
"descrip­ tion": "Get the weather in a given loca­

tion",
"para­ me­
ters": {
"type": "object",
"prop­er­
ties": {
"loca­tion": {
"type": "string",
"descrip­
tion": "The city and state, e.g.
Chicago, IL",
},
"unit": {"type": "string", "enum": ["cel­‐
sius", "fahren­ heit"]},
},
"required": ["loca­tion"],
},
}
}

18
];

const response = await ope­


nai.chat.com­
ple­
tions.cre­
ate({
model: "gemini-​
2.0-​
flash",
mes­
sages: mes­
sages,
tools: tools,
tool_choice: "auto",
});

con­
sole.log(response);
}

main();

19
REST

curl "https://gen­er­

tive­lan­

guage.googleapis.com/v1beta/ope­ nai/chat/com­
ple­
tions" \
-H "Content-​Type: appli­ca­
tion/json" \
-H "Autho­
riza­tion: Bearer GEM­INI_API_KEY" \
-d '{
"model": "gemini-​2.0-​
flash",
"mes­
sages": [
{
"role": "user",
"con­
tent": "What'\''s the weather like in Chicago
today?"
}
],
"tools": [
{
"type": "func­tion",
"func­
tion": {
"name": "get_weather",
"descrip­tion": "Get the cur­rent weather in a given
loca­
tion",
"para­me­
ters": {
"type": "object",
"prop­er­
ties": {
"loca­tion": {
"type": "string",
"descrip­
tion": "The city and state, e.g.
Chicago, IL"
},
"unit": {
"type": "string",
"enum": ["cel­sius", "fahren­
heit"]
}
},

20
"required": ["loca­
tion"]
}
}
}
],
"tool_choice": "auto"
}'

Image under­stand­ing
Gem­ini mod­els are natively mul­ti­modal and pro­vide best in class
per­for­mance on many com­mon 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­

tive­lan­

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")

response = client.chat.com­ ple­


tions.cre­
ate(
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": f"data:image/jpeg;base64,
{base64_image}"

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­

tive­
lan­

guage.googleapis.com/v1beta/ope­
nai/"
});

async func­
tion encodeIm­age(imagePath) {
try {
const image­
Buffer = await fs.read­File(imagePath);
return image­Buffer.toString('base64');
} catch (error) {
con­
sole.error("Error encod­ing image:", error);
return null;
}
}

async func­
tion main() {
const imagePath = "Path/to/agi/image.jpeg";
const base64Image = await encodeIm­
age(imagePath);

const mes­sages = [
{
"role": "user",
"con­tent": [
{
"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­

tive­
lan­‐
guage.googleapis.com/v1beta/ope­nai/chat/com­
ple­
tions" \
-H "Content-​
Type: appli­
ca­
tion/json" \
-H "Autho­
riza­
tion: Bearer GEM­INI_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}\" }
}
]
}
]
}"
'

Gen­er­ate an image
Gen­er­ate an image:

26
Python

import base64
from ope­
nai import Ope­nAI
from PIL import Image
from io import Byte­
sIO

client = Ope­
nAI(
api_key="GEM­
INI_API_KEY",
base_url="https://gen­
er­

tive­lan­

guage.googleapis.com/v1beta/ope­
nai/",
)

response = client.images.gen­er­
ate(
model="imagen-​3.0-​
generate-002",
prompt="a por­trait of a sheepadoo­
dle wear­
ing a cape",
response_for­
mat='b64_json',
n=1,
)

for image_data in response.data:


image = Image.open(Byte­

sIO(base64.b64decode(image_data.b64_json)))
image.show()

27
JavaScript

import Ope­
nAI from "ope­
nai";

const ope­
nai = new Ope­
nAI({
apiKey: "GEM­
INI_API_KEY",
baseURL: "https://gen­
er­

tive­
lan­

guage.googleapis.com/v1beta/ope­
nai/",
});

async func­tion main() {


const image = await ope­nai.images.gen­er­
ate(
{
model: "imagen-​
3.0-​
generate-002",
prompt: "a por­
trait of a sheepadoo­dle wear­
ing a
cape",
response_for­
mat: "b64_json",
n: 1,
}
);

con­
sole.log(image.data);
}

main();

28
REST

curl "https://gen­er­

tive­
lan­‐
guage.googleapis.com/v1beta/ope­ nai/images/gen­
er­

tions" \
-H "Content-​
Type: appli­ ca­
tion/json" \
-H "Autho­
riza­tion: Bearer GEM­INI_API_KEY" \
-d '{
"model": "imagen-​3.0-​generate-002",
"prompt": "a por­trait of a sheepadoo­dle wear­
ing a
cape",
"response_for­mat": "b64_json",
"n": 1,
}'

Audio under­stand­ing
Ana­lyze 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­

tive­lan­

guage.googleapis.com/v1beta/ope­
nai/"
)

with open("/path/to/your/audio/file.wav", "rb") as


audio_file:
base64_audio =
base64.b64encode(audio_file.read()).decode('utf-8')

response = client.chat.com­ ple­


tions.cre­
ate(
model="gemini-​ 2.0-​
flash",
mes­
sages=[
{
"role": "user",
"con­ tent": [
{
"type": "text",
"text": "Tran­ scribe this audio",
},
{
"type": "input_audio",
"input_audio": {
"data": base64_audio,
"for­
mat": "wav"
}
}
],
}

30
],
)

print(response.choices[0].mes­
sage.con­
tent)

31
JavaScript

import fs from "fs";


import Ope­
nAI from "ope­
nai";

const client = new Ope­


nAI({
apiKey: "GEM­
INI_API_KEY",
baseURL: "https://gen­
er­

tive­
lan­

guage.googleapis.com/v1beta/ope­
nai/",
});

const audioFile = fs.read­


File­

Sync("/path/to/your/audio/file.wav");
const base64Audio =
Buffer.from(audioFile).toString("base64");

async func­ tion main() {


const response = await client.chat.com­ ple­
tions.cre­
ate({
model: "gemini-​ 2.0-​
flash",
mes­
sages: [
{
role: "user",
con­tent: [
{
type: "text",
text: "Tran­
scribe this audio",
},
{
type: "input_audio",
input_audio: {
data: base64Audio,
for­
mat: "wav",
},
},
],

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­

tive­
lan­‐
guage.googleapis.com/v1beta/ope­nai/chat/com­
ple­
tions" \
-H "Content-​
Type: appli­
ca­
tion/json" \
-H "Autho­
riza­
tion: Bearer GEM­INI_API_KEY" \
-d "{
\"model\": \"gemini-​
2.0-​flash\",
\"mes­
sages\": [
{
\"role\": \"user\",
\"con­
tent\": [
{ \"type\": \"text\", \"text\": \"Tran­scribe
this audio file.\" },
{
\"type\": \"input_audio\",
\"input_audio\": {
\"data\": \"${base64_audio}\",
\"for­
mat\": \"wav\"
}
}
]
}
]
}"
'

34
Struc­tured out­put
Gem­ini mod­els can out­put JSON objects in any struc­ture 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­

tive­lan­

guage.googleapis.com/v1beta/ope­
nai/"
)

class Cal­
en­darEvent(Base­
Model):
name: str
date: str
par­
tic­

pants: list[str]

com­
ple­tion = client.beta.chat.com­ ple­
tions.parse(
model="gemini-​2.0-​flash",
mes­
sages=[
{"role": "sys­
tem", "con­ tent": "Extract the event
infor­ma­
tion."},
{"role": "user", "con­ tent": "John and Susan are
going to an AI con­ fer­
ence on Fri­ day."},
],
response_for­mat=Cal­en­
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­

tive­lan­

guage.googleapis.com/v1beta/ope­
nai"
});

const Cal­en­
darEvent = z.object({
name: z.string(),
date: z.string(),
par­
tic­

pants: z.array(z.string()),
});

const com­ple­
tion = await ope­nai.beta.chat.com­
ple­

tions.parse({
model: "gemini-​2.0-​
flash",
mes­
sages: [
{ role: "sys­tem", con­
tent: "Extract the event infor­
ma­‐
tion." },
{ role: "user", con­tent: "John and Susan are going to
an AI con­
fer­ence on Fri­
day" },
],
response_for­mat: zodRespon­
se­For­
mat(Cal­
en­
darEvent,
"event"),
});

const event = com­


ple­
tion.choices[0].mes­
sage.parsed;
con­
sole.log(event);

36
Embed­dings
Text embed­dings mea­sure the relat­ed­ness of text strings and can be
gen­er­ated using the Gem­ini 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­

tive­lan­

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­

tive­lan­

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­
a­tive­
lan­‐
guage.googleapis.com/v1beta/ope­ nai/embed­
dings" \
-H "Content-​
Type: appli­ca­tion/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 sev­eral fea­tures sup­ported by Gem­ini that are not avail­‐
able in Ope­nAI mod­els but can be enabled using the extra_body
field.
extra_body fea­tures

safety_set­
‐ Cor­re­sponds to Gem­ini's Safe­ty­Set­ting.
tings

cached_con­
tent Cor­re­sponds to Gem­ini's Gen­er­ate­Con­tentRe­‐
quest.cached_con­ tent.

think­
ing_con­
‐ Cor­re­sponds to Gem­ini's Think­ing­Con­fig.
fig

cached_con­
tent
Here's an exam­ple of using extra_body to set cached_con­tent:

39
Python

from ope­
nai import Ope­
nAI

client = Ope­
nAI(
api_key=MY_API_KEY,
base_url="https://gen­
er­

tive­
lan­

guage.googleapis.com/v1beta/"
)

stream = client.chat.com­ple­tions.cre­
ate(
model="gemini-​2.5-pro",
n=1,
mes­
sages=[
{
"role": "user",
"con­
tent": "Sum­ma­
rize the video"
}
],
stream=True,
stream_options={'include_usage': True},
extra_body={
'extra_body':
{
'google': {
'cached_con­
tent': "cached­Con­

tents/0000aaaa1111bbbb2222cccc3333dddd4444eeee"
}
}
}
)

for chunk in stream:


print(chunk)

40
print(chunk.usage.to_dict())

List mod­els
Get a list of avail­able Gem­ini mod­els:

Python

from ope­
nai import Ope­
nAI

client = Ope­
nAI(
api_key="GEM­
INI_API_KEY",
base_url="https://gen­
er­

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­

tive­
lan­

guage.googleapis.com/v1beta/ope­
nai/",
});

async func­
tion main() {
const list = await ope­
nai.mod­
els.list();

for await (const model of list) {


con­
sole.log(model);
}
}
main();

REST

curl https://gen­
er­

tive­
lan­
guage.googleapis.com/v1beta/ope­

nai/mod­
els \
-H "Autho­
riza­
tion: Bearer GEM­
INI_API_KEY"

Retrieve a model
Retrieve a Gem­ini model:

42
Python

from ope­
nai import Ope­
nAI

client = Ope­
nAI(
api_key="GEM­
INI_API_KEY",
base_url="https://gen­
er­

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­

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­
a­tive­
lan­
guage.googleapis.com/v1beta/ope­

nai/mod­
els/gemini-​
2.0-​flash \
-H "Autho­
riza­
tion: Bearer GEM­ INI_API_KEY"

Cur­rent lim­it­ a­tions


Sup­port for the Ope­nAI libraries is still in beta while we extend fea­‐
ture sup­port.
If you have ques­tions about sup­ported para­me­ters, upcom­ing
fea­tures, or run into any issues get­ting started with Gem­ini, join
our Devel­oper Forum → https://discuss.ai.google.dev/c/gemini-api/4.

What's next
Try our Ope­nAI Com­pat­ib
­ il­ity Colab → https://colab.sandbox.google.com/
github/google-gemini/cookbook/blob/main/quickstarts/Get_started_OpenAI_Compat
ibility.ipynbto work through more detailed exam­ples.
Except as oth­er­wise noted, the con­tent of this page is licensed under
the Cre­ative Com­mons Attri­bu­tion 4.0 License → https://creativecommon
s.org/licenses/by/4.0/, and code sam­ples are licensed under the Apache
2.0 License → https://www.apache.org/licenses/LICENSE-2.0. For details, see
the Google Devel­op­ers Site Poli­cies → https://developers.google.com/site-p
olicies. Java is a reg­is­tered trade­mark of Ora­cle and/or its affil­ia
­ tes.
Last updated 2025-06-18 UTC.

44

You might also like