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 3, 22:45 UTC
[Frontend] Return rendered prompt text in chat completion response (#42052)
Signed-off-by: Wang, Zhipeng | RASIA <zhipeng.wang@rakuten.com> Co-authored-by: Wang, Zhipeng | RASIA <zhipeng.wang@rakuten.com> Co-authored-by: Cursor <cursor@cursor.com> Co-authored-by: Chauncey <chaunceyjiang@gmail.com>
汪志鹏 committed
May 11, 2026 at 13:53 UTC
581b5e9afc98b2711c920699326f3e15f928eea9
2 files changed
+23
vllm/entrypoints/openai/chat_completion/protocol.py
+15
@@ -111,6 +111,9 @@ class ChatCompletionResponse(OpenAIBaseModel):
111
# vLLM-specific fields that are not in OpenAI spec
112
prompt_logprobs: list[dict[int, Logprob] | None] | None = None
113
prompt_token_ids: list[int] | None = None
114
+ # Rendered prompt text from chat templating (only set when
115
+ # ``return_prompt_text=True`` on the request).
116
+ prompt_text: str | None = None
117
kv_transfer_params: dict[str, Any] | None = Field(
118
default=None, description="KVTransfer parameters."
119
)
@@ -138,6 +141,9 @@ class ChatCompletionStreamResponse(OpenAIBaseModel):
141
system_fingerprint: str | None = None
142
# not part of the OpenAI spec but for tracing the tokens
143
prompt_token_ids: list[int] | None = None
144
+ # Rendered prompt text from chat templating (only set when
145
+ # ``return_prompt_text=True`` on the request); only sent on the first chunk.
146
+ prompt_text: str | None = None
147
148
149
class ChatCompletionToolsParam(OpenAIBaseModel):
@@ -352,6 +358,15 @@ class ChatCompletionRequest(OpenAIBaseModel):
358
"need to map generated text back to input tokens."
359
),
360
)
361
+ return_prompt_text: bool | None = Field(
362
+ default=None,
363
+ description=(
364
+ "If true, the response will include ``prompt_text`` containing the "
365
+ "prompt string produced by chat templating. In streaming mode it "
366
+ "is sent only on the first chunk. This is useful for inspecting "
367
+ "exactly what was fed into the model."
368
+ ),
369
+ )
370
371
cache_salt: str | None = Field(
372
default=None,
vllm/entrypoints/openai/chat_completion/serving.py
+8
@@ -508,6 +508,9 @@ class OpenAIServingChat(OpenAIServing):
508
# the role
509
role = self.get_chat_request_role(request)
510
511
+ # ``res.prompt`` is the rendered chat-templated prompt
512
+ prompt_text = res.prompt if request.return_prompt_text else None
513
+
514
# NOTE num_choices defaults to 1 so this usually executes
515
# once per request
516
for i in range(num_choices):
@@ -533,6 +536,7 @@ class OpenAIServingChat(OpenAIServing):
536
if request.return_token_ids
537
else None
538
),
539
+ prompt_text=prompt_text,
540
)
541
542
# if continuous usage stats are requested, add it
@@ -1371,6 +1375,9 @@ class OpenAIServingChat(OpenAIServing):
1375
if final_res.prompt_routed_experts is not None:
1376
prompt_routed_experts = final_res.prompt_routed_experts.tolist()
1377
1378
+ # ``final_res.prompt`` is the rendered chat-templated prompt text
1379
+ prompt_text = final_res.prompt if request.return_prompt_text else None
1380
+
1381
response = ChatCompletionResponse(
1382
id=request_id,
1383
created=created_time,
@@ -1382,6 +1389,7 @@ class OpenAIServingChat(OpenAIServing):
1389
prompt_token_ids=(
1390
final_res.prompt_token_ids if request.return_token_ids else None
1391
),
1392
+ prompt_text=prompt_text,
1393
kv_transfer_params=final_res.kv_transfer_params,
1394
prompt_routed_experts=prompt_routed_experts,
1395
)