@tduncan / Nvidia-Optimization-Stack-VLLM Mirror · read-only / commits / 94b71ae106
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, 23:45 UTC

Use `metavar` to list the choices for a CLI arg when custom values are also accepted (#21760)

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

Harry Mellor committed Jul 28, 2025 at 20:31 UTC 94b71ae10626d6e05677062f653051e01eb225ab
3 files changed +20 -7
docs/mkdocs/hooks/generate_argparse.py
+5
@@ -62,6 +62,11 @@ class MarkdownFormatter(HelpFormatter):
62 choices = f'`{"`, `".join(str(c) for c in choices)}`'
63 self._markdown_output.append(
64 f"Possible choices: {choices}\n\n")
65 + elif ((metavar := action.metavar)
66 + and isinstance(metavar, (list, tuple))):
67 + metavar = f'`{"`, `".join(str(m) for m in metavar)}`'
68 + self._markdown_output.append(
69 + f"Possible choices: {metavar}\n\n")
70
71 self._markdown_output.append(f"{action.help}\n\n")
72
tests/engine/test_arg_utils.py
+4
@@ -72,6 +72,10 @@ def test_get_type(type_hints, type, expected):
72 "type": int,
73 "choices": [1, 2]
74 }),
75 + ({str, Literal["x", "y"]}, {
76 + "type": str,
77 + "metavar": ["x", "y"]
78 + }),
79 ({Literal[1, "a"]}, Exception),
80 ])
81 def test_literal_to_kwargs(type_hints, expected):
vllm/engine/arg_utils.py
+11 -7
@@ -108,15 +108,19 @@ def get_type(type_hints: set[TypeHint], type: TypeHintT) -> TypeHintT:
108
109
110 def literal_to_kwargs(type_hints: set[TypeHint]) -> dict[str, Any]:
111 - """Convert Literal type hints to argparse kwargs."""
111 + """Get the `type` and `choices` from a `Literal` type hint in `type_hints`.
112 +
113 + If `type_hints` also contains `str`, we use `metavar` instead of `choices`.
114 + """
115 type_hint = get_type(type_hints, Literal)
113 - choices = get_args(type_hint)
114 - choice_type = type(choices[0])
115 - if not all(isinstance(choice, choice_type) for choice in choices):
116 + options = get_args(type_hint)
117 + option_type = type(options[0])
118 + if not all(isinstance(option, option_type) for option in options):
119 raise ValueError(
117 - "All choices must be of the same type. "
118 - f"Got {choices} with types {[type(c) for c in choices]}")
119 - return {"type": choice_type, "choices": sorted(choices)}
120 + "All options must be of the same type. "
121 + f"Got {options} with types {[type(c) for c in options]}")
122 + kwarg = "metavar" if contains_type(type_hints, str) else "choices"
123 + return {"type": option_type, kwarg: sorted(options)}
124
125
126 def is_not_builtin(type_hint: TypeHint) -> bool: