sgl-project/sglang · error · ValueError

material URI has invalid base64 padding

Error message

material URI has invalid base64 padding

What it means

While scanning the inline base64 payload, more than two '=' padding characters were encountered. The strict streamer allows at most 2 trailing '=' per chunk.

Source

Thrown at python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/minimax_h3/material_io.py:617

        batch,
        condition_type=condition_type,
        condition_index=condition_index,
        media_type=media_type,
    )
    total = 0
    encoded_size = 0
    padding = 0
    saw_padding = False
    encoded_chunk = bytearray()
    try:
        with partial_path.open("wb") as output:
            for value in _iter_base64_payload_bytes(uri, payload_start):
                encoded_size += 1
                if value == ord("="):
                    saw_padding = True
                    padding += 1
                    if padding > 2:
                        raise ValueError("material URI has invalid base64 padding")
                elif saw_padding:
                    raise ValueError("material URI has data after base64 padding")
                encoded_chunk.append(value)
                if len(encoded_chunk) == MINIMAX_H3_BASE64_DECODE_CHUNK_CHARS:
                    decoded_chunk = _decode_base64_chunk(encoded_chunk)
                    total += len(decoded_chunk)
                    output.write(decoded_chunk)
                    encoded_chunk.clear()
            if encoded_size == 0:
                raise ValueError("material URI base64 payload is empty")
            if encoded_size % 4 == 1 or (padding and encoded_size % 4):
                raise ValueError("material URI has an invalid base64 payload length")
            if encoded_chunk:
                encoded_chunk.extend(b"=" * (-len(encoded_chunk) % 4))
                decoded_chunk = _decode_base64_chunk(encoded_chunk)
                total += len(decoded_chunk)
                output.write(decoded_chunk)
        decoded_size = (encoded_size * 3) // 4 - padding

View on GitHub (pinned to 0132848349)

Solutions

  1. Encode the whole payload once, unpadded, URL-safe: base64.urlsafe_b64encode(blob).rstrip(b'=')
  2. If chunking is required, strip '=' from each chunk and pad only the final one
  3. Regenerate the URI rather than hand-assembling it

Example fix

# before
payload = "".join(base64.urlsafe_b64encode(c).decode() for c in chunks)  # each has '='
# after
payload = base64.urlsafe_b64encode(b"".join(chunks)).decode().rstrip('=')
Defensive patterns

Strategy: validation

Validate before calling

payload = uri_payload
assert payload.count("=") <= 2 and payload.endswith("=" * payload.count("=")), "bad padding"

Prevention

When it happens

Trigger: A material URI payload with '===' or padding characters repeated (e.g. payload built by naively concatenating independently-padded base64 segments).

Common situations: Joining multiple base64-encoded chunks each carrying their own padding; double-encoding; copy-paste duplicating '=' characters.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/c6939dc5f77d9f50. Report an issue: GitHub.