AIMX (apple-intelligence-models)
AIMX is a safe Rust library for Apple's FoundationModels on-device language model framework, also known as Apple Intelligence.
The package is published as apple-intelligence-models and imported as aimx.
The model runs locally on supported Apple hardware, so a basic application does
not need an API key, a network request, or a hosted inference provider.
The documentation is written in the style of official Rust crate docs: start with the smallest correct example, name every boundary, and document the errors that users can recover from. The tutorial then teaches the same API one concept at a time.
Status
This crate is a thin safe wrapper around Apple's FoundationModels framework. It is designed as a library crate, not an application. The public API uses typed boundaries for prompts, instructions, generation options, response text, structured output schemas, and tool-call results.
The crate also compiles on unsupported hosts. If the Swift bridge cannot be
built, or if Apple Intelligence is not available at runtime, model APIs return
Error::Unavailable instead of panicking or failing to link.
Requirements
| Requirement | Value |
|---|---|
| Rust | 1.85 or newer |
| macOS for live model use | 26 (Tahoe) or newer |
| Hardware for live model use | Apple Silicon (M1 or newer) |
| System setting | Apple Intelligence enabled |
| Build tool for live bridge | Xcode with the macOS 26 SDK |
How To Read The Docs
If you are new to AIMX, read the docs in this order:
- Run the Quick Start to see the smallest complete program.
- Read The Mental Model to understand the core types.
- Use Builder-Style Sessions for real applications.
- Follow TUTORIAL.md when you want a guided, course-style path.
- Use the files in
references/for implementation details.
The Mental Model
AIMX has three layers:
| Layer | Rust type | What it teaches |
|---|---|---|
| Availability | availability, AppleIntelligenceModels::availability |
Check whether the local model can run before doing work. |
| Session | LanguageModelSession |
Keep instructions, options, tools, and conversation state together. |
| Boundaries | Prompt, SystemInstructions, Temperature, MaxTokens, GenerationSchema |
Convert raw input into typed values before it crosses FFI or model boundaries. |
The important idea is simple: raw input is allowed at the edge of your program, but AIMX turns it into typed Rust values before it reaches Apple FoundationModels. That keeps failures recoverable and makes examples behave the same way on supported and unsupported hosts.
Install
Add the crate to Cargo.toml:
[]
= { = "apple-intelligence-models", = "0.2" }
The crate is runtime-agnostic. Use the async executor already present in your
application. The examples below use Tokio and futures-util for convenience:
[]
= { = "1", = ["macros", "rt-multi-thread"] }
= "0.3"
= { = "apple-intelligence-models", = "0.2" }
Quick Start
use ;
async
Run the checked-in executor-neutral example:
Explore deterministic agent use-case examples:
For a full walkthrough, see TUTORIAL.md.
Builder-Style Sessions
Use AppleIntelligenceModels::default().session() when you want AIMX-owned
session state: instructions, default generation options, tools, or multi-turn
context.
agent() and preamble() are kept as Rig-style aliases.
use ;
async
Generation Options
Generation settings are typed after input crosses your application boundary.
Use try_temperature and try_max_tokens only where raw UI, CLI, or JSON
values first enter your code.
use ;
Streaming
use StreamExt as _;
use LanguageModelSession;
async
Structured Generation
Describe the expected JSON object with a GenerationSchema, then deserialize the response
into your own type.
use ;
use Deserialize;
async
Tool Calling
Tools are Rust handlers registered on a session. Tool outputs and failures use typed boundaries instead of raw strings.
use ;
use Value;
async
MLX-Style Aliases
The same session exposes MLX-flavored names when that reads better in model inference code:
use AppleIntelligenceModels;
async
Availability Checking
use ;
match availability
Development
Run the standard gates:
RUSTDOCFLAGS="-D warnings"
Run the deterministic Rust-layer performance benchmarks:
Live model tests are ignored by default because they require Apple Intelligence:
How It Works
The crate ships bridge.swift, which build.rs compiles into a static library
with xcrun swiftc when the target and SDK support it. Swift exports a small C
ABI surface with @_cdecl; Rust keeps those calls behind a private FFI layer
and exposes safe APIs from src/lib.rs.
If bridge compilation fails, the build continues without the bridge cfg and
public model APIs return Error::Unavailable.
See references/async-architecture.md for the callback, channel, cancellation, and tool-handler panic boundaries. See references/performance.md for the benchmark scope and commands. See references/documentation-style.md for the repository documentation style guide.
Contributing
See CONTRIBUTING.md. Changes should keep the public API documented, typed at FFI boundaries, safe on unsupported hosts, and consistent with the repository documentation style.
License
Licensed under the Mozilla Public License 2.0.