vllm.reasoning.kimi_k3_reasoning_parser ¶
Reasoning parser for the Kimi K3 (XTML) chat format.
This strips the think channel out of generated text and hands the remainder (response + tools channels) downstream. Kimi K3 wraps the thinking channel as an XTML element built from special tokens::
<|open|>think<|sep|> <reasoning> <|close|>think<|sep|>
Two subtleties drive the implementation
- Unlike Kimi-K2 (a single
<think>token), each K3 marker is a 3-token sequence, so the token-id helpers search for the marker subsequence rather than a single id. - In thinking mode the serving layer may feed
<|open|>think<|sep|>as the generation prefix, so the model's output can begin inside the think channel with no open marker. The text paths therefore treat a missing open marker as "reasoning starts at offset 0".
When thinking is disabled (chat_template_kwargs={"thinking": False} or {"enable_thinking": False}, i.e. instruct mode) the parser returns every delta as normal content; there is simply no think channel to extract.
Classes:
-
KimiK3ReasoningParser–Reasoning parser for the Kimi K3 (XTML) think channel.
KimiK3ReasoningParser ¶
Bases: ReasoningParser
Reasoning parser for the Kimi K3 (XTML) think channel.
Methods:
-
extract_reasoning–Split full text into
(reasoning, rest)for the non-streaming path. -
is_reasoning_end_streaming–Reasoning-end check for a single decode step.
-
strip_content_streaming–Strip XTML content wrappers from streaming deltas after reasoning.
Source code in vllm/reasoning/kimi_k3_reasoning_parser.py
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 | |
_content_ready_to_emit(text) ¶
Return the content prefix that is safe to stream now.
Mirrors _reasoning_text_ready_to_emit but for the post-reasoning content phase. Strips the <|open|>response<|sep|> prefix, holds back any partial marker suffix, and removes complete <|close|>response<|sep|> / <|close|>message<|sep|> markers.
Source code in vllm/reasoning/kimi_k3_reasoning_parser.py
_reasoning_text_ready_to_emit(text) ¶
Return the reasoning prefix that is safe to stream now.
Work from accumulated text, not from the current delta alone. That turns split-open and split-close handling into the same prefix-diff problem.
split think-open marker.
chunks: <|open|> / think / <|sep|>reasoning current text after chunk 1: <|open|> -> emit "" current text after chunk 2: <|open|>think -> emit "" current text after chunk 3: <|open|>think<|sep|>reasoning -> emit reasoning
split think-close marker.
chunks: reasoning / <|close|> / think<|sep|>... after <|close|>, the suffix is only a partial close marker, so the sendable reasoning is still reasoning and the delta is empty. Once think<|sep|> arrives, the close branch hands the following response or tools text to the downstream parser.
Source code in vllm/reasoning/kimi_k3_reasoning_parser.py
_strip_content_wrapper(text) ¶
Strip <|open|>response<|sep|>…<|close|>response<|sep|> wrapper and <|close|>message<|sep|> from text.
When Kimi K3 tool parsing is active it needs the raw XTML response + tools channels. Otherwise the reasoning parser cleans up the response wrapper itself so API users do not see XTML markers.
Source code in vllm/reasoning/kimi_k3_reasoning_parser.py
extract_reasoning(model_output, request) ¶
Split full text into (reasoning, rest) for the non-streaming path.
Handles three shapes
- no think channel at all ->
(None, model_output)(all content) - open marker present -> reasoning starts after
<|open|>think<|sep|> - open marker absent but a close marker exists (gen-prefix consumed the open) -> reasoning starts at offset 0
rest is whatever follows the close marker, fed on to the tool parser.
Source code in vllm/reasoning/kimi_k3_reasoning_parser.py
is_reasoning_end_streaming(input_ids, delta_ids) ¶
Reasoning-end check for a single decode step.
The engine calls this once per structured-output request per decode step while the request is still inside the think channel, so it only has to look at the tokens generated this step, plus len(marker) - 1 tokens of context in case a marker straddles the step boundary.
The inherited default re-runs the full-sequence is_reasoning_end, which makes the scheduler O(context) per request per step and starves the GPU on long agentic contexts.
Source code in vllm/reasoning/kimi_k3_reasoning_parser.py
strip_content_streaming(previous_text, current_text) ¶
Strip XTML content wrappers from streaming deltas after reasoning.
Called by KimiK3Parser when no tool parser is configured, so the reasoning parser handles <|open|>response<|sep|> / <|close|>response<|sep|> / <|close|>message<|sep|> stripping itself.
Works from accumulated text (previous_text / current_text already contain only post-reasoning content).
Source code in vllm/reasoning/kimi_k3_reasoning_parser.py
_match_at(haystack, i, needle) ¶
Whether needle occurs in haystack starting at i.
Compares element by element rather than slicing: haystack is usually a ConstantList, whose slices cost a Python __getitem__ plus a list allocation on every probe.
Source code in vllm/reasoning/kimi_k3_reasoning_parser.py
_newest_marker(haystack, a, b) ¶
Report which of a / b occurs last in haystack.
Returns 0 if a is the newest marker, 1 if b is, -1 if neither occurs. Equivalent to comparing two _subseq_index results, but a single backward pass that stops at the first hit instead of walking to index 0 twice.
Source code in vllm/reasoning/kimi_k3_reasoning_parser.py
_subseq_index(haystack, needle) ¶
Return start index of the last occurrence of needle in haystack, or -1.