StreamedRequestBody
Index
Async Resource Clients
Methods
Properties
Async Resource Clients
is_async
Whether the chunks can only be produced asynchronously, so only HttpClientAsync can send the body.
Methods
__init__
Initialize the streamed request body.
Parameters
source: StreamedBodySource
The object the chunks come from. See
is_streamablefor the accepted kinds.optionalkeyword-onlychunk_size: int = STREAMED_BODY_CHUNK_SIZE
Size of the chunks an
io.IOBasesource is read in - bytes from a binary stream, characters from a text-mode one. Any other source decides its own chunk sizes.
Returns None
aclose_chunks
Close the chunks the last
aiter_bytescall handed out, and with them a generator or async generator source.The asynchronous counterpart of
close_chunks.Returns None
aiter_bytes
Yield the body in chunks asynchronously, pulling a synchronous source in a worker thread.
A blocking
reador__next__would stall the event loop, so a synchronous file-like object or iterator is pulled throughasyncio.to_thread, one chunk at a time. Closing the returned generator, directly or throughaclose_chunks, also closes a generator or async generator source.Returns AsyncGenerator[bytes]
close_chunks
Close the chunks the last
iter_bytescall handed out, and with them a generator source.The retry loop calls this once an attempt ends. A transport may stop pulling the chunks early, for example on an error response sent before the whole body arrived, and hold on to them for as long as its client lives, which keeps the source suspended. When the transport is still pulling a chunk in a worker thread, a generator source is closed and a file-like source is read no further as soon as that chunk arrives.
Returns None
is_streamable
Return whether a request body can be streamed from a value.
These are, checked in this order, a streamed
HttpResponse(anything with a callableiter_bytes), anio.IOBasestream, any other file-like object (anything with a callableread), and an iterable or async iterable of byte chunks: an iterator such as a generator, or an object that only implements__iter__or__aiter__, which is what a class yielding chunks from a generator method looks like. Astr,bytes,bytearray, a container such as alist,tuple,set, ordict, and a pydantic model are not sources, even though all of them can be iterated. They are the values the client uploads whole or serializes as JSON.A
StreamedRequestBodymatches on its owniter_bytes, which is how a hand-built body reaches the request pipeline untouched. The constructor refuses one, so a caller that builds a body from what this accepts has to check for an existing body first.Parameters
value: object
Returns TypeGuard[StreamedBodySource]
iter_bytes
Yield the body in chunks, reading an
io.IOBasesource inchunk_sizepieces.Closing the returned generator, directly or through
close_chunks, also closes a generator source.Returns Generator[bytes]
rewind
Seek the source back to where it was when the body was created, so the body can be sent again.
Returns None
Properties
error
The exception the source raised while the chunks were pulled, if any.
A transport reports such a failure as its own error, which may wrap the cause beyond recognition. The retry loop raises this exception instead, since sending the body again cannot fix its source.
rewindable
Whether the body can be sent again after rewind, which only a seekable io.IOBase source allows.
A request body sent from its source in chunks, so the body is never held in memory whole.
HttpClient.callandHttpClientAsync.callwrap adataargument that is a file-like object, an iterable of byte chunks, or a streamedHttpResponsein this class. The transport pulls the chunks fromiter_bytesoraiter_bytesand sends each one as it arrives, and the body is never compressed. Build one yourself and pass it as thedatato choose thechunk_size, and it is sent as it is.An
io.IOBasestream, such as an open file or anio.BytesIO, is read inchunk_sizepieces. Any other object with a callablereadis read whole with a singleread()call and sent as one chunk, since nothing else about its shape is known.The shared retry loop can send a body again only when its source is a seekable
io.IOBasestream, in which caserewindseeks back to where the source was when the body was created. Any other source is consumed by the attempt that sends it, so the request gets a single attempt.A file opened in text mode, or an iterable yielding strings, is UTF-8 encoded chunk by chunk. A file-like object whose
readis a coroutine function, asaiofilesprovides, and an async iterable can only be sent by the asynchronous client.This is an experimental feature. The behavior and interface may change in future versions.