@tduncan / Nvidia-Optimization-Stack-VLLM Mirror · read-only / commits / fb0e0d46fc
Fork 0

A high-throughput and memory-efficient inference and serving engine for LLMs

Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git

Synced Nov 14, 22:35 UTC

Fix `get_kwargs` for case where type hint is `list[Union[str, type]]` (#22016)

Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>

Harry Mellor committed Aug 1, 2025 at 13:26 UTC fb0e0d46fc443f08bc2a859b839f0f66c6a7f670
2 files changed +12 -5
tests/engine/test_arg_utils.py
+6 -1
@@ -5,7 +5,7 @@ import json
5 from argparse import ArgumentError
6 from contextlib import nullcontext
7 from dataclasses import dataclass, field
8 -from typing import Annotated, Literal, Optional
8 +from typing import Annotated, Literal, Optional, Union
9
10 import pytest
11
@@ -136,6 +136,8 @@ class DummyConfig:
136 """List with variable length"""
137 list_literal: list[Literal[1, 2]] = field(default_factory=list)
138 """List with literal choices"""
139 + list_union: list[Union[str, type[object]]] = field(default_factory=list)
140 + """List with union type"""
141 literal_literal: Literal[Literal[1], Literal[2]] = 1
142 """Literal of literals with default 1"""
143 json_tip: dict = field(default_factory=dict)
@@ -187,6 +189,9 @@ def test_get_kwargs():
189 assert kwargs["list_literal"]["type"] is int
190 assert kwargs["list_literal"]["nargs"] == "+"
191 assert kwargs["list_literal"]["choices"] == [1, 2]
192 + # lists with unions should become str type.
193 + # If not, we cannot know which type to use for parsing
194 + assert kwargs["list_union"]["type"] is str
195 # literals of literals should have merged choices
196 assert kwargs["literal_literal"]["choices"] == [1, 2]
197 # dict should have json tip in help
vllm/engine/arg_utils.py
+6 -4
@@ -217,10 +217,12 @@ Additionally, list elements can be passed individually using `+`:
217 elif contains_type(type_hints, list):
218 type_hint = get_type(type_hints, list)
219 types = get_args(type_hint)
220 - assert len(types) == 1, (
221 - "List type must have exactly one type. Got "
222 - f"{type_hint} with types {types}")
223 - kwargs[name]["type"] = types[0]
220 + list_type = types[0]
221 + if get_origin(list_type) is Union:
222 + msg = "List type must contain str if it is a Union."
223 + assert str in get_args(list_type), msg
224 + list_type = str
225 + kwargs[name]["type"] = list_type
226 kwargs[name]["nargs"] = "+"
227 elif contains_type(type_hints, int):
228 kwargs[name]["type"] = int