Skip to content

feat: Add multi-model provider support#1

Open
frannovo wants to merge 1 commit into
ccronca:mainfrom
frannovo:feature/multi-model-provider-support
Open

feat: Add multi-model provider support#1
frannovo wants to merge 1 commit into
ccronca:mainfrom
frannovo:feature/multi-model-provider-support

Conversation

@frannovo
Copy link
Copy Markdown

@frannovo frannovo commented Mar 6, 2026

Summary

  • Add provider abstraction layer supporting Gemini (default), Claude (Anthropic), Grok (xAI), and Ollama (local models)
  • Provider is selected at startup via AI_PROVIDER environment variable
  • Guardrails security validation dynamically uses the configured provider for LLM-based checks
  • Security validation can be toggled via ENABLE_SECURITY env var

Motivation

The service was hardcoded to Google Gemini, limiting flexibility. This change allows users to choose their preferred AI provider based on availability, cost, privacy (local models via Ollama), or preference.

Overview of changes

  • src/providers.py (new): Provider dispatch module with lazy SDK imports so missing optional dependencies don't crash unused providers. Each provider function catches SDK-specific errors and wraps them in ProviderError with the original status code. OLLAMA_BASE_URL is validated for scheme, and AI_PROVIDER is validated on every call (fail fast).
  • src/models.py (new): Extracted RewriteRequest Pydantic model from security.py to decouple request validation from guardrails dependencies.
  • src/security.py: Lazy initialization of guardrails guard. Falls back to passthrough with a logged warning if hub validators are not installed. Replaced deprecated Guard.use_many with Guard.use.
  • src/main.py: Uses generate() from providers module. Catches ProviderError and returns the provider's status code and message. Security validation gated by ENABLE_SECURITY.
  • pyproject.toml: Added optional dependency groups (claude, grok, ollama, all).
  • tests/: Added 20 new test cases covering provider selection, validation, error propagation, API endpoint behavior, and security toggle.

Test plan

  • uv run pytest -v -- 25 passed, 5 skipped (guardrails hub validators require auth + API key)
  • With guardrails configured: GEMINI_API_KEY=... uv run pytest tests/test_security.py -v -- all 10 passed
  • Manual testing with AI_PROVIDER=ollama ENABLE_SECURITY=false confirmed working

This PR description was generated with AI assistance (Claude Opus 4.6).

Replace hardcoded Gemini integration with a provider abstraction
supporting Gemini, Claude (Anthropic), Grok (xAI), and Ollama.
Provider is selected at startup via AI_PROVIDER env var.

- Extract RewriteRequest model to src/models.py for clean imports
- Add src/providers.py with lazy SDK imports and error handling
- Make guardrails security validation lazy and optional (ENABLE_SECURITY)
- Propagate provider API errors with original status codes
- Validate OLLAMA_BASE_URL scheme and AI_PROVIDER value (fail fast)
- Fix deprecated Guard.use_many call
- Add tests for provider dispatch, error handling, and API endpoint

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Fran Novo <fjnovo@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant