-
Notifications
You must be signed in to change notification settings - Fork 155
Fix tool calling for Mistral 3 #132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
davidkoski
merged 9 commits into
ml-explore:main
from
shareup:fix-tool-calling-for-mistral3vlm
Mar 10, 2026
+510
−6
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
92abd7d
Support tool calling in Mistral3
atdrendel e5499db
Add Mistral3 tool calling integration tests
atdrendel 5437890
Add support for Mistral3 tool calling
atdrendel 6ee390d
Address pull request comment
atdrendel 74adf25
Address pull request comment
atdrendel 1bb8801
Address pull request comments
atdrendel b7f8cf7
Address pull request comments
atdrendel 6441dcc
Merge remote-tracking branch 'upstream/main' into fix-tool-calling-fo…
atdrendel 8051a45
Fix compilation error
atdrendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
Libraries/MLXLMCommon/Tool/Parsers/MistralToolCallParser.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // Copyright © 2025 Apple Inc. | ||
|
|
||
| import Foundation | ||
|
|
||
| /// Parser for Mistral V13 tool call format: `[TOOL_CALLS]name[ARGS]{"json_args"}` | ||
| /// | ||
| /// This format is used by Mistral3/Ministral-3 2512 models and Devstral 2. | ||
| /// The special tokens `[TOOL_CALLS]` (token ID 9) and `[ARGS]` (ID 32) are used | ||
| /// as delimiters. Multiple tool calls use repeated `[TOOL_CALLS]` tokens. | ||
| /// | ||
| /// Also handles the older V11 format which includes an optional `[CALL_ID]` | ||
| /// between the function name and `[ARGS]` (V13 does not use `[CALL_ID]`). | ||
| /// | ||
| /// Examples: | ||
| /// - `[TOOL_CALLS]get_weather[ARGS]{"location": "Tokyo"}` | ||
| /// - `[TOOL_CALLS]fn1[ARGS]{...}[TOOL_CALLS]fn2[ARGS]{...}` (multiple calls) | ||
| /// | ||
| /// Mistral does not use an end tag — tool calls end at EOS. Since stop tokens | ||
| /// are intercepted at the token ID level before detokenization, tool calls are | ||
| /// extracted via `ToolCallProcessor.processEOS()` at generation end. | ||
| public struct MistralToolCallParser: ToolCallParser, Sendable { | ||
| public let startTag: String? = "[TOOL_CALLS]" | ||
| public let endTag: String? = nil | ||
|
|
||
| public init() {} | ||
|
|
||
| public func parse(content: String, tools: [[String: any Sendable]]?) -> ToolCall? { | ||
| var text = content.trimmingCharacters(in: .whitespacesAndNewlines) | ||
|
|
||
| // Strip wrapper tags only when they appear at boundaries. | ||
| // This keeps literal tag strings inside argument values intact. | ||
| if let start = startTag, text.hasPrefix(start) { | ||
| text = String(text.dropFirst(start.count)) | ||
| text = text.trimmingCharacters(in: .whitespacesAndNewlines) | ||
| } | ||
| // Split on [ARGS] to get function name and arguments | ||
| guard let argsRange = text.range(of: "[ARGS]") else { | ||
| return nil | ||
| } | ||
|
|
||
| var namePart = String(text[..<argsRange.lowerBound]) | ||
| .trimmingCharacters(in: .whitespacesAndNewlines) | ||
| let argsPart = String(text[argsRange.upperBound...]) | ||
| .trimmingCharacters(in: .whitespacesAndNewlines) | ||
|
|
||
| // Handle optional [CALL_ID] between name and [ARGS] | ||
| if let callIdRange = namePart.range(of: "[CALL_ID]") { | ||
| namePart = String(namePart[..<callIdRange.lowerBound]) | ||
| .trimmingCharacters(in: .whitespacesAndNewlines) | ||
| } | ||
|
|
||
| guard !namePart.isEmpty else { return nil } | ||
|
|
||
| // Parse arguments as JSON using tryParseJSON from ParserUtilities | ||
| guard let argsDict = tryParseJSON(argsPart) as? [String: any Sendable] else { | ||
| return nil | ||
| } | ||
|
|
||
| return ToolCall( | ||
| function: ToolCall.Function(name: namePart, arguments: argsDict) | ||
| ) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -329,6 +329,11 @@ public final class VLMModelFactory: ModelFactory { | |
| var mutableConfiguration = configuration | ||
| mutableConfiguration.eosTokenIds = eosTokenIds | ||
|
|
||
| // Auto-detect tool call format from model type if not explicitly set | ||
| if mutableConfiguration.toolCallFormat == nil { | ||
| mutableConfiguration.toolCallFormat = ToolCallFormat.infer(from: baseConfig.modelType) | ||
| } | ||
|
Comment on lines
+332
to
+335
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seemed like an oversight. It should always have been present, as far as I could tell. |
||
|
|
||
| // Load tokenizer, processor config, and weights in parallel using async let. | ||
| // Note: loadProcessorConfig does synchronous I/O but is marked async to enable | ||
| // parallel scheduling. This may briefly block a cooperative thread pool thread, | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes in this file were the initial changes I made. Without these, the Mistral3 models never even received the tools from the caller.