aio-libs/aiohttp · error · TypeError

value argument must support collections.abc.AsyncIterable…

Error message

value argument must support collections.abc.AsyncIterable interface, got {type(value)!r}

What it means

Raised by AsyncIterablePayload.__init__ when value does not implement collections.abc.AsyncIterable. This payload class streams a body from an async source, so a plain iterable, a sync generator, a list, or a file object is rejected. The check uses isinstance against AsyncIterable, so even an object with __iter__ but no __aiter__ fails.

Solutions

  1. Use an async generator: async def gen(): yield chunk; AsyncIterablePayload(gen()).
  2. For a sync iterable/list, use the sync IterablePayload or aiohttp's stream-based helpers instead.
  3. Open files with aiofiles and iterate the async file object so it is AsyncIterable.

Example fix

# before
def gen():
    yield b'chunk'
payload = AsyncIterablePayload(gen())

# after
async def gen():
    yield b'chunk'
payload = AsyncIterablePayload(gen())
Defensive patterns

Strategy: type-guard

Validate before calling

from collections.abc import AsyncIterable
if not isinstance(value, AsyncIterable):
    raise TypeError('value must be async-iterable')
payload = AsyncIterablePayload(value)

Type guard

from collections.abc import AsyncIterable
def is_async_iterable(v) -> bool:
    return isinstance(v, AsyncIterable)

Try / catch

try:
    payload = AsyncIterablePayload(value)
except TypeError:
    # fall back to wrapping a sync iterable as an async generator
    async def _wrap():
        for chunk in value:
            yield chunk
    payload = AsyncIterablePayload(_wrap())

Prevention

When it happens

Trigger: Constructing AsyncIterablePayload([b'a', b'b']) with a list; passing a sync generator (def gen(): yield b'x') instead of an async generator (async def gen(): yield b'x'); passing a file object opened with open() instead of aiofiles.

Common situations: Streaming an uploaded file using blocking open(); using a generator function without async; confusing IterablePayload (sync) with AsyncIterablePayload (async).

Related errors


AI-assisted analysis of aio-libs/aiohttp@d041d4d0fd (2026-08-11). Data as JSON: /api/errors/d67425664e5566f3. Report an issue: GitHub.

Appendix: source

Thrown at aiohttp/payload.py:985

    ) -> None:
        super().__init__(
            dumps(value),
            content_type=content_type,
            *args,
            **kwargs,
        )


class AsyncIterablePayload(Payload):
    _iter: AsyncIterator[bytes] | None = None
    _value: AsyncIterable[bytes]
    _cached_chunks: list[bytes] | None = None
    # _consumed stays False to allow reuse with cached content
    _autoclose = True  # Iterator doesn't need explicit closing

    def __init__(self, value: AsyncIterable[bytes], *args: Any, **kwargs: Any) -> None:
        if not isinstance(value, AsyncIterable):
            raise TypeError(
                "value argument must support "
                "collections.abc.AsyncIterable interface, "
                f"got {type(value)!r}"
            )

        if "content_type" not in kwargs:
            kwargs["content_type"] = "application/octet-stream"

        super().__init__(value, *args, **kwargs)

        self._iter = value.__aiter__()

    async def write(self, writer: AbstractStreamWriter) -> None:
        """
        Write the entire async iterable payload to the writer stream.

        Args:
            writer: An AbstractStreamWriter instance that handles the actual writing

View on GitHub (pinned to d041d4d0fd)