Skip to content

server: validate safetensors architecture before copying blobs#16630

Open
nac7 wants to merge 1 commit into
ollama:mainfrom
nac7:fix/safetensors-early-arch-check
Open

server: validate safetensors architecture before copying blobs#16630
nac7 wants to merge 1 commit into
ollama:mainfrom
nac7:fix/safetensors-early-arch-check

Conversation

@nac7

@nac7 nac7 commented Jun 8, 2026

Copy link
Copy Markdown

Fixes #15949.

Problem

When importing a safetensors model with an unsupported architecture, Ollama copies all blobs into a staging directory first and only then attempts conversion — surfacing the unsupported architecture error after potentially many gigabytes of unnecessary I/O:

converting model
Error: unsupported architecture "MistralForCausalLM"

A user reporting this issue had already copied 77 GB of safetensors files before getting the error.

Fix

convert.CheckArchitecture(configPath string) error reads config.json from the given path, parses ModelParameters.Architectures, and returns a descriptive error for any architecture that is not handled by LoadModelMetadata's switch statement.

convertFromSafetensors in server/create.go calls this before os.MkdirTemp and before any blob links are created. If config.json is missing, unreadable, or has no architectures field, CheckArchitecture returns nil so that ConvertModel can surface those errors with full context as before.

Tests

TestCheckArchitecture in convert/convert_test.go covers:

  • Supported architectures (LlamaForCausalLM, Gemma3ForCausalLM) → no error
  • Known-unsupported architecture (MistralForCausalLM) → error with architecture name
  • Unknown/future architecture → error
  • Empty architectures list → nil (let ConvertModel handle)
  • Missing config.json → nil (let ConvertModel handle)
  • Invalid JSON → nil (let ConvertModel handle)

@
server: validate safetensors architecture before copying blobs

When importing a safetensors model directory with an unsupported
architecture, Ollama previously copied all blobs into the staging
directory and only then attempted conversion, surfacing the
"unsupported architecture" error after potentially gigabytes of I/O
(issue ollama#15949).

Fix: read config.json from the blob store at the start of
convertFromSafetensors and call convert.CheckArchitecture before
creating the temp directory or linking any files. If the architecture
is positively identified as unsupported the error is returned
immediately. Unreadable or unparseable config.json files are silently
skipped so that ConvertModel can surface those errors with full context
as before.

Adds convert.CheckArchitecture and a unit test covering supported
architectures, known-unsupported architectures, missing file, empty
architectures list, and invalid JSON.

Fixes ollama#15949
@

@stevei101 stevei101 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.

Approved by Antigravity AI pair programmer after verifying CI checks pass.

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.

Ollama doesn't validate model architecture when importing safetensors

2 participants