All articles
4 min·

FoundationModels: Understanding and Using Apple's New AI Framework

A practical guide to Apple's FoundationModels framework for on-device AI in iOS and macOS apps, with a hands-on SwiftUI tutorial.

Par Carolane Lefebvre
iOSSwiftSwiftUIApple IntelligenceFoundationModels
FoundationModels: Understanding and Using Apple's New AI Framework

A New Approach to AI on iOS

Apple has introduced a framework that reshapes how developers can bring artificial intelligence into their iOS applications: FoundationModels. Unlike cloud-based solutions such as OpenAI or Anthropic, Apple went in a fundamentally different direction. No requests are sent to external servers. Everything runs locally on the device, leveraging the Neural Engine.

What is FoundationModels?

FoundationModels serves as the gateway to Apple Intelligence's language models within your applications. It is not an external service but a framework built into iOS and macOS, accessible directly through Swift.

Its core principles:

  • Local execution (data never leaves the device)
  • Native system integration
  • Ability to generate structured data using Swift annotations
  • An extensible system with Tools (custom functions the model can invoke)

How it Works

Everything starts with SystemLanguageModel, the primary interface for checking whether Apple Intelligence is available and initiating generation.

let model = SystemLanguageModel.default
 
switch model.availability {
case .available:
    Text("Apple Intelligence ready")
case .unavailable:
    Text("Model not available")
}

You then create a LanguageModelSession with instructions and optional tools. Results are typically received as a stream for real-time display.

To reduce latency further, Apple provides prewarm(), which prepares the model in the background so the first request responds almost instantly.

let session = LanguageModelSession(instructions: Instructions {
  "Generate a short summary."
})
 
await session.prewarm()

Generating Typed Data with @Generable

One of the most compelling innovations is direct generation of typed Swift structs. With the @Generable annotation, you describe the expected data shape and the model fills in the fields accordingly.

@Generable
struct Itinerary {
    @Guide(description: "Trip title")
    let title: String
    @Guide(.count(3))
    let days: [DayPlan]
}
 
@Generable
struct DayPlan {
    let title: String
    let activities: [String]
}

Generation is handled via a ResponseStream<Type>, where each step provides a Snapshot containing the current partial content and metadata such as streaming progress. This enables progressive display as the response is being built.

Guiding Generation with @Guide

The @Guide annotation lets you add constraints or hints. You can enforce formats, restrict element counts, or limit values to predefined sets.

@Guide(.format(.email))
let contactEmail: String
 
@Guide(.count(5))
let tags: [String]
 
@Guide(description: "A list of tags describing the landmark")
let tags: [String]

These guides help the model stay coherent and produce results aligned with your expectations, significantly reducing post-processing.

Extending with Custom Tools

A Tool is a Swift function exposed to the model that it can call when needed. This bridges generative intelligence with your application's business logic.

final class FindCafesTool: Tool {
    @Generable
    struct Arguments {
        let city: String
    }
 
    func call(arguments: Arguments) async throws -> String {
        return "Popular cafes in \(arguments.city): Blue Bottle, Arabica, etc."
    }
}

Once defined, the tool is added to a LanguageModelSession and the model automatically knows it can use it.

SwiftUI Tutorial: A Travel Planner

Putting it all together in a SwiftUI project: a travel planner that generates a personalized itinerary and displays it progressively.

The ViewModel:

@Observable
@MainActor
final class TripPlanner {
    private let session: LanguageModelSession
    private(set) var snapshot: ResponseStream<Itinerary>.Snapshot?
 
    init() {
        self.session = LanguageModelSession(
            instructions: Instructions { "Generate a 3-day trip to Tokyo." }
        )
    }
 
    func generate() async {
        let stream = session.streamResponse(generating: Itinerary.self)
        for try await snapshot in stream {
            self.snapshot = snapshot
        }
    }
}

The SwiftUI view:

struct TripView: View {
    @State private var planner = TripPlanner()
 
    var body: some View {
        VStack {
            if let itinerary = planner.snapshot?.content {
                Text(itinerary.title ?? "Loading...")
            }
        }
        .task { await planner.generate() }
    }
}

In roughly fifty lines of code, you have an app that generates a trip in real time and displays it piece by piece. This illustrates the FoundationModels philosophy: smooth integration, reactivity, and an engaging user experience.

Strengths and Limitations

Strengths: speed, privacy (everything is local), zero network latency, no cloud cost (no token billing, no external API to manage).

Limitations: only available on Apple Intelligence-compatible devices, currently text-focused (no advanced multimodal features yet), no fine-tuning, limited model choice.

Compared to OpenAI, FoundationModels excels in security and system integration but offers less flexibility. A quintessentially Apple approach: closed but optimized.

Conclusion

With FoundationModels, Apple opens a new chapter for developers. AI is no longer a remote service consumed over the network but a built-in system component designed to be fast, private, and transparent. It is not perfect yet, but the direction is clear: local artificial intelligence that enriches our apps without compromising user privacy. And this is undoubtedly just the beginning.

Commentaires

Connecte-toi pour laisser un commentaire.

Aucun commentaire pour le moment. Sois le premier !

More articles

.task vs .onAppear vs .refreshable en SwiftUI

iOS 26 a livré 5 améliorations discrètes en SwiftUI

safeAreaInset vs safeAreaBar en SwiftUI