Skip to content

Conversation

@ilopezluna
Copy link
Contributor

@ilopezluna ilopezluna commented Oct 6, 2025

This pull request adds support for packaging models stored in Safetensors format in both the CLI (docker model package) and the mdltool utility. The changes introduce a new code path for handling Safetensors directories, including validation, file discovery, and temporary archiving of configuration files. The packaging logic for Safetensors is refactored into a reusable function in a new packaging package.

Summary by Sourcery

Add support for packaging models stored in the Safetensors format across both the CLI and mdltool, extracting Safetensors directory scanning and config archiving into a reusable packaging package.

New Features:

  • Support packaging Safetensors models in the CLI via the new --safetensors-dir option.
  • Enable mdltool to detect and package Safetensors directories for model distribution.

Enhancements:

  • Refactor Safetensors directory scanning and temporary config archive creation into pkg/distribution/packaging.PackageFromDirectory.
  • Update command validation to enforce mutual exclusivity of --gguf and --safetensors-dir and cleanly select the packaging path.
  • Move internal PackageFromDirectory logic from cmd/mdltool into a shared packaging package.

Build:

  • Update go.mod to reference the new packaging package and adjust model-runner module paths.

Documentation:

  • Update CLI command usage and help text to describe Safetensors support.

Copilot AI review requested due to automatic review settings October 6, 2025 10:26
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Oct 6, 2025

Reviewer's Guide

This PR adds support for packaging Safetensors-format models by extracting directory scanning and config archiving into a new packaging package, and updating both the CLI and mdltool to leverage this reusable logic with appropriate flags, validations, and builder integrations.

File-Level Changes

Change Details Files
Extracted Safetensors packaging logic into a reusable package
  • Moved directory scanning and temp config archive creation into pkg/distribution/packaging
  • Renamed createTempConfigArchive to CreateTempConfigArchive and adjusted exporting
  • Removed inline packageFromDirectory and createTempConfigArchive implementations from mdltool
pkg/distribution/packaging/safetensors.go
cmd/mdltool/main.go
Enhanced CLI to support Safetensors directory packaging
  • Added --safetensors-dir flag and mutually exclusive validation with --gguf
  • Updated help text and usage to reflect both GGUF and Safetensors options
  • Integrated packaging.PackageFromDirectory, builder.FromSafetensors, and config archive addition
cmd/cli/commands/package.go
Refactored mdltool command to use new packaging function
  • Replaced inline call to packageFromDirectory with packaging.PackageFromDirectory
  • Handled returned safetensors paths and config archive uniformly
cmd/mdltool/main.go
Updated imports and module dependencies for model-runner packaging
  • Switched distribution imports from docker/model-distribution to model-runner/pkg/distribution
  • Bumped model-runner module version and added a replace directive
  • Cleaned up obsolete go.mod and go.sum entries
cmd/cli/go.mod
cmd/cli/go.sum
cmd/cli/commands/package.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @ilopezluna, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the model packaging capabilities by integrating native support for the Safetensors format. Users can now package Safetensors models directly from a directory, which will automatically discover .safetensors files and archive relevant configuration files like .json and merges.txt. This change streamlines the process for a broader range of AI models and improves code organization by centralizing the new packaging logic.

Highlights

  • Safetensors Model Packaging Support: Adds the ability to package models stored in the Safetensors format using both the docker model package CLI command and the mdltool utility.
  • New CLI Option for Safetensors: Introduces the --safetensors-dir flag to the docker model package command, allowing users to specify a directory containing Safetensors files and associated configuration.
  • Enhanced Input Validation: Implements new validation logic to ensure that either --gguf or --safetensors-dir is provided (but not both), and verifies that the specified Safetensors directory exists and is an absolute path.
  • Refactored Packaging Logic: Extracts the Safetensors model packaging logic, including file discovery, configuration file archiving, and temporary file management, into a new, reusable pkg/distribution/packaging package.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request adds support for packaging Safetensors models alongside existing GGUF support. The implementation introduces a new code path for handling Safetensors directories, including validation, file discovery, and temporary archiving of configuration files.

Key changes:

  • Refactored packaging logic into a reusable packaging package containing directory scanning and tar archive creation
  • Updated CLI to support --safetensors-dir flag as an alternative to --gguf
  • Modified mdltool to use the new shared packaging functionality

Reviewed Changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 1 comment.

File Description
pkg/distribution/packaging/safetensors.go New package providing directory scanning and config archive creation for Safetensors models
cmd/mdltool/main.go Refactored to use shared packaging functions, removed duplicate code
cmd/cli/go.mod Updated module dependencies and added local replacement
cmd/cli/commands/package.go Added Safetensors support with new CLI flag and validation logic

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@ilopezluna ilopezluna requested a review from a team October 6, 2025 10:27
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • Use cobra’s MarkFlagsMutuallyExclusive for --gguf and --safetensors-dir to simplify mutual‐exclusion validation and improve CLI help messaging.
  • Extract the repeated absolute‐path validation and cleaning logic into a shared helper to reduce duplication between GGUF and Safetensors flag handling.
  • Consider refactoring PackageFromDirectory to return an io.Reader (or in‐memory buffer) for the config archive so callers don’t need to manage temporary file creation and cleanup.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Use cobra’s MarkFlagsMutuallyExclusive for --gguf and --safetensors-dir to simplify mutual‐exclusion validation and improve CLI help messaging.
- Extract the repeated absolute‐path validation and cleaning logic into a shared helper to reduce duplication between GGUF and Safetensors flag handling.
- Consider refactoring PackageFromDirectory to return an io.Reader (or in‐memory buffer) for the config archive so callers don’t need to manage temporary file creation and cleanup.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Contributor

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

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 effectively adds support for packaging Safetensors models. The changes are well-structured, introducing a new packaging package to share logic between the CLI and mdltool, which is a great refactoring. The command-line argument handling and validation for the new --safetensors-dir flag are thorough. I have a couple of suggestions to improve error handling and code maintainability.

Comment on lines 81 to 128
for _, filePath := range configFiles {
// Open the file
file, err := os.Open(filePath)
if err != nil {
tw.Close()
tmpFile.Close()
os.Remove(tmpPath)
return "", fmt.Errorf("open config file %s: %w", filePath, err)
}

// Get file info for tar header
fileInfo, err := file.Stat()
if err != nil {
file.Close()
tw.Close()
tmpFile.Close()
os.Remove(tmpPath)
return "", fmt.Errorf("stat config file %s: %w", filePath, err)
}

// Create tar header (use only basename, not full path)
header := &tar.Header{
Name: filepath.Base(filePath),
Size: fileInfo.Size(),
Mode: int64(fileInfo.Mode()),
ModTime: fileInfo.ModTime(),
}

// Write header
if err := tw.WriteHeader(header); err != nil {
file.Close()
tw.Close()
tmpFile.Close()
os.Remove(tmpPath)
return "", fmt.Errorf("write tar header for %s: %w", filePath, err)
}

// Copy file contents
if _, err := io.Copy(tw, file); err != nil {
file.Close()
tw.Close()
tmpFile.Close()
os.Remove(tmpPath)
return "", fmt.Errorf("write tar content for %s: %w", filePath, err)
}

file.Close()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The error handling logic within this for loop is repeated in multiple places (e.g., lines 84-88, 93-98, etc.). The cleanup sequence tw.Close(); tmpFile.Close(); os.Remove(tmpPath) is duplicated, which makes the code verbose and harder to maintain. Consider refactoring to reduce this duplication. One approach would be to extract the logic for adding a single file to the tar archive into a helper function. Another is to wrap the loop in a closure to centralize the cleanup logic.

Copy link
Contributor

Choose a reason for hiding this comment

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

@ilopezluna worth considering?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ericcurtin addressed here

@doringeman doringeman mentioned this pull request Oct 6, 2025
Copilot AI review requested due to automatic review settings October 7, 2025 12:49
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@ilopezluna ilopezluna requested a review from ericcurtin October 7, 2025 14:33
@ericcurtin ericcurtin merged commit af6253a into main Oct 7, 2025
9 checks passed
@ericcurtin ericcurtin deleted the package-safetensors branch October 7, 2025 14:54
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