Skip to content
Skillv1.0.0

gemini-api-python

Integrate Google Gemini API from Python serverless backends (Vercel, Cloud Run, etc.). Covers API setup, system prompting with live context, error handling, and frontend chat UI patterns for hackathon

by abdullibrahim733-pixel(0) 0 installs
Free
Sign in to install

Free account. Installing gives you the manifest plus copy-paste snippets.

See reviews

About

Imported from abdullibrahim733-pixel/Eagle (hermes-backup/skills/mlops/gemini-api-python/SKILL.md). Install upstream with npx skills add abdullibrahim733-pixel/Eagle --skill gemini-api-python. Copyright stays with the author.

Gemini API Python Integration

Call Google's Gemini models from Python serverless functions — with system instructions, live context injection, and proper error handling.

Architecture (Frontend → Serverless → Gemini)

Browser UI → POST /api/ai/chat → Python serverless → Gemini API → response back

The serverless function acts as a proxy that:

  1. Receives the user's message + live app context (telemetry, state, etc.)
  2. Builds a structured prompt combining system instructions + context + user question
  3. Calls Gemini API with the API key (hidden from the client)
  4. Returns the response to the frontend

Setup

1. API Key

Get a key at https://aistudio.google.com/apikey (free tier: 60 requests/min with billing-enabled project).

Vercel env var:

echo "YOUR_API_KEY" | vercel env add GOOGLE_AI_API_KEY production --yes
# Use --yes to skip interactive confirmation

Important: Use --yes flag when piping the key via stdin. Without it, the piped content is consumed as interactive prompt answers instead of the env value.

# In your serverless handler:
GOOGLE_AI_API_KEY = os.environ.get("GOOGLE_AI_API_KEY", "")

2. API Endpoint

import os, json
import urllib.request, urllib.error

GEMINI_MODEL = "gemini-2.0-flash"
GEMINI_API_URL = f"https://generativelanguage.googleapis.com/v1/models/{GEMINI_MODEL}:generateContent"

V1 vs v1beta: Use /v1/ (not /v1beta/). The v1beta endpoint may not support certain models (e.g., gemini-1.5-flash returns 404 on v1beta). Prefer v1 for stability.

3. System Instruction Pattern

SYSTEM_PROMPT = """You are an AI assistant inside [app name].

Your role:
- You receive live telemetry/context with each message.
- Answer questions concisely (1-3 sentences) based on that context.
- Be helpful, specific, and grounded in the data provided.
"""

payload = {
    "contents": [
        {
            "role": "user",
            "parts": [{"text": f"## Current State\n{context_text}\n\n## User Question\n{user_message}"}]
        }
    ],
    "systemInstruction": {
        "parts": [{"text": SYSTEM_PROMPT}]
    },
    "generationConfig": {
        "temperature": 0.7,
        "maxOutputTokens": 250,
        "topP": 0.9
    }
}

Key design choices:

  • Inject live context as structured markdown section before the user question — Gemini handles this well.
  • systemInstruction is passed as a separate top-level field (not inside contents).
  • Keep maxOutputTokens low (150–300) for chat responses; higher (1024+) for analysis/creative tasks.

4. Calling the API

req = urllib.request.Request(
    f"{GEMINI_API_URL}?key={GOOGLE_AI_API_KEY}",
    data=json.dumps(payload).encode("utf-8"),
    headers={"Content-Type": "application/json"},
    method="POST"
)

with urllib.request.urlopen(req, timeout=10) as resp:
    result = json.loads(resp.read().decode())

candidates = result.get("candidates", [])
if candidates:
    reply = candidates[0].get("content", {}).get("parts", [{}])[0].get("text", "")

5. Frontend Chat UI Pattern (Vanilla JS)

async function sendMessage(text) {
    const context = collectAppState();  // Grab live state from the app

    const resp = await fetch("/api/ai/chat", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ message: text, context: context }),
        signal: AbortSignal.timeout(15000),
    });

    const data = await resp.json();
    displayReply(data.reply);
}

Context shape (sent from browser):

{
  "speed": 15,
  "battery": 72,
  "state": "Path Following",
  "decision": "Following path",
  "waypoint": 12,
  "sensors": {"fm": {"distance": 8, "active": false}}
}

Error Handling

Common Gemini API errors

HTTP Error Meaning Fix
429 Quota exceeded for metric: generativelanguage.googleapis.com/generate_content_free_tier_requests, limit: 0 Free tier quota exhausted for this project/API key Enable billing on the Google Cloud project (you won't be charged at free tier rates). Or create a new project + new key.
404 models/gemini-1.5-flash is not found for API version v1beta Model not available at the API version used Use v1/ instead of v1beta/. Check model availability at the chosen endpoint.
403 API key not valid Key was revoked or doesn't have Gemini API enabled Generate a new key at aistudio.google.com. Verify the project has the Generative Language API enabled.
400 systemInstruction is not supported Model doesn't support system instructions Use gemini-2.0-flash or newer. Older models (1.0, 1.5-pro) may not support systemInstruction as a top-level field — inline it into the user message instead.

Python error handling pattern

try:
    with urllib.request.urlopen(req, timeout=10) as resp:
        result = json.loads(resp.read().decode())
except urllib.error.HTTPError as e:
    error_body = e.read().decode()
    reply = f"Error ({e.code}): {parse_error_message(error_body)}"
except urllib.error.URLError as e:
    reply = f"Network error: {e.reason}"

Model Selection

Model Strengths Latency System Prompt Free Tier
gemini-2.0-flash Fast, good context, supports systemInstruction Low ✅ Yes 60 req/min
gemini-2.0-flash-lite Faster, cheaper, slightly lower quality Very low ✅ Yes 60 req/min
gemini-1.5-flash Previous gen, still widely available Low ✅ Yes Varies
gemini-1.5-pro Higher quality, slower, more expensive High ✅ Yes Varies

Default choice: gemini-2.0-flash — balances speed and quality, supports system instructions.

Pitfalls

  • Piping API keys to vercel env add without --yes — The key content gets consumed as interactive prompt answers. Always use vercel env add <name> <env> --yes when piping from stdin.
  • Environment variable not picked up — After adding an env var to Vercel, you need a new deployment. Use git commit --allow-empty -m "redeploy" && git push to trigger a fresh deploy.
  • CORS on POST requests — Python BaseHTTPRequestHandler needs explicit CORS headers on both do_OPTIONS (preflight) and do_POST (response). Missing preflight handler = browser blocks the request before it reaches your code.
  • Timeout for LLM calls — Gemini can take 5-15 seconds. Set frontend timeout to at least 15s (AbortSignal.timeout(15000)). Set the serverless function timeout higher (Vercel Pro: 60s, Hobby: 10s).
  • urllib.error not imported — Use import urllib.request, urllib.error (not just import urllib.request). The HTTPError class lives in urllib.error.
  • 429 on brand-new key — If the key was created from a project that already exhausted its quota (same billing project), a new key won't help. Create a new project in Google Cloud Console, enable the Generative Language API, then generate a key.
  • maxOutputTokens ignored on some models — Older models may have different token limits. Always check the model's docs for the correct maxOutputTokens range.
  • System instructions ignored on non-proxy setup — If calling Gemini directly from the browser (not recommended — exposes your API key), systemInstruction may be silently dropped. Always proxy through your backend.

See also

Use it

Copy one of these into your project. Installing also returns the manifest and these snippets.

yaml
targets:
  - https://api.opensmartroute.ai/api/v1/registry/abdullibrahim733-pixel-eagle-gemini-api-python/manifest   # or paste the manifest below

Manifest

An Open Capability Manifest: the router reads it to know what this does, what it costs and when to pick it.

abdullibrahim733-pixel-eagle-gemini-api-python.ocm.jsonjson
{
  "ocm": "1",
  "id": "abdullibrahim733-pixel-eagle-gemini-api-python",
  "kind": "skill",
  "name": "gemini-api-python",
  "description": "Integrate Google Gemini API from Python serverless backends (Vercel, Cloud Run, etc.). Covers API setup, system prompting with live context, error handling, and frontend chat UI patterns for hackathons and production.",
  "publisher": "abdullibrahim733-pixel",
  "version": "1.0.0",
  "capabilities": {
    "domains": [
      "coding"
    ],
    "tags": [
      "skill-md",
      "gemini",
      "google-ai",
      "llm-api",
      "serverless",
      "python",
      "vercel",
      "github"
    ],
    "languages": [
      "en"
    ]
  },
  "quality_prior": 0.6,
  "examples": [
    "Integrate Google Gemini API from Python serverless backends (Vercel, Cloud Run, etc.). Covers API setup, system prompting with live context, error handling, and frontend chat UI patterns for hackathons and production."
  ],
  "primary": false,
  "metadata": {
    "source": {
      "provider": "github",
      "repository": "https://github.com/abdullibrahim733-pixel/Eagle",
      "path": "hermes-backup/skills/mlops/gemini-api-python/SKILL.md",
      "ref": "1f420fcb3f20524e8a830b9d010e0c0ee312d437",
      "url": "https://github.com/abdullibrahim733-pixel/Eagle/blob/1f420fcb3f20524e8a830b9d010e0c0ee312d437/hermes-backup/skills/mlops/gemini-api-python/SKILL.md",
      "key": "abdullibrahim733-pixel/Eagle/hermes-backup/skills/mlops/gemini-api-python/SKILL.md"
    }
  },
  "instructions": "# Gemini API Python Integration\n\nCall Google's Gemini models from Python serverless functions — with system instructions, live context injection, and proper error handling.\n\n## Architecture (Frontend → Serverless → Gemini)\n\n```\nBrowser UI → POST /api/ai/chat → Python serverless → Gemini API → response back\n```\n\nThe serverless function acts as a **proxy** that:\n1. Receives the user's message + live app context (telemetry, state, etc.)\n2. Builds a structured prompt combining system instructions + context + user question\n3. Calls Gemini API with the API key (hidden from the client)\n4. Returns the",
  "cost": {
    "context_tokens": 1918
  }
}

Fetch it by URL: GET /api/v1/registry/abdullibrahim733-pixel-eagle-gemini-api-python/manifest?version=1.0.0

Reviews

Star ratings from people who tried it. One review per account; edit yours any time.

No reviews yet. Install it, try it, and be the first to rate it.