Skip to content

Annotated[AsyncIterator[T], ...] return annotations fail route registration #16334

Closed

Description

@Marco260104

Annotated[AsyncIterator[T], ...] return annotations fail route registration

Summary

On FastAPI revision 50113da16fec53b66b80d75e80a89296de4fa5a5, wrapping an
async-stream return annotation in typing.Annotated causes route registration
to raise FastAPIError, while the equivalent unwrapped annotation registers
successfully.

This is distinct from the APIRouter.include_router propagation issue in
#15401: the failure happens while the route is first constructed, even when it
is declared directly on FastAPI.

Reproduction

from collections.abc import AsyncIterator
from typing import Annotated

from fastapi import FastAPI


def plain_app() -> FastAPI:
    app = FastAPI()

    @app.get("/plain")
    async def plain() -> AsyncIterator[int]:
        yield 1

    return app


def annotated_app() -> FastAPI:
    app = FastAPI()

    @app.get("/annotated")
    async def annotated() -> Annotated[AsyncIterator[int], "stream-item"]:
        yield 1

    return app


plain_app()       # succeeds
annotated_app()   # raises FastAPIError: Invalid args for response field

The reproduction was run against the current checkout with the same result
for typing.AsyncIterator and collections.abc.AsyncIterator.

Actual behavior

get_stream_item_type() checks only whether the outer get_origin(annotation)
is one of the supported stream origins. For an Annotated return annotation,
the outer origin is Annotated, so the helper returns None. The complete
Annotated[AsyncIterator[int], ...] object is then treated as a response model
and route construction fails before the application can start.

Relevant code:

  • fastapi/dependencies/utils.py:261-268, get_stream_item_type()
  • fastapi/routing.py:1086-1087, where the helper is called

Expected behavior

Annotated[AsyncIterator[int], metadata] should be accepted wherever the
equivalent AsyncIterator[int] annotation is accepted. The metadata should be
preserved or ignored according to FastAPI's existing Annotated conventions,
and the stream item type should resolve to int.

Impact

Applications using metadata-bearing return annotations cannot start. This is a
compatibility failure for valid PEP 593 typing syntax and affects deployment
before the first request. It also prevents the framework from generating the
intended stream item schema.

Suggested fix

Unwrap Annotated before stream-origin detection (or recurse through it inside
get_stream_item_type()), matching the existing annotation-unwrapping logic
used for parameters. Add regression coverage for Annotated[AsyncIterator[T], ...] and Annotated[AsyncGenerator[T, None], ...] return annotations.

Environment

  • FastAPI revision: 50113da16fec53b66b80d75e80a89296de4fa5a5
  • Python: 3.13
  • Pydantic: 2.x

Duplicate check

Activity

  1. locked and limited conversation to collaborators on Sep 8, 2026
  2. converted this issue into a discussion #16337 on Sep 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions