[Perf][Frontend] Chunk in-memory image file responses (#7459)

Signed-off-by: QI JIA <qi.jia@shengshu.ai>
Co-authored-by: QI JIA <qi.jia@shengshu.ai>
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
authored and GitHub committed 2026-09-13 20:33:10 +08:00
1 parent 311e714d34
commit e284d907b5
3 files changed
+289 -2

No files matched your search

@@ -11,6 +11,7 @@ import base64
import io
import uuid
import zipfile
from collections.abc import AsyncIterator
from enum import Enum
from http import HTTPStatus
from typing import Any, Literal
@@ -25,6 +26,8 @@ from vllm_omni.entrypoints.openai.image_api_utils import validate_layered_layers
_INT64_MIN = -(2**63)
_INT64_MAX = 2**63 - 1
_FILE_RESPONSE_CHUNK_SIZE = 64 * 1024
_IMAGE_FILE_METADATA = {
"jpg": ("jpg", "image/jpeg"),
"jpeg": ("jpeg", "image/jpeg"),
@@ -33,6 +36,13 @@ _IMAGE_FILE_METADATA = {
}
async def _iter_file_chunks(data: bytes) -> AsyncIterator[memoryview]:
"""Yield fixed-size chunks of an in-memory payload."""
view = memoryview(data)
for offset in range(0, len(view), _FILE_RESPONSE_CHUNK_SIZE):
yield view[offset : offset + _FILE_RESPONSE_CHUNK_SIZE]
class ResponseFormat(str, Enum):
"""Image response format"""
@@ -235,7 +245,7 @@ class ImageGenerationResponse(BaseModel):
image_bytes = base64.b64decode(self.data[0].b64_json)
filename = f"image_{uuid.uuid4().hex[:8]}.{extension}"
return StreamingResponse(
io.BytesIO(image_bytes),
_iter_file_chunks(image_bytes),
media_type=media_type,
headers={
"Content-Disposition": f'attachment; filename="{filename}"',
@@ -251,7 +261,7 @@ class ImageGenerationResponse(BaseModel):
zip_bytes = zip_buffer.getvalue()
filename = f"images_{uuid.uuid4().hex[:8]}.zip"
return StreamingResponse(
io.BytesIO(zip_bytes),
_iter_file_chunks(zip_bytes),
media_type="application/zip",
headers={
"Content-Disposition": f'attachment; filename="{filename}"',