Loading…
Introducing swift-huggingface: The Complete Swift Client for Hugging Face
Hugging FaceMattt
Summary
Hugging Face released swift-huggingface, a dedicated Swift package offering complete Hub API integration, reliable file downloads, and inference provider access. The library addresses previous limitations in swift-transformers 1.0, where interrupted multi-gigabyte model downloads could not resume and cache structures differed from Python. To resolve cache duplication, swift-huggingface implements a Python-compatible content-addressed storage layout using symlinks and flock file locking. Authentication is standardized through a TokenProvider pattern supporting auto-detection, Keychain integration, static CI/CD tokens, and OAuth 2.0 sign-in with automatic token refresh. The package also provides URLSession-backed snapshot downloads with granular progress tracking and will soon replace the HubApi implementation inside swift-transformers.
Context
swift-transformers 1.0 faced community issues with slow, non-resumable downloads of multi-gigabyte models, prompting developers to bundle models manually. Additionally, Swift apps used a distinct cache structure from Python's ~/.cache/huggingface/hub, resulting in duplicate downloads across ecosystems, while authentication token sourcing was ambiguous across environment variables, files, and Keychain.
Approach / What changed
swift-huggingface was built as a standalone Swift package providing complete Hugging Face Hub API coverage, URLSession-based downloads with resume support, flock-based file locking, and Python-compatible content-addressed caching. It introduces a TokenProvider pattern with auto-detection, explicit static and Keychain options, OAuth 2.0 support via HuggingFaceAuthenticationManager, and an InferenceClient for ML inference providers.
Takeaways
- swift-huggingface shares a content-addressed cache structure with Python's huggingface_hub using blobs, refs, snapshots symlinks, and flock(2) locking to prevent duplicate downloads and race conditions.
- The TokenProvider architecture resolves credential ambiguity by supporting environment variable auto-detection, static tokens, Keychain storage, and an OAuth 2.0 flow that automates token refresh.
- Model downloads leverage URLSession download tasks to provide accurate per-file progress tracking, resumable downloads for interrupted transfers, and selective snapshot retrieval.
Related reading
Swift Transformers Reaches 1.0 – and Looks to the Future
swift-transformers has released version 1.0 to provide a stable foundation for running local models on Apple Silicon platforms. The library supplies components missing from Core ML and MLX, focusing heavily on tokenization, chat templates, and Hugging Face Hub integration. In this major release, Tokenizers and Hub become standalone top-level modules, enabling developers to import only the components they require. The update also integrates an updated swift-jinja library for faster template processing, adopts Modern Core ML stateful models and MLTensor APIs, removes unnecessary CLI dependencies, and adds full Swift 6 support. Future development will prioritize deeper integration with MLX frameworks and agentic workflows like MCP.
Pedro Cuenca, Christopher Fleetwood, Mattt, Vaibhav SrivastavIntroducing AnyLanguageModel: One API for Local and Remote LLMs on Apple Platforms