Skip to content

Google Gemini Integration #13

@kaseywright

Description

@kaseywright

Implement a reusable Google Gemini client integration using the AI Studio API (google-generativeai SDK). This is foundational infrastructure for translation features, supporting configurable model selection with gemini-2.5-flash-lite as the default.

We can also use the gemini-3.1-flash-lite model if the above is not available.


Acceptance Criteria

Configuration & Setup

  • Add google-generativeai to pyproject.toml dependencies
  • Add GOOGLE_AI_API_KEY to Settings class in src/app/config.py
  • Add GOOGLE_AI_MODEL setting with default "gemini-2.5-flash-lite"
  • Add both vars to .env.example with empty placeholder values
  • Re-run ./fai.sh build to verify dependency installation

Client Implementation

  • Create src/app/core/ai_clients/google_gemini.py with:
    • GoogleGeminiClient class wrapping google.generativeai.GenerativeModel
    • Async initialization using genai.configure(api_key=...)
    • generate_content(prompt: str) -> str async method
    • Basic error handling: catch SDK exceptions and raise HTTPException(status.HTTP_502_BAD_GATEWAY) with detail message
  • Add singleton/dependency factory in src/app/dependencies.py:
    async def get_google_gemini_client() -> GoogleGeminiClient:
        # Returns cached singleton instance

Tests

  • Create src/tests/test_google_gemini.py with:
    • Test client initializes with valid API key from config
    • Test generate_content with mocked SDK response (patch genai.GenerativeModel.generate_content_async)
    • Test error handling: SDK exception → HTTPException with 502 status

Contract

# src/app/core/ai_clients/google_gemini.py
class GoogleGeminiClient:
    async def generate_content(self, prompt: str) -> str: ...

Out of Scope (Future Tickets)

  • Actual translation endpoints/routers
  • Prompt templates for translation
  • Response streaming
  • Rate limiting / retry logic
  • Structured logging integration (separate ticket in progress)

Testing Commands

./fai.sh test src/tests/test_google_gemini.py -v
./fai.sh lint
./fai.sh typecheck

References

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

Status

Dev Ready

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions