BerriAI/litellm · error · TypeError

aretrieve_container_file_content expected bytes, got {type(c

Error message

aretrieve_container_file_content expected bytes, got {type(content).__name__}

What it means

TypeError raised inside the container file-content handler in litellm/proxy/container_endpoints/handler_factory.py when aretrieve_container_file_content returns a value that is not bytes. The handler must build a FastAPI Response(content=...) with raw bytes; a str or None from the provider transformation layer violates that contract. This is an internal invariant failure, typically caused by a provider transformation or a custom/mock handler returning decoded text.

Source

Thrown at litellm/proxy/container_endpoints/handler_factory.py:248

        content_type = "application/octet-stream"
        file_id_lower: Final = file_id.lower()
        if ".png" in file_id_lower or file_id_lower.endswith("png"):
            content_type = "image/png"
        elif ".jpg" in file_id_lower or ".jpeg" in file_id_lower:
            content_type = "image/jpeg"
        elif ".gif" in file_id_lower:
            content_type = "image/gif"
        elif ".csv" in file_id_lower:
            content_type = "text/csv"
        elif ".json" in file_id_lower:
            content_type = "application/json"
        elif ".txt" in file_id_lower:
            content_type = "text/plain"
        elif ".pdf" in file_id_lower:
            content_type = "application/pdf"

        if not isinstance(content, bytes):
            raise TypeError(f"aretrieve_container_file_content expected bytes, got {type(content).__name__}")

        return Response(
            content=content,
            headers=dict(fastapi_response.headers),
            media_type=content_type,
        )

    except Exception as e:
        raise await processor._handle_llm_api_exception(
            e=e,
            user_api_key_dict=user_api_key_dict,
            proxy_logging_obj=proxy_logging_obj,
            version=version,
        )


async def _process_multipart_upload_request(
    request: Request,

View on GitHub (pinned to 77b7c6c40c)

Solutions

  1. Check litellm version and upgrade — report the provider + file type to the LiteLLM maintainers if it reproduces on the latest release.
  2. If you control the transformation/handler, ensure the content is encoded: content.encode('utf-8') or the raw bytes from the HTTP response.
  3. As a workaround for binary retrieval, call the provider's file-content endpoint directly rather than through the proxy container route.
Defensive patterns

Strategy: type-guard

Validate before calling

# Server-side: verify before building the Response
content = await provider_handler.aretrieve_container_file_content(...)
if not isinstance(content, bytes):
    content = content.encode("utf-8") if isinstance(content, str) else bytes(content)

Type guard

def is_bytes_payload(content: object) -> bool:
    return isinstance(content, (bytes, bytearray, memoryview))

Try / catch

try:
    data = await client.get(f"/v1/containers/{cid}/files/{fid}/content")
except TypeError as e:
    if "expected bytes" in str(e):
        # proxy/provider contract broken: report, don't retry blindly
        capture_diagnostics(cid, fid, litellm_version)
    raise

Prevention

When it happens

Trigger: GET /v1/containers/{container_id}/files/{file_id}/content where the underlying provider handler returns str (e.g. text decoded upstream) or None instead of bytes; a custom handler_factory wrapper that transforms the provider response; Azure/OpenAI file-content transformation returning the wrong type in a specific LiteLLM version.

Common situations: Hitting the bug after a LiteLLM upgrade changed a transformation's return type; mocking aretrieve_container_file_content in tests with a string payload; a provider returning an empty/JSON-encoded body that the transformation layer passes through un-decoded.

Related errors


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/c80db9411a0eea6a. Report an issue: GitHub.