vllm.parser.chat_parsing
¶
Response parsing: convert model-emitted text into the assistant-message
dict used by chat templates, driven by a declarative response_template spec.
Modules:
-
content_parsers–This file contains the parsers used by chat response parsing. Each parser takes a chunk of captured text
-
response_parser– -
response_templates–Template loading and validation for response_template dicts.
Classes:
-
ResponseParser–This class implements a streaming parser with a
response_template. If you don't need streaming and
Functions:
-
parse_response–The main function for response parsing when you don't want streaming. Takes generated output
ResponseParser
¶
This class implements a streaming parser with a response_template. If you don't need streaming and
just want to parse a complete message, use the parse_response function above. Streaming parsing emits
events indicating when regions (message fields) are opened and closed, with the model writing to the region
that is currently open.
Usage
parser = ResponseParser(response_template, prefix=chat_prompt) for event in parser.initial_events: handle(event) for chunk in model_text_stream: for event in parser.feed(chunk): handle(event) message, final_events = parser.finalize() for event in final_events: handle(event)
Pass OpenAI-style tools= dictionaries to cast tool-call arguments
using the calling tool's JSON schema as each region closes.
Events can be "region_open", "region_chunk", "region_close", or "region_malformed".
Open, close and malformed events carry start and end, the span of input_text the
event consumed: its opening or closing delimiter, empty for implicit boundaries.
Explicit opens also carry the opener's named captures. A region whose value fails to
parse ends with "region_malformed" instead of "region_close", carrying the error, and
parsing continues after it.
ResponseParser requires the chat prefix (i.e. the chat history, the prefill before the current generation).
This is because chat templates or assistant prefills can sometimes write part of the message, and if we
only see the model output, and not the template, then we can't reliably parse the message in those cases.
Any events produced while consuming the prefix are exposed as initial_events, so renderers can show
prefill regions before the model writes anything; closed prefill regions also land in the output dict.
Methods:
-
feed–Feeds more text/tokens from the model output into the tokenizer, and returns any events that result
-
finalize–Close the stream and return the final message dict together with
Attributes:
-
input_text(str) –Raw parser input after start-anchor truncation. Before the first
feed(), this is
Source code in vllm/parser/chat_parsing/response_parser.py
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 | |
input_text
property
¶
Raw parser input after start-anchor truncation. Before the first feed(), this is
exactly the truncated prefix, so its length marks where generated text begins.
_accumulate(events, text)
¶
Route text into the currently active region. When the current
region is the null sink (no implicit declared, no explicit open), we
silently discard. Every routed chunk emits a region_chunk event so
consumers can render live; dirty=True flags chunks from structured
parsers (json, xml-inline, kv-lines) whose raw bytes will only be
parsed into the final value on close.
Source code in vllm/parser/chat_parsing/response_parser.py
_can_grow(kind, field, m)
¶
Whether a complete match ending at the current buffer edge could still
change as more input arrives -- in which case we defer rather than commit. A
match ending before the edge has already seen its terminating byte and is
final. At the edge: zero-width matches ($ / \Z) are only real at true
EOS; a fully-present literal that no other literal in its set extends cannot
grow (the fast path that keeps literal delimiters zero-latency); anything
else (regex delimiters, prefix-overlapping literal lists) might.
Source code in vllm/parser/chat_parsing/response_parser.py
_close_current(events, close_start, close_end)
¶
Close the current region and reset to the implicit/null region. Skipped (aside from the reset) when the current region never opened -- avoids vacuous open/close pairs at every explicit boundary.
Source code in vllm/parser/chat_parsing/response_parser.py
_consume_prefix(prefix)
¶
Loads the prefix (the chat prefill sent to the model), right-truncates it to the start of the
assistant message (as determined by start_anchor) and then runs the remainder through the parser.
Events produced while processing the prefix are stashed on initial_events so callers can replay
them into a renderer before feeding model output.
Think of this as the "get the parser up to speed on the story so far" method.
Source code in vllm/parser/chat_parsing/response_parser.py
_scan(watch, eos)
¶
Single pass over the watched delimiters, using the regex module's
partial matching to decide -- per delimiter -- whether it can be committed
now or must be held. Returns (best, hold_start):
bestis the earliest-starting delimiter we can safely commit now (longest on ties, opens before closes), orNone.hold_startis the leftmost buffer position occupied by a still-pending match: a partial (incomplete) delimiter, or a complete one ending at the buffer edge that more input could still grow. Bytes before it are safe to emit; bytes from it onward must be held. It stayslen(self._buffer)when nothing is pending, letting the caller flush the whole buffer.
A complete match is committable only if it starts strictly before
hold_start -- otherwise an earlier (or co-located) pending delimiter could
turn out to be the real one. At EOS nothing can grow, so partial matching is
skipped and every complete match is committable.
(The regex module always reports the empty string as a live prefix, so a
partial search with no real match returns a zero-width match at the buffer
end; that lands in the pending branch with start == len(self._buffer), a
no-op for hold_start.)
Source code in vllm/parser/chat_parsing/response_parser.py
_watchlist()
¶
Patterns we care about right now: the close of the currently-open explicit region, or -- if we're in the implicit/null region -- every explicit open plus the implicit's own close (if any).
Source code in vllm/parser/chat_parsing/response_parser.py
feed(text)
¶
Feeds more text/tokens from the model output into the tokenizer, and returns any events that result (regions entered or left). This is the method you want to call after each generation step.
Source code in vllm/parser/chat_parsing/response_parser.py
finalize()
¶
Close the stream and return the final message dict together with any finalization events. This is necessary because some regions may only end at the end of the sequence, so you won't see the event telling you they're ready until the sequence is finalized.
Source code in vllm/parser/chat_parsing/response_parser.py
parse_response(text, response_template, *, prefix=None, tools=None)
¶
The main function for response parsing when you don't want streaming. Takes generated output and the prompt prefix and parses them without streaming any events, then returns the parsed message.
Pass OpenAI-style tools dictionaries to cast tool-call arguments
using the calling tool's JSON schema.