Skip to main content

fastapi-struct

Production-ready FastAPI architecture generator tailor-made for AI, GenAI, LLM, and Machine Learning services.

Package version Supported Python versions FastAPI Pydantic v2 License: MIT


FastAPI Documentation: https://fastapi.tiangolo.com
PyPI Package: https://pypi.org/project/fastapi-struct/


⚡ Why fastapi-struct for AI & ML Engineers?

Traditional web generator templates force relational database CRUD patterns (repositories/, models/, users/, email.py), cluttering modern AI microservices.

fastapi-struct defaults to a lean, enterprise-grade AI & Machine Learning architecture:

  • 🤖 GenAI Token Streaming: Real-time token-by-token streaming via StreamingResponse (Server-Sent Events) for LLM chat completions.
  • 📚 RAG & Vector Search: Modular vector retrieval layer ready for Pinecone, Qdrant, ChromaDB, or pgvector.
  • 🧠 Lifespan Model Caching: Heavy model weights (PyTorch, ONNX, HuggingFace) load into GPU/CPU memory once at startup via FastAPI's @asynccontextmanager lifespan.
  • 🩺 GPU & Readiness Probes: Built-in /health endpoint reporting GPU/device readiness (cpu or cuda).
  • 🧹 Zero __pycache__ Clutter: Automatically excludes byte-compiled files and stray caches during project scaffolding.
  • 🧪 Preconfigured Pytest Suite: 4 automated integration tests for health, prediction, chat, and RAG.

📦 Quick Start & CLI Usage

Generate in the Current Directory (.)

fastapi-struct .

Or Create a Named Project Folder

fastapi-struct my_ai_service

(You can also run it instantly with uvx without installing: uvx fastapi-struct .)


📂 The AI & ML Architecture

my_ai_service/
│
├── app/
│   ├── __init__.py
│   ├── main.py                 # FastAPI initialization + Lifespan AI/ML model loader
│   │
│   ├── api/
│   │   ├── __init__.py
│   │   └── v1/
│   │       ├── __init__.py
│   │       ├── api.py          # Central router aggregating AI, RAG, and ML routes
│   │       └── routes/
│   │           ├── __init__.py
│   │           ├── health.py   # /health probe (GPU / device readiness check)
│   │           ├── chat.py     # /chat & /stream (GenAI real-time token streaming)
│   │           ├── rag.py      # /rag/query (Retrieval-Augmented Generation)
│   │           └── predict.py  # /predict (Classical ML / Deep Learning inference)
│   │
│   ├── core/
│   │   ├── __init__.py
│   │   └── config.py           # LLM_MODEL, EMBEDDING_MODEL, DEVICE, API_KEYS
│   │
│   ├── ai/                     # GenAI, LLMs & Retrieval Engine
│   │   ├── __init__.py
│   │   ├── llm.py              # LLM client & async token streaming generator
│   │   ├── prompt.py           # System prompts & prompt engineering templates
│   │   └── rag.py              # Vector retriever & RAG context builder
│   │
│   ├── ml/                     # Machine Learning & Deep Learning Engine
│   │   ├── __init__.py
│   │   ├── model.py            # Model wrapper (PyTorch / ONNX / Transformers)
│   │   └── pipeline.py         # Tensor preprocessing & postprocessing
│   │
│   ├── schemas/                # Pydantic request & response models
│   │   ├── __init__.py
│   │   ├── chat.py             # ChatRequest, ChatResponse, ChatMessage
│   │   ├── rag.py              # RAGRequest, RAGResponse, DocumentChunk
│   │   └── predict.py          # PredictionRequest, PredictionResponse, HealthResponse
│   │
│   └── services/               # Business logic & latency tracking
│       ├── __init__.py
│       ├── ai_service.py       # GenAI orchestration (Chat, Stream, RAG)
│       └── inference.py        # ML inference orchestration & latency benchmarking
│
├── tests/                      # Automated test suite
│   ├── __init__.py
│   ├── conftest.py             # TestClient fixtures + mock AI/ML providers
│   ├── test_chat.py            # Pytest test cases for chat & RAG
│   └── test_predict.py         # Pytest test cases for /health & /predict
│
├── .gitignore                  # Ignores __pycache__, .env, and large model weights (*.pt, *.bin)
├── .env                        # Local model configuration
├── .env.example
├── requirements.txt            # Ready for pip install -r requirements.txt
├── pyproject.toml              # Build & pytest configuration
└── README.md

🚀 Running Your Service

1. Create and activate a virtual environment

Windows PowerShell:

python -m venv .venv
.\.venv\Scripts\Activate.ps1

Windows CMD:

python -m venv .venv
call .venv\Scripts\activate.bat

macOS / Linux:

python3 -m venv .venv
source .venv/bin/activate

2. Install dependencies

pip install -r requirements.txt

3. Start the development server

python -m uvicorn app.main:app --reload

🔍 Interactive Endpoints

Visit http://127.0.0.1:8000/docs for interactive Swagger UI:

Method Endpoint Description
GET / API status and capabilities overview
GET /api/v1/health Health & device readiness probe (cpu or cuda)
POST /api/v1/chat/ Standard GenAI chat completion
POST /api/v1/chat/stream Real-time token-by-token streaming (SSE)
POST /api/v1/rag/query Retrieval-Augmented Generation (Vector Search + LLM)
POST /api/v1/predict/ ML model inference with execution latency tracking

🧪 Running Automated Tests

Run the included Pytest suite:

python -m pytest

Output:

tests\test_chat.py ..                                                    [ 50%]
tests\test_predict.py ..                                                 [100%]
============================== 4 passed in 0.15s ==============================

🛠 Customizing Your AI & ML Models

Connecting Real LLMs (OpenAI, Anthropic, Gemini, Ollama)

In app/ai/llm.py, plug your provider call directly into generate_response() and stream_tokens():

from openai import AsyncOpenAI
client = AsyncOpenAI(api_key=settings.OPENAI_API_KEY)

async def stream_tokens(self, messages):
    response = await client.chat.completions.create(
        model=self.model_name,
        messages=[m.model_dump() for m in messages],
        stream=True,
    )
    async for chunk in response:
        content = chunk.choices[0].delta.content or ""
        yield f"data: {content}\n\n"

Loading PyTorch / ONNX Weights

In app/ml/model.py, initialize your local weights inside load():

import torch

def load(self):
    self.model = torch.load(settings.ML_MODEL_PATH, map_location=settings.DEVICE)
    self.model.eval()
    self.is_loaded = True

📜 License

This project is open-source software licensed under the MIT License.

Metadata

Release files for fastapi-struct 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-struct 0.3.0
File Size Uploaded Provenance Actions
fastapi_struct-0.3.0.tar.gz 26.1 kB Without provenance Details for fastapi_struct-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-struct 0.3.0
File Interpreter ABI Platform Actions
fastapi_struct-0.3.0-py3-none-any.whl Python 3 none any Details for fastapi_struct-0.3.0-py3-none-any.whl

Total release size: 65.1 kB

Release files / fastapi_struct-0.3.0.tar.gz

Download URL fastapi_struct-0.3.0.tar.gz
Size 26.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c8a5058806947d4657fa27e9f5a7bf74b607a4928907e9dcaf76aeeeff504416
BLAKE2b-256 checksum
How to use checksums
465bbddb24d13818ea38a8ccdfa5949981bcfb596757cc4d593eddb89b9fa638
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Release files / fastapi_struct-0.3.0-py3-none-any.whl

Download URL fastapi_struct-0.3.0-py3-none-any.whl
Size 39.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
479b04c50cb0864cf0212bcb612b3ac8ea136d861603138c340583198f969761
BLAKE2b-256 checksum
How to use checksums
993967b414bde38c235f67be68b0232ec103cdbd1f844530c0ca0f55b62fd060
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Security for fastapi-struct 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page