Repository navigation
fix(studio): recover MLX VLM image prompts - #7094
Conversation
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
💡 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".
There was a problem hiding this comment.
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.
_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.
|
Pushed a small hardening to Behavior is unchanged for the healthy and already-registered paths: the first config that carries a |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
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
image,image_url, andinput_imagealiases recursively and require the structured media count to match the attached images.configor_config.mlx_vlm.prompt_utils.apply_chat_template()only when that model type is present in mlx-vlm'sMODEL_CONFIGregistry.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...HEADThe branch was rebased onto current
mainbefore validation. No dependency or lockfile changes are included.Real-model validation performed during implementation confirmed:
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.