$npx -y skills add tranhieutt/software_development_department --skill gemini-api-integrationProvides code patterns for Google Gemini API integration including text generation, multimodal inputs, and streaming. Use when working with Google AI SDK or when the user mentions Gemini API, Google AI, or Vertex AI.
| 1 | # Gemini API Integration |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | This skill guides AI agents through integrating Google Gemini API into applications — from basic text generation to advanced multimodal, function calling, and streaming use cases. It covers the full Gemini SDK lifecycle with production-grade patterns. |
| 6 | |
| 7 | ## When to Use This Skill |
| 8 | |
| 9 | - Use when setting up Gemini API for the first time in a Node.js, Python, or browser project |
| 10 | - Use when implementing multimodal inputs (text + image/audio/video) |
| 11 | - Use when adding streaming responses to improve perceived latency |
| 12 | - Use when implementing function calling / tool use with Gemini |
| 13 | - Use when optimizing model selection (Flash vs Pro vs Ultra) for cost and performance |
| 14 | - Use when debugging Gemini API errors, rate limits, or quota issues |
| 15 | |
| 16 | ## Step-by-Step Guide |
| 17 | |
| 18 | ### 1. Installation & Setup |
| 19 | |
| 20 | **Node.js / TypeScript:** |
| 21 | ```bash |
| 22 | npm install @google/generative-ai |
| 23 | ``` |
| 24 | |
| 25 | **Python:** |
| 26 | ```bash |
| 27 | pip install google-generativeai |
| 28 | ``` |
| 29 | |
| 30 | Set your API key securely: |
| 31 | ```bash |
| 32 | export GEMINI_API_KEY="your-api-key-here" |
| 33 | ``` |
| 34 | |
| 35 | ### 2. Basic Text Generation |
| 36 | |
| 37 | **Node.js:** |
| 38 | ```javascript |
| 39 | import { GoogleGenerativeAI } from "@google/generative-ai"; |
| 40 | |
| 41 | const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY); |
| 42 | const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" }); |
| 43 | |
| 44 | const result = await model.generateContent("Explain async/await in JavaScript"); |
| 45 | console.log(result.response.text()); |
| 46 | ``` |
| 47 | |
| 48 | **Python:** |
| 49 | ```python |
| 50 | import google.generativeai as genai |
| 51 | import os |
| 52 | |
| 53 | genai.configure(api_key=os.environ["GEMINI_API_KEY"]) |
| 54 | model = genai.GenerativeModel("gemini-1.5-flash") |
| 55 | |
| 56 | response = model.generate_content("Explain async/await in JavaScript") |
| 57 | print(response.text) |
| 58 | ``` |
| 59 | |
| 60 | ### 3. Streaming Responses |
| 61 | |
| 62 | ```javascript |
| 63 | const result = await model.generateContentStream("Write a detailed blog post about AI"); |
| 64 | |
| 65 | for await (const chunk of result.stream) { |
| 66 | process.stdout.write(chunk.text()); |
| 67 | } |
| 68 | ``` |
| 69 | |
| 70 | ### 4. Multimodal Input (Text + Image) |
| 71 | |
| 72 | ```javascript |
| 73 | import fs from "fs"; |
| 74 | |
| 75 | const imageData = fs.readFileSync("screenshot.png"); |
| 76 | const imagePart = { |
| 77 | inlineData: { |
| 78 | data: imageData.toString("base64"), |
| 79 | mimeType: "image/png", |
| 80 | }, |
| 81 | }; |
| 82 | |
| 83 | const result = await model.generateContent(["Describe this image:", imagePart]); |
| 84 | console.log(result.response.text()); |
| 85 | ``` |
| 86 | |
| 87 | ### 5. Function Calling / Tool Use |
| 88 | |
| 89 | ```javascript |
| 90 | const tools = [{ |
| 91 | functionDeclarations: [{ |
| 92 | name: "get_weather", |
| 93 | description: "Get current weather for a city", |
| 94 | parameters: { |
| 95 | type: "OBJECT", |
| 96 | properties: { |
| 97 | city: { type: "STRING", description: "City name" }, |
| 98 | }, |
| 99 | required: ["city"], |
| 100 | }, |
| 101 | }], |
| 102 | }]; |
| 103 | |
| 104 | const model = genAI.getGenerativeModel({ model: "gemini-1.5-pro", tools }); |
| 105 | const result = await model.generateContent("What's the weather in Mumbai?"); |
| 106 | |
| 107 | const call = result.response.functionCalls()?.[0]; |
| 108 | if (call) { |
| 109 | // Execute the actual function |
| 110 | const weatherData = await getWeather(call.args.city); |
| 111 | // Send result back to model |
| 112 | } |
| 113 | ``` |
| 114 | |
| 115 | ### 6. Multi-turn Chat |
| 116 | |
| 117 | ```javascript |
| 118 | const chat = model.startChat({ |
| 119 | history: [ |
| 120 | { role: "user", parts: [{ text: "You are a helpful coding assistant." }] }, |
| 121 | { role: "model", parts: [{ text: "Sure! I'm ready to help with code." }] }, |
| 122 | ], |
| 123 | }); |
| 124 | |
| 125 | const response = await chat.sendMessage("How do I reverse a string in Python?"); |
| 126 | console.log(response.response.text()); |
| 127 | ``` |
| 128 | |
| 129 | ### 7. Model Selection Guide |
| 130 | |
| 131 | | Model | Best For | Speed | Cost | |
| 132 | |-------|----------|-------|------| |
| 133 | | `gemini-1.5-flash` | High-throughput, cost-sensitive tasks | Fast | Low | |
| 134 | | `gemini-1.5-pro` | Complex reasoning, long context | Medium | Medium | |
| 135 | | `gemini-2.0-flash` | Latest fast model, multimodal | Very Fast | Low | |
| 136 | | `gemini-2.0-pro` | Most capable, advanced tasks | Slow | High | |
| 137 | |
| 138 | ## Best Practices |
| 139 | |
| 140 | - ✅ **Do:** Use `gemini-1.5-flash` for most tasks — it's fast and cost-effective |
| 141 | - ✅ **Do:** Always stream responses for user-facing chat UIs to reduce perceived latency |
| 142 | - ✅ **Do:** Store API keys in environment variables, never hard-code them |
| 143 | - ✅ **Do:** Implement exponential backoff for rate limit (429) errors |
| 144 | - ✅ **Do:** Use `systemInstruction` to set persistent model behavior |
| 145 | - ❌ **Don't:** Use `gemini-pro` for simple tasks — Flash is cheaper and faster |
| 146 | - ❌ **Don't:** Send large base64 images inline for files > 20MB — use File API instead |
| 147 | - ❌ **Don't:** Ignore safety ratings in responses for production apps |
| 148 | |
| 149 | ## Error Handling |
| 150 | |
| 151 | ```javascript |
| 152 | try { |
| 153 | const result = await mo |