Skip to content

[Mistral Grammar] Support Grammar Factory#38150

Merged
robertgshaw2-redhat merged 11 commits into
vllm-project:mainfrom
juliendenize:improve_mistral_parsing
Apr 6, 2026
Merged

[Mistral Grammar] Support Grammar Factory#38150
robertgshaw2-redhat merged 11 commits into
vllm-project:mainfrom
juliendenize:improve_mistral_parsing

Conversation

@juliendenize

@juliendenize juliendenize commented Mar 25, 2026

Copy link
Copy Markdown
Contributor

Purpose

This PR adds support to the Mistral grammar factory that creates lark grammar based on tools, tool_choice, structured_outputs and reasoning.
To do that it adds the following:

  • Guidance backend now support MistralTokenizer
  • MistralTokenizer can cache a mistral-common GrammarFactory
  • MistralTokenizer can create and cache a llguidance tokenizer
  • MistralToolParser for tokenizers that support grammar (tokenizer version >= v11 and Tekkenizer) adjust the request to add the lark grammar.

For this PR to work, mistral-common must be installed from last release that was bumped.

The Grammar factory details can be found in this mistral-common PR description regarding how tool choices and reasoning influence the grammar.

This PR will break the tool call parsing for some requests that expect a json grammar (such as tool_choice="required"). A follow-up PR will be submitted to ensure this is fixed.

This is the first PR that aims to replicate some features from the #37081 while ensuring better separation of concerns and that mistral-common hosts the grammar factory logic.

Test Plan

Added tests to:

  • ensure the connection between vLLM and mistral-common factory
  • ensure the guidance backend can use a tokenizer instantiated from a MistralTokenizer.

Test Result

They all pass.


Essential Elements of an Effective PR Description Checklist
  • The purpose of the PR, such as "Fix some issue (link existing issues this PR will resolve)".
  • The test plan, such as providing test command.
  • The test results, such as pasting the results comparison before and after, or e2e results
  • (Optional) The necessary documentation update, such as updating supported_models.md and examples for a new model.
  • (Optional) Release notes update. If your change is user facing, please update the release notes draft in the Google Doc.

BEFORE SUBMITTING, PLEASE READ https://docs.vllm.ai/en/latest/contributing (anything written below this line will be removed by GitHub Actions)

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request integrates llguidance and mistral-common's GrammarFactory into vLLM's Mistral tokenizer and tool parsing logic. The MistralTokenizer now exposes properties to check grammar support and access GrammarFactory and llguidance.LLTokenizer instances. The MistralToolParser's adjust_request method is enhanced to dynamically generate Lark grammars for tool calling based on request parameters for supported Mistral tokenizers. Additionally, structured output validation in sampling_params.py is refined to allow Tekken Mistral tokenizers with the guidance backend, while explicitly disallowing non-Tekken Mistral tokenizers. New tests have been added to cover this new functionality. There is no feedback to provide on the review comments as none were given.

@mergify

mergify Bot commented Mar 26, 2026

Copy link
Copy Markdown
Contributor

This pull request has merge conflicts that must be resolved before it can be
merged. Please rebase the PR, @juliendenize.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify Bot added the needs-rebase label Mar 26, 2026

@bbrowning bbrowning left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is a draft, but I took a first pass at this anyway and have a few inline comments. All of these items may already be planned to handle as work progresses, so feel free to discard if they're already on your radar.

Comment thread vllm/sampling_params.py
Comment thread vllm/sampling_params.py
Comment thread vllm/tool_parsers/mistral_tool_parser.py
Comment thread vllm/tokenizers/mistral.py
Comment thread vllm/tool_parsers/mistral_tool_parser.py
or request.structured_outputs is not None
or (
request.response_format is not None
and request.response_format.type != "text"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be a bit broad. For example, if the response_format was set to json_object would we still want to enable the grammar-based guiding? Or does the model not output JSON for its response in that case?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in this case we just ignore and leave the grammar to vLLM but we're indeed considering injecting our lark grammar there as well as we can handle json_schema with or without tool choice set (the model choses between calling a tool or creating a json)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good - if it's scope creep, feel free to go with what you were originally going to do and we can iterate on this in future PRs.

@juliendenize juliendenize force-pushed the improve_mistral_parsing branch from a9eaa24 to 1324904 Compare March 26, 2026 17:21
@mergify mergify Bot removed the needs-rebase label Mar 26, 2026
@bbrowning

Copy link
Copy Markdown
Collaborator

I reviewed the latest updates, and they look good to me. Once this is ready to come out of draft state with the updated mistral_common dependency I'm happy to take another look but we'll need an approver in this area ready to review as well to get this merged quickly.

@mergify

mergify Bot commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

This pull request has merge conflicts that must be resolved before it can be
merged. Please rebase the PR, @juliendenize.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify

mergify Bot commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

Hi @juliendenize, the pre-commit checks have failed. Please run:

uv pip install pre-commit>=4.5.1
pre-commit install
pre-commit run --all-files

Then, commit the changes and push to your branch.

For future commits, pre-commit will run automatically on changed files before each commit.

Tip

Is mypy failing?
mypy is run differently in CI. If the failure is related to this check, please use the following command to run it locally:
# For mypy (substitute "3.10" with the failing version if needed)
pre-commit run --hook-stage manual mypy-3.10

@mergify mergify Bot added the ci/build label Apr 1, 2026
@juliendenize juliendenize force-pushed the improve_mistral_parsing branch from e747e48 to 5c0f3b2 Compare April 2, 2026 15:24

@sfeng33 sfeng33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@robertgshaw2-redhat robertgshaw2-redhat merged commit fef56c1 into vllm-project:main Apr 6, 2026
137 of 140 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in AMD Apr 6, 2026
HenryTangDev pushed a commit to HenryTangMain/vllm that referenced this pull request Apr 6, 2026
Signed-off-by: juliendenize <julien.denize@mistral.ai>
puririshi98 pushed a commit to puririshi98/vllm that referenced this pull request Apr 7, 2026
Signed-off-by: juliendenize <julien.denize@mistral.ai>
Signed-off-by: Rishi Puri <riship@nvidia.com>
mtparet pushed a commit to blackfuel-ai/vllm that referenced this pull request Apr 9, 2026
Signed-off-by: juliendenize <julien.denize@mistral.ai>
@juliendenize juliendenize mentioned this pull request Apr 10, 2026
3 tasks
mystous pushed a commit to mystous/vllm_hybrid that referenced this pull request May 10, 2026
Signed-off-by: juliendenize <julien.denize@mistral.ai>
my-other-github-account pushed a commit to my-other-github-account/vllm that referenced this pull request May 15, 2026
Signed-off-by: juliendenize <julien.denize@mistral.ai>
my-other-github-account pushed a commit to my-other-github-account/vllm that referenced this pull request May 15, 2026
Signed-off-by: juliendenize <julien.denize@mistral.ai>
jhu960213 pushed a commit to jhu960213/vllm that referenced this pull request May 20, 2026
Signed-off-by: juliendenize <julien.denize@mistral.ai>
mvanhorn pushed a commit to mvanhorn/vllm that referenced this pull request Jun 4, 2026
Signed-off-by: juliendenize <julien.denize@mistral.ai>
Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci/build ready ONLY add when PR is ready to merge/full CI is needed rocm Related to AMD ROCm structured-output tool-calling v1

Projects

Status: Done
Status: Done
Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants