vllm_mlx.tool_parsers.mistral_tool_parser¶
Mistral tool call parser for vllm-mlx.
View the complete module source at #L1-L512.
API details¶
Each callable below includes its exact signature, type annotations, inputs, defaults, return contract, documented exceptions, implementation source, and parsed docstring sections when the source provides them.
vllm_mlx.tool_parsers.mistral_tool_parser
¶
Mistral tool call parser for vllm-mlx.
Handles Mistral's tool calling format:
- Format: [TOOL_CALLS] [{"name": "func", "arguments": {...}}]
- Or newer: [TOOL_CALLS]func_name{"arg": "value"}
- Or newest (Ministral 3, Devstral Small 2, Dec 2025 tokenizers):
[TOOL_CALLS]func_name[ARGS]{"arg": "value"}
Confirmed directly in these models' chat_template.jinja:
{{- '[TOOL_CALLS]' + tool['function']['name'] + '[ARGS]' + arguments }}
Used with models like Mistral-7B-Instruct, Devstral, Ministral 3, etc.
vllm_mlx.tool_parsers.mistral_tool_parser.ALPHANUMERIC
module-attribute
¶
vllm_mlx.tool_parsers.mistral_tool_parser._TOOL_NAME_PATTERN
module-attribute
¶
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser
¶
Bases: ToolParser
Tool call parser for Mistral models.
Supports both old and new Mistral tool call formats:
- Old (< v11): [TOOL_CALLS] [{"name": "add", "arguments": {"a": 1, "b": 2}}]
- New (>= v11): [TOOL_CALLS]add{"a": 1, "b": 2}
Used when --enable-auto-tool-choice --tool-call-parser mistral are set.
Source code in vllm_mlx/tool_parsers/mistral_tool_parser.py
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser.SUPPORTS_NATIVE_TOOL_FORMAT
class-attribute
instance-attribute
¶
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser.BOT_TOKEN
class-attribute
instance-attribute
¶
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser.ARGS_TOKEN
class-attribute
instance-attribute
¶
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser.TOOL_CALL_REGEX
class-attribute
instance-attribute
¶
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser._NAME_BUFFER_LIMIT
class-attribute
instance-attribute
¶
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser.bot_token_id
instance-attribute
¶
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser._args_started
instance-attribute
¶
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser._args_in_string
instance-attribute
¶
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser._args_escaped
instance-attribute
¶
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser._name_buffer
instance-attribute
¶
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser._name_buffer_overflow
instance-attribute
¶
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser._current_tool_call_id
instance-attribute
¶
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser._tool_call_id_emitted
instance-attribute
¶
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser.reset
¶
Reset shared and Mistral-specific streaming tool-call state.
Source code in vllm_mlx/tool_parsers/mistral_tool_parser.py
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser._start_new_tool_call
¶
Begin a new streaming tool call: bump the index and reset the per-call name/arguments and id state.
Source code in vllm_mlx/tool_parsers/mistral_tool_parser.py
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser._scan_args_for_new_call
¶
Scan an argument delta, updating the persistent JSON string state, and return the position of the first [TOOL_CALLS] marker that sits outside a string (a new call), or -1 when there is none.
Quote state is carried across deltas so a marker inside a quoted
value (e.g. {"city": "[TOOL_CALLS]rm"}) stays argument data while
a marker between two calls opens the next index.
Source code in vllm_mlx/tool_parsers/mistral_tool_parser.py
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser._split_on_tool_call_markers
¶
Split on [TOOL_CALLS] occurrences that are outside JSON strings.
A marker appearing inside a quoted string value is argument data, not a new call — splitting there would let untrusted model output forge a second dispatchable call.
The quote-state scan starts at the first marker, not at index 0: the
text before the first marker is prose, not JSON, so an odd number of
double quotes there must not leave in_string set when the marker
arrives (that would hide the call entirely).
Source code in vllm_mlx/tool_parsers/mistral_tool_parser.py
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser.extract_tool_calls
¶
extract_tool_calls(model_output: str, request: dict[str, Any] | None = None) -> ExtractedToolCallInformation
Extract tool calls from a complete Mistral model response.
Parameters:
-
model_output(str) –The complete model output string
-
request(dict[str, Any] | None, default:None) –Optional request context
Returns:
-
ExtractedToolCallInformation–ExtractedToolCallInformation with parsed tool calls
Source code in vllm_mlx/tool_parsers/mistral_tool_parser.py
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 | |
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser.extract_tool_calls_streaming
¶
extract_tool_calls_streaming(previous_text: str, current_text: str, delta_text: str, previous_token_ids: Sequence[int] | None = None, current_token_ids: Sequence[int] | None = None, delta_token_ids: Sequence[int] | None = None, request: dict[str, Any] | None = None) -> dict[str, Any] | None
Extract tool calls from streaming Mistral model output.
For streaming, we detect when [TOOL_CALLS] appears and start accumulating tool call data.
Source code in vllm_mlx/tool_parsers/mistral_tool_parser.py
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 | |
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser._parse_streaming_tool_delta
¶
Parse a streaming delta for tool call information.
Once the name/arguments boundary (the [ARGS] marker, or a bare {
for older checkpoints) has been seen for the current tool call, every
subsequent delta is argument text and is never re-classified — JSON
string content (bare keys/values like city or Paris) has no
distinguishing leading punctuation, so re-evaluating each delta in
isolation (the previous approach) misclassified mid-argument
fragments as more of the function name.
Source code in vllm_mlx/tool_parsers/mistral_tool_parser.py
vllm_mlx.tool_parsers.mistral_tool_parser.generate_mistral_tool_id
¶
Generate a random Mistral-compatible tool call ID.
Mistral Tool Call IDs must be alphanumeric with a length of 9.
vllm_mlx.tool_parsers.mistral_tool_parser._is_plain_tool_name
¶
Complete contract reference¶
Expand any definition for its exact inputs, annotations, defaults, return contract, directly raised exceptions, source-grounded behavior, and immutable line link. This section includes private and nested definitions that ordinary API generators omit.
vllm_mlx.tool_parsers.mistral_tool_parser.generate_mistral_tool_id · function
Generate a random Mistral-compatible tool call ID.
Parameters
This callable has no explicit inputs.
Returns
- Type:
str - Direct return expressions:
''.join(choices(ALPHANUMERIC, k=9))
Exceptions and behavior
Function generate_mistral_tool_id calls ''.join, choices; returns ''.join(choices(ALPHANUMERIC, k=9)).
No direct raise statement appears in this definition.
vllm_mlx.tool_parsers.mistral_tool_parser._is_plain_tool_name · function
Return True for names that are safe to dispatch as function calls.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
name |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
bool - Direct return expressions:
bool(_TOOL_NAME_PATTERN.match(name))
Exceptions and behavior
Function _is_plain_tool_name calls bool, _TOOL_NAME_PATTERN.match; returns bool(_TOOL_NAME_PATTERN.match(name)).
No direct raise statement appears in this definition.
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser · class
Tool call parser for Mistral models.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
tokenizer |
not annotated |
no |
None |
Optional positional or keyword input; defaults to None. |
Returns
- Constructs:
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser
Exceptions and behavior
Class MistralToolParser derives from ToolParser and declares 8 direct member(s).
No direct raise statement appears in this definition.
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser.__init__ · method
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser.__init__(tokenizer = None) -> not annotated
Method MistralToolParser.__init__ updates self.bot_token_id, self._args_started, self._args_in_string, self._args_escaped; calls super().__init__, super, self.vocab.get.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
tokenizer |
not annotated |
no |
None |
Optional positional or keyword input; defaults to None. |
Returns
- Type:
not annotated
Exceptions and behavior
Method MistralToolParser.__init__ updates self.bot_token_id, self._args_started, self._args_in_string, self._args_escaped; calls super().__init__, super, self.vocab.get.
No direct raise statement appears in this definition.
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser.reset · method
Reset shared and Mistral-specific streaming tool-call state.
Parameters
This callable has no explicit inputs.
Returns
- Type:
None
Exceptions and behavior
Method MistralToolParser.reset updates self._args_started, self._args_in_string, self._args_escaped, self._name_buffer; calls super().reset, super.
No direct raise statement appears in this definition.
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser._start_new_tool_call · method
Begin a new streaming tool call: bump the index and reset the per-call name/arguments and id state.
Parameters
This callable has no explicit inputs.
Returns
- Type:
None
Exceptions and behavior
Method MistralToolParser._start_new_tool_call updates self.current_tool_id, self._args_started, self._args_in_string, self._args_escaped; calls generate_mistral_tool_id.
No direct raise statement appears in this definition.
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser._scan_args_for_new_call · method
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser._scan_args_for_new_call(text: str) -> int
Scan an argument delta, updating the persistent JSON string state, and return the position of the first [TOOL_CALLS] marker that sits outside a string (a new call), or -1 when there is none.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
text |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
int - Direct return expressions:
i;-1
Exceptions and behavior
Method MistralToolParser._scan_args_for_new_call updates self._args_escaped, self._args_in_string; calls len, text.startswith; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser._split_on_tool_call_markers · method
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser._split_on_tool_call_markers(text: str) -> list[str]
Split on [TOOL_CALLS] occurrences that are outside JSON strings.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
text |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
list[str] - Direct return expressions:
[text];parts
Exceptions and behavior
Method MistralToolParser._split_on_tool_call_markers calls text.find, len, text.startswith, parts.append; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser.extract_tool_calls · method
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser.extract_tool_calls(model_output: str, request: dict[str, Any] | None = None) -> ExtractedToolCallInformation
Extract tool calls from a complete Mistral model response.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
model_output |
str |
yes |
none |
The complete model output string |
request |
dict[str, Any] \| None |
no |
None |
Optional request context |
Returns
- Type:
ExtractedToolCallInformation - Direct return expressions:
ExtractedToolCallInformation(tools_called=False, tool_calls=[], content=model_output);ExtractedToolCallInformation(tools_called=True, tool_calls=tool_calls, content=content if content else None)
Exceptions and behavior
Method MistralToolParser.extract_tool_calls calls ExtractedToolCallInformation, self._split_on_tool_call_markers, content_and_raw_tool_calls[0].strip, raw_tool_call.strip; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser.extract_tool_calls_streaming · method
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser.extract_tool_calls_streaming(previous_text: str, current_text: str, delta_text: str, previous_token_ids: Sequence[int] | None = None, current_token_ids: Sequence[int] | None = None, delta_token_ids: Sequence[int] | None = None, request: dict[str, Any] | None = None) -> dict[str, Any] | None
Extract tool calls from streaming Mistral model output.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
previous_text |
str |
yes |
none |
Required positional or keyword input. |
current_text |
str |
yes |
none |
Required positional or keyword input. |
delta_text |
str |
yes |
none |
Required positional or keyword input. |
previous_token_ids |
Sequence[int] \| None |
no |
None |
Optional positional or keyword input; defaults to None. |
current_token_ids |
Sequence[int] \| None |
no |
None |
Optional positional or keyword input; defaults to None. |
delta_token_ids |
Sequence[int] \| None |
no |
None |
Optional positional or keyword input; defaults to None. |
request |
dict[str, Any] \| None |
no |
None |
Optional positional or keyword input; defaults to None. |
Returns
- Type:
dict[str, Any] | None - Direct return expressions:
{'tool_calls': [{'index': self.current_tool_id, 'type': 'function', 'function': {'arguments': delta_text}}]};result if result else None;{'content': delta_text};{'content': tool_delta['content']};{'tool_calls': [tool_call]};None
Exceptions and behavior
Method MistralToolParser.extract_tool_calls_streaming updates self._tool_call_id_emitted; calls self._scan_args_for_new_call, self._start_new_tool_call, self._parse_streaming_tool_delta, len; has 6 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser._parse_streaming_tool_delta · method
vllm_mlx.tool_parsers.mistral_tool_parser.MistralToolParser._parse_streaming_tool_delta(text: str) -> dict[str, str] | None
Parse a streaming delta for tool call information.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
text |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
dict[str, str] | None - Direct return expressions:
None;{'arguments': text};result if result else None;{'content': overflowed}
Exceptions and behavior
Method MistralToolParser._parse_streaming_tool_delta updates self._name_buffer, self._args_started, self._name_buffer_overflow; calls self._name_buffer.find, len, self._name_buffer[:idx].strip; has 4 explicit return paths.
No direct raise statement appears in this definition.
Complete symbol map¶
This map also includes private definitions and nested helpers. The signature column exposes every explicit input even when an internal helper has no dedicated parameter prose.
| Symbol | Kind | Signature and inputs | What it does | Source |
|---|---|---|---|---|
generate_mistral_tool_id |
function | generate_mistral_tool_id() -> str |
Generate a random Mistral-compatible tool call ID. | #L34-L40 |
_is_plain_tool_name |
function | _is_plain_tool_name(name: str) -> bool |
Return True for names that are safe to dispatch as function calls. | #L43-L45 |
MistralToolParser |
class | MistralToolParser(tokenizer = None) |
Tool call parser for Mistral models. | #L49-L512 |
MistralToolParser.__init__ |
method | MistralToolParser.__init__(tokenizer = None) -> not annotated |
Method MistralToolParser.__init__ updates self.bot_token_id, self._args_started, self._args_in_string, self._args_escaped; calls super().__init__, super, self.vocab.get. |
#L68-L90 |
MistralToolParser.reset |
method | MistralToolParser.reset() -> None |
Reset shared and Mistral-specific streaming tool-call state. | #L92-L102 |
MistralToolParser._start_new_tool_call |
method | MistralToolParser._start_new_tool_call() -> None |
Begin a new streaming tool call: bump the index and reset the per-call name/arguments and id state. | #L104-L114 |
MistralToolParser._scan_args_for_new_call |
method | MistralToolParser._scan_args_for_new_call(text: str) -> int |
Scan an argument delta, updating the persistent JSON string state, and return the position of the first [TOOL_CALLS] marker that sits outside a string (a new call), or -1 when there is none. | #L116-L146 |
MistralToolParser._split_on_tool_call_markers |
method | MistralToolParser._split_on_tool_call_markers(text: str) -> list[str] |
Split on [TOOL_CALLS] occurrences that are outside JSON strings. | #L148-L192 |
MistralToolParser.extract_tool_calls |
method | MistralToolParser.extract_tool_calls(model_output: str, request: dict[str, Any] \| None = None) -> ExtractedToolCallInformation |
Extract tool calls from a complete Mistral model response. | #L194-L332 |
MistralToolParser.extract_tool_calls_streaming |
method | MistralToolParser.extract_tool_calls_streaming(previous_text: str, current_text: str, delta_text: str, previous_token_ids: Sequence[int] \| None = None, current_token_ids: Sequence[int] \| None = None, delta_token_ids: Sequence[int] \| None = None, request: dict[str, Any] \| None = None) -> dict[str, Any] \| None |
Extract tool calls from streaming Mistral model output. | #L334-L453 |
MistralToolParser._parse_streaming_tool_delta |
method | MistralToolParser._parse_streaming_tool_delta(text: str) -> dict[str, str] \| None |
Parse a streaming delta for tool call information. | #L455-L512 |