Skip to main content

Common python code used in out backend services.

Project description

dcc-backend-common

Commit activity License

Common utilities and components for backend services developed by the Data Competence Center Basel-Stadt.

Overview

dcc-backend-common is a Python library that provides shared functionality for backend services, including:

  • FastAPI Health Probes: Kubernetes-ready health check endpoints (liveness, readiness, startup)
  • Structured Logging: Integration with structlog for consistent logging across services
  • Configuration Management: Environment-based configuration with python-dotenv
  • DSPy Utilities: Helpers for DSPy modules, streaming listeners, metrics, and dataset preparation

Installation

Basic Installation (uv)

uv add dcc-backend-common

With FastAPI Support

uv add "dcc-backend-common[fastapi]"

Requirements

  • Python 3.12 or higher
  • Dependencies:
    • dspy>=3.0.4
    • python-dotenv>=1.0.1
    • structlog>=25.1.0

Optional Dependencies

  • FastAPI extras: aiohttp>=3.13.2, fastapi>=0.115,<1.0

Features

FastAPI Health Probes

The library provides Kubernetes-ready health check endpoints that follow best practices for container orchestration:

Example Usage

from fastapi import FastAPI
from dcc_backend_common.fastapi_health_probes import health_probe_router

app = FastAPI()

# Define external service dependencies
service_dependencies = [
    {
        "name": "database",
        "health_check_url": "http://postgres:5432/health",
        "api_key": None  # Optional API key for authenticated health checks
    },
    {
        "name": "external-api",
        "health_check_url": "https://api.example.com/health",
        "api_key": "your-api-key-here"
    }
]

# Include health probe router
app.include_router(health_probe_router(service_dependencies))

Available Endpoints

1. Liveness Probe (GET /health/liveness)
  • Purpose: Checks if the application process is running and not deadlocked
  • Kubernetes Action: If this fails, the container is killed and restarted
  • Response: Returns uptime in seconds
  • Rule: Keep it simple. Do NOT check databases or external dependencies here
{
  "status": "up",
  "uptime_seconds": 123.45
}
2. Readiness Probe (GET /health/readiness)
  • Purpose: Checks if the app is ready to handle user requests
  • Kubernetes Action: If this fails, traffic stops being sent to this pod
  • Response: Returns status of all configured service dependencies
  • Rule: Check critical dependencies here (databases, external APIs, etc.)
{
  "status": "ready",
  "checks": {
    "database": "healthy",
    "external-api": "healthy"
  }
}

If a dependency fails:

{
  "status": "unhealthy",
  "checks": {
    "database": "error: Connection refused",
    "external-api": "unhealthy (status: 503)"
  },
  "error": "Service unavailable"
}
3. Startup Probe (GET /health/startup)
  • Purpose: Checks if the application has finished initialization
  • Kubernetes Action: Blocks liveness/readiness probes until this returns 200
  • Response: Returns startup timestamp
  • Rule: Useful for apps that need to load large ML models or caches on boot
{
  "status": "started",
  "timestamp": "2025-12-04T10:30:00.000000+00:00"
}

Features

  • Automatic Logging Suppression: Health check endpoints are automatically excluded from access logs to reduce noise
  • Dependency Health Checks: Readiness probe checks external service dependencies with configurable timeouts (5 seconds default)
  • Authentication Support: Optional API key support for authenticated health checks
  • Kubernetes-Ready: HTTP status codes follow Kubernetes conventions (200 = healthy, 503 = unhealthy)

Structured Logging

  • Initialize structured logging with init_logger(), which auto-selects JSON output in production (IS_PROD=true) and colored console output otherwise.
  • Retrieve loggers via get_logger(__name__). A request_id and timestamp are added automatically.

Application Configuration

Load strongly-typed configuration from environment variables:

from dcc_backend_common.config.app_config import AppConfig

config = AppConfig.from_env()
print(config)  # secrets are redacted in __str__

Required variables: CLIENT_URL, HMAC_SECRET, OPENAI_API_KEY, LLM_URL, DOCLING_URL, WHISPER_URL, OCR_URL. Missing values raise AppConfigError.

Development

Setup

  1. Clone the repository:
git clone https://github.com/DCC-BS/backend-common.git
cd backend-common
  1. Install development dependencies:
uv sync --group dev --all-extras  # include all extras for local dev

Running Tests

uv run pytest

Code Quality

This project uses:

  • Ruff: For linting and formatting
  • Pre-commit: For automated code quality checks
  • Tox: For testing across multiple Python versions

Run linting:

uv run ruff check .

Run pre-commit hooks:

uv run pre-commit run --all-files

Releasing

This project uses GitHub Actions for automated releases to PyPI.

To release a new version:

  1. Update the version: Update the version field in pyproject.toml.
  2. Commit and push: Commit the version change and push it to the main branch.
  3. Trigger the workflow:
    • Navigate to the Actions tab in the GitHub repository.
    • Select the Publish to PyPI workflow.
    • Click Run workflow.
  4. Automated steps: The workflow will:
    • Automatically detect the version using uv version --short.
    • Create and push a git tag (e.g., v0.1.0).
    • Build the package with uv build.
    • Publish to PyPI using Trusted Publishing.

Contributing

See CONTRIBUTING.md for details on how to contribute to this project.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Authors

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dcc_backend_common-0.1.4.tar.gz (235.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dcc_backend_common-0.1.4-py3-none-any.whl (24.0 kB view details)

Uploaded Python 3

File details

Details for the file dcc_backend_common-0.1.4.tar.gz.

File metadata

  • Download URL: dcc_backend_common-0.1.4.tar.gz
  • Upload date:
  • Size: 235.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.29 {"installer":{"name":"uv","version":"0.9.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for dcc_backend_common-0.1.4.tar.gz
Algorithm Hash digest
SHA256 187a34e6783bf55657bee5eb823a5aebbda8ddba396d12c5d65a896cc2816841
MD5 a1ac2dce36b58493cc692c3161b7c0ae
BLAKE2b-256 ccd31a38dc3833b2d55bb62774bb1b7c32a1c045b2a79e3c6a264e2297e0756b

See more details on using hashes here.

File details

Details for the file dcc_backend_common-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: dcc_backend_common-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 24.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.29 {"installer":{"name":"uv","version":"0.9.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for dcc_backend_common-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 48cf6a8fdb20265a6bcea9714e4c2da1ad0bb10e211da886b64c8cd304f09c44
MD5 54a72bd5de2cb3b3d500cd2e392a800c
BLAKE2b-256 f16ee69f2b3b88de268429cce37dd0fd9f0d8b5bbc53c78455d0a38d6d57de57

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page