Skip to main content

9 posts tagged with "fastapi"

fastapi tag description

View All Tags

Python Annotations Rare Use Cases

· 7 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

Python annotations, introduced in PEP 3107 for function parameters and return values, were initially generic metadata slots. While their primary use has become type hinting (PEP 484), expert developers leverage them for advanced and niche applications that go far beyond simple type declarations.

These use cases often involve frameworks or metaprogramming to make annotations act as declarative configuration or runtime execution instructions.

FastAPI Core Middleware Examples and Use Cases

· 7 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

Middleware in FastAPI (which is built on Starlette) is a powerful mechanism for executing code before and after a request is processed by the route handler. Middleware allows you to apply logic globally or across a group of routes, such as logging, authentication, CORS, and response manipulation.

The standard way to implement custom middleware is by defining an async function that takes the request and the call_next callable.

Fastapi Depends with parameters and arguments

· 8 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

FastAPI's Dependency Injection (DI) system is remarkably flexible: dependency functions aren't just executed independently; they can be designed to accept and process arguments from the current HTTP request or the results of other dependencies.

Using parameters within a Depends function allows you to implement complex logic, dynamic configuration, and validation rules while keeping your main route handler clean.

FastAPI Internal Requests, Depends, and Accessing the Request Object

· 7 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

Reliable way to make an internal HTTP request from one FastAPI route handler to another within the same application, specifically to handle a POST request with a request body.

The best and most idiomatic way to handle this in FastAPI (which is built on Starlette) is by using the TestClient from the starlette.testclient module. This allows you to treat your application as an independent service and make internal requests without incurring any actual network overhead, which is crucial for testing and internal service calls.

When working with FastAPI, the correct method to call one route from another is not by importing the handler function directly, but by using the TestClient class. This simulates a genuine HTTP request, ensuring all middleware, dependencies, and validation logic run exactly as they would for an external client.

FastAPI Dependency Injection: The Complete Guide from Basics to Advanced Patterns

· 9 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

Dependency Injection (DI) is one of the core architectural design choices that makes FastAPI so powerful. It handles cross-cutting concerns-such as database connections, security, authentication, and request parsing-with minimal boilerplate.

Whether you are a beginner looking to understand the Depends syntax or an expert building complex authorization layers and mockable tests, this guide covers everything you need to master FastAPI's dependency injection system.

Benchmark: msgspec vs. Pydantic v2

· 5 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

msgspec is an extremely fast serialization and validation library that consistently outperforms Pydantic v2 in benchmarks. This performance advantage comes from its design as a lean, compiled-code-based library focused on a narrow set of data handling tasks, whereas Pydantic v2 is a feature-rich framework.

The performance differences are most pronounced in two key areas: parsing/decoding (converting data like JSON into Python objects) and serialization/encoding (converting Python objects into data like JSON).

Msgspec FastAPI Integration: High Performance, Validation, and OpenAPI Docs

· 5 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

msgspec is a Python library designed for high-performance serialization and validation, which makes it a powerful partner for FastAPI. It can be used as a drop-in replacement for FastAPI's default Pydantic models to handle data validation and schema generation, often leading to a significant speedup in API performance.

How to Measure Execution Time in Python: Vanilla Python, FastAPI, Flask, and Django

· 8 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

Measuring how long your Python code takes to run is a critical skill for performance optimization, benchmarking, and identifying bottlenecks in production applications. Depending on whether you are analyzing a single micro-operation, profiling a complex function, or tracking HTTP request-response lifecycles, Python offers a wide variety of tools.

This guide provides a comprehensive breakdown of how to measure execution time in vanilla Python, followed by specialized implementations for the three major web frameworks: FastAPI, Flask, and Django.

How to Connect and Integrate Supabase with Python: FastAPI, Flask, and Django

· 8 min read
Serhii Hrekov
Senior Software Engineer & System Architect specializing in Python, Web Systems, Cloud Infrastructure & Automation

Supabase is a popular PostgreSQL-based BaaS (Backend-as-a-Service). Because it runs on a standard PostgreSQL database, you can connect to it using traditional database drivers (like psycopg2 or SQLAlchemy) or leverage its built-in REST API using client SDKs.

This guide provides a comprehensive walkthrough for integrating Supabase into your Python web applications, detailing implementations for FastAPI, Flask, and Django.