This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: fastapi-taskgroup-structured-concurrency-isolation

FastAPI & Pydantic v2 Structured Concurrency and TaskGroup Isolation

Enforces Python 3.12+ asyncio.TaskGroup boundaries and Pydantic v2 zero-copy Rust-backed validation for concurrent request workloads. Eliminates fire-and-forget coroutine leaks, uncaught background exceptions, and unhandled sub-task failures by mandating PEP 654 ExceptionGroup handling and strict lifecycle scoping.

Prohibited Anti-Pattern: Unbounded asyncio.create_task Invocations and Leaked Coroutine Lifecycles Bypassing Exception Boundaries

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: FastAPI & Pydantic v2 Structured Concurrency and TaskGroup Isolation - Enforces Python 3.12+ asyncio.TaskGroup boundaries and Pydantic v2 zero-copy Rust-backed validation for concurrent request workloads. Eliminates fire-and-forget coroutine leaks, uncaught background exceptions, and unhandled sub-task failures by mandating PEP 654 ExceptionGroup handling and strict lifecycle scoping.
globs: app/**/*.py, src/**/*.py, services/**/*.py, routers/**/*.py
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/fastapi-taskgroup-structured-concurrency-isolation"
---

# FastAPI & Pydantic v2 Structured Concurrency and TaskGroup Isolation

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded asyncio.create_task Invocations and Leaked Coroutine Lifecycles Bypassing Exception Boundaries

## System Constraints
You are a Principal Software Architect enforcing production-grade structured concurrency in FastAPI using Python 3.12+ and Pydantic v2. You must reject all unbounded `asyncio.create_task` calls and naked `asyncio.gather` invocations that lack structured cancellation or leak memory. You must require `asyncio.TaskGroup` for fan-out concurrency, PEP 654 `except*` exception group unwrapping, and Pydantic v2 `TypeAdapter`/`model_validate` for type enforcement across task boundaries.

## Strict Guidelines
- Engineers MUST ALWAYS enclose concurrent sub-tasks within an explicit `asyncio.TaskGroup` context manager to ensure parent-child lifecycle binding and automatic cancellation upon failure.
- Engineers MUST NOT invoke `asyncio.create_task` directly inside route handlers or dependency providers without an explicit task tracker or enclosing `TaskGroup`.
- Code MUST catch concurrent task failures using Python 3.12+ `except*` (ExceptionGroup) syntax to prevent unhandled sub-task exceptions from silently terminating worker processes.
- Services MUST process external data inside concurrent workers using Pydantic v2's compiled Rust core (`TypeAdapter.validate_python` or `Model.model_validate`) to minimize serialization overhead and catch data corruption at task boundaries.
- Components NEVER use `asyncio.gather(..., return_exceptions=True)` as a substitute for structured concurrency when task cancellation invariants are required on failure.

## Bad vs Good Implementations
### Concurrent API Aggregation with TaskGroup and Pydantic v2 Validation

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Using raw `asyncio.create_task` with `asyncio.gather` leaks orphaned coroutines when one task crashes. If `fetch_activity` fails, `fetch_billing` continues consuming resources without cancellation. Pydantic v1-style dictionary unpacking incurs unnecessary Python overhead instead of leveraging Pydantic v2's compiled Rust validator.
```python
import asyncio
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel

router = APIRouter()

class UserMetrics(BaseModel):
    user_id: str
    activity_score: float
    billing_status: str

async def fetch_billing(user_id: str) -> dict:
    await asyncio.sleep(0.01)
    return {"billing_status": "active"}

async def fetch_activity(user_id: str) -> dict:
    await asyncio.sleep(0.01)
    raise ConnectionResetError("Downstream timeout")

@router.get("/users/{user_id}/metrics", response_model=UserMetrics)
async def get_metrics(user_id: str):
    # ANTI-PATTERN: asyncio.create_task creates orphaned coroutines if one fails.
    # asyncio.gather without structured boundaries leaks memory and state.
    t1 = asyncio.create_task(fetch_billing(user_id))
    t2 = asyncio.create_task(fetch_activity(user_id))
    
    # If fetch_activity raises, t1 remains dangling or unmanaged;
    # exceptions are not isolated or mapped to structured HTTP responses.
    results = await asyncio.gather(t1, t2)
    merged = {**results[0], **results[1], "user_id": user_id}
    return UserMetrics(**merged)
```

#### Production Standard (Correct)
> **Standard**: Employs Python 3.12+ `asyncio.TaskGroup` for strict lifetime management. If either task raises an exception, the sibling task is deterministically cancelled immediately. Downstream errors are safely unwrapped with PEP 654 `except*` syntax and converted to clear HTTP 503 errors. The response payload is validated at the edge using Pydantic v2's Rust-backed `TypeAdapter.validate_python`.
```python
import asyncio
from fastapi import APIRouter, HTTPException, status
from pydantic import BaseModel, ConfigDict, TypeAdapter

router = APIRouter()

class UserMetrics(BaseModel):
    model_config = ConfigDict(frozen=True, extra="forbid")
    user_id: str
    activity_score: float
    billing_status: str

metrics_adapter = TypeAdapter(UserMetrics)

async def fetch_billing(user_id: str) -> dict[str, str]:
    await asyncio.sleep(0.01)
    return {"billing_status": "active"}

async def fetch_activity(user_id: str) -> dict[str, float]:
    await asyncio.sleep(0.01)
    return {"activity_score": 98.5}

@router.get("/users/{user_id}/metrics", response_model=UserMetrics)
async def get_metrics(user_id: str) -> UserMetrics:
    billing_res: dict[str, str] = {}
    activity_res: dict[str, float] = {}

    try:
        # Structured Concurrency: TaskGroup ensures sibling tasks cancel if one fails
        async with asyncio.TaskGroup() as tg:
            async def run_billing():
                nonlocal billing_res
                billing_res = await fetch_billing(user_id)

            async def run_activity():
                nonlocal activity_res
                activity_res = await fetch_activity(user_id)

            tg.create_task(run_billing())
            tg.create_task(run_activity())

    except* (ConnectionResetError, TimeoutError) as exc_group:
        # PEP 654 fine-grained group unwrapping
        raise HTTPException(
            status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
            detail=f"Upstream telemetry unavailable: {exc_group.exceptions}"
        ) from exc_group

    # Zero-copy Rust-backed validation via Pydantic v2 TypeAdapter
    payload = {"user_id": user_id, **billing_res, **activity_res}
    return metrics_adapter.validate_python(payload)
```

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 & Pydantic v2 Structured Concurrency and TaskGroup Isolation

FastAPI & Pydantic v2 Structured Concurrency and TaskGroup Isolation defines an industrial-grade behavioral standard for backend engineering. Enforces Python 3.12+ asyncio.TaskGroup boundaries and Pydantic v2 zero-copy Rust-backed validation for concurrent request workloads. Eliminates fire-and-forget coroutine leaks, uncaught background exceptions, and unhandled sub-task failures by mandating PEP 654 ExceptionGroup handling and strict lifecycle scoping. 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 asyncio.create_task Invocations and Leaked Coroutine Lifecycles Bypassing Exception Boundaries 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 asyncio.create_task Invocations and Leaked Coroutine Lifecycles Bypassing Exception BoundariesCritical
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 enclose concurrent sub-tasks within an explicit `asyncio.TaskGroup` context manager to ensure parent-child lifecycle binding and automatic cancellation upon failure.

Engineers MUST ALWAYS enclose concurrent sub-tasks within an explicit `asyncio.TaskGroup` context manager to ensure parent-child lifecycle binding and automatic cancellation upon failure.

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 invoke `asyncio.create_task` directly inside route handlers or dependency providers without an explicit task tracker or enclosing `TaskGroup`.

Engineers MUST NOT invoke `asyncio.create_task` directly inside route handlers or dependency providers without an explicit task tracker or enclosing `TaskGroup`.

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 catch concurrent task failures using Python 3.12+ `except*` (ExceptionGroup) syntax to prevent unhandled sub-task exceptions from silently terminating worker processes.

Code MUST catch concurrent task failures using Python 3.12+ `except*` (ExceptionGroup) syntax to prevent unhandled sub-task exceptions from silently terminating worker processes.

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 process external data inside concurrent workers using Pydantic v2's compiled Rust core (`TypeAdapter.validate_python` or `Model.model_validate`) to minimize serialization overhead and catch data corruption at task boundaries.

Services MUST process external data inside concurrent workers using Pydantic v2's compiled Rust core (`TypeAdapter.validate_python` or `Model.model_validate`) to minimize serialization overhead and catch data corruption at task boundaries.

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 use `asyncio.gather(..., return_exceptions=True)` as a substitute for structured concurrency when task cancellation invariants are required on failure.

Components NEVER use `asyncio.gather(..., return_exceptions=True)` as a substitute for structured concurrency when task cancellation invariants are required on failure.

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 & Pydantic v2 Structured Concurrency and TaskGroup Isolation critical for AI coding assistants?

Enforces Python 3.12+ asyncio.TaskGroup boundaries and Pydantic v2 zero-copy Rust-backed validation for concurrent request workloads. Eliminates fire-and-forget coroutine leaks, uncaught background exceptions, and unhandled sub-task failures by mandating PEP 654 ExceptionGroup handling and strict lifecycle scoping. 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 asyncio.create_task Invocations and Leaked Coroutine Lifecycles Bypassing Exception Boundaries) 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