Skip to content

Google AI Prompting

Best practices for prompting Gemini and Vertex AI models.


Structured JSON Output

Request JSON responses with explicit schema:

from google.genai import types

config = types.GenerateContentConfig(
    response_mime_type="application/json"
)

prompt = """
Analyze this outfit and return JSON:
{
    "style": "casual|formal|sporty",
    "colors": ["color1", "color2"],
    "season": "spring|summer|fall|winter",
    "occasions": ["occasion1", "occasion2"]
}
"""

Styling Prompts

Outfit Generation System Prompt

You are a professional fashion stylist creating outfit recommendations.

Rules:
1. Each outfit MUST include the core product provided
2. Select complementary items from the catalog ONLY
3. Return valid product IDs - do not invent products
4. Consider color coordination, style consistency, and occasion
5. Create 3-5 distinct looks with different vibes

Product catalog is provided in TSV format with columns:
id, name, category, color, material

Return JSON:
{
    "looks": [
        {
            "name": "Look name (e.g., 'Weekend Brunch')",
            "vibe": "Description of the look's mood",
            "items": [
                {"id": "product-uuid", "role": "top|bottom|accessory|shoe|bag"}
            ]
        }
    ]
}

Styling User Prompt Template

user_prompt = f"""
Core product (must be in every look):
ID: {core_product['id']}
Name: {core_product['name']}
Category: {core_product['category']}
Color: {core_product['color']}

Available catalog:
{catalog_tsv}

Create {num_looks} complete outfit looks.
"""

Captioning Prompts

Product Description

Analyze this product image and provide a detailed description.

Focus on:
- Product type and category
- Materials and construction
- Colors and patterns
- Design details and features
- Target audience/use case

Return JSON:
{
    "category": "...",
    "description": "2-3 sentence technical description",
    "short_description": "1 sentence summary",
    "attributes": {
        "color": "...",
        "material": "...",
        "pattern": "...",
        "style": "..."
    }
}

Person Description (for Subject Matching)

Describe the person in this image for casting/matching purposes.

Provide objective, factual observations about:
- Approximate age range
- Body type and proportions
- Hair (color, length, style)
- Distinctive features
- Overall aesthetic

Be respectful and professional. Focus on visual characteristics only.

Return JSON:
{
    "age_range": "20-25",
    "body_type": "...",
    "hair": {"color": "...", "length": "...", "style": "..."},
    "features": ["..."],
    "aesthetic": "..."
}

Vertex AI Image Generation

Text-to-Image Prompt Structure

[Subject] [Action/Pose] [Clothing Details] [Setting] [Lighting] [Style]

Example:
"Professional fashion photograph of a female model wearing an elegant
black evening dress, standing confidently, studio setting with soft
diffused lighting, high-end editorial style, 4K quality"

Virtual Try-On Prompt

Task: virtual try-on
Make the person in the base image wear the garment from the reference image.
Maintain the person's pose, expression, and overall composition.
The garment should fit naturally and realistically.

Image Editing Prompt

Edit this fashion photograph:
- [Specific change 1]
- [Specific change 2]

Preserve:
- Model's face and expression
- Overall lighting and composition
- Image quality and resolution

Multimodal Vision Prompts

Image Analysis

contents = types.Content(
    role="user",
    parts=[
        types.Part.from_bytes(data=image_bytes, mime_type="image/jpeg"),
        types.Part.from_text(text="""
            Analyze this fashion photograph:
            1. Describe the outfit in detail
            2. Identify the style aesthetic
            3. Rate the composition (1-10)
            4. Suggest improvements

            Return structured JSON response.
        """)
    ]
)

Multi-Image Comparison

contents = types.Content(
    role="user",
    parts=[
        types.Part.from_text(text="Compare these fashion images:"),
        types.Part.from_bytes(data=image1_bytes, mime_type="image/jpeg"),
        types.Part.from_bytes(data=image2_bytes, mime_type="image/jpeg"),
        types.Part.from_text(text="""
            Evaluate:
            1. Style consistency between images
            2. Color palette comparison
            3. Quality differences
            4. Which is more suitable for e-commerce

            Return JSON comparison.
        """)
    ]
)

Temperature Guidelines

Use Case Temperature Rationale
Captioning 0.3-0.4 Factual, consistent
Styling 0.6-0.7 Creative but coherent
Analysis 0.2-0.3 Precise evaluation
Image Generation 0.7-0.9 Creative variety

Token Management

Count Tokens Before Request

from google.generativeai import GenerativeModel
from google.generativeai.types.content_types import to_contents

model = GenerativeModel("gemini-2.0-flash")
result = model.count_tokens(to_contents(prompt_text))
print(f"Token count: {result.total_tokens}")

Optimize Long Catalogs

Use TSV format instead of JSON for product catalogs:

# Efficient TSV format
catalog_tsv = "id\tname\tcategory\tcolor\n"
for product in products:
    catalog_tsv += f"{product['id']}\t{product['name']}\t{product['category']}\t{product['color']}\n"

# Instead of verbose JSON
# catalog_json = json.dumps(products, indent=2)  # More tokens!

Error Handling in Prompts

Include fallback instructions:

If you cannot complete the analysis:
1. Return {"error": "description of issue"}
2. Explain what information is missing
3. Do not hallucinate or guess

If a product ID is not in the catalog:
1. Skip that item
2. Note it in "warnings" field
3. Continue with valid items only

Safety Considerations

For Fashion Content (Vertex AI)

Safety filters are disabled for legitimate fashion use cases:

safety_settings = [
    SafetySetting(category=HARM_CATEGORY_SEXUALLY_EXPLICIT, threshold=OFF),
    # Allows swimwear, lingerie, form-fitting clothing
]

For User-Facing Content (Gemini API)

Keep standard safety settings:

safety_settings = [
    {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
]

Response Validation

Always validate LLM responses:

def validate_outfit_response(response: dict, catalog_ids: set) -> dict:
    """Validate outfit response against known product IDs."""
    validated_looks = []

    for look in response.get("looks", []):
        valid_items = []
        for item in look.get("items", []):
            if item["id"] in catalog_ids:
                valid_items.append(item)
            else:
                print(f"Warning: Unknown product ID {item['id']}")

        if valid_items:
            look["items"] = valid_items
            validated_looks.append(look)

    return {"looks": validated_looks}