Skip to main content

Crate rusty_foundationmodels

Crate rusty_foundationmodels 

Source
Expand description

Safe Rust bindings for Apple’s FoundationModels on-device AI framework (Apple Intelligence).

§Overview

FoundationModels gives access to an on-device ≈3B-parameter language model that runs entirely locally — no network requests, no API keys, no data leaving the device.

This crate exposes the full session lifecycle:

§Requirements

RequirementValue
macOS26 (Tahoe) or later
HardwareApple Silicon (M1 or later)
SettingApple Intelligence enabled in System Settings
Build toolXcode with the macOS 26 SDK

On unsupported hardware or older macOS the crate still compiles and links; all APIs return Error::Unavailable at runtime.

§Quick start

use rusty_foundationmodels::{is_available, respond};

if !is_available() {
    eprintln!("Apple Intelligence not available on this device");
    return Ok(());
}

let answer = respond("What is the capital of France?").await?;
println!("{answer}");

§Multi-turn conversation

use rusty_foundationmodels::Session;

let session = Session::with_instructions("You are a concise Rust expert.")?;
let r1 = session.respond("What is ownership?").await?;
let r2 = session.respond("Give me a one-line example.").await?;
println!("{r1}\n{r2}");

§Streaming

use rusty_foundationmodels::Session;

let session = Session::new()?;
let stream = session.stream("Tell me a short story.")?;

§Structured generation

use serde::Deserialize;
use rusty_foundationmodels::{Session, Schema, SchemaProperty, SchemaPropertyType};

#[derive(Deserialize)]
struct CityInfo { name: String, population: f64, country: String }

let session = Session::new()?;
let schema = Schema::new("CityInfo")
    .property(SchemaProperty::new("name", SchemaPropertyType::String))
    .property(SchemaProperty::new("population", SchemaPropertyType::Double))
    .property(SchemaProperty::new("country", SchemaPropertyType::String));

let info: CityInfo = session.respond_as("Describe Paris.", &schema).await?;
println!("{} has {} people", info.name, info.population);

§Tool calling

use rusty_foundationmodels::{Session, ToolDefinition, Schema, SchemaProperty, SchemaPropertyType};

let tool = ToolDefinition::new(
    "get_weather",
    "Get current weather for a city",
    Schema::new("GetWeatherArgs")
        .property(SchemaProperty::new("city", SchemaPropertyType::String)
            .description("City name")),
    |args| {
        let city = args["city"].as_str().unwrap_or("unknown");
        Ok(format!("Weather in {city}: sunny, 72°F"))
    },
);

let session = Session::with_tools("You are a weather assistant.", vec![tool])?;
let response = session.respond("What's the weather in Tokyo?").await?;
println!("{response}");

Structs§

Attachment
A media attachment to include in a prompt.
GenerationOptions
Tuning parameters for a single generation request.
ResponseStream
An async stream of text chunks produced by Session::stream.
ResponseStructuredStream
An async stream of JSON chunks produced by Session::stream_structured.
Schema
Describes the JSON object shape that the model must produce for structured generation.
SchemaProperty
A single property within a Schema.
Session
A stateful conversation session backed by a LanguageModelSession.
ToolDefinition
A function that the model can invoke when responding to a prompt.

Enums§

Error
Errors returned by this crate.
SchemaPropertyType
The type of a single property in a Schema.
UnavailabilityReason
Reasons why Apple Intelligence is not available on the current device.

Functions§

availability
Returns Ok(()) if Apple Intelligence is ready, or the specific UnavailabilityReason.
is_available
Returns true if Apple Intelligence is available and ready on this device.
respond
Sends a single prompt to the model and returns the response text.
respond_with_options
Like respond but allows tuning generation via GenerationOptions.