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. -
strip_content_streaming–Strip XTML content wrappers from streaming deltas after reasoning.
Source code in vllm/reasoning/kimi_k3_reasoning_parser.py
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 | |
_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
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
_subseq_index(haystack, needle) ¶
Return start index of the last occurrence of needle in haystack, or -1.