flutter_foundation_models library

Flutter Foundation Models - A Flutter plugin for Apple's on-device Foundation Models.

This package provides a Flutter interface to Apple's on-device language model, available on iOS 26+ and macOS 26+. It enables text generation, structured output, streaming responses, and tool use.

Quick Start

import 'package:flutter_foundation_models/flutter_foundation_models.dart';

// Check availability and create a session
if (await SystemLanguageModel.isAvailable) {
  final session = await LanguageModelSession.create();

  // Generate text
  final response = await session.respondTo("What is Flutter?");
  print(response);

  // Clean up
  session.dispose();
}

Structured Output

Define your data models with the @Generable annotation:

@Generable()
class MovieRecommendation {
  @Guide(description: "Movie title")
  final String title;

  @Guide(description: "Year released")
  final int year;

  MovieRecommendation({required this.title, required this.year});
}

Then generate structured content:

final content = await session.respondToWithSchema(
  "Recommend a sci-fi movie",
  schema: $MovieRecommendationGenerable.generationSchema,
);
final movie = $MovieRecommendationGenerable.fromGeneratedContent(content);
print('${movie.title} (${movie.year})');

Streaming

For real-time UI updates during generation:

final stream = session.streamResponseToWithSchema(
  "Generate a story",
  schema: $StoryGenerable.generationSchema,
);

stream.listen((partial) {
  final story = $StoryGenerable.fromPartialGeneratedContent(partial);
  print(story.text ?? "Loading...");
});

Tools

Enable the model to call your functions:

class WeatherTool extends Tool {
  @override
  String name = "getWeather";

  @override
  String description = "Get weather for a city";

  @override
  GenerationSchema get parameters => $WeatherArgsGenerable.generationSchema;

  @override
  Future<GeneratedContent> call(GeneratedContent arguments) async {
    // Fetch real weather data...
    return WeatherResult(...).toGeneratedContent();
  }
}

final session = await LanguageModelSession.create(tools: [WeatherTool()]);

Classes

Adapter
A model adapter for customizing language model behavior.
AnyOfGenerationSchema
Schema for union types (one of several possible schemas).
AnyOfGuide
Constrains a String field to one of the specified values.
AnyOfStringsGenerationSchema
Schema for enum-like string values.
ArrayGenerationSchema
Schema for array/list types.
ConstantGuide
Constrains a String field to an exact constant value.
ConvertibleToGeneratedContent
Interface for types that can be converted to GeneratedContent.
CountRangeGuide
Constrains an array field to a range of element counts.
DynamicGenerationSchema
Base class for dynamic generation schema types.
DynamicGenerationSchemaProperty
A property definition within a StructGenerationSchema.
ElementGuide
Applies a guide to each element of an array.
ExactCountGuide
Constrains an array field to an exact number of elements.
Generable
Marks a class or enum as generable by the Foundation Models.
GenerationGuide
Base class for generation guides that constrain output values.
GenerationOptions
Options for controlling text generation behavior.
GenerationSchema
Defines the structure for generated content output.
GreedySamplingMode
Greedy sampling mode - always picks the most likely token.
Guide
Provides guidance for a field in a @Generable class.
LanguageModelSession
A session for interacting with Apple's on-device Foundation Models.
MaximumCountGuide
Constrains an array field to a maximum number of elements.
MaximumGuide
Constrains a numeric field to a maximum value.
MinimumCountGuide
Constrains an array field to a minimum number of elements.
MinimumGuide
Constrains a numeric field to a minimum value.
ModelAvailability
Represents the availability status of the language model.
PatternGuide
Constrains a String field to match a regex pattern.
RangeGuide
Constrains a numeric field to a range of values.
SamplingMode
Sampling mode for text generation.
StructGenerationSchema
Schema for structured objects with named properties.
SystemLanguageModel
Represents a system language model for on-device AI.
Tool
Base class for defining tools that can be called by the language model.
TopKSamplingMode
Top-K sampling mode - samples from the top K most likely tokens.
TopPSamplingMode
Top-P (nucleus) sampling mode.
Transcript
A transcript containing the conversation history of a session.
TranscriptEntry
Base class for transcript entries.
TranscriptInstructions
System instructions entry.
TranscriptPrompt
User prompt entry.
TranscriptResponse
Model response entry.
TranscriptSegment
Base class for transcript segments.
TranscriptStructuredSegment
Structured segment containing generated content.
TranscriptTextSegment
Text segment containing plain text content.
TranscriptToolCall
A single tool call.
TranscriptToolCalls
Tool calls entry (model requesting tool invocations).
TranscriptToolDefinition
Tool definition in transcript.
TranscriptToolOutput
Tool output entry (result from a tool invocation).
TranscriptUnknown
Unknown entry type (for forward compatibility).
ValueGenerationSchema
Schema for primitive value types.

Enums

Guardrails
Guardrails setting for the language model.
UseCase
Use case for the language model.

Extension Types

GeneratedContent
A wrapper for content generated by the language model.