FastApi-GeminiApi/TROUBLESHOOTING.md
2025-11-16 18:30:04 +03:30

3.1 KiB

Troubleshooting Guide

API Key Not Loading from .env

Step 1: Verify .env File Exists and Location

The .env file must be in the project root (same directory as docker-compose.yml and main.py).

Check if it exists:

# Windows PowerShell
Test-Path .env

# Should return: True

Step 2: Check .env File Format

Your .env file should look exactly like this:

GEMINI_API_KEY=AIzaSyYourActualKeyHere123456789

Common mistakes:

  • GEMINI_API_KEY = value (spaces around =)
  • GEMINI_API_KEY="value" (quotes)
  • GEMINI_API_KEY='value' (single quotes)
  • GEMINI_API_KEY= value (space after =)
  • GEMINI_API_KEY=value (correct!)

Step 3: Test if API Key is Loaded

  1. If using Docker:

    docker-compose restart
    docker-compose logs aifit-fastapi
    

    Look for: ✓ GEMINI_API_KEY loaded: AIzaSy...

  2. Use the debug endpoint:

    • Open browser: http://localhost:8000/debug/api-key-status
    • Or in Postman: GET http://localhost:8000/debug/api-key-status
    • Should return:
      {
        "status": "ok",
        "message": "API key is loaded",
        "key_length": 39,
        "key_preview": "AIzaSy...",
        "key_ends_with": "xyz"
      }
      

Step 4: If Using Docker - Restart After Creating .env

If you created the .env file after starting Docker:

  1. Stop the container:

    docker-compose down
    
  2. Start again:

    docker-compose up --build
    

Docker needs to be restarted to pick up new .env files.

Step 5: Alternative - Set Environment Variable Directly

If .env still doesn't work, you can set it directly in docker-compose.yml:

environment:
  - GEMINI_API_KEY=your_actual_key_here
  # ... other vars

⚠️ Warning: Don't commit this to git! Use .env file instead.

Step 6: Verify API Key is Valid

  1. Check the key starts with AIza (most Gemini keys do)
  2. Make sure there are no extra characters or line breaks
  3. Try regenerating the key from Google AI Studio if needed

Step 7: Check Docker Logs

docker-compose logs aifit-fastapi

Look for:

  • ✓ GEMINI_API_KEY loaded: ... = Good!
  • ⚠ API key seems too short = Key might be truncated
  • GEMINI_API_KEY not set = File not found or wrong format

Quick Fix Checklist

  • .env file exists in project root
  • .env file has exactly: GEMINI_API_KEY=your_key (no spaces, no quotes)
  • Restarted Docker after creating/editing .env
  • Checked /debug/api-key-status endpoint
  • Verified API key is valid (starts with AIza typically)
  • Checked Docker logs for loading confirmation

Still Not Working?

  1. Test without Docker:

    # Activate venv
    python -m venv venv
    venv\Scripts\activate  # Windows
    
    # Install deps
    pip install -r requirements.txt
    
    # Run directly (will use .env)
    uvicorn main:app --reload
    
  2. Check if python-dotenv is installed:

    pip list | findstr dotenv
    
  3. Manually set environment variable:

    # Windows PowerShell
    $env:GEMINI_API_KEY="your_key_here"
    uvicorn main:app --reload