Back to Swift Reference Ask BeeAI
🧠 Apple IntelligenceAdvancediOS 26+
@Generable Guided Generation
Get typed Swift structs back from the model instead of parsing free-form text.
Mark a struct @Generable and describe fields with @Guide. The framework constrains the model output so it always decodes into your type.
This removes brittle JSON parsing and makes AI features as safe to use as any other Swift API.
generable.swift
1import FoundationModels
2
3@Generable
4struct Recipe {
5 @Guide(description: "A short, appetizing title")
6 var title: String
7 @Guide(description: "Ingredients with quantities", .count(3...8))
8 var ingredients: [String]
9 @Guide(description: "Minutes to cook", .range(5...120))
10 var minutes: Int
11}
12
13func suggestRecipe(from pantry: [String]) async throws -> Recipe {
14 let session = LanguageModelSession()
15 let response = try await session.respond(
16 to: "Create a recipe using: \(pantry.joined(separator: ", "))",
17 generating: Recipe.self
18 )
19 return response.content
20}Common pitfalls
- Keep generable types small and well described — clear @Guide text noticeably improves output quality.
Still unclear? Ask BeeAI about @Generable Guided Generation.
Get a tailored explanation, or paste your own code for a Swift 6 review. Free members get daily questions — Pro is unlimited.
Related topics
Become a Founding Pro member: unlimited BeeAI, upcoming Swift paths and every new reference entry first.
See Pro Plans