This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: fastapi-pydantic-v2-zerocopy-streaming

FastAPI with Pydantic v2 Zero-Copy Serialization Streaming Standard

Enforces direct streaming serialization using Pydantic v2's Rust-backed engine (pydantic-core) and chunked byte generators for large JSON payloads. Eliminates multi-gigabyte heap allocations and Python GIL stalls by bypassing Starlette's default intermediate dict materialization.

Prohibited Anti-Pattern: In-Memory Materialization of Bulk Model Collections via Default FastAPI response_model

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI with Pydantic v2 Zero-Copy Serialization Streaming Standard - Enforces direct streaming serialization using Pydantic v2's Rust-backed engine (pydantic-core) and chunked byte generators for large JSON payloads. Eliminates multi-gigabyte heap allocations and Python GIL stalls by bypassing Starlette's default intermediate dict materialization.
globs: **/*.py
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/fastapi-pydantic-v2-zerocopy-streaming"
---

# FastAPI with Pydantic v2 Zero-Copy Serialization Streaming Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: In-Memory Materialization of Bulk Model Collections via Default FastAPI response_model

## System Constraints
You are a Principal Software Architect enforcing zero-copy binary streaming patterns in FastAPI applications utilizing Pydantic v2. You must prevent in-memory serialization bottlenecks, excessive garbage collection, and GIL freezing caused by eager Python object materialization on high-throughput or bulk data endpoints.

## Strict Guidelines
- Engineers MUST ALWAYS return a `StreamingResponse` with an async generator yielding raw `bytes` when serializing unbounded or large (>1,000 items or >5MB) datasets.
- Engineers MUST NOT use `response_model=List[Schema]` or return raw Python collection literals on endpoints that export, report, or stream bulk entities.
- Code MUST use Pydantic v2's Rust-backed serialization (`TypeAdapter(Schema).dump_json(...)` or `model.model_dump_json().encode('utf-8')`) directly into chunk streams rather than invoking `.model_dump()` or `jsonable_encoder`.
- Services MUST configure database query iterators with batched server-side cursors (e.g., SQLAlchemy `yield_per` or async streaming cursors) to maintain O(1) constant memory usage.
- Components NEVER buffer the entire JSON array or NDJSON stream in Python runtime memory prior to emitting chunks to the client network socket.

## Bad vs Good Implementations
### Zero-Copy Chunked Streaming Serialization vs. In-Memory Array Materialization

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Fetches all database records into memory at once, instantiates intermediate Pydantic Python models, and allows FastAPI to use default jsonable_encoder. This duplicates memory (ORM -> Pydantic -> dict -> JSON str -> bytes), triggers severe GC pauses, and risks OOM worker crashes.
```python
from fastapi import FastAPI, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from app.models import LargeRecordORM, LargeRecordSchema
from app.db import get_session

app = FastAPI()

# ANTI-PATTERN: Eagerly loads thousands of ORM objects and forces FastAPI
# to run jsonable_encoder across Python dicts, causing multi-gigabyte memory spikes
# and locking the GIL for several seconds.
@app.get("/records", response_model=list[LargeRecordSchema])
async def get_all_records(db: AsyncSession = Depends(get_session)):
    result = await db.execute(select(LargeRecordORM))
    records = result.scalars().all()
    return [LargeRecordSchema.model_validate(r) for r in records]
```

#### Production Standard (Correct)
> **Standard**: Leverages SQLAlchemy cursor streaming (`yield_per`) and Pydantic v2's native Rust serialization via `TypeAdapter.dump_json` to yield byte chunks directly to the HTTP socket. Maintains constant O(1) memory overhead and avoids GIL saturation regardless of payload size.
```python
from collections.abc import AsyncIterator
from fastapi import FastAPI, Depends
from fastapi.responses import StreamingResponse
from pydantic import TypeAdapter
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from app.models import LargeRecordORM, LargeRecordSchema
from app.db import get_session

app = FastAPI()
record_adapter = TypeAdapter(LargeRecordSchema)

async def stream_json_array(session: AsyncSession) -> AsyncIterator[bytes]:
    # Stream in chunks from cursor without loading full table into RAM
    stream = await session.stream_scalars(select(LargeRecordORM).execution_options(yield_per=1000))
    yield b"["
    first = True
    async for record in stream:
        if not first:
            yield b","
        else:
            first = False
        # Zero-copy Rust serialization directly to UTF-8 encoded bytes
        yield record_adapter.dump_json(record, from_attributes=True)
    yield b"]"

@app.get("/records")
async def get_all_records(db: AsyncSession = Depends(get_session)) -> StreamingResponse:
    return StreamingResponse(
        stream_json_array(db),
        media_type="application/json",
        headers={"Content-Type": "application/json", "X-Content-Type-Options": "nosniff"}
    )
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: FastAPI with Pydantic v2 Zero-Copy Serialization Streaming Standard

FastAPI with Pydantic v2 Zero-Copy Serialization Streaming Standard defines an industrial-grade behavioral standard for backend engineering. Enforces direct streaming serialization using Pydantic v2's Rust-backed engine (pydantic-core) and chunked byte generators for large JSON payloads. Eliminates multi-gigabyte heap allocations and Python GIL stalls by bypassing Starlette's default intermediate dict materialization. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: In-Memory Materialization of Bulk Model Collections via Default FastAPI response_model Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftIn-Memory Materialization of Bulk Model Collections via Default FastAPI response_modelCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS return a `StreamingResponse` with an async generator yielding raw `bytes` when serializing unbounded or large (>1,000 items or >5MB) datasets.

Engineers MUST ALWAYS return a `StreamingResponse` with an async generator yielding raw `bytes` when serializing unbounded or large (>1,000 items or >5MB) datasets.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST NOT use `response_model=List[Schema]` or return raw Python collection literals on endpoints that export, report, or stream bulk entities.

Engineers MUST NOT use `response_model=List[Schema]` or return raw Python collection literals on endpoints that export, report, or stream bulk entities.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Code MUST use Pydantic v2's Rust-backed serialization (`TypeAdapter(Schema).dump_json(...)` or `model.model_dump_json().encode('utf-8')`) directly into chunk streams rather than invoking `.model_dump()` or `jsonable_encoder`.

Code MUST use Pydantic v2's Rust-backed serialization (`TypeAdapter(Schema).dump_json(...)` or `model.model_dump_json().encode('utf-8')`) directly into chunk streams rather than invoking `.model_dump()` or `jsonable_encoder`.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Services MUST configure database query iterators with batched server-side cursors (e.g., SQLAlchemy `yield_per` or async streaming cursors) to maintain O(1) constant memory usage.

Services MUST configure database query iterators with batched server-side cursors (e.g., SQLAlchemy `yield_per` or async streaming cursors) to maintain O(1) constant memory usage.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Components NEVER buffer the entire JSON array or NDJSON stream in Python runtime memory prior to emitting chunks to the client network socket.

Components NEVER buffer the entire JSON array or NDJSON stream in Python runtime memory prior to emitting chunks to the client network socket.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is FastAPI with Pydantic v2 Zero-Copy Serialization Streaming Standard critical for AI coding assistants?

Enforces direct streaming serialization using Pydantic v2's Rust-backed engine (pydantic-core) and chunked byte generators for large JSON payloads. Eliminates multi-gigabyte heap allocations and Python GIL stalls by bypassing Starlette's default intermediate dict materialization. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (In-Memory Materialization of Bulk Model Collections via Default FastAPI response_model) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

FastAPI