architecting – Tech Newst https://technewst.com Sat, 12 Sep 2026 22:41:19 +0000 en-US hourly 1 https://wordpress.org/?v=7.1.5 https://technewst.com/wp-content/uploads/2025/10/cropped-cut_out20260420_014916-32x32.webp architecting – Tech Newst https://technewst.com 32 32 Architecting with portability in mind is the best way to minimize lock-in as AI pricing models evolve. Price hikes are inevitable. https://technewst.com/architecting-with-portability-in-mind-is-the-best-way-to-minimize-lock-in-as-ai-pricing-models-evolve-price-hikes-are-inevitable/ https://technewst.com/architecting-with-portability-in-mind-is-the-best-way-to-minimize-lock-in-as-ai-pricing-models-evolve-price-hikes-are-inevitable/#respond Sat, 12 Sep 2026 22:41:19 +0000 https://technewst.com/architecting-with-portability-in-mind-is-the-best-way-to-minimize-lock-in-as-ai-pricing-models-evolve-price-hikes-are-inevitable/ The modern software development landscape is currently living through an unprecedented economic illusion. Every artificial intelligence feature, copilot integration, and automated pipeline being deployed by enterprises worldwide is operating on a subsidized cost structure that cannot possibly persist indefinitely. Just as early consumers of ride-sharing services like Uber and Lyft enjoyed artificially depressed fares that vastly underrepresented the true cost of transportation—only to watch prices surge as investors demanded profitability—businesses building upon current generative AI infrastructure are hurtling toward a financial reckoning. Model providers, fueled by billions in venture capital and corporate backing, are deliberately handing out cheap computing power and discounted API calls to capture market share. However, as the massive capital expenditures required to train and run frontier models demand returns, price hikes are not merely a possibility; they are an economic certainty.

This impending reality presents a profound operational challenge for enterprise IT leaders. Historically, technology adoption cycles have forced organizations to carefully balance speed-to-market against long-term vendor dependency. What makes the current artificial intelligence boom distinct is the blistering velocity at which these dependencies are being established. Driven by intense competitive pressures to launch AI-powered features, development teams are integrating proprietary vendor models directly into their core codebases, often with little to no visibility into the structural lock-in they are cultivating. Across the broader IT ecosystem, long-term scalability has always been inextricably linked to architectural portability. Designing systems with portability at the forefront is no longer just a theoretical best practice; it is the single most effective strategy for mitigating vendor lock-in, retaining leverage during future contract negotiations, and maintaining a viable escape hatch when API costs inevitably skyrocket.

The Economic Realities of Subsidized Artificial Intelligence

To understand why architectural portability is paramount, one must first examine the economics governing the current artificial intelligence market. The race to dominate the generative AI sector has triggered a massive capital expenditure cycle among tech giants and specialized model providers alike. Training frontier models requires thousands of specialized GPUs, vast amounts of energy, and immense engineering talent, resulting in upfront costs measured in the hundreds of millions—and increasingly, billions—of dollars.

To lower the barrier to entry and entice developers away from competing platforms, providers have priced their API services far below the actual marginal cost of inference. This market-share-grab strategy mirrors historical precedents in the broader technology and transportation sectors. When cloud computing first emerged, early adopters experienced aggressive discounting before standardized pricing models matured. Similarly, the current generation of AI consumers is building products atop a foundation of unsustainable subsidies.

As investors increasingly prioritize profitability over unbridled growth, these subsidies are beginning to taper off. Industry analysts project that as specialized hardware constraints ease and market consolidation takes hold, providers will be forced to adjust their pricing structures to reflect true operational expenses. Organizations that have optimized their architectures solely for a single provider’s ecosystem will find themselves cornered, facing a stark choice between absorbing exorbitant cost increases or undertaking a painful, emergency code rewrite.

The Illusion of Painless Migration: Code Versus Behavior

The primary obstacle to achieving true AI portability is that modern model providers have a clear, structural incentive to make customer migration as difficult as possible. This is achieved not merely through high switching costs, but through deep technological integration. Vendors encourage the adoption of proprietary features—such as specialized fine-tuning frameworks, unique tokenization schemas, advanced tool-calling conventions, and proprietary vector embedding dimensions—that anchor a customer’s application deeply into their specific ecosystem.

While enterprise architects frequently focus on the superficial aspects of migration, industry experts emphasize a critical distinction: code portability is cheap, whereas behavioral portability is expensive.

Modifying an application to send requests to a different API endpoint is a straightforward engineering task. However, adapting an application to behave identically across different foundational models is an entirely different matter. Prompts meticulously engineered and tuned to exploit the specific nuances and behavioral quirks of one model will rarely produce equivalent results on another. Furthermore, vector embeddings generated by one provider’s model are fundamentally incompatible with those of a competitor, meaning that any organization switching platforms must completely re-embed its entire knowledge base—a process that incurs both computational costs and potential downtime.

Similarly, advanced features like structured outputs, function calling, and batch processing mechanisms vary wildly from one vendor to another. Code written around one model’s specific schema must be thoroughly rewritten and rigorously tested before deployment on a new backend. Consequently, while gateway tools can successfully abstract the network layer, no off-the-shelf software can completely eliminate the behavioral friction inherent in transitioning between distinct artificial intelligence models.

Architectural Strategies for Mitigating Vendor Dependency

Despite the inherent challenges of behavioral portability, organizations are far from helpless. Mitigating concentration risk requires a deliberate approach to system design, separating the components of an application that can be controlled from those dictated by external vendors.

Implementing the Abstraction Layer

The cornerstone of any resilient AI architecture is the internal abstraction layer—often referred to as an AI gateway or proxy. Instead of configuring application code to communicate directly with external endpoints such as OpenAI’s GPT, Anthropic’s Claude, or Google’s Gemini, internal services route requests through an internal translation layer. This proxy accepts requests in a standardized, organization-defined format and translates them into the specific payload expected by whichever vendor sits behind the curtain.

Building this capability no longer requires building from the ground up. Open-source routing and interface gateways, such as LiteLLM and Portkey, have matured rapidly, offering robust frameworks that handle load balancing, fallback routing, and interface standardization. For a typical engineering team, scoping this abstraction layer to handle essential operations—such as standard text generation and basic tool calls—represents a modest project that can be completed by a small team within a matter of weeks. Once established, transitioning between underlying vendors shifts from a massive code refactoring effort to a simple configuration update.

Auditing and Scoping the Codebase

To operationalize portability, IT leaders must conduct comprehensive audits of their existing codebases to identify every instance where vendor Software Development Kits (SDKs) are imported directly. Engineering teams are advised to systematically route these direct calls through the newly established internal proxy layer.

Crucially, architects should deliberately avoid building dependencies on niche, vendor-specific features—such as proprietary fine-tuning pipelines or specialized batch APIs—unless those features are core to the product’s value proposition. By focusing on the foundational 80% use case (standard text inputs, outputs, and basic function calling), organizations preserve maximum flexibility without sacrificing immediate product velocity.

Industry Perspectives and Strategic Implications

As the enterprise software market matures, technology executives are increasingly drawing parallels between artificial intelligence adoption and legacy cloud migration strategies. Industry analysts and enterprise architects argue that treating AI providers as ordinary infrastructure vendors is essential for long-term risk management.

Tech sector commentators have frequently highlighted the dangers of vendor lock-in across enterprise software, noting that historical lessons from relational databases and cloud storage services apply equally well to the generative AI boom. Organizations that fail to establish multi-vendor strategies or fallback mechanisms expose themselves to severe financial vulnerability.

In response to these risks, forward-thinking enterprises are beginning to adopt formal AI governance frameworks. These policies mandate that no critical workflow may rely on a single, proprietary model without a documented contingency plan. Furthermore, organizations are increasingly exploring open-weight models hosted within their own private infrastructure. By utilizing open-weight alternatives for sensitive or high-volume tasks, companies can retain complete control over their fine-tuned weights and vector embeddings, effectively insulating themselves from external price volatility.

Best Practices for Building with Open Eyes

For technology leaders seeking to evaluate their current exposure to vendor lock-in, theoretical assessments and architectural checklists are rarely sufficient. The most effective way to measure portability is through empirical testing.

Industry experts recommend selecting a non-critical internal workflow or secondary feature and attempting to port it from its current vendor to a competing platform. Documenting what breaks, analyzing why it failed, and measuring the exact time required to complete the migration provides an unvarnished reality check. This practical exercise exposes hidden dependencies—such as hardcoded prompt assumptions or incompatible embedding spaces—far more effectively than any administrative audit.

Ultimately, the proliferation of artificial intelligence in the enterprise does not mean organizations must avoid proprietary models altogether. Rather, it requires a shift toward deliberate dependency management. By acknowledging that current pricing models are temporary, understanding the boundary between code and behavioral portability, and implementing robust abstraction layers, enterprises can ensure they retain their strategic autonomy. When the inevitable AI price hikes arrive, organizations that built with portability in mind will maintain the leverage needed to navigate the market on their own terms.

]]>
https://technewst.com/architecting-with-portability-in-mind-is-the-best-way-to-minimize-lock-in-as-ai-pricing-models-evolve-price-hikes-are-inevitable/feed/ 0
Architecting Scalable FastAPI Applications: A Modular Approach to Mitigate Development Challenges https://technewst.com/architecting-scalable-fastapi-applications-a-modular-approach-to-mitigate-development-challenges/ https://technewst.com/architecting-scalable-fastapi-applications-a-modular-approach-to-mitigate-development-challenges/#respond Sat, 24 Jan 2026 10:46:44 +0000 http://technewst.com/architecting-scalable-fastapi-applications-a-modular-approach-to-mitigate-development-challenges/ FastAPI, a modern, fast (high-performance), web framework for building APIs with Python 3.8+ based on standard Python type hints, has rapidly ascended to prominence within the developer community. Its core appeal lies in its inherent flexibility and "freedom," empowering developers to craft bespoke solutions tailored precisely to their project’s needs. This very freedom, however, presents a significant paradox: while it allows for unparalleled customization, it can also lead to a lack of conventional structure, potentially resulting in what some developers colloquially term "Frankenstein" projects—applications pieced together from disparate libraries and architectural choices, often lacking consistency and long-term maintainability. This article outlines a robust, opinionated, and modular blueprint for initializing and structuring FastAPI applications, aiming to harness its power while mitigating the common pitfalls associated with excessive architectural liberty.

The Rise of FastAPI and its Foundational Paradox

FastAPI’s meteoric rise since its inception is well-documented. It consistently ranks high in developer surveys for satisfaction and usage, often cited alongside established frameworks like Django and Flask. Its key differentiators—native asynchronous support, automatic data validation and serialization via Pydantic, and self-generating interactive API documentation (Swagger UI/ReDoc)—address critical needs in modern web service development. Benchmarking data frequently places FastAPI among the fastest Python web frameworks, sometimes even rivaling frameworks in other languages like Node.js and Go for I/O-bound tasks, a significant advantage for high-throughput API services.

However, this inherent flexibility, while celebrated, can become a double-edged sword. Unlike opinionated frameworks that dictate a specific directory structure, ORM, or templating engine, FastAPI leaves many architectural decisions to the developer. For small, single-developer projects, this allows for rapid prototyping and minimal overhead. Yet, in larger teams or projects with evolving requirements, this absence of convention can lead to:

  • Inconsistent Codebases: Different developers or teams may adopt varying patterns for configuration, database interaction, error handling, or module organization.
  • Increased Cognitive Load: New team members face a steeper learning curve trying to decipher a project’s unique architectural choices.
  • Maintainability Challenges: Debugging and extending functionality become arduous as the project grows, resembling a patchwork of individual preferences rather than a cohesive system.
  • Reduced Reusability: Components built without a consistent structure are harder to extract and reuse across projects.

The "Frankenstein" analogy aptly describes this scenario: projects assembled from various parts, each functional in isolation, but together forming an unwieldy and potentially unstable whole. The solution lies not in abandoning FastAPI’s flexibility but in adopting a well-defined, modular structure that provides a consistent framework for development, ensuring scalability and maintainability from the outset.

Establishing a Robust Development Environment with UV

The foundation of any scalable application begins with a well-managed development environment. While Python’s traditional venv remains a viable option, the emergence of tools like UV offers significant advantages in speed and dependency resolution. UV, developed by Astral, aims to be a faster alternative to pip and venv, streamlining the process of creating virtual environments and installing packages.

Chronology of Environment Setup:

  1. UV Installation: The first step involves installing UV according to the operating system’s specifications, typically via curl or pipx.
  2. Project Initialization: Navigate to the desired project directory (e.g., franky) and execute uv init. This command not only creates a virtual environment but also initializes a pyproject.toml file for project metadata and a README.md, along with optional Git initialization. This pyproject.toml file becomes the central point for managing project dependencies, adhering to modern Python packaging standards.
  3. Core Dependency Installation: A comprehensive set of libraries is crucial for a full-featured FastAPI application. The following command installs essential components:

    uv add 'fastapi[standard]' pydantic-settings python-dotenv sqlmodel uvicorn alembic httpx pytest pytest_asyncio greenlet aiosqlite

    This selection includes:

    • fastapi[standard]: The core framework, bundled with Pydantic for data validation and serialization.
    • pydantic-settings and python-dotenv: For robust configuration management, loading environment variables from .env files and validating them with Pydantic models.
    • sqlmodel: A powerful ORM that seamlessly integrates Pydantic with SQLAlchemy, simplifying database interactions and model definitions.
    • uvicorn: An ASGI server essential for running asynchronous Python web applications.
    • alembic: The industry-standard tool for database migrations, crucial for evolving database schemas in a controlled manner.
    • httpx: An asynchronous HTTP client, ideal for testing API endpoints.
    • pytest, pytest_asyncio, greenlet: The foundational toolkit for writing and running asynchronous tests.
    • aiosqlite: An asynchronous driver for SQLite, necessary for sqlmodel to perform async operations with SQLite databases.

    The pyproject.toml file, post-installation, reflects these dependencies, typically specifying version ranges to allow for minor updates while maintaining compatibility. For example:

    [project]
    name = "franky"
    version = "0.1.0"
    description = "A structured FastAPI project"
    readme = "README.md"
    requires-python = ">=3.13"
    dependencies = [
        "aiosqlite>=0.22.1",
        "alembic>=1.18.4",
        "fastapi[standard]>=0.136.0",
        "greenlet>=3.4.0",
        "httpx>=0.28.1",
        "pydantic-settings>=2.13.1",
        "pytest>=9.0.3",
        "pytest-asyncio>=1.3.0",
        "python-dotenv>=1.2.2",
        "sqlmodel>=0.0.38",
        "uvicorn>=0.44.0",
    ]

    This meticulous dependency management ensures a consistent environment across development, testing, and production.

Core Project Configuration, Dependencies, and Logging

A well-structured application requires centralized management for configuration, database interactions, and logging. This approach enhances clarity, reduces redundancy, and simplifies maintenance.

1. Configuration Management (src/core/config.py):
The pydantic-settings library, combined with python-dotenv, offers a robust solution for handling application settings. A Config class, inheriting from BaseSettings, allows environment variables to be loaded and validated with type hints, ensuring that settings are always in the expected format.

import os
from dotenv import load_dotenv
from pydantic_settings import BaseSettings

load_dotenv()

class Config(BaseSettings):
    app_name: str = "Franky"
    debug: bool = True
    db_name: str = os.getenv("DB_NAME") # Loaded from .env

    @property
    def db_url(self):
        # Using aiosqlite for async SQLite operations
        return f"sqlite+aiosqlite:///./self.db_name"

config = Config()

An accompanying .env file in the project root defines critical variables like DB_NAME:

DB_NAME=db.sqlite3

This setup ensures that sensitive configurations or environment-specific values are kept separate from the codebase, facilitating deployment across different environments (development, staging, production) with ease. The use of sqlite+aiosqlite explicitly signals the intent for asynchronous database operations, a cornerstone of FastAPI’s performance model.

2. Asynchronous Database Dependency (src/core/dependencies.py):
For an async framework like FastAPI, asynchronous database interaction is paramount. SQLAlchemy, the underlying technology for SQLModel, provides excellent async capabilities.

from typing import Annotated
from fastapi import Depends
from src.core.config import config
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession

engine = create_async_engine(config.db_url, connect_args="check_same_thread": False)
SessionLocal = async_sessionmaker(
    autocommit=False,
    autoflush=False,
    bind=engine,
    class_=AsyncSession
)

async def get_session() -> AsyncSession:
    async with SessionLocal() as session:
        try:
            yield session
        finally:
            await session.close()

SessionDep = Annotated[AsyncSession, Depends(get_session)]

This module establishes an asynchronous database engine and session factory. The get_session function, designed as a FastAPI dependency, yields an AsyncSession, ensuring proper session management (opening, yielding, and closing) for each request that requires database access. The SessionDep type annotation simplifies dependency injection into route handlers and services.

3. Structured Logging (src/core/logging.py):
Effective logging is critical for monitoring, debugging, and auditing applications. A dedicated logging setup centralizes configuration and ensures consistent log formats.

import logging

def setup_logging():
    logging.basicConfig(
        level=logging.INFO,
        format="%(asctime)s %(levelname)s [%(name)s] %(message)s"
    )

Integrating this into the main FastAPI application (main.py) ensures that all application logs adhere to a defined format, including timestamps, log levels, and the source module, which is invaluable for tracing issues in complex systems. This transforms default, less informative logs into structured, actionable output, a critical aspect of operational visibility.

Unified Response Schema and Exception Handling

A consistent API contract, particularly for responses and errors, significantly improves the developer experience for API consumers and simplifies client-side integration. This architecture proposes a unified response schema and comprehensive exception handling.

1. Standardized Response Model (src/core/models.py):
A generic IResponse model provides a consistent envelope for all successful API responses.

from typing import Generic, TypeVar
from pydantic import BaseModel

T = TypeVar("T")

class IResponse(BaseModel, Generic[T]):
    success: bool = True
    message: str = "Operation successful"
    data: T | None = None

This schema ensures every successful response includes success: true, a message, and the actual data. For errors, the success field would be false, and data would be null.

2. Response Unification Middleware (src/core/middlewares.py):
A custom middleware intercepts outgoing responses and wraps them in the IResponse schema, maintaining consistency without requiring manual wrapping in every endpoint.

import json
from fastapi import Request, Response
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.concurrency import iterate_in_threadpool
from src.core.models import IResponse

class UnifiedResponseMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request: Request, call_next):
        if request.url.path in ["/openapi.json", "/docs", "/redoc"]:
            return await call_next(request)

        response = await call_next(request)

        content_type = response.headers.get("content-type", "")
        if response.status_code < 400 and "application/json" in content_type:
            body = b"".join([section async for section in response.body_iterator])
            if not body:
                return response

            try:
                data = json.loads(body.decode("utf-8"))
                if isinstance(data, dict) and "success" in data: # Already wrapped
                    response.body_iterator = iterate_in_threadpool(iter([body]))
                    return response

                wrapped_data = IResponse(data=data).model_dump_json()

                headers = dict(response.headers)
                headers.pop("Content-Length", None) # Important for proper re-calculation
                headers.pop("content-length", None)

                return Response(
                    content=wrapped_data,
                    status_code=response.status_code,
                    headers=headers,
                    media_type="application/json"
                )
            except (json.JSONDecodeError, UnicodeDecodeError):
                response.body_iterator = iterate_in_threadpool(iter([body]))
                return response
        return response

This middleware strategically intercepts responses. It bypasses FastAPI’s documentation endpoints to avoid conflicts and only processes JSON responses for successful requests (status codes < 400). It re-encodes the response body into the IResponse format, crucial for maintaining a uniform API surface. A key technical detail is the removal of the Content-Length header, as the body’s size changes after wrapping, which would otherwise lead to client-side errors.

3. Centralized Exception Handlers (src/core/exceptions.py):
To ensure error responses also adhere to the unified schema, global exception handlers are implemented.

from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.exceptions import RequestValidationError
from starlette.exceptions import HTTPException as StarletteHTTPException

async def common_exception_handler(request: Request, exc: Exception):
    status_code = 400
    message = str(exc)

    if isinstance(exc, StarletteHTTPException):
        status_code = exc.status_code
        message = exc.detail
    elif isinstance(exc, RequestValidationError):
        status_code = 422 # Unprocessable Entity
        message = "Validation error: " + str(exc.errors()) # Detailed validation errors

    return JSONResponse(
        status_code=status_code,
        content=
            "success": False,
            "message": message,
            "data": None
        
    )

def setup_exception_handlers(app: FastAPI):
    app.add_exception_handler(StarletteHTTPException, common_exception_handler)
    app.add_exception_handler(RequestValidationError, common_exception_handler)
    app.add_exception_handler(Exception, common_exception_handler)

This setup_exception_handlers function registers a single common_exception_handler for StarletteHTTPException (FastAPI’s base HTTP exception), RequestValidationError (for Pydantic validation failures), and a generic Exception catch-all. This guarantees that all errors, regardless of their origin, are transformed into the standardized error response format (success: false, message, data: null), providing predictable error handling for API consumers.

Modular Application Structure: The appointments Example

A modular project structure is fundamental for scalability, maintainability, and team collaboration. It promotes separation of concerns, making code easier to navigate, test, and extend. The proposed structure organizes features into self-contained modules within a src directory.

--src
----module1
------__init__.py
------dependencies.py
------models.py
------router.py
------service.py
...

Let’s illustrate this with an appointments module:

1. Models (src/appointments/models.py):
This file defines the SQLModel entities that map to database tables, along with Pydantic schemas for request and response validation.

from datetime import datetime, UTC
from enum import Enum
from typing import Optional
from sqlmodel import SQLModel, Field

class AppointmentStatus(str, Enum):
    scheduled = "scheduled"
    completed = "completed"
    cancelled = "cancelled"

class AppointmentBase(SQLModel):
     str = Field(min_length=1, max_length=255)
    description: Optional[str] = Field(default=None, max_length=1000)
    start_time: datetime
    end_time: datetime
    location: Optional[str] = Field(default=None, max_length=500)
    status: AppointmentStatus = AppointmentStatus.scheduled

class Appointment(AppointmentBase, table=True): # Maps to a DB table
    id: Optional[int] = Field(default=None, primary_key=True)
    created_at: datetime = Field(default_factory=lambda: datetime.now(UTC))
    updated_at: datetime = Field(default_factory=lambda: datetime.now(UTC))

class AppointmentCreate(AppointmentBase): # Schema for creation
    pass

class AppointmentUpdate(SQLModel): # Schema for partial updates
     Optional[str] = Field(default=None, min_length=1, max_length=255)
    description: Optional[str] = Field(default=None, max_length=1000)
    start_time: Optional[datetime] = None
    end_time: Optional[datetime] = None
    location: Optional[str] = Field(default=None, max_length=500)
    status: Optional[AppointmentStatus] = None

class AppointmentRead(AppointmentBase): # Schema for reading/responses
    id: int
    created_at: datetime
    updated_at: datetime

This modular approach to models clearly separates the database representation (Appointment) from the API input (AppointmentCreate, AppointmentUpdate) and output (AppointmentRead) schemas, leveraging SQLModel’s ability to combine ORM and Pydantic functionalities.

2. Service Layer (src/appointments/service.py):
The service layer encapsulates the business logic and database interactions, abstracting them from the API endpoints. This promotes testability and reusability.

from datetime import datetime, UTC
from typing import Optional, Sequence
from sqlalchemy.ext.asyncio import AsyncSession
from sqlmodel import select
from src.appointments.models import Appointment, AppointmentCreate, AppointmentUpdate

class AppointmentService:
    def __init__(self, session: AsyncSession) -> None:
        self.session = session

    async def create(self, data: AppointmentCreate) -> Appointment:
        appointment = Appointment.model_validate(data)
        self.session.add(appointment)
        await self.session.commit()
        await self.session.refresh(appointment)
        return appointment

    async def get(self, appointment_id: int) -> Optional[Appointment]:
        return await self.session.get(Appointment, appointment_id)

    async def list(self, offset: int = 0, limit: int = 20) -> Sequence[Appointment]:
        result = await self.session.execute(
            select(Appointment).offset(offset).limit(limit)
        )
        return result.scalars().all()

    async def update(
        self, appointment_id: int, data: AppointmentUpdate
    ) -> Optional[Appointment]:
        appointment = await self.session.get(Appointment, appointment_id)
        if not appointment:
            return None
        updates = data.model_dump(exclude_unset=True)
        for key, value in updates.items():
            setattr(appointment, key, value)
        appointment.updated_at = datetime.now(UTC)
        self.session.add(appointment)
        await self.session.commit()
        await self.session.refresh(appointment)
        return appointment

    async def delete(self, appointment_id: int) -> bool:
        appointment = await self.session.get(Appointment, appointment_id)
        if not appointment:
            return False
        await self.session.delete(appointment)
        await self.session.commit()
        return True

The AppointmentService provides standard CRUD (Create, Read, Update, Delete) operations, accepting Pydantic models as input and returning SQLModel instances. This design ensures that the core business logic remains independent of the API layer.

3. Dependency Injection for Services (src/appointments/dependencies.py):
FastAPI’s dependency injection system is leveraged to provide instances of AppointmentService to route handlers.

from typing import Annotated
from fastapi import Depends
from src.core.dependencies import SessionDep
from src.appointments.service import AppointmentService

def get_appointment_service(session: SessionDep) -> AppointmentService:
    return AppointmentService(session)

AppointmentServiceDep = Annotated[AppointmentService, Depends(get_appointment_service)]

This pattern neatly injects a database session into the service, which is then injected into the router, ensuring that each request operates within its own transactional context.

4. API Router (src/appointments/router.py):
The router defines the API endpoints for the appointments module, using APIRouter to group related routes.

from fastapi import APIRouter, HTTPException, Query
from src.appointments.dependencies import AppointmentServiceDep
from src.appointments.models import AppointmentCreate, AppointmentRead, AppointmentUpdate

router = APIRouter(prefix="/appointments", tags=["appointments"])

@router.post("/", response_model=AppointmentRead, status_code=201)
async def create_appointment(data: AppointmentCreate, service: AppointmentServiceDep):
    return await service.create(data)

@router.get("/", response_model=list[AppointmentRead])
async def list_appointments(
    service: AppointmentServiceDep,
    offset: int = Query(0, ge=0),
    limit: int = Query(20, ge=1, le=100),
):
    return await service.list(offset=offset, limit=limit)

@router.get("/appointment_id", response_model=AppointmentRead)
async def get_appointment(appointment_id: int, service: AppointmentServiceDep):
    appointment = await service.get(appointment_id)
    if not appointment:
        raise HTTPException(status_code=404, detail="Appointment not found")
    return appointment

@router.patch("/appointment_id", response_model=AppointmentRead)
async def update_appointment(
    appointment_id: int,
    data: AppointmentUpdate,
    service: AppointmentServiceDep,
):
    appointment = await service.update(appointment_id, data)
    if not appointment:
        raise HTTPException(status_code=404, detail="Appointment not found")
    return appointment

@router.delete("/appointment_id", status_code=204)
async def delete_appointment(appointment_id: int, service: AppointmentServiceDep):
    deleted = await service.delete(appointment_id)
    if not deleted:
        raise HTTPException(status_code=404, detail="Appointment not found")

This router clearly defines the API endpoints, their input/output schemas, and integrates the AppointmentServiceDep for business logic execution. FastAPI’s automatic documentation will pick up these routes and their schemas, providing a comprehensive API specification.

FastAPI Database Migration with Alembic

Database schema evolution is a critical aspect of application development. Alembic, a lightweight database migration tool for SQLAlchemy, provides a robust mechanism to manage schema changes in a version-controlled manner.

Chronology of Alembic Setup:

  1. Alembic Initialization:

    uv run alembic init -t async migrations

    This command initializes Alembic within the project, creating a migrations directory with configuration files (alembic.ini, env.py, script.py.mako) tailored for asynchronous database operations. The -t async flag is crucial for configuring Alembic to work with asyncio.

  2. Schema Template Update (migrations/script.py.mako):
    To ensure Alembic correctly autogenerates migrations for SQLModel entities, the template for new migration scripts needs a minor adjustment to import sqlmodel.

    # ...
    import sqlmodel             # NEW
    # ...
  3. Environment Configuration (migrations/env.py):
    The env.py file, responsible for how Alembic interacts with the database and models, requires updates:

    • Import SQLModel and set target_metadata = SQLModel.metadata. This tells Alembic where to find the declarative base of the application’s database models.
    • Import specific SQLModel entities (e.g., Appointment) from the application’s modules. This allows Alembic’s autogenerate feature to detect changes in these models.
      
      # ...
      from sqlmodel import SQLModel   #new
      from src.appointments.models import Appointment   #new

    …

    target_metadata = SQLModel.metadata #updated

    …

  4. Database URL Configuration (alembic.ini):
    The alembic.ini file needs to be updated to point to the correct database URL, ensuring Alembic can connect and inspect the database.

    
    # ...
    sqlalchemy.url = sqlite+aiosqlite:///./db.sqlite3
]]>
https://technewst.com/architecting-scalable-fastapi-applications-a-modular-approach-to-mitigate-development-challenges/feed/ 0