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

FastAPI Streaming: Rust-Pydantic Zero-Copy & TCP Backpressure Drain Invariant

Enforces zero-copy byte serialization via Pydantic V2 Rust core and synchronous socket drain propagation on FastAPI SSE/StreamingResponse generators. Prevents resident set size (RSS) bloat and out-of-memory cascades under asymmetric network conditions by halting async producer loops when the client TCP buffer saturates or closes.

Prohibited Anti-Pattern: Unbounded Async Queue Decoupling with Python Object Materialization in SSE Streams

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI Streaming: Rust-Pydantic Zero-Copy & TCP Backpressure Drain Invariant - Enforces zero-copy byte serialization via Pydantic V2 Rust core and synchronous socket drain propagation on FastAPI SSE/StreamingResponse generators. Prevents resident set size (RSS) bloat and out-of-memory cascades under asymmetric network conditions by halting async producer loops when the client TCP buffer saturates or closes.
globs: app/**/*.py, src/**/*.py, routers/**/*.py, api/**/*.py
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/fastapi-streaming-pydantic-backpressure-drain"
---

# FastAPI Streaming: Rust-Pydantic Zero-Copy & TCP Backpressure Drain Invariant

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded Async Queue Decoupling with Python Object Materialization in SSE Streams

## System Constraints
You are a Principal Backend Architect enforcing memory safety and TCP backpressure propagation in high-throughput FastAPI and Starlette streaming services. Mandate zero-copy Rust-side Pydantic serialization and direct ASGI backpressure synchronization on all StreamingResponse and EventSourceResponse pipelines.

## Strict Guidelines
- Engineers MUST ALWAYS emit pre-encoded bytes directly using `model.model_dump_json().encode('utf-8')` to leverage Pydantic V2's Rust-backed serialization and bypass Python dict allocation.
- Engineers MUST NOT decouple async streaming producers using unbounded `asyncio.Queue(maxsize=0)` or fire-and-forget background tasks that ignore client drain latency.
- Code SHOULD poll `await request.is_disconnected()` or rely on raw ASGI `send()` backpressure propagation to terminate chunk generation immediately upon client disconnection.
- Services MUST configure `StreamingResponse(content, media_type='text/event-stream')` directly with backpressured async generators, ensuring the ASGI server's network buffer governs producer pace.
- Components NEVER buffer batches of unformatted domain models in heap memory prior to yielding SSE frames.

## Bad vs Good Implementations
### Streaming Chunk Generation with Rust-Pydantic Serialization and Disconnect Guard

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Allocates intermediate Python dictionaries for each chunk using `model_dump()`, causing heavy GC churn and Python memory bloat. Emits strings instead of UTF-8 bytes, and lacks connection liveness checks, leading to memory accumulation if slow clients fall behind.
```python
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
import asyncio
import json

app = FastAPI()

class Telemetry(BaseModel):
    id: str
    metric: float

@app.get('/stream/telemetry')
async def stream_telemetry():
    async def bad_producer():
        # ANTI-PATTERN: Inflates heap by materializing intermediate Python dicts via .model_dump()
        # Uses json.dumps in Python instead of Rust-backed serialization.
        # Runs continuously without checking client disconnect or respecting socket flow control.
        while True:
            item = Telemetry(id='sensor-1', metric=42.0)
            payload = json.dumps(item.model_dump()) # Allocates intermediate dict + string
            yield f"data: {payload}\n\n" # Yields strings, forcing Starlette to encode repeatedly
            await asyncio.sleep(0.01)

    return StreamingResponse(bad_producer(), media_type='text/event-stream')
```

#### Production Standard (Correct)
> **Standard**: Uses Pydantic V2's `model_dump_json()` which serializes in Rust, avoiding intermediate dict allocations. Yields raw `bytes` to avoid Python string re-encoding, includes reverse-proxy unbuffering headers (`X-Accel-Buffering: no`), and verifies `request.is_disconnected()` to guarantee memory reclamation.
```python
from fastapi import FastAPI, Request
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
from collections.abc import AsyncIterator
import asyncio

app = FastAPI()

class Telemetry(BaseModel):
    id: str
    metric: float

@app.get('/stream/telemetry')
async def stream_telemetry(request: Request) -> StreamingResponse:
    async def backpressured_producer() -> AsyncIterator[bytes]:
        # Direct zero-copy: model_dump_json() executes natively in Rust (pydantic-core)
        # Returning pre-encoded bytes directly bypasses Starlette's implicit re-encoding.
        while True:
            if await request.is_disconnected():
                break

            item = Telemetry(id='sensor-1', metric=42.0)
            # Rust serialization directly to JSON bytes with SSE framing
            yield b'data: ' + item.model_dump_json().encode('utf-8') + b'\n\n'
            await asyncio.sleep(0.01)

    return StreamingResponse(
        backpressured_producer(),
        media_type='text/event-stream',
        headers={
            'Cache-Control': 'no-cache',
            'X-Accel-Buffering': 'no'
        }
    )
```

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 Streaming: Rust-Pydantic Zero-Copy & TCP Backpressure Drain Invariant

FastAPI Streaming: Rust-Pydantic Zero-Copy & TCP Backpressure Drain Invariant defines an industrial-grade behavioral standard for backend engineering. Enforces zero-copy byte serialization via Pydantic V2 Rust core and synchronous socket drain propagation on FastAPI SSE/StreamingResponse generators. Prevents resident set size (RSS) bloat and out-of-memory cascades under asymmetric network conditions by halting async producer loops when the client TCP buffer saturates or closes. 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: Unbounded Async Queue Decoupling with Python Object Materialization in SSE Streams 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 DriftUnbounded Async Queue Decoupling with Python Object Materialization in SSE StreamsCritical
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 emit pre-encoded bytes directly using `model.model_dump_json().encode('utf-8')` to leverage Pydantic V2's Rust-backed serialization and bypass Python dict allocation.

Engineers MUST ALWAYS emit pre-encoded bytes directly using `model.model_dump_json().encode('utf-8')` to leverage Pydantic V2's Rust-backed serialization and bypass Python dict allocation.

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 decouple async streaming producers using unbounded `asyncio.Queue(maxsize=0)` or fire-and-forget background tasks that ignore client drain latency.

Engineers MUST NOT decouple async streaming producers using unbounded `asyncio.Queue(maxsize=0)` or fire-and-forget background tasks that ignore client drain latency.

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 SHOULD poll `await request.is_disconnected()` or rely on raw ASGI `send()` backpressure propagation to terminate chunk generation immediately upon client disconnection.

Code SHOULD poll `await request.is_disconnected()` or rely on raw ASGI `send()` backpressure propagation to terminate chunk generation immediately upon client disconnection.

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 `StreamingResponse(content, media_type='text/event-stream')` directly with backpressured async generators, ensuring the ASGI server's network buffer governs producer pace.

Services MUST configure `StreamingResponse(content, media_type='text/event-stream')` directly with backpressured async generators, ensuring the ASGI server's network buffer governs producer pace.

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 batches of unformatted domain models in heap memory prior to yielding SSE frames.

Components NEVER buffer batches of unformatted domain models in heap memory prior to yielding SSE frames.

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 Streaming: Rust-Pydantic Zero-Copy & TCP Backpressure Drain Invariant critical for AI coding assistants?

Enforces zero-copy byte serialization via Pydantic V2 Rust core and synchronous socket drain propagation on FastAPI SSE/StreamingResponse generators. Prevents resident set size (RSS) bloat and out-of-memory cascades under asymmetric network conditions by halting async producer loops when the client TCP buffer saturates or closes. 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 (Unbounded Async Queue Decoupling with Python Object Materialization in SSE Streams) 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