> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/aistore/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.nvidia.com/aistore/_mcp/server.

# aistore.sdk.batch.multipart.multipart_stream_buffer

## Module Contents

### Classes

| Name                                                                                                  | Description                                                            |
| ----------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------- |
| [`MultipartStreamBuffer`](#aistore-sdk-batch-multipart-multipart_stream_buffer-MultipartStreamBuffer) | Manages reading content from an iterator and buffering it efficiently. |

### Data

[`BOUNDARY_MARGIN`](#aistore-sdk-batch-multipart-multipart_stream_buffer-BOUNDARY_MARGIN)

### API

```python
class aistore.sdk.batch.multipart.multipart_stream_buffer.MultipartStreamBuffer(
    content_iter: typing.Iterator[bytes],
    boundary: bytes,
    max_buffer_size: int
)
```

Manages reading content from an iterator and buffering it efficiently.

This class handles the low-level details of reading from content iterators
and managing the sliding window buffer. It provides an abstraction layer
that prevents higher-level classes from directly manipulating the buffer.

**Parameters:**

Iterator yielding chunks of content data

The multipart boundary marker to detect

Maximum size of the internal sliding window buffer

Get the boundary pattern.

```python
aistore.sdk.batch.multipart.multipart_stream_buffer.MultipartStreamBuffer._fill_buffer_safely(
    min_size: int = 0
) -> bool
```

Fill buffer ensuring we have at least min\_size bytes available,
with boundary safety margin.

This method ensures we always have enough data in the buffer to avoid
boundary splits when consuming data.

**Parameters:**

Minimum buffer size required

**Returns:** `bool`

True if sufficient data available, False if stream exhausted

```python
aistore.sdk.batch.multipart.multipart_stream_buffer.MultipartStreamBuffer._read_next_chunk() -> bool
```

Read one chunk from content iterator into buffer.

**Returns:** `bool`

True if chunk read successfully, False if iterator exhausted

```python
aistore.sdk.batch.multipart.multipart_stream_buffer.MultipartStreamBuffer.consume_before_pattern(
    pos: int,
    pattern: bytes,
    clean_endings: bool = True
) -> bytes
```

Consume content before a pattern at a known position and return it.

**Parameters:**

Known position of the pattern in the buffer

Pattern to search for in the buffer (for length calculation)

Whether to strip whitespace and line endings
from consumed content. Defaults to True.

**Returns:** `bytes`

Content found before the pattern, with optional cleaning applied.
Returns empty bytes if position is invalid.

```python
aistore.sdk.batch.multipart.multipart_stream_buffer.MultipartStreamBuffer.consume_data(
    length: int
) -> bytes
```

Consume and return data from buffer, ensuring boundary safety.

This method ensures that after consumption, we maintain enough
buffer data to avoid boundary splits.

**Parameters:**

Number of bytes to consume

**Returns:** `bytes`

Consumed data

```python
aistore.sdk.batch.multipart.multipart_stream_buffer.MultipartStreamBuffer.ensure_data_available(
    min_bytes: int = 0
) -> bool
```

Ensure at least min\_bytes are available in buffer.

**Parameters:**

Minimum buffer size required. Defaults to 0.

**Returns:** `bool`

True if sufficient buffer available, False if stream exhausted

```python
aistore.sdk.batch.multipart.multipart_stream_buffer.MultipartStreamBuffer.find_boundary_position() -> int
```

Find boundary position in current buffer.

**Returns:** `int`

Position of boundary within buffer, -1 if not found or no boundary set

**Raises:**

* `ValueError`: If no boundary has been set for this reader

```python
aistore.sdk.batch.multipart.multipart_stream_buffer.MultipartStreamBuffer.find_pattern(
    pattern: bytes,
    start: int = 0
) -> int
```

Find pattern in current buffer data.

**Parameters:**

Pattern to search for

Starting offset for search. Defaults to 0.

**Returns:** `int`

Position of pattern relative to buffer start, -1 if not found

```python
aistore.sdk.batch.multipart.multipart_stream_buffer.MultipartStreamBuffer.get_boundary_size() -> int
```

Get the size of the boundary pattern.

**Returns:** `int`

Size of boundary in bytes, 0 if no boundary set

```python
aistore.sdk.batch.multipart.multipart_stream_buffer.MultipartStreamBuffer.get_buffer_size() -> int
```

Get current buffer size for safe content calculations.

**Returns:** `int`

Current size of buffer

```python
aistore.sdk.batch.multipart.multipart_stream_buffer.MultipartStreamBuffer.get_data_slice(
    start: int = 0,
    end: typing.Optional[int] = None
) -> bytes
```

Get a copy of data slice from buffer.

**Parameters:**

Starting position. Defaults to 0.

Ending position, None for end of buffer.

**Returns:** `bytes`

Copy of requested data range

```python
aistore.sdk.batch.multipart.multipart_stream_buffer.MultipartStreamBuffer.get_safe_content_size() -> int
```

Get size of content that can be safely consumed without risking boundary split.

This ensures we don't consume data that might contain a partial boundary
that spans across buffer chunks.

**Returns:** `int`

Size of content consumable without a boundary split.
Returns full buffer size if no boundary is set.

```python
aistore.sdk.batch.multipart.multipart_stream_buffer.MultipartStreamBuffer.has_boundary() -> bool
```

Check if a boundary has been configured for this reader.

**Returns:** `bool`

True if boundary is set, False otherwise

```python
aistore.sdk.batch.multipart.multipart_stream_buffer.MultipartStreamBuffer.has_data() -> bool
```

Check if buffer has any data or can get more.

**Returns:** `bool`

True if data is available or can be obtained

```python
aistore.sdk.batch.multipart.multipart_stream_buffer.MultipartStreamBuffer.is_exhausted() -> bool
```

Check if both stream and buffer are exhausted.

**Returns:** `bool`

True if no more data can be obtained

```python
aistore.sdk.batch.multipart.multipart_stream_buffer.MultipartStreamBuffer.read_until_boundary(
    clean_endings: bool = True
) -> typing.Iterator[bytes]
```

Read and consume all data until the next boundary is found.

**Parameters:**

Whether to strip whitespace and line endings
from consumed content. Defaults to True.

**Raises:**

* `ValueError`: If no boundary has been set for this reader

```python
aistore.sdk.batch.multipart.multipart_stream_buffer.MultipartStreamBuffer.skip_whitespace() -> int
```

Skip leading whitespace in buffer.

**Returns:** `int`

Number of whitespace bytes that were skipped

```python
aistore.sdk.batch.multipart.multipart_stream_buffer.BOUNDARY_MARGIN = 8
```