A new Apple framework for measuring the quality and reliability of intelligent features powered by generative AI. It integrates with Swift Testing to let developers define datasets, metrics, and optimization targets that automatically assess probabilistic outputs from language models.
⢠Solves the core testing problem with generative AI: since the same input can produce different outputs, traditional unit tests are insufficient ā Evaluations lets you measure how often your feature behaves correctly across many samples
⢠Integrates directly with Swift Testing via the .evaluates trait and #expect macros, so evaluations run alongside your existing test suite in Xcode's test report
⢠Includes a SampleGenerator for synthetically expanding small datasets into thousands of samples, enabling statistically meaningful trend detection without manual data authoring
Demonstrates building an Evaluation that measures whether a BookTaggingService generates an appropriate number of tags (3ā8), aggregates results across multiple samples, and asserts an 80% pass rate using Swift Testing.
import Evaluations
import Testing
import FoundationModels
// MARK: - Domain Types
struct BookReview {
let title: String
let reviewText: String
}
struct BookTags {
let tags: [String]
}
// MARK: - Simulated Service
actor BookTaggingService {
func generateTags(for review: BookReview) async throws -> BookTags {
// In production this calls a LanguageModelSession.
// Simulated here for demonstration.
return BookTags(tags: ["classic", "romance", "19th-century", "british", "fiction"])
}
}
// MARK: - Evaluation Definition
struct BookTaggingEvaluation: Evaluation {
typealias Input = BookReview
typealias Output = BookTags
// Step 1: Define what code you are measuring.
func subject(from input: BookReview) async throws -> BookTags {
let service = BookTaggingService()
return try await service.generateTags(for: input)
}
// Step 2: Define the dataset.
var dataset: [ModelSample<BookReview, BookTags>] {
[
ModelSample(
input: BookReview(title: "Pride & Prejudice",
reviewText: "A witty exploration of marriage and social class in Regency England."),
expectedOutput: BookTags(tags: ["classic", "romance", "regency", "british"])
),
ModelSample(
input: BookReview(title: "Dracula",
reviewText: "A gothic horror novel told through letters and diary entries."),
expectedOutput: BookTags(tags: ["horror", "gothic", "vampire", "victorian"])
),
ModelSample(
input: BookReview(title: "The Secret Garden",
reviewText: "A neglected garden transforms a lonely child in this timeless classic."),
expectedOutput: BookTags(tags: ["classic", "children", "nature", "british"])
)
]
}
// Step 3: Define metrics.
var metrics: [any Metric<BookTags>] {
[
Metric(name: "TagCount") { output in
let count = output.tags.count
return (3...8).contains(count) ? .pass : .fail
}
]
}
// Step 4: Summarize across all samples.
func aggregateMetrics(using results: EvaluationResults<BookTags>) -> [AggregateMetric] {
let tagCountMetrics = results.metrics(named: "TagCount")
let passRate = tagCountMetrics.passRate
return [
AggregateMetric(name: "TagCountPassRate", value: passRate)
]
}
}
// MARK: - Swift Testing Integration
@Suite("Book Tagging Quality", .tags(.evaluation))
struct BookTaggingTests {
let evaluation = BookTaggingEvaluation()
@Test("Tag count meets quality bar",
.evaluates(BookTaggingEvaluation(),
notes: ["model": "on-device", "version": "1.0"]))
func tagCountQuality(results: EvaluationResultsBundle<BookTags>) async throws {
let passRate = results.aggregateValue(named: "TagCountPassRate") ?? 0.0
// Assert the service generates the correct number of tags at least 80% of the time.
#expect(passRate >= 0.80, "TagCount pass rate \(passRate) is below the 0.80 optimization target")
}
}Foundation Models is a new Apple framework introduced in iOS 27 that gives developers on-device access to the same Apple Intelligence language model powering system features, enabling text generation, structured output, and tool-calling entirely on-device without a network connection.
iOS 27 opens the Foundation Models framework to third-party LLM providers via a new public LanguageModel protocol, enabling anyone to integrate custom, server-based, or open-source models using the same Swift API as Apple's on-device system model.
App Schemas let developers describe their app's content and actions using pre-defined domain schemas (like the Calendar domain) so Siri can understand, search, and act on app data without custom NLP. Entities conforming to IndexedEntity are donated to Spotlight's semantic index, enabling natural-language queries over app content.
In-depth guide
iOS 27 On-Device AI & Apple Intelligence āEvaluations measure probabilistic behavior ā optimization targets like 80% pass rate are intentional design choices, not bugs. Running evaluations with only 2 samples gives misleading trends; aim for hundreds or thousands of samples. The .evaluates test trait and Evaluations report navigator are new Xcode features exclusive to Xcode 18+.
Features powered by on-device language models require Apple Intelligence-capable devices (iPhone 15 Pro or later, iPad and Mac with M1 or later).
Visual Intelligence brings iOS 17's Visual Look Up capabilities to a new developer-facing API surface in iOS 27, letting apps pipe live camera frames or static images through on-device scene understanding to extract subjects, text, barcodes, and rich semantic labels without any cloud round-trip.