sgl-project/sglang · error · TypeError

HTTP material response.read() must return bytes, got {type(c

Error message

HTTP material response.read() must return bytes, got {type(chunk).__name__}

What it means

While downloading an HTTP(S) material, response.read(n) returned a non-bytes object (e.g. str). The downloader requires a bytes-returning file-like response body.

Source

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

        suffix = (
            _safe_suffix(uri)
            or _SUFFIX_BY_MEDIA_TYPE.get(str(media_type or "").lower())
            or _DEFAULT_SUFFIX_BY_TYPE.get(condition_type, ".bin")
        )
        output_path = (
            Path(_material_workdir(batch))
            / f"condition_{int(condition_index):04d}{suffix}"
        )
        partial_path = output_path.with_name(output_path.name + ".partial")
        total = 0
        try:
            with partial_path.open("wb") as output:
                while True:
                    chunk = response.read(MINIMAX_H3_HTTP_READ_CHUNK_BYTES)
                    if not chunk:
                        break
                    if not isinstance(chunk, bytes):
                        raise TypeError(
                            "HTTP material response.read() must return bytes, got "
                            f"{type(chunk).__name__}"
                        )
                    total += len(chunk)
                    output.write(chunk)
            if total == 0:
                raise ValueError(f"HTTP material body is empty: {uri}")
            partial_path.replace(output_path)
        except Exception:
            partial_path.unlink(missing_ok=True)
            output_path.unlink(missing_ok=True)
            raise
    return str(output_path)


def minimax_h3_localize_material_uri(
    batch: Any,
    uri: str,

View on GitHub (pinned to 0132848349)

Solutions

  1. Ensure the response body is opened in binary mode with no text-decoding wrapper
  2. In tests, mock read() to return b'...' not '...'
  3. Remove any response-class override that decodes content

Example fix

# before
mock_resp.read.return_value = 'PNGDATA'
# after
mock_resp.read.return_value = b'PNGDATA'
Defensive patterns

Strategy: type-guard

Validate before calling

chunk = response.read(1)
assert isinstance(chunk, bytes)

Type guard

def is_binary_response(resp) -> bool:
    return isinstance(resp.read(1), bytes)

Try / catch

except TypeError as e: fix the HTTP client to return bytes

Prevention

When it happens

Trigger: A custom HTTP client / mocked response whose read() returns str, or a urllib opener wrapper that decodes bodies to text; hit inside _stream_http_material.

Common situations: Unit tests with MagicMock responses returning strings; custom opener installed via build_opener that text-decodes bodies.

Related errors


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