apple_foundation_models library

Run Apple's on-device language model from Flutter.

Private, offline, and free — nothing leaves the device and there is no API key. Requires iOS 26 or macOS 26 on Apple Intelligence hardware, so always check AppleFoundationModels.availability before showing an AI feature.

if (!await AppleFoundationModels.isAvailable) return;

final session = await LanguageModelSession.create(
  instructions: 'You summarise text in one sentence.',
);
print(await session.respond(article));
await session.dispose();

Classes

AppleFoundationModels
Entry point for the on-device model.
GenerationOptions
Per-request generation settings.
GreedySampling
Deterministic sampling. See SamplingMode.greedy.
LanguageModelSession
A conversation with the on-device model.
LanguageModelTool
A Dart function the model may call mid-generation.
ModelAvailability
Whether the on-device model can be used, and if not, why.
ModelAvailable
The model is loaded and ready.
ModelUnavailable
The model cannot be used, with the reason attached.
SamplingMode
How the model picks each next token.
Schema
A description of the shape you want the model to produce.
TopKSampling
Top-k sampling. See SamplingMode.topK.
TopPSampling
Nucleus sampling. See SamplingMode.topP.
TranscriptEntry
One turn in a session's history.

Enums

ModelUnavailableReason
Why the on-device model cannot be used right now.
ModelUseCase
Which flavour of the on-device model to talk to.
TranscriptRole
Who produced a transcript entry.

Extensions

ModelStreamDeltas on Stream<String>
Helpers for the cumulative snapshots the model emits.

Typedefs

ToolHandler = FutureOr<String> Function(Map<String, Object?> arguments)
Runs a tool the model asked for and returns text to feed back to it.

Exceptions / Errors

AssetsUnavailableException
The model's assets are unavailable, usually mid-download.
ConcurrentRequestException
A second request was made while the session was still responding.
ContextWindowExceededException
The prompt plus transcript no longer fit in the context window.
DecodingFailureException
The model's output did not conform to the requested schema.
FoundationModelsException
Base class for every failure this package raises.
FoundationModelsPlatformException
Anything the platform reported that does not map to a specific case.
GuardrailViolationException
The prompt or the response tripped Apple's safety guardrails.
ModelUnavailableException
Generation was attempted while the model was unavailable.
RateLimitedException
Too many requests were issued too quickly.
RefusalException
The model declined to answer.
SchemaException
A schema was rejected as invalid or unsupported.
ToolCallException
A tool invoked by the model threw.
UnsupportedLanguageException
The requested language or locale is not supported by the on-device model.