How to Use Gemini API Advanced Features for Coding and Data Analysis

Gemini API Advanced

How to prime behaviors, extract structured data, and analyze multimodal images

Using Your Own AI Assistant 200%: Advanced Google Gemini API Techniques

In our previous guide, we built a basic AI assistant in just 10 minutes using a simple Python script. But once you have the connection working, how do you take it to the next level?

How do you turn a simple chatbot into a professional assistant that performs specialized tasks like a human expert? In this advanced guide, we will explore three key techniques: System Instructions, Structured Outputs (JSON), and Multimodal Analysis.


1. System Instructions: Priming the Perfect Persona

By default, Gemini replies as a generic assistant. With System Instructions, you can lock in its role before the conversation even starts, ensuring it never breaks character during long chats.

For example, if you want your assistant to act as a strict code reviewer who only answers in code without polite chit-chat:

from google import genai
from google.genai import types

client = genai.Client(api_key="YOUR_API_KEY_HERE")

response = client.models.generate_content(
    model='gemini-2.5-flash',
    contents='Can you help me fix this loop?',
    config=types.GenerateContentConfig(
        system_instruction="You are a cold, blunt senior software engineer. Do not say hello or offer polite remarks. Answer only with corrected code and a single-line explanation."
    )
)

print(response.text)

By doing this, the AI will bypass conversational fluff like "Sure, I can help you with that!" and directly output the corrected code block.


2. Structured Outputs: Getting Data in JSON Format

If you are building an automation workflow (for example, reading a product review and saving the sentiment and key keywords to a database), raw text paragraphs are hard for computers to parse. You need structured data (JSON).

By combining Python's Pydantic library with Gemini's response_schema parameter, we can force Gemini to output a strict JSON structure:

from google import genai
from google.genai import types
from pydantic import BaseModel

client = genai.Client(api_key="YOUR_API_KEY_HERE")

class ReviewAnalysis(BaseModel):
    sentiment: str # 'positive', 'negative', or 'neutral'
    keywords: list[str]
    score: int # 1 to 5 stars

review = "I bought this keyboard yesterday. The typing sounds amazing, but the spacebar is slightly loose."

response = client.models.generate_content(
    model='gemini-2.5-flash',
    contents=review,
    config=types.GenerateContentConfig(
        response_mime_type="application/json",
        response_schema=ReviewAnalysis,
        system_instruction="Analyze the review and extract the structured data."
    ),
)

print(response.text)

The output will be a perfect JSON string: {"sentiment": "positive", "keywords": ["keyboard", "typing sound", "spacebar"], "score": 4}. Your code can instantly parse and save this to Excel or a SQL database.


3. Multimodal Analysis: Reading Images and Visuals

Google Gemini is a native multimodal model. This means it doesn't just read text; it can "see" images and files within the same simple request.

Let's make our assistant look at a local image (like a receipt or a chart screenshot) and analyze it:

from google import genai
from PIL import Image

client = genai.Client(api_key="YOUR_API_KEY_HERE")

# Load local receipt image
img = Image.open('receipt_screenshot.png')

response = client.models.generate_content(
    model='gemini-2.5-flash',
    contents=[img, 'Extract the total spent amount and itemize the purchased products in a bulleted list.']
)

print(response.text)

By passing the image object in the contents list, Gemini reads the text inside the image and returns a clean, structured text output.


5. Frequently Asked Questions (FAQ)

Q: Why should I use System Instructions instead of just writing it in the prompt?

When you write instructions in the main prompt, Gemini can sometimes get distracted by user input and forget the rules (prompt injection). System Instructions act as a hardwired framework that stays robust even during a long, complex conversation.

Q: Does structured output work with the free tier of the API?

Yes, absolutely. Structured outputs and JSON schemas are fully supported in the free tier of Google AI Studio. It is a built-in feature of the Gemini engine.

Q: How large of an image can I upload for Multimodal analysis?

Gemini can handle very large images, but for optimal performance and speed, it is best to resize images to standard resolutions (like 1024x1024) before passing them to the API.

댓글

이 블로그의 인기 게시물

Gemini Many-Shot Prompting: Why 500 Examples Beat Fine-Tuning

No More Git Conflicts: Automate PR Reviews with Cline

기밀 유출 없는 DeepSeek R1 무료 로컬 실행법