DEV Community

Phil IPS
Phil IPS

Posted on

Getting Started with Gemini 3.6 Flash API for Text Generation

Getting Started with Gemini 3.6 Flash API for Text Generation

The Gemini 3.6 Flash model offers fast, cost-effective text generation through Google's API. Here's a practical guide to integrate it into your Python projects.

Prerequisites

  • Python 3.8+
  • Google Cloud account with Vertex AI enabled
  • API key or service account credentials

Installation

pip install google-cloud-aiplatform vertexai
Enter fullscreen mode Exit fullscreen mode

Basic Setup

import vertexai
from vertexai.generative_models import GenerativeModel

# Initialize Vertex AI
vertexai.init(project="your-project-id", location="us-central1")

# Load the Flash model
model = GenerativeModel("gemini-1.5-flash-001")
Enter fullscreen mode Exit fullscreen mode

Text Generation with Parameters

def generate_text(prompt: str, max_tokens: int = 1024, temperature: float = 0.7) -> str:
    """Generate text using Gemini 3.6 Flash with configurable parameters."""

    generation_config = {
        "max_output_tokens": max_tokens,      # Limit response length
        "temperature": temperature,           # Creativity (0.0-1.0)
        "top_p": 0.95,                       # Nucleus sampling
        "top_k": 40,                         # Top-k sampling
    }

    safety_settings = {
        "HARM_CATEGORY_HATE_SPEECH": "BLOCK_MEDIUM_AND_ABOVE",
        "HARM_CATEGORY_DANGEROUS_CONTENT": "BLOCK_MEDIUM_AND_ABOVE",
        "HARM_CATEGORY_SEXUAL_CONTENT": "BLOCK_MEDIUM_AND_ABOVE",
        "HARM_CATEGORY_HARASSMENT": "BLOCK_MEDIUM_AND_ABOVE",
    }

    response = model.generate_content(
        prompt,
        generation_config=generation_config,
        safety_settings=safety_settings
    )

    return response.text

# Example usage
result = generate_text(
    "Explain quantum computing in simple terms",
    max_tokens=500,
    temperature=0.8
)
print(result)
Enter fullscreen mode Exit fullscreen mode

Key Parameters Explained

Parameter Range Effect
max_output_tokens 1-8192 Maximum tokens in response
temperature 0.0-1.0 Higher = more creative, lower = more deterministic
top_p 0.0-1.0 Nucleus sampling threshold
top_k 1-40 Top-k sampling limit

Streaming Responses

def generate_stream(prompt: str):
    """Stream tokens as they're generated for real-time UX."""
    response = model.generate_content(
        prompt,
        generation_config={"max_output_tokens": 2048},
        stream=True
    )

    for chunk in response:
        if chunk.text:
            yield chunk.text  # Process incrementally

# Usage
for token in generate_stream("Write a short story about a robot"):
    print(token, end="", flush=True)
Enter fullscreen mode Exit fullscreen mode

Cost Optimization Tips

  1. Use Flash over Pro — 10x cheaper for most tasks
  2. Set max_output_tokens — Prevent runaway generation
  3. Batch requests — Combine multiple prompts when possible
  4. Cache responses — Store frequent queries locally

Error Handling

from google.api_core.exceptions import ResourceExhausted, GoogleAPICallError

try:
    result = generate_text(prompt)
except ResourceExhausted:
    print("Rate limit hit — implement exponential backoff")
except GoogleAPICallError as e:
    print(f"API error: {e}")
Enter fullscreen mode Exit fullscreen mode

Conclusion

Gemini 3.6 Flash provides excellent price-performance for text generation. Start with the basic config above, tune parameters for your use case, and implement proper error handling for production workloads.


Published via IncomeAutomaton tutorial pipeline

Top comments (0)