Environment:ALwrity ALwrity Python Backend
| Knowledge Sources | |
|---|---|
| Domains | Backend, Web_API, Multi_Tenant |
| Last Updated | 2026-10-24 00:00 GMT |
Overview
Python 3.11.9 environment with FastAPI, uvicorn/gunicorn, SQLAlchemy, and a multi-tenant SQLite database, serving the entire ALwrity backend API.
Description
This environment provides the standard CPU-based Python runtime for the ALwrity backend. It is built on Python 3.11.9 (declared in `backend/runtime.txt`) and uses FastAPI as the web framework, served by uvicorn (development) or gunicorn (production on Render). Data persistence is handled by SQLAlchemy 2.0+ with a per-user SQLite database file (multi-tenant isolation). Authentication is provided by Clerk (`fastapi-clerk-auth`) with JWT middleware. Logging uses loguru, and the application is configured via `python-dotenv` from a `.env` file.
The stack includes HTTP clients (`httpx`, `aiohttp`, `requests`), payment processing (`stripe`), task scheduling (`apscheduler`), and Google API integrations (`google-api-python-client`, `google-auth-oauthlib`).
Usage
Use this environment for any ALwrity backend operation. It is the mandatory prerequisite for all 30 documented implementations — every Implementation page in the knowledge graph requires this base environment. Deploy with:
- Development: `uvicorn backend.main:app --reload`
- Production: `gunicorn -c backend/gunicorn_config.py backend.main:app`
System Requirements
| Category | Requirement | Notes |
|---|---|---|
| OS | Linux (Ubuntu recommended) | Render deployment uses Linux; Windows requires WSL2 for file-lock compatibility |
| Hardware | 2+ CPU cores, 4GB RAM | Minimum for single-user; scale with concurrent users |
| Disk | 10GB SSD | Per-user SQLite DBs grow with content assets and txtai indices |
Dependencies
System Packages
- `python` = 3.11.9 (declared in `runtime.txt`)
- `ffmpeg` (required by `imageio-ffmpeg` for video composition)
- `git`
Python Packages
Core web server:
- `fastapi` >= 0.115.14
- `starlette` >= 0.40.0, < 0.47.0
- `sse-starlette` < 3.0.0
- `uvicorn` >= 0.24.0
- `gunicorn` >= 21.0.0
- `python-multipart` >= 0.0.6
- `python-dotenv` >= 1.0.0
Auth and database:
- `fastapi-clerk-auth` >= 0.0.7
- `PyJWT` >= 2.8.0
- `cryptography` >= 41.0.0
- `sqlalchemy` >= 2.0.25
Payment:
- `stripe` >= 8.0.0
HTTP clients:
- `httpx` >= 0.28.1
- `aiohttp` >= 3.9.0
- `requests` >= 2.31.0
AI / LLM:
- `openai` >= 1.3.0
- `google-genai` >= 1.0.0
- `exa-py` == 1.9.1
Data processing:
- `pandas` >= 2.0.0
- `numpy` >= 1.24.0
Utilities:
- `loguru` >= 0.7.2
- `tenacity` >= 8.2.3
- `pydantic` >= 2.5.2, < 3.0.0
- `pydantic-settings` >= 2.0.0
- `apscheduler` >= 3.10.0
- `redis` >= 5.0.0
- `aiofiles` >= 23.2.0
- `psutil` >= 5.9.0
Google APIs:
- `google-api-python-client` >= 2.100.0
- `google-auth` >= 2.23.0
- `google-auth-oauthlib` >= 1.0.0
Testing:
- `pytest` >= 7.4.0
- `pytest-asyncio` >= 0.21.0
Credentials
The following environment variables must be set in `.env` (names only — never commit values):
LLM API keys (at least one required):
- `GEMINI_API_KEY`: Google Gemini API key (default provider)
- `OPENAI_API_KEY`: OpenAI API key (optional)
- `ANTHROPIC_API_KEY`: Anthropic API key (optional)
- `MISTRAL_API_KEY`: Mistral API key (optional)
- `HF_TOKEN`: HuggingFace token for inference + image generation
Research API keys:
- `EXA_API_KEY`: Exa neural search API key (required for research)
- `TAVILY_API_KEY`: Tavily search API key (optional)
- `SERPER_API_KEY`: Serper Google SERP API key (optional)
Authentication:
- `CLERK_SECRET_KEY`: Clerk JWT verification secret
OAuth token encryption:
- `OAUTH_TOKEN_ENCRYPTION_KEY`: Shared Fernet key for OAuth token encryption (see Environment:ALwrity_ALwrity_Fernet_Requirements)
Server configuration:
- `HOST`: Bind address (default `0.0.0.0`)
- `PORT`: Listen port (default `8000`)
- `DEBUG`: Debug mode (default `true`)
- `LOG_LEVEL`: Logging level (default `INFO`)
- `FRONTEND_URL`: Frontend URL for OAuth callbacks
- `OAUTH_CALLBACK_ALLOWED_ORIGINS`: Comma-separated trusted origins for OAuth postMessage
Quick Install
# Core backend dependencies
pip install fastapi>=0.115.14 uvicorn[standard]>=0.24.0 gunicorn>=21.0.0 \
python-dotenv>=1.0.0 loguru>=0.7.2 tenacity>=8.2.3 \
pydantic>=2.5.2,<3.0.0 pydantic-settings>=2.0.0 \
sqlalchemy>=2.0.25 fastapi-clerk-auth>=0.0.7 PyJWT>=2.8.0 cryptography>=41.0.0 \
stripe>=8.0.0 httpx>=0.28.1 aiohttp>=3.9.0 requests>=2.31.0 \
openai>=1.3.0 google-genai>=1.0.0 exa-py==1.9.1 \
pandas>=2.0.0 numpy>=1.24.0 \
apscheduler>=3.10.0 redis>=5.0.0 aiofiles>=23.2.0 psutil>=5.9.0 \
google-api-python-client>=2.100.0 google-auth>=2.23.0 google-auth-oauthlib>=1.0.0 \
pytest>=7.4.0 pytest-asyncio>=0.21.0
Code Evidence
Python runtime version from `backend/runtime.txt:1`:
python-3.11.9
Provider key resolution and fail-fast from `backend/services/llm_providers/textgen_utils/llm_text_generator.py:131`:
raise RuntimeError("No LLM API keys configured for tenant or environment defaults.")
Per-user SQLite engine cache (multi-tenant isolation) from `backend/services/database.py:61-62`:
# Engine cache for multi-tenant support
_user_engines = {}
Schema backfill for legacy tenant DBs from `backend/services/database.py:65-93`:
def _ensure_daily_workflow_schema(engine, user_id: str) -> None:
"""Backfill required daily_workflow_plans columns for legacy tenant DBs."""
required_columns = {
"generation_mode": "VARCHAR(30) NOT NULL DEFAULT 'llm_generation'",
...
}
Common Errors
| Error Message | Cause | Solution |
|---|---|---|
| `No LLM API keys configured for tenant or environment defaults.` | No provider API key in `.env` or per-tenant config | Set `GEMINI_API_KEY` (or another provider key) in `.env` |
| `CLERK_SECRET_KEY is not configured` | Clerk auth secret missing | Set `CLERK_SECRET_KEY` in `.env` for JWT verification |
| `sqlite3.OperationalError: no such table` | User DB not initialised | Run `backend/scripts/create_all_tables.py` or trigger onboarding |
| `ModuleNotFoundError: No module named 'fastapi-clerk-auth'` | Auth dependency missing | `pip install fastapi-clerk-auth>=0.0.7` |
Compatibility Notes
- Windows: File-lock handling for txtai indices requires WSL2; native Windows may crash on concurrent index access.
- macOS: Supported for development; `pyttsx3` TTS uses the system `say` command.
- Render deployment: Uses `gunicorn` with `backend/gunicorn_config.py`; worker count auto-tuned to CPU cores.
- Feature flags: `ALWRITY_ENABLED_FEATURES` env var enables lean modes (`podcast`, `linkedin`) with reduced dependency sets.
Related Pages
- Implementation:ALwrity_ALwrity_llm_text_gen
- Implementation:ALwrity_ALwrity_initialize_onboarding
- Implementation:ALwrity_ALwrity_APIKeyManager
- Implementation:ALwrity_ALwrity_OnboardingProgressService
- Implementation:ALwrity_ALwrity_WebsiteIntakeService_generate_site_brief
- Implementation:ALwrity_ALwrity_PersonaAnalysisService_generate_persona_from_onboarding
- Implementation:ALwrity_ALwrity_ResearchService_research
- Implementation:ALwrity_ALwrity_OutlineService_generate_outline
- Implementation:ALwrity_ALwrity_EnhancedContentGenerator_generate_section
- Implementation:ALwrity_ALwrity_HallucinationDetector
- Implementation:ALwrity_ALwrity_WordPressPublisher_resolve_content_manager
- Implementation:ALwrity_ALwrity_OnPageSEOService
- Implementation:ALwrity_ALwrity_LinkedInService_generate_linkedin_post
- Implementation:ALwrity_ALwrity_CitationManager
- Implementation:ALwrity_ALwrity_OAuthProviderBase
- Implementation:ALwrity_ALwrity_OAuthTokenMonitoringService
- Implementation:ALwrity_ALwrity_CreateStudioService
- Implementation:ALwrity_ALwrity_BrollComposer
- Implementation:ALwrity_ALwrity_BaseALwrityAgent
- Implementation:ALwrity_ALwrity_TaskMemoryService_record_task_outcome
- Implementation:ALwrity_ALwrity_TxtaiIntelligenceService_index_content
- Implementation:ALwrity_ALwrity_PodcastService_create_project
- Implementation:ALwrity_ALwrity_PodcastContextBuilder_build_enhance_context
- Implementation:ALwrity_ALwrity_PodcastVideoCombinationService_combine_videos
- Implementation:ALwrity_ALwrity_ContentGapAnalyzer_analyze_comprehensive_gap
- Implementation:ALwrity_ALwrity_GSCAnalyzerService_analyze_search_performance
- Implementation:ALwrity_ALwrity_BingOAuthService
- Implementation:ALwrity_ALwrity_Plan_Context_Threshold
- Implementation:ALwrity_ALwrity_ContentQualityAnalyzer
- Implementation:ALwrity_ALwrity_save_and_track_text_content