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
- Tool calling — register Rust callbacks as
FoundationModelsTools - Structured generation — JSON-schema validation, dynamic schemas, string-choice schemas, array guides, 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, and adapter handles
- 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 schema/generation 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.7.1", = ["macos_26_0"] }
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). SystemLanguageModel.Adapter::isCompatible(_ assetPack:)is not wrapped because it depends onBackgroundAssets.AssetPack, which this crate does not expose.GenerationIDnow round-trips asGenerationIdviaGeneratedContent::generation_id_handle();GeneratedContent::generation_id()remains as a best-effort string helper.- Typed generation/schema refusal metadata is available through
FMError::{generation_error_context, schema_error_context, recovery_suggestion, failure_reason, refusal, tool_call_error}. - Xcode 26.2'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.