Apple showed how the @Generable and @Guide macros in FoundationModels simplify Swift development
Apple showed how FoundationModels can shorten the path from prompt to a ready data structure. The @Generable macro marks a Swift model as suitable for…
AI-processed from Habr AI; edited by Hamidun News
Apple showed how @Generable and @Guide macros in
FoundationModels simplify Swift development
Apple continues to improve its approach to integrating artificial intelligence into Swift. The latest step is the @Generable and @Guide macros, which allow developers to work with structured generation from language models in a type-safe way.
What are @Generable and @Guide?
@Generable is a macro that marks Swift structures for generation by language models. Instead of getting raw text from an LLM and parsing it manually, developers can now specify a structure and ask the model to generate objects that match it. This approach is more reliable than text-based generation because the model works with a predefined format.
@Guide complements @Generable by providing hints at the field level. With @Guide, developers can specify what each field should contain, giving the model more context for accurate generation. For example, if a field expects a summary, you can tell the model: "This field should contain a brief description, no more than two sentences."
How does structured generation work?
The process is straightforward. First, you define a Swift structure and mark it with @Generable. Then you use a LanguageModelSession to send a prompt to the model. The model understands the structure and returns data that matches your type definition. This eliminates the need for string parsing and reduces the chance of errors.
Apple provides streaming support, so as the model generates data, your app receives chunks progressively. This allows UI updates to happen in real-time instead of waiting for the complete response. SwiftUI integration makes it easy to bind these updates to the interface.
Why @Guide matters
Without @Guide, the model might interpret fields ambiguously. With it, developers gain fine-grained control. Here are the main benefits:
- Clarity — the model understands exactly what each field should contain
- Validation — hints reduce the likelihood of incorrect or irrelevant data
- Consistency — across multiple generations, outputs remain predictable
- Efficiency — fewer retries needed because the first attempt is more likely to succeed
- Type safety — integration with Swift's type system prevents runtime errors
The bigger picture
Apple's approach represents a shift from fragile text parsing to structured, type-safe AI integration. Instead of prompting for natural language and hoping the output can be parsed, developers now work with language models as data generators. This makes AI features more reliable and easier to integrate into production apps. The macros handle the complexity, leaving developers free to focus on their application logic.
Want to stop reading about AI and start using it?
AI News is a curated feed of AI/tech news. Hamidun Academy teaches you to use AI systematically in your work.