Commit 509e9b7
committed
Codec: token-native binary transport for OpenAI completions + chat streaming
Adds a binary streaming wire format (MessagePack and length-prefixed
Protocol Buffers) for /v1/completions and /v1/chat/completions. Opt-in
via the request body field `stream_format`. Default behaviour is
byte-identical to current vllm.
## Why
Real cross-stack measurements (run 2026-05-15T20-00-00Z, 2K-token
completion on Qwen2.5-0.5B-Instruct; full matrix at
https://github.com/wdunn001/Codec/blob/main/packages/bench/results/2026-05-15T20-00-00Z/MATRIX.md):
| Engine | JSON-SSE | Best Codec | Reduction |
| sglang | 485.2 KB | 291 B (msgpack + dict-zstd) | 1,707× |
| **vllm** | 517.8 KB | 3.9 KB (msgpack + gzip) | **137×** |
| llama.cpp | 528.8 KB | 140 B (msgpack + dict-zstd, fp16)| 3,868× |
vllm's 137× headline is gzip-only because the model's output at temp=0
is content-bound, not protocol-bound — when a dict-zstd path is wired
in (Codec v0.5 adds discoverable .well-known/codec/dicts/), vllm
moves to the same multi-thousand-× range as sglang + llama.cpp.
The savings come from:
1. Not detokenizing at the serving server (no UTF-8 / JSON envelope
per chunk).
2. Letting the client opt into HTTP-level compression (gzip / br /
zstd-with-dict) on the binary stream.
3. Skipping the re-tokenize round-trip in agent-to-agent and
tool-dispatch hops — the consumer reads uint32 IDs directly.
## What this PR adds (additive only)
13 files, +2,032 / -16 lines. All under `vllm/entrypoints/`.
New modules:
- `codec_frame.py` — MessagePack + Protocol Buffers encoders for
CodecFrame{ids[], done, finish_reason?, tool_calls?}. Hand-rolled
protobuf (no codegen step). Accepts Union[Sequence[int], np.ndarray
[uint32], array.array('I'), bytes] on ids so future CODEC_OPENAI_BYPASS
work (skip the PyLong-list allocation per token in tokenizer_manager)
can be wired in without further encoder churn.
- `codec_compression.py` — Accept-Encoding negotiation for zstd
(with pre-trained dictionary), br, gzip, identity. Emits
Codec-Zstd-Dict: sha256:<hex> on every zstd response.
- `codec_agent.py` — ToolWatcher: uint32-compare state machine
detecting delimited regions (tool calls, reasoning blocks,
multimodal spans) in the raw token stream without detokenizing.
~100× faster than detokenize+regex.
- `codec_dispatcher.py` — bolt-on tool dispatch
(CODEC_BOLT_ON_DISPATCH=1, default off). Reads tool manifests
from CODEC_TOOL_MANIFEST_URLS at boot, hash-validates each
manifest's tokenizerHash against the active model's tokenizer,
POSTs CodecToolCall (msgpack-framed), reinjects response_ids into
the generation stream.
- `codec_version.py` — protocol-version negotiation (Codec-Client-
Version, Codec-Min-Version headers + 426 Upgrade Required +
VERSION_INCOMPATIBLE frame).
Modified existing files:
- `openai/completion/serving.py` + `openai/chat_completion/serving.py`
— dispatch to binary generator when stream_format != "json";
preserve JSON-SSE path byte-for-byte when unset.
- `openai/completion/protocol.py` + `openai/chat_completion/protocol.py`
— stream_format, tool_watcher, tool_watcher_start, tool_watcher_end
fields on request types.
- `openai/completion/api_router.py` +
`openai/chat_completion/api_router.py` — route registration.
- `openai/server_utils.py` — codec-aware response helpers.
Plus 1 new test file: `test_codec_compression.py`.
## Trust posture / opt-in
- Default behaviour byte-identical to current vllm. Client that
doesn't set stream_format gets JSON-SSE exactly as today.
- No new mandatory dependencies. Wire emit uses msgspec (already in
vllm's reqs); compression uses brotli + zstandard (graceful
fallthrough to identity if missing).
- Engine boot unchanged unless CODEC_BOLT_ON_DISPATCH=1 set;
dispatcher loads manifests lazily.
## Cross-stack reference + spec
- Wire format spec: https://github.com/wdunn001/Codec/blob/main/spec/versions/v0.5.md
- .well-known/codec/ discovery surface: https://github.com/wdunn001/Codec/blob/main/spec/WELL_KNOWN_DISCOVERY.md
- 6 client-language reference implementations (TS / Python / Rust /
.NET / Java / C) consume this wire byte-equally:
https://github.com/wdunn001/Codec/tree/main/packages
- Cross-stack bench: https://github.com/wdunn001/Codec/blob/main/packages/bench/RESULTS.md
Companion PR against sgl-project/sglang filed in parallel
(sgl-project/sglang#25544). The wdunn001/vllm fork has carried this
code in production-grade wdunn001/codec-vllm Docker images for two
releases; cross-client byte-equality is 24/24 cells unanimous as of
v0.4.1.
Signed-off-by: William Dunn <wdunn001@gmail.com>1 parent 966903e commit 509e9b7
13 files changed
Lines changed: 2032 additions & 16 deletions
File tree
- vllm/entrypoints
- openai
- chat_completion
- completion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
0 commit comments