vllm.renderers.online_derenderer
¶
Classes:
OnlineDerenderer
¶
Methods:
-
derender_chat_stream–Process one GenerateStreamResponse chunk for streaming chat derender.
-
derender_completion_stream–Process one GenerateStreamResponse chunk for streaming completions.
Source code in vllm/renderers/online_derenderer.py
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 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 | |
_derender_chat_stream_parsed(parser_cls, model, generate_chunk, state, chat_request, prompt_tokens, prompt_token_ids)
¶
Parser path for streaming chat derender: replay + parse_delta.
Parser internal state (buffered markup, reasoning/tool phase, etc.)
cannot be serialized into DerenderStreamState, so each call
builds a fresh parser and replays every prior output token through
parse_delta (discarding the result) before processing this
chunk's tokens for real.
Every chunk, replayed or live, goes through one parse_delta call
with the tokens it arrived with (more than one under e.g.
speculative decoding). Replay recovers those boundaries from
state.output_chunk_lens, so the rebuilt parser state matches
standard serving, which calls parse_delta once per engine step.
Text comes from a fresh incremental detokenizer with special tokens
preserved (skip_special_tokens=False), seeded from the prompt
tail on every call since replay starts from scratch.
Source code in vllm/renderers/online_derenderer.py
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 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 | |
_detokenize_delta(tokenizer, delta_token_ids, state, skip_special_tokens=True, spaces_between_special_tokens=True)
¶
Incrementally detokenize delta_token_ids from prior stream state.
Resumes decoding from the offsets carried in state rather than
replaying token history. state.prev_tokens holds the trailing decode
window (from prefix_offset onward) that detokenize_incrementally
still needs to reproduce any partially read multi-byte character
(tracked by read_offset). The delta tokens are fed straight onto it.
The window is bounded. detokenize_incrementally never reads before
prefix_offset, so after each token we trim prev_tokens to that
tail and rebase the offsets to it. State transport therefore stays
O(window) per chunk instead of re-sending the full token history.
Parameters:
-
(tokenizer¶TokenizerLike) –The tokenizer to decode with.
-
(delta_token_ids¶list[int]) –New token IDs from this generate chunk.
-
(state¶DerenderStreamState) –Client carried detok state from the previous call.
-
(skip_special_tokens¶bool, default:True) –Passed through to the tokenizer.
-
(spaces_between_special_tokens¶bool, default:True) –Passed through to the tokenizer.
Returns:
-
str–(new_text, updated_state) — the delta text for this chunk and the
-
DerenderStreamState–state to pass to the next call.
Source code in vllm/renderers/online_derenderer.py
derender_chat_stream(model, generate_chunk, state=None, chat_request=None, prompt_tokens=None, prompt_token_ids=None)
async
¶
Process one GenerateStreamResponse chunk for streaming chat derender.
Unlike OpenAI's API, which always emits role: "assistant" on the
very first chunk, this emits it on the first chunk with a non empty
choices list. A leading usage only chunk therefore defers the
role to the following content chunk instead of sending an empty
role only delta.
Parameters:
-
(model¶str) –Model name for the response object.
-
(generate_chunk¶GenerateTokensStreamResponse) –One SSE chunk from
/inference/v1/generate. -
(state¶DerenderStreamState | None, default:None) –Client carried detok state (
Nonefor first call). -
(chat_request¶ChatCompletionRequest | None, default:None) –Original ChatCompletionRequest from
/render. Required when a reasoning or tool parser is configured (validated by the caller — seeServingDerender) because plain detokenization would leak raw parser markup intocontent. -
(prompt_tokens¶int | None, default:None) –Prompt token count for the usage chunk.
-
(prompt_token_ids¶list[int] | None, default:None) –Prompt token IDs. Required on the parser path (validated by the caller) and optional otherwise, falling back to
generate_chunk.prompt_token_ids. SeeDerenderChatStreamRequest.prompt_token_ids.
Returns:
-
ChatCompletionStreamResponse–(chunk, updated_state) — the derendered SSE chunk and the state
-
DerenderStreamState–the client must pass to the next call.
Source code in vllm/renderers/online_derenderer.py
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 | |
derender_completion_stream(model, generate_chunk, state=None, prompt_tokens=None, completion_request=None, prompt_token_ids=None)
async
¶
Process one GenerateStreamResponse chunk for streaming completions.
Each call takes one SSE chunk from /inference/v1/generate plus the
client carried stream_state and returns a CompletionStreamResponse
chunk and the updated state.
The generate stream emits one choice per SSE event, so this method
processes one output sequence at a time. For n > 1 the client
maintains one DerenderStreamState per choice.index.
Parameters:
-
(model¶str) –Model name for the response object.
-
(generate_chunk¶GenerateTokensStreamResponse) –One SSE chunk from
/inference/v1/generate. -
(state¶DerenderStreamState | None, default:None) –Client carried detok state (
None→ first call). -
(prompt_tokens¶int | None, default:None) –Prompt token count for usage (from the render step).
-
(completion_request¶CompletionRequest | None, default:None) –Original CompletionRequest from
/render; suppliesskip_special_tokens. -
(prompt_token_ids¶list[int] | None, default:None) –Seeds the first chunk's decode. Falls back to
generate_chunk.prompt_token_ids.
Returns:
-
tuple[CompletionStreamResponse, DerenderStreamState]–(chunk, updated_state) — the derendered chunk and updated state.
Source code in vllm/renderers/online_derenderer.py
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 | |
_convert_chat_logprobs_to_completion_logprobs(logprobs, initial_text_offset=0)
¶
Convert ChatCompletionLogProbs (per-token objects) to CompletionLogProbs (parallel flat lists) as required by the /v1/completions response schema.
initial_text_offset keeps text_offset absolute across streaming
chunks, mirroring the generate streaming path.
Source code in vllm/renderers/online_derenderer.py
_correct_decoded_token(token_id, context_token_ids, tokenizer)
¶
Use preceding tokens as context to fix U+FFFD from byte-fallback.
Mirrors LogprobsProcessor._correct_decoded_token in v1/engine/logprobs.py.
Source code in vllm/renderers/online_derenderer.py
_decode_params(tokenizer, request, preserve_special=False)
¶
Derive (skip_special_tokens, spaces_between_special_tokens) the way
the engine does (IncrementalDetokenizer).
Parameters:
-
(tokenizer¶TokenizerLike) –Picks the engine's fast or slow detokenizer rules.
-
(request¶ChatCompletionRequest | CompletionRequest | None) –The original request, if the caller supplied one.
-
(preserve_special¶bool, default:False) –Keep special tokens so a parser can see markers. The serving side does this via
adjust_request.
Source code in vllm/renderers/online_derenderer.py
_logprob_context_tail(context_token_ids, delta_token_ids)
¶
Advance the carried logprob context by this chunk's sampled tokens.
Source code in vllm/renderers/online_derenderer.py
_parse_token_id_placeholder(token)
¶
Extract token ID from a 'token_id:N' placeholder string.
Source code in vllm/renderers/online_derenderer.py
_resolve_logprobs(logprobs, tokenizer, initial_context_token_ids=())
¶
Resolve token_id:N placeholders in a ChatCompletionLogProbs object.
initial_context_token_ids seeds the byte-fallback correction context
with sampled IDs from preceding chunks (streaming), so multi-byte
characters split across chunk boundaries still resolve.
Source code in vllm/renderers/online_derenderer.py
_seed_stream_state(tokenizer, prompt_token_ids, skip_special_tokens)
¶
Build the initial decode state from the prompt tail, the same way the engine primes its incremental detokenizer. Without it, Metaspace tokenizers drop the first output token's leading space.
Returns an empty state when prompt_token_ids is empty or omitted.