foundation-models
Safe, idiomatic Rust bindings for Apple's FoundationModels framework — the on-device large language model that ships with Apple Intelligence on macOS 26+.
Features
- Sessions and multi-turn chat — create, restore, inspect, and persist
LanguageModelSessions - Streaming — text deltas and structured-generation snapshots
- Async API — executor-agnostic
Futurewrappers forrespond(to:),respond(to:generating:), and adapter lifecycle (seeasync_apimodule) - Tool calling — register Rust callbacks as
FoundationModelsTools - Structured generation — JSON-schema validation, dynamic schemas, string-choice schemas, array guides, explicit nil-representation helpers, and Rust
Generabletraits - Structured content helpers — typed
GenerationId, string-backedDecimal,GeneratedContentKind, and generated-content builders with optional IDs - System model configuration — availability, use cases, guardrails, locales, adapter handles, and async token counting
- Transcript support — typed transcript inspection plus raw JSON round-tripping
- Response / tool definitions —
ResponseFormat::generating, inferredTool::generable, and transcriptToolDefinitionhelpers - Typed error metadata —
FMErroraccessors for recovery suggestions, refusal helpers, tool-call details, and generation/schema/adapter-error contexts - Feedback attachments — full
LanguageModelFeedbackissue/sentiment support
Requirements
- macOS 26.0 or newer (build host and runtime)
- Xcode 26 SDK
- Apple Intelligence enabled in System Settings
- Apple Silicon
Installation
[]
= { = "0.11.2", = ["macos_26_0"] }
Async API
Enable the async feature to get executor-agnostic Future wrappers that
work with any async runtime (Tokio, async-std, smol, pollster, …):
[]
= { = "0.11.2", = ["macos_26_0", "async"] }
use ;
use AsyncSession;
#
| Type | Apple API |
|---|---|
AsyncSession::respond |
LanguageModelSession.respond(to:) |
AsyncSession::respond_generating |
LanguageModelSession.respond(to:generating:) |
AsyncAdapter::from_name |
SystemLanguageModel.Adapter init(name:) |
AsyncAdapter::compatibility |
Adapter.compatibleAdapterIdentifiers(name:) |
AsyncAdapter::compile |
SystemLanguageModel.Adapter.compile() |
SystemLanguageModel::token_count |
SystemLanguageModel.tokenCount(for:) |
Tier 2 note:
LanguageModelSession.streamResponse(to:)is anAsyncSequence(multi-fire stream). It is deferred to Tier 2. UseLanguageModelSession::streamfor synchronous streaming today.
Quick start
use *;
Tool calling
use *;
use Deserialize;
#
Structured generation
use *;
#
Coverage-oriented helpers
use *;
#
Smoke example
Notes
- Swift-only compile-time macros such as
@Generableand@Guideare exposed as Rust runtime traits/builders (Generable,GenerationGuide,DynamicGenerationSchema). - The previously audited
SystemLanguageModel.Adapter::isCompatible(_ assetPack:)exemption has been removed: Xcode 26.5'sFoundationModels.swiftinterfaceno longer exposes that symbol. Enable the optionalbackgroundassetsfeature to pull in the siblingbackgroundassetscrate for future interop helpers. GenerationIDnow round-trips asGenerationIdviaGeneratedContent::generation_id_handle();GeneratedContent::generation_id()remains as a best-effort string helper.- Typed generation/schema/adapter error metadata plus refusal helpers are available through
FMError::{generation_error_context, adapter_asset_error_context, schema_error_context, recovery_suggestion, failure_reason, refusal, tool_call_error}. - Xcode 26.5's
FoundationModels.swiftinterfacedoes not expose standalonePromptTag,Conversation,ToolCallingMode,SystemPrompt,Examples,LanguageModelInputContent,LanguageModelOutputContent, orStreamingsymbols; seeCOVERAGE.mdfor the audited matrix.
License
Licensed under either of Apache-2.0 or MIT at your option.