Using the Gemini SDK
This guide shows you how to use the official Google Generative AI SDK (for Gemini) with Liona, allowing you to integrate Gemini AI capabilities while maintaining security and cost control. Liona works as a drop-in replacement for Google’s API, requiring minimal changes to your existing code.
JavaScript/TypeScript Integration
The Google Generative AI JavaScript/TypeScript SDK is fully compatible with Liona. Follow these steps to integrate it into your application.
Install the Google Generative AI SDK
If you haven’t already, install the Google Generative AI SDK using npm, yarn, or pnpm:
npm install @google/generative-ai
# or
yarn add @google/generative-ai
# or
pnpm add @google/generative-ai
Initialize the Gemini client
Initialize the Gemini client using your Liona access key instead of your Google API key:
import { GoogleGenerativeAI } from '@google/generative-ai';
const genAI = new GoogleGenerativeAI('your_liona_access_key_here');
Use the Gemini SDK as normal
Now you can use the Gemini SDK exactly as you would normally, with all the same methods and parameters:
async function generateWithGemini() {
const model = genAI.getGenerativeModel({ model: 'gemini-2.0-flash' }, {baseUrl: "https://api.liona.ai/v1/provider/gemini"});
const result = await model.generateContent('Explain how wind turbines work.');
const response = await result.response;
const text = response.text();
console.log(text);
}
Client-side usage (browser)
One of Liona’s key benefits is enabling secure client-side usage of the Gemini SDK. You can safely use your Liona access key directly in browser-based applications:
// In a React component or other client-side code
import { GoogleGenerativeAI } from '@google/generative-ai';
function GeminiComponent() {
const [result, setResult] = useState('');
async function handleSubmit(userInput) {
// Safe to use in client-side code with Liona!
const genAI = new GoogleGenerativeAI('your_liona_access_key_here', {
});
const model = genAI.getGenerativeModel({ model: 'gemini-2.0-flash' }, {baseUrl: "https://api.liona.ai/v1/provider/gemini"});
const result = await model.generateContent(userInput);
const response = await result.response;
const text = response.text();
setResult(text);
}
// Component rendering...
}
Handle rate limits and policy errors
When using Liona, you should check for rate limit errors (HTTP 429) or policy limit exceeded messages:
async function callGemini() {
try {
const genAI = new GoogleGenerativeAI('your_liona_access_key_here', {
});
const model = genAI.getGenerativeModel({ model: 'gemini-2.0-flash' }, {baseUrl: "https://api.liona.ai/v1/provider/gemini"});
const result = await model.generateContent('Hello, Gemini!');
const response = await result.response;
return response.text();
} catch (error) {
if (error.status === 429 || error.message.includes('policy limit exceeded')) {
console.log('Rate limit or policy limit reached. Please try again later.');
// Handle gracefully - perhaps show a user-friendly message
} else {
console.error('Error:', error);
}
}
}
Python Integration
The Google Generative AI Python SDK also works seamlessly with Liona.
Install the Google Generative AI Python package
If you haven’t already, install the Google Generative AI Python package:
pip install google-generativeai
# or
poetry add google-generativeai
Initialize the Gemini client
Initialize the Gemini client with your Liona access key:
import google.generativeai as genai
genai.configure(
api_key="your_liona_access_key_here",
transport="rest",
client_options={"api_endpoint": "https://api.liona.ai/v1/provider/gemini"}
)
Use the Gemini client as normal
You can now use the Gemini client normally:
model = genai.GenerativeModel('gemini-2.0-flash')
response = model.generate_content('Explain the water cycle in simple terms.')
print(response.text)
Handle rate limits and policy errors
Handle rate limits and policy errors in your Python applications:
import google.generativeai as genai
genai.configure(
api_key="your_liona_access_key_here",
transport="rest",
client_options={"api_endpoint": "https://api.liona.ai/v1/provider/gemini"}
)
try:
model = genai.GenerativeModel('gemini-2.0-flash')
response = model.generate_content('Hello, Gemini!')
print(response.text)
except Exception as e:
if getattr(e, 'status_code', None) == 429 or "policy limit exceeded" in str(e).lower():
print("Rate limit or policy limit reached. Please try again later.")
else:
print(f"Error: {e}")
Additional Integration Options
Environment Variables
You can use environment variables to configure the Gemini SDK:
# JavaScript
GOOGLE_API_KEY=your_liona_access_key_here
GOOGLE_API_ENDPOINT=https://api.liona.ai/v1/provider/gemini
# Python
GOOGLE_API_KEY=your_liona_access_key_here
GOOGLE_API_ENDPOINT=https://api.liona.ai/v1/provider/gemini
Using with Next.js
For Next.js applications using server components:
// In app/api/route.js or similar
import { GoogleGenerativeAI } from '@google/generative-ai';
import { NextResponse } from 'next/server';
export async function POST(request) {
const { prompt } = await request.json();
const genAI = new GoogleGenerativeAI(process.env.LIONA_ACCESS_KEY);
const model = genAI.getGenerativeModel({ model: 'gemini-2.0-flash' }, {baseUrl: "https://api.liona.ai/v1/provider/gemini"});
const result = await model.generateContent(prompt);
const response = await result.response;
return NextResponse.json({ result: response.text() });
}
Common Issues and Troubleshooting
Rate Limits and Policies
If you encounter rate limits or permission errors, check:
- The policy assigned to your user in Liona
- Your current usage against the set limits
- Whether the specific Gemini model is allowed by your policy
You can check your usage and limits in the Liona dashboard under the “Usage” section.
Error Response Codes
Liona preserves Google’s error structure while adding additional context:
- HTTP 429: Rate limit or policy limit exceeded
- HTTP 403: Unauthorized access to a specific model or feature
- HTTP 401: Invalid or expired access key
Model Availability
Ensure you’ve connected the Google Gemini provider in your Liona dashboard and that the specific models you need (like gemini-pro
or gemini-pro-vision
) are available through your provider.
Next Steps
Now that you’ve integrated the Gemini SDK with Liona, you might want to:
- Set up provider balancing to distribute requests across multiple keys
- Configure request caching to improve performance and reduce costs
- Enable request tracing for debugging and auditing