how can i use gemini models (2.5 flash) with python 3.8
genai is not supported in pytohn versions < 3.9
1 Like
@Rim_Benjaballah , Welcome to the community.
you can always use a library like ârequestsâ to make a REST API call.
you can try something like the following.
import requests
import json
import os
api_key = âxxxâyour api key hereâxxxxâ
url = âhttps://generativelanguage.googleapis.com/v1beta/openai/chat/completionsâ
headers = {
âContent-Typeâ: âapplication/jsonâ,
âAuthorizationâ: fâBearer {api_key}â
}
payload = {
âmodelâ: âgemini-2.0-flashâ,
âmessagesâ: [
{âroleâ: âuserâ, âcontentâ: âExplain to me how AI worksâ}
]
}
try:
response = requests.post(url, headers=headers, json=payload)
response_data = response.json()
print("Request Successful!")
print("Response:")
print(json.dumps(response_data, indent=2))
except requests.exceptions.RequestException as e:
print(f"Request failed: {e}")