Skill v1.0.0
currentAutomated scan100/100name: azure-cosmos-db-py description: Build Azure Cosmos DB NoSQL services with Python/FastAPI following production-grade patterns. Use when implementing database client setup with dual auth (DefaultAzureCredential + emulator), service layer classes with CRUD operations, partition key strategies, parameterized queries, or TDD patterns for Cosmos. Triggers on phrases like "Cosmos DB", "NoSQL database", "document store", "add persistence", "database service layer", or "Python Cosmos SDK". license: MIT metadata: author: Microsoft version: "1.0.0" package: azure-cosmos
Cosmos DB Service Implementation
Build production-grade Azure Cosmos DB NoSQL services following clean code, security best practices, and TDD principles.
Installation
pip install azure-cosmos azure-identity
Environment Variables
COSMOS_ENDPOINT=https://<account>.documents.azure.com:443/ # Required for all auth methodsCOSMOS_DATABASE_NAME=<database-name> # Required for all auth methodsCOSMOS_CONTAINER_ID=<container-id> # Required for all auth methods# For emulator only (not production)COSMOS_KEY=<emulator-key> # Only required for key-based auth or emulatorAZURE_TOKEN_CREDENTIALS=prod # Required only if DefaultAzureCredential is used in production
Authentication & Lifecycle
🔑 Two rules apply to every code sample below:1. Prefer `DefaultAzureCredential`. It works locally (Azure CLI / VS Code / Developer CLI) and in Azure (managed identity, workload identity) with no code change. Avoid connection strings, account/API keys — they bypass Entra audit and rotation.- Local dev:DefaultAzureCredentialworks as-is.- Production: setAZURE_TOKEN_CREDENTIALS=prod(orAZURE_TOKEN_CREDENTIALS=<specific_credential>) to constrain the credential chain to production-safe credentials.2. Wrap every client in a context manager so HTTP transports, sockets, and token caches are released deterministically:- Sync:with <Client>(...) as client:- Async:async with <Client>(...) as client:andasync with DefaultAzureCredential() as credential:(fromazure.identity.aio)Snippets may abbreviate this setup, but production code should always follow both rules.
DefaultAzureCredential (preferred):
import osfrom azure.cosmos import CosmosClientfrom azure.identity import DefaultAzureCredential, ManagedIdentityCredential# Local dev: DefaultAzureCredential. Production: set AZURE_TOKEN_CREDENTIALS=prod or AZURE_TOKEN_CREDENTIALS=<specific_credential>credential = DefaultAzureCredential(require_envvar=True)# Or use a specific credential directly in production:# See https://learn.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python#credential-classes# credential = ManagedIdentityCredential()with CosmosClient(url=os.environ["COSMOS_ENDPOINT"],credential=credential) as client:# Use client here (see following sections for operations)...
Emulator (local development):
from azure.cosmos import CosmosClientwith CosmosClient(url="https://localhost:8081",credential=os.environ["COSMOS_KEY"],connection_verify=False) as client:# Use client here (see following sections for operations)...
Architecture Overview
┌─────────────────────────────────────────────────────────────────┐│ FastAPI Router ││ - Auth dependencies (get_current_user, get_current_user_required)│ - HTTP error responses (HTTPException) │└──────────────────────────────┬──────────────────────────────────┘│┌──────────────────────────────▼──────────────────────────────────┐│ Service Layer ││ - Business logic and validation ││ - Document ↔ Model conversion ││ - Graceful degradation when Cosmos unavailable │└──────────────────────────────┬──────────────────────────────────┘│┌──────────────────────────────▼──────────────────────────────────┐│ Cosmos DB Client Module ││ - Singleton container initialization ││ - Dual auth: DefaultAzureCredential (Azure) / Key (emulator) ││ - Async wrapper via run_in_threadpool │└─────────────────────────────────────────────────────────────────┘
Quick Start
1. Client Module Setup
Create a singleton Cosmos client with dual authentication:
# db/cosmos.pyfrom azure.cosmos import CosmosClientfrom azure.identity import DefaultAzureCredentialfrom starlette.concurrency import run_in_threadpool_cosmos_container = Nonedef _is_emulator_endpoint(endpoint: str) -> bool:return "localhost" in endpoint or "127.0.0.1" in endpointasync def get_container():global _cosmos_containerif _cosmos_container is None:# Singleton: client lives for the FastAPI app lifetime; close in a lifespan shutdown handler.if _is_emulator_endpoint(settings.cosmos_endpoint):client = CosmosClient(url=settings.cosmos_endpoint,credential=settings.cosmos_key,connection_verify=False)else:client = CosmosClient(url=settings.cosmos_endpoint,credential=DefaultAzureCredential())db = client.get_database_client(settings.cosmos_database_name)_cosmos_container = db.get_container_client(settings.cosmos_container_id)return _cosmos_container
Full implementation: See references/client-setup.md
2. Pydantic Model Hierarchy
Use five-tier model pattern for clean separation:
class ProjectBase(BaseModel): # Shared fieldsname: str = Field(..., min_length=1, max_length=200)class ProjectCreate(ProjectBase): # Creation requestworkspace_id: str = Field(..., alias="workspaceId")class ProjectUpdate(BaseModel): # Partial updates (all optional)name: Optional[str] = Field(None, min_length=1)class Project(ProjectBase): # API responseid: strcreated_at: datetime = Field(..., alias="createdAt")class ProjectInDB(Project): # Internal with docTypedoc_type: str = "project"
3. Service Layer Pattern
class ProjectService:def _use_cosmos(self) -> bool:return get_container() is not Noneasync def get_by_id(self, project_id: str, workspace_id: str) -> Project | None:if not self._use_cosmos():return Nonedoc = await get_document(project_id, partition_key=workspace_id)if doc is None:return Nonereturn self._doc_to_model(doc)
Full patterns: See references/service-layer.md
Core Principles
Security Requirements
- RBAC Authentication: Use
DefaultAzureCredentialin Azure — never store keys in code - Emulator-Only Keys: Hardcode the well-known emulator key only for local development
- Parameterized Queries: Always use
@parametersyntax — never string concatenation - Partition Key Validation: Validate partition key access matches user authorization
Clean Code Conventions
- Single Responsibility: Client module handles connection; services handle business logic
- Graceful Degradation: Services return
None/[]when Cosmos unavailable - Consistent Naming:
_doc_to_model(),_model_to_doc(),_use_cosmos() - Type Hints: Full typing on all public methods
- CamelCase Aliases: Use
Field(alias="camelCase")for JSON serialization
TDD Requirements
Write tests BEFORE implementation using these patterns:
@pytest.fixturedef mock_cosmos_container(mocker):container = mocker.MagicMock()mocker.patch("app.db.cosmos.get_container", return_value=container)return container@pytest.mark.asyncioasync def test_get_project_by_id_returns_project(mock_cosmos_container):# Arrangemock_cosmos_container.read_item.return_value = {"id": "123", "name": "Test"}# Actresult = await project_service.get_by_id("123", "workspace-1")# Assertassert result.id == "123"assert result.name == "Test"
Full testing guide: See references/testing.md
Best Practices
- Pick sync OR async and stay consistent. Do not mix
azure.xxxsync clients withazure.xxx.aioasync clients in the same call path. Choose one mode per module. - Always use context managers for clients and async credentials. Wrap the client in
async with CosmosClient(...) as client:(or manage its lifetime via FastAPI lifespan and close it explicitly). For asyncDefaultAzureCredentialfromazure.identity.aio, also useasync with credential:so tokens and transports are cleaned up.
Reference Files
| File | When to Read | |
|---|---|---|
| references/client-setup.md | Setting up Cosmos client with dual auth, SSL config, singleton pattern | |
| references/service-layer.md | Implementing full service class with CRUD, conversions, graceful degradation | |
| references/testing.md | Writing pytest tests, mocking Cosmos, integration test setup | |
| references/partitioning.md | Choosing partition keys, cross-partition queries, move operations | |
| references/error-handling.md | Handling CosmosResourceNotFoundError, logging, HTTP error mapping |
Template Files
| File | Purpose | |
|---|---|---|
| assets/cosmos_client_template.py | Ready-to-use client module | |
| assets/service_template.py | Service class skeleton | |
| assets/conftest_template.py | pytest fixtures for Cosmos mocking |
Quality Attributes (NFRs)
Reliability
- Graceful degradation when Cosmos unavailable
- Retry logic with exponential backoff for transient failures
- Connection pooling via singleton pattern
Security
- Zero secrets in code (RBAC via DefaultAzureCredential)
- Parameterized queries prevent injection
- Partition key isolation enforces data boundaries
Maintainability
- Five-tier model pattern enables schema evolution
- Service layer decouples business logic from storage
- Consistent patterns across all entity services
Testability
- Dependency injection via
get_container() - Easy mocking with module-level globals
- Clear separation enables unit testing without Cosmos
Performance
- Partition key queries avoid cross-partition scans
- Async wrapping prevents blocking FastAPI event loop
- Minimal document conversion overhead