[Frontend][EPD] Use JSON arrays for multimodal metadata (#56090)

Signed-off-by: Tianyu Guo <guoty@inferact.ai>
This commit is contained in:
Tianyu Guo
2026-09-10 05:04:14 +00:00
committed by GitHub
parent 2a02f6efe3
commit 479611c0c6
6 changed files with 167 additions and 62 deletions
+76 -44
View File
@@ -73,7 +73,7 @@ from vllm.renderers.embed_utils import (
)
from vllm.transformers_utils.processor import get_video_processor_cls_name
from vllm.utils import random_uuid
from vllm.utils.collection_utils import is_list_of
from vllm.utils.collection_utils import is_list_of, is_list_of_numbers
from vllm.utils.import_utils import LazyLoader
if TYPE_CHECKING:
@@ -146,12 +146,15 @@ class ChatCompletionContentPartAudioParam(TypedDict, total=False):
"""The type of the content part."""
MultiModalEmbedsPayload: TypeAlias = str | dict[str, str | list[int | float]]
class ChatCompletionContentPartImageEmbedsParam(TypedDict, total=False):
image_embeds: str | dict[str, str] | None
image_embeds: MultiModalEmbedsPayload | None
"""
The image embeddings. It can be either:
- A single base64 string.
- A dictionary where each value is a base64 string.
- A dictionary of base64 tensors or numeric JSON metadata arrays.
"""
type: Required[Literal["image_embeds"]]
"""The type of the content part."""
@@ -163,11 +166,11 @@ class ChatCompletionContentPartImageEmbedsParam(TypedDict, total=False):
class ChatCompletionContentPartAudioEmbedsParam(TypedDict, total=False):
audio_embeds: str | dict[str, str] | None
audio_embeds: MultiModalEmbedsPayload | None
"""
The audio embeddings. It can be either:
- A single base64 string representing a serialized torch tensor.
- A dictionary where each value is a base64 string.
- A dictionary of base64 tensors or numeric JSON metadata arrays.
"""
type: Required[Literal["audio_embeds"]]
"""The type of the content part."""
@@ -179,11 +182,11 @@ class ChatCompletionContentPartAudioEmbedsParam(TypedDict, total=False):
class ChatCompletionContentPartVideoEmbedsParam(TypedDict, total=False):
video_embeds: str | dict[str, str] | None
video_embeds: MultiModalEmbedsPayload | None
"""
The video embeddings. It can be either:
- A single base64 string representing a serialized torch tensor.
- A dictionary where each value is a base64 string.
- A dictionary of base64 tensors or numeric JSON metadata arrays.
"""
type: Required[Literal["video_embeds"]]
"""The type of the content part."""
@@ -527,6 +530,27 @@ def _merge_embeds(
return data_merged
async def _load_embeds_dict(
data: dict[str, str | list[int | float]],
fetch: Callable[[str], Awaitable["torch.Tensor"]],
) -> dict[str, Any]:
encoded = {key: value for key, value in data.items() if isinstance(value, str)}
tensors = await asyncio.gather(*(fetch(value) for value in encoded.values()))
return {**data, **dict(zip(encoded, tensors))}
def _parse_metadata_array(key: str, value: list, metadata_fields: set[str]):
if key not in metadata_fields:
raise VLLMValidationError(f"JSON arrays are only supported for metadata: {key}")
if not is_list_of_numbers(value):
raise VLLMValidationError(f"Metadata {key} must be a finite numeric array.")
dtype = torch.float64 if any(isinstance(v, float) for v in value) else torch.int64
try:
return torch.tensor(value, dtype=dtype)
except (ValueError, TypeError, OverflowError, RuntimeError) as error:
raise VLLMValidationError(f"Invalid metadata array: {key}") from error
def _get_embeds_data(
modality: str,
data_items: list[Any],
@@ -544,6 +568,18 @@ def _get_embeds_data(
return _merge_embeds(dict_items, mm_processor)[embeds_key]
if is_list_of(data_items, dict):
metadata_fields = mm_processor.info.data_parser.placeholder_metadata_fields(
modality
)
data_items = [
{
key: _parse_metadata_array(key, value, metadata_fields)
if isinstance(value, list)
else value
for key, value in item.items()
}
for item in data_items
]
return _merge_embeds(data_items, mm_processor)
raise NotImplementedError(type(data_items))
@@ -946,7 +982,7 @@ class BaseMultiModalContentParser(ABC):
@abstractmethod
def parse_image_embeds(
self,
image_embeds: str | dict[str, str] | None,
image_embeds: MultiModalEmbedsPayload | None,
uuid: str | None = None,
) -> None:
raise NotImplementedError
@@ -970,7 +1006,7 @@ class BaseMultiModalContentParser(ABC):
@abstractmethod
def parse_audio_embeds(
self,
audio_embeds: str | dict[str, str] | None,
audio_embeds: MultiModalEmbedsPayload | None,
uuid: str | None = None,
) -> None:
raise NotImplementedError
@@ -986,7 +1022,7 @@ class BaseMultiModalContentParser(ABC):
@abstractmethod
def parse_video_embeds(
self,
video_embeds: str | dict[str, str] | None,
video_embeds: MultiModalEmbedsPayload | None,
uuid: str | None = None,
) -> None:
raise NotImplementedError
@@ -1043,7 +1079,7 @@ class MultiModalContentParser(BaseMultiModalContentParser):
def parse_image_embeds(
self,
image_embeds: str | dict[str, str] | None,
image_embeds: MultiModalEmbedsPayload | None,
uuid: str | None = None,
) -> None:
mm_config = self.model_config.get_multimodal_config()
@@ -1055,7 +1091,7 @@ class MultiModalContentParser(BaseMultiModalContentParser):
if isinstance(image_embeds, dict):
embeds = {
k: self._connector.fetch_image_embedding(v)
k: self._connector.fetch_image_embedding(v) if isinstance(v, str) else v
for k, v in image_embeds.items()
}
placeholder = self._tracker.add("image_embeds", (embeds, uuid))
@@ -1071,7 +1107,7 @@ class MultiModalContentParser(BaseMultiModalContentParser):
def parse_audio_embeds(
self,
audio_embeds: str | dict[str, str] | None,
audio_embeds: MultiModalEmbedsPayload | None,
uuid: str | None = None,
) -> None:
mm_config = self.model_config.get_multimodal_config()
@@ -1083,7 +1119,7 @@ class MultiModalContentParser(BaseMultiModalContentParser):
if isinstance(audio_embeds, dict):
embeds = {
k: self._connector.fetch_audio_embedding(v)
k: self._connector.fetch_audio_embedding(v) if isinstance(v, str) else v
for k, v in audio_embeds.items()
}
placeholder = self._tracker.add("audio_embeds", (embeds, uuid))
@@ -1148,7 +1184,7 @@ class MultiModalContentParser(BaseMultiModalContentParser):
def parse_video_embeds(
self,
video_embeds: str | dict[str, str] | None,
video_embeds: MultiModalEmbedsPayload | None,
uuid: str | None = None,
) -> None:
mm_config = self.model_config.get_multimodal_config()
@@ -1160,7 +1196,7 @@ class MultiModalContentParser(BaseMultiModalContentParser):
if isinstance(video_embeds, dict):
embeds = {
k: self._connector.fetch_video_embedding(v)
k: self._connector.fetch_video_embedding(v) if isinstance(v, str) else v
for k, v in video_embeds.items()
}
placeholder = self._tracker.add("video_embeds", (embeds, uuid))
@@ -1242,7 +1278,7 @@ class AsyncMultiModalContentParser(BaseMultiModalContentParser):
def parse_image_embeds(
self,
image_embeds: str | dict[str, str] | None,
image_embeds: MultiModalEmbedsPayload | None,
uuid: str | None = None,
) -> None:
mm_config = self.model_config.get_multimodal_config()
@@ -1260,17 +1296,13 @@ class AsyncMultiModalContentParser(BaseMultiModalContentParser):
async def _image_embeds_with_uuid_async(
self,
image_embeds: str | dict[str, str] | None,
image_embeds: MultiModalEmbedsPayload | None,
uuid: str | None,
):
if isinstance(image_embeds, dict):
tensors = await asyncio.gather(
*(
self._connector.fetch_image_embedding_async(v)
for v in image_embeds.values()
)
embeds = await _load_embeds_dict(
image_embeds, self._connector.fetch_image_embedding_async
)
embeds = dict(zip(image_embeds, tensors))
elif isinstance(image_embeds, str):
embeds = await self._connector.fetch_image_embedding_async(image_embeds)
else:
@@ -1279,7 +1311,7 @@ class AsyncMultiModalContentParser(BaseMultiModalContentParser):
def parse_audio_embeds(
self,
audio_embeds: str | dict[str, str] | None,
audio_embeds: MultiModalEmbedsPayload | None,
uuid: str | None = None,
) -> None:
mm_config = self.model_config.get_multimodal_config()
@@ -1297,17 +1329,13 @@ class AsyncMultiModalContentParser(BaseMultiModalContentParser):
async def _audio_embeds_with_uuid_async(
self,
audio_embeds: str | dict[str, str] | None,
audio_embeds: MultiModalEmbedsPayload | None,
uuid: str | None,
):
if isinstance(audio_embeds, dict):
tensors = await asyncio.gather(
*(
self._connector.fetch_audio_embedding_async(v)
for v in audio_embeds.values()
)
embeds = await _load_embeds_dict(
audio_embeds, self._connector.fetch_audio_embedding_async
)
embeds = dict(zip(audio_embeds, tensors))
elif isinstance(audio_embeds, str):
embeds = await self._connector.fetch_audio_embedding_async(audio_embeds)
else:
@@ -1382,7 +1410,7 @@ class AsyncMultiModalContentParser(BaseMultiModalContentParser):
def parse_video_embeds(
self,
video_embeds: str | dict[str, str] | None,
video_embeds: MultiModalEmbedsPayload | None,
uuid: str | None = None,
) -> None:
mm_config = self.model_config.get_multimodal_config()
@@ -1400,17 +1428,13 @@ class AsyncMultiModalContentParser(BaseMultiModalContentParser):
async def _video_embeds_with_uuid_async(
self,
video_embeds: str | dict[str, str] | None,
video_embeds: MultiModalEmbedsPayload | None,
uuid: str | None,
):
if isinstance(video_embeds, dict):
tensors = await asyncio.gather(
*(
self._connector.fetch_video_embedding_async(v)
for v in video_embeds.values()
)
embeds = await _load_embeds_dict(
video_embeds, self._connector.fetch_video_embedding_async
)
embeds = dict(zip(video_embeds, tensors))
elif isinstance(video_embeds, str):
embeds = await self._connector.fetch_video_embedding_async(video_embeds)
else:
@@ -1611,7 +1635,9 @@ _AudioParser = TypeAdapter(ChatCompletionContentPartAudioParam).validate_python
_VideoParser = TypeAdapter(ChatCompletionContentPartVideoParam).validate_python
_ResponsesInputImageParser = TypeAdapter(ResponseInputImageParam).validate_python
_ContentPart: TypeAlias = str | dict[str, str] | InputAudio | PILImage
_ContentPart: TypeAlias = (
MultiModalEmbedsPayload | dict[str, str] | InputAudio | PILImage
)
# Define a mapping from part types to their corresponding parsing functions.
MM_PARSER_MAP: dict[
@@ -1901,15 +1927,21 @@ def _parse_chat_message_content_part(
mm_parser.parse_image(str_content, uuid)
modality = "image"
elif part_type == "image_embeds":
content = cast(str | dict[str, str], content) if content is not None else None
content = (
cast(MultiModalEmbedsPayload, content) if content is not None else None
)
mm_parser.parse_image_embeds(content, uuid)
modality = "image"
elif part_type == "audio_embeds":
content = cast(str | dict[str, str], content) if content is not None else None
content = (
cast(MultiModalEmbedsPayload, content) if content is not None else None
)
mm_parser.parse_audio_embeds(content, uuid)
modality = "audio"
elif part_type == "video_embeds":
content = cast(str | dict[str, str], content) if content is not None else None
content = (
cast(MultiModalEmbedsPayload, content) if content is not None else None
)
mm_parser.parse_video_embeds(content, uuid)
modality = "video"
elif part_type == "prompt_embeds":