Rust bindings for Apple's Foundation Models framework
⚠️ Beta Warning: This crate is currently in beta. Future updates may contain breaking changes to the API.
Overview
Goal: Offer a safe Rust interface to Apple's on-device Foundation Models so Rust applications can request blocking or streaming language model responses without leaving the Rust ecosystem.
Architecture: A Swift bridge (swift/FoundationModelsFFI.swift) is compiled at build time via build.rs, and the Rust LanguageModelSession wraps its callbacks through a zero-copy FFI layer with typed errors for availability, validation, and generation failures.
Platform Support
This crate supports:
- macOS 26+ (Apple Silicon and Intel)
- iOS 26+ (device and simulator)
Both platforms require Apple Intelligence to be enabled.
Quick Start
Here's a simple example to get started:
use LanguageModelSession;
For more examples, see the examples directory.
Features
- Blocking responses - Get complete responses with
response() - Streaming responses - Real-time token streaming with
stream_response() - Stream cancellation - Cancel ongoing streams with
cancel_stream() - System instructions - Configure model behavior with custom prompts
- Session persistence - Save and restore conversation context
Usage Examples
Basic Response
let session = new?;
let response = session.response?;
println!;
With Instructions
let session = with_instructions?;
let response = session.response?;
Streaming Response
use ;
let session = new?;
session.stream_response?;
Session Persistence
// Save session
let transcript = session.transcript_json?;
write?;
// Restore later
let saved = read_to_string?;
let session = from_transcript_json?;
For complete examples, see the examples directory:
response.rs- Blocking response generationstream_response.rs- Streaming responsestranscript_persistence.rs- Save/restore sessions
Error Handling
The crate uses a custom Error type with these variants:
use ;
match session.response
Building
macOS
On macOS, simply run:
iOS
To build for iOS, you need to specify the target:
# iOS device (ARM64)
# iOS simulator (ARM64, for Apple Silicon Macs)
# iOS simulator (x86_64, for Intel Macs)
Cross-compilation Notes
- This crate must be built on macOS as it requires the Swift compiler and Apple SDKs
- The build script automatically detects the target platform and configures the appropriate SDK and library type
- macOS builds use dynamic libraries (
.dylib) - iOS builds use static libraries (
.a)
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Legal
This project is independent and not affiliated with, endorsed by, or sponsored by Apple Inc. Apple, macOS, iOS, Apple Intelligence, and Apple silicon are trademarks of Apple Inc., registered in the U.S. and other countries and regions. Use of these marks here is for identification only.