Skip to content

fix(studio): recover MLX VLM image prompts - #7094

Merged
danielhanchen merged 4 commits into
unslothai:mainfrom
Lyxot:fix/mlx-vlm-model-aware-prompts
Jul 15, 2026
Merged

danielhanchen merged 4 commits into
unslothai:mainfrom
Lyxot:fix/mlx-vlm-model-aware-prompts

Conversation

@Lyxot

@Lyxot Lyxot commented Jul 13, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Restore model-aware prompt rendering for MLX vision-language chat when Studio's generic chat-template path serializes structured image content instead of emitting the model's media marker.

This keeps the existing generic renderer as the primary path for healthy processors, including text-only VLM turns, tools, and reasoning controls. Recovery is activated only for malformed or failed image prompt rendering and only when mlx-vlm registers a formatter for the loaded model type.

Root Cause

Studio normally formats MLX chat requests through the shared chat-template helper. Some VLM processors, including DeepSeek-VL2, expect a text-oriented conversation shape and can render an OpenAI-style structured image list as literal Python-like content. The image still reaches mlx-vlm, but the prompt no longer contains the model-specific media token, so preprocessing or generation fails downstream.

Using mlx-vlm's renderer unconditionally would create a different regression: release renderers do not consistently preserve Studio's tool history, tool declarations, or reasoning controls. The fix therefore detects the malformed generic result and performs a capability-gated recovery rather than replacing the normal path.

What Changed

  • Detect empty prompts and extra serialized full content lists or individual image items in Python-repr or Transformers-JSON form, while discounting exact representations quoted in user text.
  • Count image, image_url, and input_image aliases recursively and require the structured media count to match the attached images.
  • Resolve the loaded MLX model type from either config or _config.
  • Recover through mlx_vlm.prompt_utils.apply_chat_template() only when that model type is present in mlx-vlm's MODEL_CONFIG registry.
  • Preserve the healthy generic prompt byte-for-byte when no issue is detected.
  • Preserve text-only VLM behavior and continue forwarding tools and reasoning controls through Studio's shared helper.
  • Refuse lossy recovery when tool-call history, requested tools, or explicit reasoning controls would be dropped.
  • Raise precise prompt-contract errors when no safe registered recovery exists.
  • Reuse existing structured media aliases during Studio image injection instead of inserting a duplicate placeholder.

User Impact

Image chat for affected MLX VLMs now reaches generation with the model's expected image marker. Healthy Qwen, Gemma, Phi, and text-only prompt paths retain their existing rendering behavior. Unsupported combinations fail before model execution with an actionable prompt error instead of a later processor assertion.

Validation

python -m pytest studio/backend/tests/test_mlx_inference_backend.py -q
# 12 passed

python -m py_compile \
  studio/backend/core/inference/mlx_inference.py \
  studio/backend/tests/test_mlx_inference_backend.py

ruff check --select E9,F63,F7,F82 \
  studio/backend/core/inference/mlx_inference.py \
  studio/backend/tests/test_mlx_inference_backend.py

git diff --check origin/main...HEAD

The branch was rebased onto current main before validation. No dependency or lockfile changes are included.

Real-model validation performed during implementation confirmed:

  • DeepSeek-VL2 tiny preprocessing produced model-ready image tensors and completed Studio-shaped generation.
  • Phi retained its model-specific image token dialect.
  • Healthy Qwen and Gemma prompt paths remained on their existing renderer.

Risk

The change is scoped to Studio's MLX VLM path. Recovery requires all of the following: an attached image, an invalid generic prompt, no lossy tool/reasoning state, a loaded model config, and a renderer registered by mlx-vlm for that exact model type. Requests outside that boundary retain existing behavior.

@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 enhances the MLX inference backend's VLM prompt rendering by introducing robust error detection and model-aware recovery mechanisms when chat templates fail or incorrectly serialize structured image content. It also adds comprehensive unit tests to validate these rendering behaviors. The review feedback suggests improving the robustness of _prompt_serializes_vlm_media by recursively checking for serialized individual media/image dicts rather than just the entire content list, preventing potential detection failures with certain templates.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@Lyxot
Lyxot marked this pull request as ready for review July 13, 2026 05:28
@Lyxot
Lyxot requested a review from danielhanchen as a code owner July 13, 2026 05:28
Copilot AI review requested due to automatic review settings July 13, 2026 05:28

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3f54f7ed7c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Copilot AI 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.

Pull request overview

Note

Copilot couldn't run its full agentic review because no GitHub Actions runner was available. Make sure your repository has a runner available to run Copilot's review, or add a copilot-setup-steps.yml file specifying one with the runs-on attribute. See the docs for more details.

Restores model-aware prompt rendering for MLX vision-language chat when Studio’s generic chat-template path produces empty prompts or serializes structured image content, by capability-gating a recovery path through mlx_vlm’s registered model formatter.

Changes:

  • Add VLM prompt validation (empty/serialized-media detection) and gated recovery via mlx_vlm.prompt_utils.apply_chat_template() for registered model types.
  • Enforce structured-image vs attached-image count matching and reuse existing structured media aliases during image injection.
  • Add targeted backend tests covering recovery gating, tool/reasoning lossy-guardrails, and media alias reuse.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
studio/backend/core/inference/mlx_inference.py Adds prompt issue detection, structured image counting, and model-aware recovery rendering for MLX VLM prompts.
studio/backend/tests/test_mlx_inference_backend.py Adds regression tests for renderer selection/recovery behavior and media-alias reuse during image injection.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Lyxot and others added 2 commits July 13, 2026 14:24
_mlx_vlm_model_config only fell back to _config when config was entirely
missing, so a model that exposes a config without a model_type (while _config
carries it) skipped model-aware recovery. Prefer whichever of config / _config
actually has a model_type. Adds a focused test.
@danielhanchen

Copy link
Copy Markdown
Member

Pushed a small hardening to _mlx_vlm_model_config. It previously fell back to _config only when config was entirely absent, so a model that exposes a config without a model_type while _config carries it would skip model-aware recovery and error out on an otherwise recoverable image prompt. It now prefers whichever of config / _config actually has a model_type and hands that one to the renderer.

Behavior is unchanged for the healthy and already-registered paths: the first config that carries a model_type still wins, so nothing regresses for Qwen, Gemma, Phi, or the config-only and _config-only cases you already covered. Added a focused test for the fallback. ruff E9,F63,F7,F82 and the full test_mlx_inference_backend.py suite pass.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: feaa03f03c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 14d0e85 into unslothai:main Jul 15, 2026
37 of 38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants