| Crates.io | fm-bindings |
| lib.rs | fm-bindings |
| version | 0.1.3 |
| created_at | 2025-10-27 22:22:49.910248+00 |
| updated_at | 2025-12-03 10:11:01.535315+00 |
| description | Rust bindings for Apple's Foundation Models framework |
| homepage | https://github.com/remdalm/fm-bindings |
| repository | https://github.com/remdalm/fm-bindings |
| max_upload_size | |
| id | 1903815 |
| size | 75,772 |
⚠️ Beta Warning: This crate is currently in beta. Future updates may contain breaking changes to the API.
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.
This crate supports:
Both platforms require Apple Intelligence to be enabled.
Here's a simple example to get started:
use fm_bindings::LanguageModelSession;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a session
let session = LanguageModelSession::new()?;
// Get a response
let response = session.response("What is Rust?")?;
println!("{}", response);
Ok(())
}
For more examples, see the examples directory.
response()stream_response()cancel_stream()let session = LanguageModelSession::new()?;
let response = session.response("Explain Rust ownership")?;
println!("{}", response);
let session = LanguageModelSession::with_instructions(
"You are a helpful coding assistant."
)?;
let response = session.response("How do I handle errors in Rust?")?;
use std::io::{self, Write};
let session = LanguageModelSession::new()?;
session.stream_response("Tell me a story", |chunk| {
print!("{}", chunk);
io::stdout().flush().unwrap();
})?;
// Save session
let transcript = session.transcript_json()?;
std::fs::write("session.json", &transcript)?;
// Restore later
let saved = std::fs::read_to_string("session.json")?;
let session = LanguageModelSession::from_transcript_json(&saved)?;
For complete examples, see the examples directory:
response.rs - Blocking response generationstream_response.rs - Streaming responsestranscript_persistence.rs - Save/restore sessionsThe crate uses a custom Error type with these variants:
use fm_bindings::{LanguageModelSession, Error};
match session.response("Hello") {
Ok(response) => println!("{}", response),
Err(Error::ModelNotAvailable) => {
eprintln!("Apple Intelligence is not enabled. Please enable it in System Settings.");
}
Err(Error::InvalidInput(msg)) => {
eprintln!("Invalid input: {}", msg);
}
Err(Error::GenerationError(msg)) => {
eprintln!("Generation failed: {}", msg);
}
Err(e) => eprintln!("Error: {}", e),
}
On macOS, simply run:
cargo build
To build for iOS, you need to specify the target:
# iOS device (ARM64)
cargo build --target aarch64-apple-ios
# iOS simulator (ARM64, for Apple Silicon Macs)
cargo build --target aarch64-apple-ios-sim
# iOS simulator (x86_64, for Intel Macs)
cargo build --target x86_64-apple-ios
.dylib).a)Licensed under either of:
at your option.
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.