huggingface/swift-transformers for Swift language-model apps
Swift Package to implement a transformers-like API in Swift
At a glance
- What is it?
- swift-transformers gives Swift applications tokenization, chat templating, tool-call formatting, Hugging Face Hub downloads, and CoreML model utilities. It is aimed at developers building on-device language-model features with SwiftPM.
- Who is it for?
- Adopt swift-transformers when a Swift app needs Hugging Face-compatible tokenizer and Hub workflows, especially for CoreML inference or offline bundled tokenizers. Do not choose it as a standalone inference engine based on this README, because the documented scope centers on model utilities, tokenization, downloads, and CoreML integration.
- Can I use it commercially?
- Yes. Apache-2.0 is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository last received commits 9 days ago.
- What is it written in?
- Mainly Swift, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on October 22, 2026, and from our analysis. They are not legal advice.
Editorial analysis
What swift-transformers adds to a Swift application
swift-transformers is a Swift Package for applications that need language-model plumbing without reproducing the tokenizer and model-distribution logic in Python or around a separate command-line service. The repository describes a transformers-like API that is intended to feel familiar to developers who have used the Python `transformers` library, while remaining idiomatic in Swift. The immediate users are Swift application developers who need to prepare prompts, download model files, construct tokenizers, or work with CoreML language-model assets.
The package does not present itself as a general-purpose model runtime. Its commonly used modules are `Tokenizers` and `Hub`, which cover tokenization and model downloads. The `Models` and `Generation` modules provide utilities for language models working with CoreML. That division matters for adoption. A team looking for Swift-native prompt preparation and asset distribution can use the package without expecting the README to document every feature available in the Python library.
The project also addresses an on-device constraint that often appears late in application development. Model checkpoints and tokenizer files may be large, distributed through the Hugging Face Hub, or bundled with a compiled CoreML model. swift-transformers gives these paths separate APIs, including progress reporting, flaky connection handling, and local tokenizer injection. The result is a library focused on the boundary between an application and model assets rather than a complete deployment platform.
Tokenizer construction and chat-template data flow
The documented tokenizer flow begins with an asynchronous `AutoTokenizer.from(pretrained:)` call. The identifier names a Hugging Face model repository, and the initializer obtains the tokenizer associated with that pretrained checkpoint. The resulting tokenizer can apply a chat template to messages, producing token identifiers that are then passed to `decode(tokens:)`. The README does not document a separate synchronous initializer for this pretrained path, so applications should preserve the `try await` context shown in the example.
The messages are represented as arrays of role-and-content dictionaries. In the example, a user asks the model to describe Swift. `applyChatTemplate(messages:)` converts that structured conversation into the encoded representation expected by the model. This is more specific than simply splitting text on whitespace. It delegates the model-specific formatting rules to the tokenizer configuration, which is important because chat formats can differ between model families.
Tool calling follows the same preparation path. A tool definition includes a function name, description, parameter schema, and required fields. The tokenizer receives both the user message and the tool array through `applyChatTemplate(messages:tools:)`. The README describes this as native support for formatting inputs for tool calling. It does not claim that swift-transformers executes the function, validates the returned arguments, or performs the tool invocation. Those responsibilities remain with the application or another model-runtime component.
Hub.snapshot downloads and offline CoreML assets
For online distribution, the documented API is `Hub.snapshot(from:matching:progressHandler:)`. The caller supplies a `Hub.Repo` identifier and a list of file patterns. The example requests `config.json` and `*.safetensors`, then receives a local directory URL. The progress handler exposes `fractionCompleted`, which the sample multiplies by `100` before printing a percentage. The API therefore supports selective downloads rather than forcing every repository file into the application.
The repository says that Hub downloads include progress reporting and handling for flaky connections. It does not document retry limits, resume semantics, disk-space checks, authentication requirements for every repository type, or behavior when a matched file is missing. Those details should be verified against the relevant source and tests before an application treats the download layer as a complete asset-management system.
For a compiled CoreML model that is already shipped with the app, swift-transformers supports a different path. `AutoTokenizer.from(modelFolder:)` constructs the tokenizer from local files, and `LanguageModel.loadCompiled(url:tokenizer:)` associates that tokenizer with a compiled model. The README explicitly says that both initializers reuse the supplied tokenizer and do not contact the Hugging Face Hub. This is a concrete benefit for offline execution, provided the bundled tokenizer is compatible with the model. The README specifically warns to use tokenizer assets from the same Hugging Face repository or otherwise ensure compatibility.
SwiftPM installation and a first tokenizer use
The package is consumed through SwiftPM. Add the repository as a dependency in `Package.swift`, then declare the `Transformers` product on the target that uses it. The documented dependency starts at version `1.3.0`, while the listed recent releases include `1.3.3`, `1.3.4`, and `1.3.5`.
dependencies: [
.package(url: "https://github.com/huggingface/swift-transformers", from: "1.3.0")
]This declares the package dependency. Add the product to the relevant target:
targets: [
.target(
name: "YourTargetName",
dependencies: [
.product(name: "Transformers", package: "swift-transformers")
]
)
]The first use can load a pretrained tokenizer, apply a chat template, and decode the result:
let tokenizer = try await AutoTokenizer.from(pretrained: "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B")
let messages = [["role": "user", "content": "Describe the Swift programming language."]]
let encoded = try tokenizer.applyChatTemplate(messages: messages)
let decoded = tokenizer.decode(tokens: encoded)This code requires an asynchronous context and accesses the model repository. The README does not document authentication for public repositories, so a deployment that uses gated repositories needs a separate documented Hub authentication flow.
CoreML execution, bundled tokenizers, and upgrade cost
The CoreML path is most relevant to applications that ship compiled model assets. The README points to an example for converting and running Mistral 7B with CoreML and identifies `Examples/Mistral7B/` as the example location. For a local tokenizer, the documented input folder must contain `tokenizer.json` and `tokenizer_config.json`.
let compiledURL: URL = ... // path to .mlmodelc
let tokenizerFolder: URL = ... // folder containing tokenizer_config.json and tokenizer.json
let tokenizer = try await AutoTokenizer.from(modelFolder: tokenizerFolder)
let model = try LanguageModel.loadCompiled(
url: compiledURL,
tokenizer: tokenizer
)This approach avoids network requests during construction, but it does not remove model conversion or asset packaging work. The application still needs a compatible `.mlmodelc` file, tokenizer files, and the application-side logic for presenting model output. The README does not provide a universal conversion command or a performance comparison between CoreML and other runtimes.
The package is not archived, and the last push was on 2026-10-14. Recent releases include `1.3.5` on 2026-10-09, `1.3.4` on 2026-09-02, and `1.3.3` on 2026-05-16. That indicates recent release activity, but it does not establish compatibility for every Swift or model configuration. Upgrades can affect tokenizer behavior, CoreML integration, and optional dependency resolution. The repository uses the Apache-2.0 license. That license identifies the project's licensing terms, but this review does not provide legal advice about compliance or redistribution.
Optional Xet transport and the boundary of the package
swift-transformers has an optional `Xet` package trait for faster, parallel Hub downloads through `swift-xet`. The README says that Xet adds transitive dependencies, including `AsyncHTTPClient`, so it is opt-in rather than part of the default configuration. Swift 6.1 and later can enable the trait in `Package.swift`:
dependencies: [
.package(
url: "https://github.com/huggingface/swift-transformers",
from: "1.3.0",
traits: ["Xet"]
)
]The command-line equivalents are also documented:
swift build --traits Xet
swift test --traits XetWithout the trait, Hub downloads use the default `URLSession`-based transport. On Swift versions earlier than 6.1, package traits are unavailable and Xet transport cannot be enabled. The README also states that Xcode does not yet provide a built-in way to declare package dependencies with traits. Its workaround is an internal Swift package that re-exports swift-transformers with `Xet` enabled, after which that package can be added as a local dependency in Xcode.
This is a transport choice, not a change to the tokenizer API. Teams should select it based on their Swift toolchain and dependency constraints. The README does not quantify download speed, memory use, or failure behavior for Xet, so the available example and trait configuration are more reliable adoption criteria than an unverified performance expectation.
When swift-transformers is the wrong layer
swift-transformers is the wrong choice if the requirement is a complete server-side model-serving stack, a training system, or a fully documented cross-platform inference engine. The README describes utilities for adopting language models in Swift apps, not model training, distributed serving, or a replacement for every component in the Python `transformers` ecosystem. It also does not document a general API for selecting an arbitrary inference backend.
It is also unsuitable as a shortcut for using incompatible model assets. A local tokenizer may load without any Hub request, but the README warns that tokenizer files should come from the original model repository or be compatible with the model. Likewise, `Hub.snapshot` can request selected files, but the documentation does not explain how an application should resolve missing files, manage quotas, or authenticate every private or gated repository.
For Swift applications whose central task is local tokenization, chat formatting, Hub distribution, or CoreML asset integration, the package addresses a specific gap. For applications that need model execution itself, a separate runtime or a broader integration must be evaluated. The repository's `Examples/transformers-cli/` and `Examples/Mistral7B/` directories are useful places to inspect for the intended integration shape, but the README does not claim that those examples cover every platform or deployment case.
Editorial conclusion
Adopt swift-transformers when a Swift app needs Hugging Face-compatible tokenizer and Hub workflows, especially for CoreML inference or offline bundled tokenizers. Do not choose it as a standalone inference engine based on this README, because the documented scope centers on model utilities, tokenization, downloads, and CoreML integration. Verify first that your model assets, tokenizer files, Swift version, and required Hub transport match the documented setup.
Frequently asked questions
How do I add swift-transformers to a Swift project?
Add the repository in `Package.swift` with a package dependency, then add the `Transformers` product to the target that needs it. The README shows a dependency range beginning at `1.3.0`.
Can swift-transformers tokenize chat messages and apply chat templates?
Yes. The documented `AutoTokenizer.from(pretrained:)` and `applyChatTemplate(messages:)` flow prepares chat messages as tokens, and `decode(tokens:)` converts the token representation back to text.
Does swift-transformers support offline CoreML tokenizers?
Yes. The README documents `AutoTokenizer.from(modelFolder:)` followed by `LanguageModel.loadCompiled(url:tokenizer:)`, with `tokenizer.json` and `tokenizer_config.json` in the local folder. It states that these initializers do not reach the Hugging Face Hub.
What is the Xet trait in swift-transformers?
The optional `Xet` trait enables faster, parallel Hub downloads through `swift-xet`. It requires Swift 6.1 or later and adds transitive dependencies including `AsyncHTTPClient`; without it, downloads use the default `URLSession` transport.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/huggingface-swift-transformers)Was this useful?