Skip to content

Conversation

EricLBuehler
Copy link
Owner

@EricLBuehler EricLBuehler commented Jun 6, 2025

Summary

  • consolidate vision and text examples
  • use CLI args for choosing model IDs
  • remove redundant per-model demo folders
  • update documentation links

Testing

  • cargo fmt --all
  • make fmt
  • timeout 60 cargo clippy --workspace --tests --examples -- -D warnings (failed: timed out)
  • timeout 60 cargo test -p mistralrs-core -p mistralrs-quant -p mistralrs-vision (failed: timed out)

https://chatgpt.com/codex/tasks/task_e_68425ab1e4fc832283388c438c1d9a64

Summary by CodeRabbit

  • Documentation

    • Updated documentation links across multiple model guides to reference new example scripts focused on vision chat functionality.
  • New Features

    • Enhanced example scripts to support command-line arguments for model selection and image input, enabling flexible usage for both Python and Rust vision chat examples.
  • Refactor

    • Removed numerous legacy example scripts for specific models, consolidating vision model usage into a single, configurable vision chat example for both Python and Rust.
  • Chores

    • Cleaned up outdated server and client example scripts to streamline and modernize the example set.

Copy link

coderabbitai bot commented Jun 6, 2025

Walkthrough

This update standardizes and modernizes vision model example usage across documentation and code. It removes numerous model-specific example scripts in Python and Rust, replacing them with a single, configurable vision_chat example. Documentation links are updated to reference this unified example. The new scripts accept command-line arguments for model and image selection, enhancing flexibility.

Changes

Files/Paths Change Summary
docs/GEMMA3.md, docs/LLAMA4.md, docs/LLaVA.md, docs/MISTRAL3.md, Updated documentation links to reference vision_chat example scripts instead of model-specific scripts in HTTP server, Rust, Python.
docs/PHI3V.md, docs/PHI4MM.md, docs/QWEN2VL.md, docs/VLLAMA.md, Updated documentation to point to vision_chat.py or vision_chat/main.rs for vision model examples.
docs/VISION_MODELS.md, docs/IDEFICS2.md Updated references to Python/HTTP API examples to use vision_chat.py.
examples/python/gemma3.py, .../deepseekr1.py, .../deepseekv2.py, Deleted model-specific Python example scripts for vision and text models.
.../llama4.py, .../llama_vision.py, .../llava_next.py, .../mistral3.py, .../phi4mm.py, .../qwen2vl.py, .../smolvlm.py
examples/server/gemma3.py, .../llama4.py, .../llama_vision.py, Deleted model-specific OpenAI API client example scripts for vision models.
.../llava.py, .../llava_next.py, .../mistral3.py, .../phi4mm.py, .../qwen2vl.py
mistralrs/examples/gemma2/main.rs, .../deepseekr1/main.rs, Deleted model-specific Rust example programs for text and vision models.
.../deepseekv2/main.rs, .../gemma3/main.rs, .../llama4/main.rs,
.../llama_vision/main.rs, .../llava_next/main.rs, .../mistral3/main.rs, .../phi3v/main.rs, .../phi4mm/main.rs, .../qwen2vl/main.rs, .../smolvlm/main.rs
examples/python/plain.py Refactored to accept model ID and architecture as command-line arguments for flexible model selection.
examples/python/vision_chat.py Refactored to accept model ID, architecture, and image URL as command-line arguments; removed hardcoded values.
examples/server/vision_chat.py Refactored to accept model name and image URL as command-line arguments; simplified response printing.
mistralrs/examples/simple/main.rs Refactored to use clap for command-line parsing; allows dynamic model ID selection.
mistralrs/examples/vision_chat/main.rs Refactored to use clap for command-line parsing; allows dynamic model ID and image URL selection.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CLI Example (vision_chat.py/.rs)
    participant Model Runner (Python/Rust)
    participant Model

    User->>CLI Example: Run with --model-id, --arch, --image-url
    CLI Example->>Model Runner: Initialize with model-id, arch
    CLI Example->>Model Runner: Send chat completion request (with image)
    Model Runner->>Model: Process request
    Model-->>Model Runner: Generate response
    Model Runner-->>CLI Example: Return response
    CLI Example-->>User: Print response content
Loading

Possibly related PRs

  • Add automatic vision loader type #1332: Refactors the internal vision loader system by making the vision loader type optional and adding an automatic vision loader selector; related as both PRs address vision model usage, though at different layers (example/docs vs. core loader).

Poem

In the garden of code, where examples once grew,
The rabbits have tidied, and pruned what they knew.
Now one vision script, with arguments so neat,
Lets every model hop in, no need to repeat!
Documentation points true, the old files now gone—
Unified, flexible, and ready for dawn.
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

github-actions bot commented Jun 6, 2025

Code Metrics Report
===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 C Header                3           62           53            0            9
 CSS                     1          473          408           14           51
 Dockerfile              1           42           23           10            9
 HTML                    1           73           61            4            8
 JavaScript              7         1248          936          174          138
 JSON                   14          123          122            0            1
 Makefile                1            6            5            0            1
 Python                 87         4097         3457          161          479
 Shell                   1           63           26           18           19
 Plain Text              3         3723            0         2413         1310
 TOML                   21          695          634           10           51
 YAML                    2           21           19            2            0
-------------------------------------------------------------------------------
 Jupyter Notebooks       3            0            0            0            0
 |- Markdown             2           77           32           31           14
 |- Python               2          205          178            1           26
 (Total)                            282          210           32           40
-------------------------------------------------------------------------------
 Markdown               60         5211            0         3984         1227
 |- BASH                11          123          117            2            4
 |- JSON                 2           42           42            0            0
 |- Python               7          121          109            0           12
 |- Rust                22          757          634            1          122
 |- TOML                 2           75           63            0           12
 (Total)                           6329          965         3987         1377
-------------------------------------------------------------------------------
 Rust                  376       132481       117880         2913        11688
 |- Markdown           175         3002           29         2662          311
 (Total)                         135483       117909         5575        11999
===============================================================================
 Total                 581       148318       123624         9703        14991
===============================================================================

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 6

🔭 Outside diff range comments (1)
docs/LLAMA4.md (1)

184-184: ⚠️ Potential issue

Fix inconsistent model name in example.

The example shows model="gemma3" in the Llama 4 documentation, which appears to be a copy-paste error from another model's documentation.

Apply this fix:

-        model="gemma3",
+        model="llama4",
🧹 Nitpick comments (1)
docs/VISION_MODELS.md (1)

16-16: Refine conjunction usage in note
The phrase “allow both base64 encoded images as well as a URL/path” is slightly redundant. Consider changing to “allow both base64 encoded images and a URL/path”.

🧰 Tools
🪛 LanguageTool

[grammar] ~16-~16: Probable usage error. Use “and” after ‘both’.
Context: ...es and allow both base64 encoded images as well as a URL/path to the image. There are many...

(BOTH_AS_WELL_AS)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cfd1e89 and edb3446.

📒 Files selected for processing (45)
  • docs/GEMMA3.md (3 hunks)
  • docs/IDEFICS2.md (1 hunks)
  • docs/LLAMA4.md (3 hunks)
  • docs/LLaVA.md (3 hunks)
  • docs/MISTRAL3.md (3 hunks)
  • docs/PHI3V.md (3 hunks)
  • docs/PHI4MM.md (3 hunks)
  • docs/QWEN2VL.md (3 hunks)
  • docs/VISION_MODELS.md (1 hunks)
  • docs/VLLAMA.md (3 hunks)
  • examples/python/deepseekr1.py (0 hunks)
  • examples/python/deepseekv2.py (0 hunks)
  • examples/python/gemma3.py (0 hunks)
  • examples/python/llama4.py (0 hunks)
  • examples/python/llama_vision.py (0 hunks)
  • examples/python/llava_next.py (0 hunks)
  • examples/python/mistral3.py (0 hunks)
  • examples/python/phi4mm.py (0 hunks)
  • examples/python/plain.py (1 hunks)
  • examples/python/qwen2vl.py (0 hunks)
  • examples/python/smolvlm.py (0 hunks)
  • examples/python/vision_chat.py (1 hunks)
  • examples/server/gemma3.py (0 hunks)
  • examples/server/llama4.py (0 hunks)
  • examples/server/llama_vision.py (0 hunks)
  • examples/server/llava.py (0 hunks)
  • examples/server/llava_next.py (0 hunks)
  • examples/server/mistral3.py (0 hunks)
  • examples/server/phi4mm.py (0 hunks)
  • examples/server/qwen2vl.py (0 hunks)
  • examples/server/vision_chat.py (3 hunks)
  • mistralrs/examples/deepseekr1/main.rs (0 hunks)
  • mistralrs/examples/deepseekv2/main.rs (0 hunks)
  • mistralrs/examples/gemma2/main.rs (0 hunks)
  • mistralrs/examples/gemma3/main.rs (0 hunks)
  • mistralrs/examples/llama4/main.rs (0 hunks)
  • mistralrs/examples/llama_vision/main.rs (0 hunks)
  • mistralrs/examples/llava_next/main.rs (0 hunks)
  • mistralrs/examples/mistral3/main.rs (0 hunks)
  • mistralrs/examples/phi3v/main.rs (0 hunks)
  • mistralrs/examples/phi4mm/main.rs (0 hunks)
  • mistralrs/examples/qwen2vl/main.rs (0 hunks)
  • mistralrs/examples/simple/main.rs (1 hunks)
  • mistralrs/examples/smolvlm/main.rs (0 hunks)
  • mistralrs/examples/vision_chat/main.rs (1 hunks)
💤 Files with no reviewable changes (30)
  • examples/python/deepseekr1.py
  • examples/python/mistral3.py
  • mistralrs/examples/qwen2vl/main.rs
  • examples/python/deepseekv2.py
  • mistralrs/examples/llama4/main.rs
  • mistralrs/examples/gemma3/main.rs
  • mistralrs/examples/gemma2/main.rs
  • examples/server/llava_next.py
  • mistralrs/examples/llama_vision/main.rs
  • examples/server/qwen2vl.py
  • mistralrs/examples/llava_next/main.rs
  • examples/python/gemma3.py
  • examples/python/llava_next.py
  • mistralrs/examples/smolvlm/main.rs
  • examples/server/mistral3.py
  • examples/python/smolvlm.py
  • mistralrs/examples/mistral3/main.rs
  • examples/server/phi4mm.py
  • examples/server/llama4.py
  • examples/python/llama4.py
  • examples/python/llama_vision.py
  • examples/python/qwen2vl.py
  • examples/server/gemma3.py
  • examples/server/llava.py
  • mistralrs/examples/phi4mm/main.rs
  • mistralrs/examples/deepseekv2/main.rs
  • examples/server/llama_vision.py
  • examples/python/phi4mm.py
  • mistralrs/examples/deepseekr1/main.rs
  • mistralrs/examples/phi3v/main.rs
🧰 Additional context used
🧬 Code Graph Analysis (1)
examples/python/plain.py (1)
mistralrs-pyo3/src/lib.rs (1)
  • mistralrs (1499-1530)
🪛 LanguageTool
docs/VISION_MODELS.md

[grammar] ~16-~16: Probable usage error. Use “and” after ‘both’.
Context: ...es and allow both base64 encoded images as well as a URL/path to the image. There are many...

(BOTH_AS_WELL_AS)

⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: Clippy
  • GitHub Check: Check (windows-latest, stable)
  • GitHub Check: Check (macOS-latest, stable)
  • GitHub Check: Docs
  • GitHub Check: Check (ubuntu-latest, stable)
  • GitHub Check: Test Suite (ubuntu-latest, stable)
  • GitHub Check: Test Suite (macOS-latest, stable)
  • GitHub Check: Test Suite (windows-latest, stable)
🔇 Additional comments (21)
docs/IDEFICS2.md (1)

140-140: Update Python example link
The documentation now correctly references the consolidated vision_chat.py script for Python examples. Ensure the relative path (../examples/python/vision_chat.py) resolves properly from this doc location.

docs/QWEN2VL.md (3)

61-61: Update HTTP server example link
The HTTP server section now points to vision_chat.py instead of the old qwen2vl.py—this aligns with the unified example approach. Verify the path (../examples/server/vision_chat.py) is correct.


139-139: Update Rust example link
The Rust section link has been updated to vision_chat/main.rs for consistency across model docs. Confirm that ../mistralrs/examples/vision_chat/main.rs exists and is accurate.


187-187: Update Python example link
The Python section now references the generic vision_chat.py. Please check that ../examples/python/vision_chat.py is reachable from this markdown.

docs/GEMMA3.md (3)

15-15: Standardize HTTP server example link
Updated to point at the unified vision_chat.py server example. Validate ../examples/server/vision_chat.py is correct.


100-100: Standardize Rust example link
The Rust example link now goes to vision_chat/main.rs. Ensure ../mistralrs/examples/vision_chat/main.rs exists.


145-145: Standardize Python example link
The Python section references vision_chat.py instead of the old gemma3.py. Confirm the path (../examples/python/vision_chat.py).

docs/PHI3V.md (3)

22-22: Standardize HTTP server example link
Now directs to the consolidated vision_chat.py. Verify ../examples/server/vision_chat.py is accurate.


99-99: Standardize Rust example link
The Rust section link has been updated to vision_chat/main.rs. Please check ../mistralrs/examples/vision_chat/main.rs.


143-143: Standardize Python example link
Updated to use the unified vision_chat.py for Python. Confirm ../examples/python/vision_chat.py resolves correctly.

docs/VLLAMA.md (1)

68-68: Documentation links updated correctly for unified examples.

The documentation links have been properly updated to point to the new consolidated vision_chat examples, which aligns with the PR objectives of standardizing example structure across different models.

Also applies to: 155-155, 201-201

docs/MISTRAL3.md (1)

25-25: Documentation links consistently updated.

The example links have been properly updated to reference the unified vision_chat examples, maintaining consistency with the refactoring effort.

Also applies to: 110-110, 155-155

docs/PHI4MM.md (1)

22-22: Example links updated to reflect unified structure.

The documentation correctly points to the new vision_chat examples, maintaining consistency across all model documentation files.

Also applies to: 97-97, 142-142

docs/LLAMA4.md (1)

27-27: Documentation links updated correctly.

The example links have been properly updated to reference the unified vision_chat examples.

Also applies to: 119-119, 165-165

examples/server/vision_chat.py (2)

1-1: Excellent addition of CLI argument parsing.

The addition of argparse with model and image URL parameters successfully achieves the PR objective of enabling model selection through CLI arguments. The default image URL provides a good fallback.

Also applies to: 32-38


46-46: Dynamic model and image URL configuration implemented correctly.

The use of parsed arguments for model and image URL makes this script flexible and reusable across different vision models.

Also applies to: 51-51

docs/LLaVA.md (1)

27-27: Documentation links updated correctly for unified example structure.

The path updates from llava_next to vision_chat correctly reflect the consolidation of model-specific examples into a unified structure, which aligns with the PR objectives.

Also applies to: 104-104, 149-149

mistralrs/examples/simple/main.rs (1)

8-12: Excellent implementation of CLI argument parsing.

The addition of command-line argument parsing with a sensible default value maintains backwards compatibility while adding flexibility. The use of clap::Parser with derive macros is idiomatic and clean.

Also applies to: 16-17

mistralrs/examples/vision_chat/main.rs (1)

5-14: Well-designed CLI argument structure for vision chat example.

The implementation correctly makes model_id required (since vision models have different capabilities) while providing a sensible default for image_url. The argument usage is consistent and the code remains clean and readable.

Also applies to: 18-19, 25-25

examples/python/vision_chat.py (2)

22-22: Model field assignment looks correct.

Using args.arch.lower() for the model field is appropriate and follows the expected pattern for chat completion requests.


29-29: Dynamic image URL assignment is well implemented.

The use of the command-line argument for the image URL provides good flexibility while maintaining a sensible default value.

"url": "https://www.nhmagazine.com/content/uploads/2019/05/mtwashingtonFranconia-2-19-18-108-Edit-Edit.jpg"
},
},
{"type": "image_url", "image_url": {"url": args.image_url}},
{
"type": "text",
"text": "<|image_1|>\nWhat is shown in this image? Write a detailed response analyzing the scene.",
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider making image token format configurable.

The hardcoded <|image_1|> token format may not be compatible with all vision models. Some models use different image token formats or don't require explicit image tokens.

Consider adding an optional CLI argument for the image token format:

parser.add_argument(
    "--image-url",
    default="https://www.nhmagazine.com/content/uploads/2019/05/mtwashingtonFranconia-2-19-18-108-Edit-Edit.jpg",
)
+parser.add_argument(
+    "--image-token",
+    default="<|image_1|>",
+    help="Image token format for the model (use empty string for no token)"
+)

Then update the text content:

{
    "type": "text",
-    "text": "<|image_1|>\nWhat is shown in this image? Write a detailed response analyzing the scene.",
+    "text": f"{args.image_token}\nWhat is shown in this image? Write a detailed response analyzing the scene." if args.image_token else "What is shown in this image? Write a detailed response analyzing the scene.",
},
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"text": "<|image_1|>\nWhat is shown in this image? Write a detailed response analyzing the scene.",
# --- around your existing parser setup ---
parser.add_argument(
"--image-url",
default="https://www.nhmagazine.com/content/uploads/2019/05/mtwashingtonFranconia-2-19-18-108-Edit-Edit.jpg",
)
parser.add_argument(
"--image-token",
default="<|image_1|>",
help="Image token format for the model (use empty string for no token)"
)
# --- later, where you build the message payload ---
message = {
"type": "text",
"text": (
f"{args.image_token}\nWhat is shown in this image? "
"Write a detailed response analyzing the scene."
)
if args.image_token
else "What is shown in this image? Write a detailed response analyzing the scene.",
}
🤖 Prompt for AI Agents
In examples/server/vision_chat.py at line 54, the image token "<|image_1|>" is
hardcoded, which may not work with all vision models. Add an optional CLI
argument to specify the image token format, then replace the hardcoded token in
the text string with this configurable value to allow flexibility for different
models.

model_id="mistralai/Mistral-7B-Instruct-v0.1",
arch=Architecture.Mistral,
model_id=args.model_id,
arch=Architecture[args.arch],
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Potential KeyError with Architecture enum access.

Direct dictionary-style access to Architecture[args.arch] will raise a KeyError if an invalid architecture name is provided, causing the program to crash ungracefully.

🤖 Prompt for AI Agents
In examples/python/plain.py at line 12, the code uses Architecture[args.arch]
which can raise a KeyError if args.arch is not a valid key. To fix this, add
validation to check if args.arch is a valid member of the Architecture enum
before accessing it, or use a safer method like
Architecture.__members__.get(args.arch) and handle the case where it returns
None by providing an appropriate error message or fallback.

Comment on lines +4 to +7
parser = argparse.ArgumentParser(description="Text model chat example")
parser.add_argument("--model-id", required=True, help="HuggingFace model id")
parser.add_argument("--arch", required=True, help="Architecture name")
args = parser.parse_args()
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add error handling for invalid architecture names.

The script now requires CLI arguments without providing helpful error messages for invalid inputs. Consider adding validation to provide user-friendly feedback.

Add error handling to prevent KeyError when an invalid architecture is provided:

+def get_architecture(arch_name):
+    try:
+        return Architecture[arch_name]
+    except KeyError:
+        available_archs = [attr for attr in dir(Architecture) if not attr.startswith('_')]
+        raise ValueError(f"Invalid architecture '{arch_name}'. Available architectures: {available_archs}")
+
 runner = Runner(
     which=Which.Plain(
         model_id=args.model_id,
-        arch=Architecture[args.arch],
+        arch=get_architecture(args.arch),
     ),
 )
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
parser = argparse.ArgumentParser(description="Text model chat example")
parser.add_argument("--model-id", required=True, help="HuggingFace model id")
parser.add_argument("--arch", required=True, help="Architecture name")
args = parser.parse_args()
def get_architecture(arch_name):
try:
return Architecture[arch_name]
except KeyError:
available_archs = [attr for attr in dir(Architecture) if not attr.startswith('_')]
raise ValueError(f"Invalid architecture '{arch_name}'. Available architectures: {available_archs}")
runner = Runner(
which=Which.Plain(
model_id=args.model_id,
arch=get_architecture(args.arch),
),
)
🤖 Prompt for AI Agents
In examples/python/plain.py around lines 4 to 7, the code parses CLI arguments
for model ID and architecture but lacks validation for the architecture
argument. Add a validation step after parsing arguments to check if the provided
architecture name is valid (e.g., by comparing against a predefined list of
supported architectures). If the architecture is invalid, print a user-friendly
error message and exit the program gracefully to prevent a KeyError later in the
code.

),
)

res = runner.send_chat_completion_request(
ChatCompletionRequest(
model="mistral",
model=args.arch.lower(),
Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Verify that lowercased architecture name is appropriate for model field.

The model field now uses args.arch.lower(), but this assumes all architecture names should be lowercased for the model identifier. This may not be universally correct.


🏁 Script executed:

#!/bin/bash
# Check how model field is used across the codebase to verify naming conventions
rg -A 3 -B 3 "model.*=" examples/ mistralrs/ --type py --type rs

Length of output: 93


🏁 Script executed:

#!/bin/bash
# Display the first 200 lines of the example to inspect how `model` is constructed and used
sed -n '1,200p' examples/python/plain.py

Length of output: 880


🏁 Script executed:

#!/bin/bash
# Display the Architecture enum definition
rg -l "class Architecture" -t py | xargs sed -n '1,200p'

# Display the ChatCompletionRequest class definition
rg -l "class ChatCompletionRequest" -t py | xargs sed -n '1,200p'

Length of output: 122


Use the correct model identifier in ChatCompletionRequest

The model field of ChatCompletionRequest should receive the user-supplied model ID (args.model_id), not the lowercased architecture name, which may not match any valid model.

• File: examples/python/plain.py, line 18

Suggested fix:

-        model=args.arch.lower(),
+        model=args.model_id,
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
model=args.arch.lower(),
model=args.model_id,
🤖 Prompt for AI Agents
In examples/python/plain.py at line 18, the model field is incorrectly set to
args.arch.lower(), which may not correspond to a valid model identifier. Replace
args.arch.lower() with args.model_id to ensure the ChatCompletionRequest
receives the correct user-supplied model ID.

model_id="microsoft/Phi-3.5-vision-instruct",
arch=VisionArchitecture.Phi3V,
model_id=args.model_id,
arch=VisionArchitecture[args.arch],
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Enum conversion is vulnerable to KeyError.

The direct enum access VisionArchitecture[args.arch] will raise a KeyError if an invalid architecture name is provided, causing the script to crash ungracefully.

With the validation added in the previous comment, this line will be safe. Alternatively, you could add error handling:

-        arch=VisionArchitecture[args.arch],
+        arch=getattr(VisionArchitecture, args.arch, None) or VisionArchitecture[args.arch],

However, the argparse validation approach is cleaner and provides better user experience.

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In examples/python/vision_chat.py at line 16, the direct enum conversion using
VisionArchitecture[args.arch] can raise a KeyError if args.arch is invalid. To
fix this, ensure that args.arch is validated beforehand, ideally by adding
choices to the argparse argument definition for arch, so only valid enum names
are accepted. This prevents invalid values from reaching this line and avoids
runtime exceptions.

Comment on lines +1 to +11
import argparse
from mistralrs import Runner, Which, ChatCompletionRequest, VisionArchitecture

parser = argparse.ArgumentParser(description="Vision model chat example")
parser.add_argument("--model-id", required=True, help="HuggingFace model id")
parser.add_argument("--arch", required=True, help="VisionArchitecture name")
parser.add_argument(
"--image-url",
default="https://www.nhmagazine.com/content/uploads/2019/05/mtwashingtonFranconia-2-19-18-108-Edit-Edit.jpg",
)
args = parser.parse_args()
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add input validation for the architecture argument.

The argument parser setup is well-structured, but there's no validation for the --arch argument. This could lead to runtime errors if an invalid architecture name is provided.

Add validation to prevent runtime errors:

+import sys
+
 parser = argparse.ArgumentParser(description="Vision model chat example")
 parser.add_argument("--model-id", required=True, help="HuggingFace model id")
-parser.add_argument("--arch", required=True, help="VisionArchitecture name")
+parser.add_argument(
+    "--arch", 
+    required=True, 
+    choices=[arch.name for arch in VisionArchitecture],
+    help="VisionArchitecture name"
+)
 parser.add_argument(
     "--image-url",
     default="https://www.nhmagazine.com/content/uploads/2019/05/mtwashingtonFranconia-2-19-18-108-Edit-Edit.jpg",
 )
 args = parser.parse_args()
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import argparse
from mistralrs import Runner, Which, ChatCompletionRequest, VisionArchitecture
parser = argparse.ArgumentParser(description="Vision model chat example")
parser.add_argument("--model-id", required=True, help="HuggingFace model id")
parser.add_argument("--arch", required=True, help="VisionArchitecture name")
parser.add_argument(
"--image-url",
default="https://www.nhmagazine.com/content/uploads/2019/05/mtwashingtonFranconia-2-19-18-108-Edit-Edit.jpg",
)
args = parser.parse_args()
import argparse
import sys
from mistralrs import Runner, Which, ChatCompletionRequest, VisionArchitecture
parser = argparse.ArgumentParser(description="Vision model chat example")
parser.add_argument("--model-id", required=True, help="HuggingFace model id")
parser.add_argument(
"--arch",
required=True,
choices=[arch.name for arch in VisionArchitecture],
help="VisionArchitecture name"
)
parser.add_argument(
"--image-url",
default="https://www.nhmagazine.com/content/uploads/2019/05/mtwashingtonFranconia-2-19-18-108-Edit-Edit.jpg",
)
args = parser.parse_args()
🤖 Prompt for AI Agents
In examples/python/vision_chat.py around lines 1 to 11, the --arch argument
lacks validation, which may cause runtime errors if an invalid architecture name
is given. Fix this by adding a check after parsing arguments to verify that
args.arch matches one of the valid VisionArchitecture names. If the value is
invalid, print an error message and exit the program gracefully to prevent
further execution with an unsupported architecture.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant