{"record":{"id":"c80db9411a0eea6a","repo":"BerriAI/litellm","slug":"aretrieve-container-file-content-expected-bytes-g","errorCode":null,"errorMessage":"aretrieve_container_file_content expected bytes, got {type(content).__name__}","messagePattern":"aretrieve_container_file_content expected bytes, got (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":500,"severity":"error","filePath":"litellm/proxy/container_endpoints/handler_factory.py","lineNumber":248,"sourceCode":"        content_type = \"application/octet-stream\"\n        file_id_lower: Final = file_id.lower()\n        if \".png\" in file_id_lower or file_id_lower.endswith(\"png\"):\n            content_type = \"image/png\"\n        elif \".jpg\" in file_id_lower or \".jpeg\" in file_id_lower:\n            content_type = \"image/jpeg\"\n        elif \".gif\" in file_id_lower:\n            content_type = \"image/gif\"\n        elif \".csv\" in file_id_lower:\n            content_type = \"text/csv\"\n        elif \".json\" in file_id_lower:\n            content_type = \"application/json\"\n        elif \".txt\" in file_id_lower:\n            content_type = \"text/plain\"\n        elif \".pdf\" in file_id_lower:\n            content_type = \"application/pdf\"\n\n        if not isinstance(content, bytes):\n            raise TypeError(f\"aretrieve_container_file_content expected bytes, got {type(content).__name__}\")\n\n        return Response(\n            content=content,\n            headers=dict(fastapi_response.headers),\n            media_type=content_type,\n        )\n\n    except Exception as e:\n        raise await processor._handle_llm_api_exception(\n            e=e,\n            user_api_key_dict=user_api_key_dict,\n            proxy_logging_obj=proxy_logging_obj,\n            version=version,\n        )\n\n\nasync def _process_multipart_upload_request(\n    request: Request,","sourceCodeStart":230,"sourceCodeEnd":266,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/proxy/container_endpoints/handler_factory.py#L230-L266","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check litellm version and upgrade — report the provider + file type to the LiteLLM maintainers if it reproduces on the latest release.","If you control the transformation/handler, ensure the content is encoded: content.encode('utf-8') or the raw bytes from the HTTP response.","As a workaround for binary retrieval, call the provider's file-content endpoint directly rather than through the proxy container route."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":"# Server-side: verify before building the Response\ncontent = await provider_handler.aretrieve_container_file_content(...)\nif not isinstance(content, bytes):\n    content = content.encode(\"utf-8\") if isinstance(content, str) else bytes(content)","typeGuard":"def is_bytes_payload(content: object) -> bool:\n    return isinstance(content, (bytes, bytearray, memoryview))","tryCatchPattern":"try:\n    data = await client.get(f\"/v1/containers/{cid}/files/{fid}/content\")\nexcept TypeError as e:\n    if \"expected bytes\" in str(e):\n        # proxy/provider contract broken: report, don't retry blindly\n        capture_diagnostics(cid, fid, litellm_version)\n    raise","preventionTips":["Pin the LiteLLM version in production and test container file retrieval after upgrades.","In custom provider transformations, always return response.content (bytes) from the upstream HTTP response.","Report reproducible type errors upstream with provider, file type, and version instead of working around them silently."],"tags":["litellm-proxy","containers","type-error","internal-invariant"],"backgroundTag":"invalid-response-type","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}