Skip to navigation

aistore.sdk.batch.multipart.stateful_streaming_parser

View as MarkdownOpen in Claude

Module Contents

Classes

NameDescription
StatefulStreamingParserA stateful streaming parser for efficiently processing multipart HTTP responses.

Data

logger

API

class aistore.sdk.batch.multipart.stateful_streaming_parser.StatefulStreamingParser(
content_iter: typing.Iterator[bytes],
boundary: bytes,
max_buffer_size: int
)

A stateful streaming parser for efficiently processing multipart HTTP responses.

The parser maintains state across multiple parts of a multipart response, automatically detecting boundaries, extracting headers, and providing streaming access to body content.

Parameters:

content_iter
Iterator[bytes]

Iterator yielding chunks of the HTTP response

boundary
bytes

The multipart boundary marker (including leading dashes)

max_buffer_size
int

Maximum size of the internal sliding window buffer

This class is not thread-safe. Each instance should be used by a single thread or proper synchronization must be implemented by the caller.

_stream_buffer
aistore.sdk.batch.multipart.stateful_streaming_parser.StatefulStreamingParser._count_header_bytes_in_chunk(
chunk: bytes
) -> int

Return the number of bytes in chunk that are header content, i.e. the bytes before the first terminator (WIN_LINE_END or UNIX_LINE_END).

Also detects terminators that span the buffer-chunk boundary (e.g. the buffer ends with \r\n\r and chunk starts with \n) to avoid over-counting and triggering a spurious MultipartDecodeError.

Parameters:

chunk
bytes

The raw chunk peeked from the stream before appending

Returns: int

Number of bytes in chunk that are header content (0 if the chunk starts with or completes a terminator)

aistore.sdk.batch.multipart.stateful_streaming_parser.StatefulStreamingParser._extract_headers() -> typing.Optional[bytes]

Extract headers using the optimized content reader abstraction.

Returns: Optional[bytes]

Optional[bytes]: Headers as bytes if found, None if stream exhausted

Raises:

  • MultipartDecodeError: If accumulated header bytes exceed max_buffer_size, or the terminator is found at a position exceeding max_buffer_size.
aistore.sdk.batch.multipart.stateful_streaming_parser.StatefulStreamingParser._get_initial_header_bytes() -> int

Return the number of header bytes already in the buffer, excluding any trailing terminator-prefix bytes (e.g. a trailing \r that is the first byte of \r\n\r\n must not inflate the header size counter).

Returns: int

Number of countable header bytes currently in the buffer

aistore.sdk.batch.multipart.stateful_streaming_parser.StatefulStreamingParser._locate_first_boundary() -> bool

Find the first boundary marker using the optimized buffer reader.

Returns: bool

True if boundary found, False if exhausted without finding boundary

aistore.sdk.batch.multipart.stateful_streaming_parser.StatefulStreamingParser.get_next_part() -> typing.Optional[typing.Tuple[bytes, aistore.sdk.batch.multipart.body_stream_reader.BodyStreamReader]]

Get the next part without loading it completely into memory.

Returns: Optional[Tuple[bytes, BodyStreamReader ]]

Optional[Tuple[bytes, BodyStreamReader]]: If part exists, returns tuple of headers and body stream

aistore.sdk.batch.multipart.stateful_streaming_parser.logger = get_logger(__name__)