-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathpostprocess_handlers.py
More file actions
663 lines (599 loc) · 27.3 KB
/
postprocess_handlers.py
File metadata and controls
663 lines (599 loc) · 27.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
from dataclasses import dataclass, field
from typing import Any, List, Literal, Optional, Tuple, Union
from tensorrt_llm.serve.responses_utils import ResponsesStreamingProcessor
from tensorrt_llm.serve.responses_utils import \
create_response_non_store as responses_api_create_response_non_store
from .._utils import nvtx_range_debug
from ..executor import (DetokenizedGenerationResultBase, GenerationResult,
GenerationResultBase)
from ..executor.postproc_worker import PostprocArgs
from ..executor.result import Logprob, TokenLogprobs
from ..llmapi import SamplingParams
from ..llmapi.reasoning_parser import (BaseReasoningParser,
ReasoningParserFactory,
ReasoningParserResult)
from ..llmapi.tokenizer import TransformersTokenizer
# yapf: disable
from .chat_utils import make_tool_call_id
from .harmony_adapter import (handle_non_streaming_response,
handle_streaming_response)
from .openai_protocol import (ChatCompletionLogProbs,
ChatCompletionLogProbsContent,
ChatCompletionNamedToolChoiceParam,
ChatCompletionRequest, ChatCompletionResponse,
ChatCompletionResponseChoice,
ChatCompletionResponseStreamChoice,
ChatCompletionStreamResponse,
ChatCompletionToolsParam, ChatMessage,
CompletionLogProbs, CompletionRequest,
CompletionResponse, CompletionResponseChoice,
CompletionResponseStreamChoice,
CompletionStreamResponse, DeltaFunctionCall,
DeltaMessage, DeltaToolCall, FunctionCall,
PromptTokensDetails, ResponsesRequest,
ResponsesResponse, StreamOptions, ToolCall,
UsageInfo, to_disaggregated_params)
from .tool_parser.base_tool_parser import BaseToolParser
from .tool_parser.core_types import ToolCallItem
from .tool_parser.tool_parser_factory import ToolParserFactory
# yapf: enable
@dataclass(kw_only=True)
class ChatPostprocArgs(PostprocArgs):
echo: bool = False
role: str
model: str
num_choices: int = 1
tools: Optional[List[ChatCompletionToolsParam]] = None
tool_choice: Optional[Union[Literal["none"],
ChatCompletionNamedToolChoiceParam]] = "none"
return_logprobs: bool = False
top_logprobs: bool = False
stream_options: Optional[StreamOptions] = None
last_message_content: Optional[str] = None
reasoning_parser: Optional[str] = None
tool_parser: Optional[str] = None
reasoning_parser_dict: dict[int, BaseReasoningParser] = field(
default_factory=dict)
tool_parser_dict: dict[int, BaseToolParser] = field(default_factory=dict)
has_tool_call: dict[int, bool] = field(default_factory=dict)
tool_call_id_type: str = "random"
chat_template_kwargs: Optional[dict[str, Any]] = None
@classmethod
def from_request(cls, request: ChatCompletionRequest):
return cls(
echo=request.echo,
role="assistant"
if request.add_generation_prompt else request.messages[-1]["role"],
model=request.model,
num_choices=request.n if request.n else 1,
tools=request.tools,
tool_choice=request.tool_choice,
stream_options=request.stream_options,
return_logprobs=bool(request.logprobs),
top_logprobs=bool(request.top_logprobs),
chat_template_kwargs=request.chat_template_kwargs,
)
def create_logprobs(token_ids: List[int], tokenizer: TransformersTokenizer,
logprobs: List[float] | TokenLogprobs,
top_logprobs: bool) -> ChatCompletionLogProbs:
assert len(token_ids) == len(logprobs), \
"token_ids and logprobs have different lengths"
content: List[ChatCompletionLogProbsContent] = []
for token_id, logprob in zip(token_ids, logprobs):
logprob: float | dict[int, Logprob]
token = tokenizer.decode(token_id)
chat_logprob = ChatCompletionLogProbsContent(
token=token,
bytes=list(token.encode("utf-8", errors="replace")),
)
if isinstance(logprob, dict):
if token_id in logprob:
chat_logprob.logprob = max(logprob[token_id].logprob, -9999.0)
if top_logprobs:
chat_logprob.top_logprobs = [
ChatCompletionLogProbsContent(
token=(tk := tokenizer.decode(tid)),
logprob=max(logprob.logprob, -9999.0),
bytes=list(tk.encode("utf-8", errors="replace")))
for tid, logprob in logprob.items()
]
else:
chat_logprob.logprob = max(logprob, -9999.0)
content.append(chat_logprob)
chat_logprobs = ChatCompletionLogProbs(content=content)
return chat_logprobs
def apply_reasoning_parser(args: ChatPostprocArgs,
output_index: int,
text: str,
streaming: bool,
finished: bool = False) -> Tuple[str, str]:
reasoning_parser = None
if args.reasoning_parser is not None:
if output_index not in args.reasoning_parser_dict:
chat_template_kwargs = getattr(args, "chat_template_kwargs", None)
args.reasoning_parser_dict[
output_index] = ReasoningParserFactory.create_reasoning_parser(
args.reasoning_parser, chat_template_kwargs)
reasoning_parser = args.reasoning_parser_dict[output_index]
if reasoning_parser is not None:
if not streaming:
result = reasoning_parser.parse(text)
else:
result = reasoning_parser.parse_delta(text)
if finished:
finish_result = reasoning_parser.finish()
result = ReasoningParserResult(
content=result.content + finish_result.content,
reasoning_content=result.reasoning_content +
finish_result.reasoning_content,
)
content, reasoning_content = result.content, result.reasoning_content
else:
content, reasoning_content = text, ""
return content, reasoning_content
def apply_tool_parser(args: ChatPostprocArgs, output_index: int, text: str,
streaming: bool) -> Tuple[str, List[ToolCallItem]]:
tool_parser = None
tools = args.tools
if args.tool_parser is not None and tools is not None:
if output_index not in args.tool_parser_dict:
args.tool_parser_dict[
output_index] = ToolParserFactory.create_tool_parser(
args.tool_parser)
tool_parser = args.tool_parser_dict[output_index]
if tool_parser is not None and tools is not None:
if not streaming:
result = tool_parser.detect_and_parse(text, tools)
else:
result = tool_parser.parse_streaming_increment(text, tools)
normal_text, calls = result.normal_text, result.calls
if result.calls:
args.has_tool_call[output_index] = True
else:
normal_text, calls = text, []
return normal_text, calls
@nvtx_range_debug("chat_stream_post_processor")
def chat_stream_post_processor(rsp: GenerationResultBase,
args: ChatPostprocArgs) -> List[str]:
def yield_first_chat(num_tokens: int,
idx: int,
role: str | None = None,
content: str | None = None):
choice_data = ChatCompletionResponseStreamChoice(index=idx,
delta=DeltaMessage(
role=role,
content=content),
finish_reason=None)
chunk = ChatCompletionStreamResponse(choices=[choice_data],
model=args.model)
if include_continuous_usage:
chunk.usage = UsageInfo(
prompt_tokens=num_tokens,
total_tokens=num_tokens,
completion_tokens=0,
prompt_tokens_details=PromptTokensDetails(
cached_tokens=rsp.cached_tokens),
)
data = chunk.model_dump_json(exclude_none=True)
return data
res: List[str] = []
finish_reason_sent = [False] * args.num_choices
prompt_tokens = args.num_prompt_tokens
if stream_option := args.stream_options:
include_usage = stream_option.include_usage
include_continuous_usage = include_usage and stream_option.continuous_usage_stats
else:
include_usage = False
include_continuous_usage = False
if args.first_iteration:
for i in range(args.num_choices):
res.append(
f"data: {yield_first_chat(prompt_tokens, i, role=args.role)} \n\n"
)
if args.echo and args.last_message_content:
res.append(
f"data: {yield_first_chat(prompt_tokens, i, content=args.last_message_content)} \n\n"
)
args.first_iteration = False
for output in rsp.outputs:
i = output.index
if finish_reason_sent[i]:
continue
delta_text = output.text_diff
delta_text, reasoning_delta_text = apply_reasoning_parser(
args,
i,
delta_text,
True,
finished=(output.finish_reason is not None))
if args.tool_choice and type(
args.tool_choice) is ChatCompletionNamedToolChoiceParam:
delta_message = DeltaMessage(tool_calls=[
DeltaToolCall(
function=DeltaFunctionCall(
name=args.tool_choice.function.name,
arguments=delta_text),
index=i,
),
], )
else:
delta_text, calls = apply_tool_parser(args, i, delta_text, True)
tool_calls = []
for call_item in calls:
# Tool call ID should be generated only once per tool call
if call_item.name:
# First chunk: include ID and function name
tool_call_id = make_tool_call_id(
id_type=args.tool_call_id_type,
func_name=call_item.name,
idx=call_item.tool_index)
function_name = call_item.name
else:
# Subsequent chunks: null ID and name for argument deltas
tool_call_id = None
function_name = None
tool_calls.append(
DeltaToolCall(
id=tool_call_id,
index=call_item.tool_index,
function=DeltaFunctionCall(
name=function_name,
arguments=call_item.parameters,
),
))
if tool_calls or delta_text or reasoning_delta_text or output.finish_reason:
delta_message = DeltaMessage(
content=delta_text,
reasoning_content=reasoning_delta_text,
tool_calls=tool_calls if tool_calls else None)
else:
continue
choice = ChatCompletionResponseStreamChoice(
index=i,
delta=delta_message,
avg_decoded_tokens_per_iter=getattr(rsp,
'avg_decoded_tokens_per_iter',
None),
stop_reason=output.stop_reason,
)
if args.return_logprobs:
logprobs = output.logprobs_diff
token_ids = output.token_ids_diff
choice.logprobs = create_logprobs(token_ids, args.tokenizer,
logprobs, args.top_logprobs)
if output.finish_reason is not None:
if output.finish_reason == "stop" and args.has_tool_call.get(
i, False):
choice.finish_reason = "tool_calls"
else:
choice.finish_reason = output.finish_reason
choice.stop_reason = output.stop_reason
finish_reason_sent[i] = True
chunk = ChatCompletionStreamResponse(choices=[choice], model=args.model)
if include_continuous_usage:
chunk.usage = UsageInfo(prompt_tokens=prompt_tokens,
completion_tokens=output.length,
total_tokens=output.length + prompt_tokens,
prompt_tokens_details=PromptTokensDetails(
cached_tokens=rsp.cached_tokens))
data = chunk.model_dump_json(exclude_none=True)
res.append(f"data: {data}\n\n")
if include_usage and rsp._done:
completion_tokens = sum(output.length for output in rsp.outputs)
final_usage = UsageInfo(
prompt_tokens=prompt_tokens,
completion_tokens=completion_tokens,
total_tokens=prompt_tokens + completion_tokens,
prompt_tokens_details=PromptTokensDetails(
cached_tokens=rsp.cached_tokens),
)
final_usage_chunk = ChatCompletionStreamResponse(choices=[],
model=args.model,
usage=final_usage)
final_usage_data = final_usage_chunk.model_dump_json()
res.append(f"data: {final_usage_data}\n\n")
return res
@nvtx_range_debug("chat_response_post_processor")
def chat_response_post_processor(
rsp: GenerationResultBase,
args: ChatPostprocArgs) -> ChatCompletionResponse:
choices: List[ChatCompletionResponseChoice] = []
role = args.role
for output in rsp.outputs:
text, reasoning_text = apply_reasoning_parser(args, output.index,
output.text, False)
if args.tool_choice and isinstance(args.tool_choice,
ChatCompletionNamedToolChoiceParam):
message = ChatMessage(
role=role,
content="",
tool_calls=[
ToolCall(function=FunctionCall(
name=args.tool_choice.function.name, arguments=text))
])
else:
if text is None:
text = ""
text, calls = apply_tool_parser(args, output.index, text, False)
tool_calls = [
ToolCall(function=FunctionCall(name=call.name or "",
arguments=call.parameters))
for call in calls
]
message = ChatMessage(role=role,
content=text,
reasoning_content=reasoning_text,
tool_calls=tool_calls)
disaggregated_params = to_disaggregated_params(
output.disaggregated_params)
choice = ChatCompletionResponseChoice(
index=output.index,
message=message,
stop_reason=output.stop_reason,
disaggregated_params=disaggregated_params,
avg_decoded_tokens_per_iter=getattr(rsp,
'avg_decoded_tokens_per_iter',
None),
)
if output.finish_reason == "stop" and args.has_tool_call.get(
output.index, False):
choice.finish_reason = "tool_calls"
else:
choice.finish_reason = output.finish_reason
if args.return_logprobs:
choice.logprobs = create_logprobs(output.token_ids, args.tokenizer,
output.logprobs,
args.top_logprobs)
choices.append(choice)
if args.echo and args.last_message_content:
for choice in choices:
full_message = args.last_message_content + choice.message.content
choice.message.content = full_message
num_prompt_tokens = args.num_prompt_tokens
num_generated_tokens = sum(len(output.token_ids) for output in rsp.outputs)
usage = UsageInfo(
prompt_tokens=num_prompt_tokens,
completion_tokens=num_generated_tokens,
total_tokens=num_prompt_tokens + num_generated_tokens,
prompt_tokens_details=PromptTokensDetails(
cached_tokens=rsp.cached_tokens),
)
response = ChatCompletionResponse(
model=args.model,
choices=choices,
usage=usage,
)
return response
@dataclass(kw_only=True)
class CompletionPostprocArgs(PostprocArgs):
echo: bool = False
model: str = None
num_choices: int = 1
prompt_idx: int = 0
detokenize: bool = True
prompt: Optional[str] = None
return_logprobs: bool = False
stream_options: Optional[StreamOptions] = None
@classmethod
def from_request(cls, request: CompletionRequest):
return cls(
echo=request.echo,
model=request.model,
num_choices=request.n if request.n else 1,
stream_options=request.stream_options,
detokenize=request.detokenize,
return_logprobs=bool(request.logprobs),
)
def create_completion_logprobs(token_ids: List[int],
tokenizer: TransformersTokenizer,
logprobs: List[float] | TokenLogprobs,
initial_offset: int = 0) -> CompletionLogProbs:
assert len(token_ids) == len(logprobs), \
"token_ids and logprobs have different lengths"
text_offset = []
token_logprobs = []
top_logprobs_list = []
tokens = []
for token_id, logprob in zip(token_ids, logprobs):
if isinstance(logprob, dict):
token_logprobs.append(max(logprob[token_id].logprob, -9999.0))
top_logprobs_list.append({
tokenizer.decode(tid):
max(lp.logprob, -9999.0)
for tid, lp in logprob.items()
})
else:
token_logprobs.append(max(logprob, -9999.0))
token = tokenizer.decode(token_id)
if len(text_offset) == 0:
text_offset.append(initial_offset)
else:
text_offset.append(text_offset[-1] + len(token))
tokens.append(token)
return CompletionLogProbs(text_offset=text_offset,
token_logprobs=token_logprobs,
tokens=tokens,
top_logprobs=top_logprobs_list)
@nvtx_range_debug("completion_stream_post_processor")
def completion_stream_post_processor(rsp: DetokenizedGenerationResultBase,
args: CompletionPostprocArgs) -> List[str]:
res: List[str] = []
prompt_tokens = args.num_prompt_tokens
if stream_option := args.stream_options:
include_usage = stream_option.include_usage
include_continuous_usage = include_usage and stream_option.continuous_usage_stats
else:
include_usage = False
include_continuous_usage = False
for output in rsp.outputs:
delta_text = output.text_diff
if args.echo and args.first_iteration:
delta_text = args.prompt + delta_text
choice = CompletionResponseStreamChoice(
index=args.prompt_idx * args.num_choices + output.index,
text=delta_text if args.detokenize else "",
token_ids=None if args.detokenize else output.token_ids_diff,
finish_reason=output.finish_reason,
stop_reason=output.stop_reason,
avg_decoded_tokens_per_iter=getattr(rsp,
'avg_decoded_tokens_per_iter',
None),
)
if args.return_logprobs:
logprobs = output.logprobs_diff
token_ids = output.token_ids_diff
choice.logprobs = create_completion_logprobs(
token_ids, args.tokenizer, logprobs, output._last_text_len)
chunk = CompletionStreamResponse(model=args.model, choices=[choice])
if include_continuous_usage:
chunk.usage = UsageInfo(prompt_tokens=prompt_tokens,
completion_tokens=output.length,
total_tokens=output.length + prompt_tokens,
prompt_tokens_details=PromptTokensDetails(
cached_tokens=rsp.cached_tokens))
data = chunk.model_dump_json(exclude_unset=False)
res.append(f"data: {data}\n\n")
if include_usage and rsp._done:
completion_tokens = sum(output.length for output in rsp.outputs)
final_usage = UsageInfo(
prompt_tokens=prompt_tokens,
completion_tokens=completion_tokens,
total_tokens=prompt_tokens + completion_tokens,
prompt_tokens_details=PromptTokensDetails(
cached_tokens=rsp.cached_tokens),
)
final_usage_chunk = ChatCompletionStreamResponse(choices=[],
model=args.model,
usage=final_usage)
final_usage_data = final_usage_chunk.model_dump_json()
res.append(f"data: {final_usage_data}\n\n")
args.first_iteration = False
return res
@nvtx_range_debug("completion_response_post_processor")
def completion_response_post_processor(
rsp: GenerationResult,
args: CompletionPostprocArgs) -> CompletionResponse:
prompt_tokens = args.num_prompt_tokens
completion_tokens = 0
choices = []
for output in rsp.outputs:
text = output.text
if args.echo:
text = args.prompt + text
disaggregated_params = to_disaggregated_params(
output.disaggregated_params)
choice = CompletionResponseChoice(
text=text if args.detokenize else "",
token_ids=None if args.detokenize else output.token_ids,
index=args.prompt_idx * args.num_choices + output.index,
disaggregated_params=disaggregated_params,
context_logits=None
if rsp.context_logits is None else rsp.context_logits.tolist(),
stop_reason=output.stop_reason,
finish_reason=output.finish_reason,
avg_decoded_tokens_per_iter=getattr(rsp,
'avg_decoded_tokens_per_iter',
None),
)
if args.return_logprobs:
logprobs = output.logprobs
token_ids = output.token_ids
choice.logprobs = create_completion_logprobs(
token_ids, args.tokenizer, logprobs)
completion_tokens += output.length
choices.append(choice)
usage = UsageInfo(prompt_tokens=prompt_tokens,
completion_tokens=completion_tokens,
total_tokens=completion_tokens + prompt_tokens,
prompt_tokens_details=PromptTokensDetails(
cached_tokens=rsp.cached_tokens))
response = CompletionResponse(choices=choices,
model=args.model,
usage=usage)
return response
@dataclass(kw_only=True)
class ChatCompletionPostprocArgs(PostprocArgs):
model: str
tools: Optional[List[ChatCompletionToolsParam]]
tool_choice: Optional[Union[Literal["none", "auto"],
ChatCompletionNamedToolChoiceParam]]
request_id: Optional[int] = None
stream_options: Optional[StreamOptions] = None
chat_template_kwargs: Optional[dict[str, Any]] = None
@classmethod
def from_request(cls, request: ChatCompletionRequest):
return cls(
model=request.model,
tools=request.tools,
tool_choice=request.tool_choice,
stream_options=request.stream_options if request.stream else None,
chat_template_kwargs=request.chat_template_kwargs,
)
@nvtx_range_debug("chat_harmony_post_processor")
def chat_harmony_post_processor(
rsp: GenerationResult,
args: ChatCompletionPostprocArgs) -> ChatCompletionResponse:
response = handle_non_streaming_response(
tools=args.tools,
tool_choice=args.tool_choice,
outputs=rsp.outputs,
model=args.model,
num_prompt_tokens=args.num_prompt_tokens,
cached_tokens=rsp.cached_tokens,
)
return response
@nvtx_range_debug("chat_harmony_streaming_post_processor")
def chat_harmony_streaming_post_processor(
rsp: GenerationResult, args: ChatCompletionPostprocArgs) -> List[str]:
# Read the request ID directly from rsp.id instead of args.request_id.
# Both are the same executor-assigned ID, but args.request_id is set too
# late (after generate_async returns) for the postprocess worker path:
# the worker receives a copy of args before the ID is assigned, so
# args.request_id is always None with num_postprocess_workers > 0.
response = handle_streaming_response(
tools=args.tools,
tool_choice=args.tool_choice,
result=rsp,
model=args.model,
request_id=str(rsp.id),
done=rsp._done,
num_prompt_tokens=args.num_prompt_tokens,
first_iteration=args.first_iteration,
stream_options=args.stream_options,
cached_tokens=rsp.cached_tokens,
)
args.first_iteration = False
return response
@dataclass(kw_only=True)
class ResponsesAPIPostprocArgs(PostprocArgs):
model: str
request: ResponsesRequest
sampling_params: SamplingParams
use_harmony: bool
reasoning_parser: Optional[str] = None
tool_parser: Optional[str] = None
streaming_processor: Optional[ResponsesStreamingProcessor] = None
@nvtx_range_debug("responses_api_post_processor")
def responses_api_post_processor(
rsp: GenerationResult,
args: ResponsesAPIPostprocArgs) -> ResponsesResponse:
return responses_api_create_response_non_store(
generation_result=rsp,
request=args.request,
sampling_params=args.sampling_params,
model_name=args.model,
use_harmony=args.use_harmony,
reasoning_parser=args.reasoning_parser,
tool_parser=args.tool_parser,
)
@nvtx_range_debug("responses_api_streaming_post_processor")
def responses_api_streaming_post_processor(
rsp: GenerationResult, args: ResponsesAPIPostprocArgs) -> List[str]:
if args.streaming_processor is None:
raise ValueError(
"streaming_processor is required for streaming post-processing")
outputs = args.streaming_processor.process_single_output(rsp)
if rsp._done:
outputs.append(
args.streaming_processor.get_final_response_non_store(rsp))
return outputs