RORK LABJP
BUILD — Rork Max runs real Macs in the cloud loaded with Xcode and the iOS SDK, writing SwiftUI, compiling, reading the errors and building again. That loop, not the code generation, is what lifts the outputNATIVE — What comes out is pure Swift and SwiftUI, not React Native. Reaching AR, Metal graphics and widgets that React Native cannot touch is the real gap between this and other buildersPLATFORMS — Coverage spans iPhone, iPad, Apple Watch, Apple TV and Vision Pro, plus iMessage. Worth a look if you want to start from a watch app or an extension rather than a phone screenCOMPANION — The Rork Companion app lets you check a generated build on a real iPhone without a paid Apple Developer account, lowering the bar for trying a first project end to endPRICING — Free to start, paid plans from $25 a month, and Rork Max on the $200 Max plan. Worth working out up front how many projects it takes to earn that backDEADLINE — From August 31, 2026, Google Play requires target API level 36 or higher for new apps and updates alike. Ten days out, and the targetSdkVersion of what you generate is yours to verifyBUILD — Rork Max runs real Macs in the cloud loaded with Xcode and the iOS SDK, writing SwiftUI, compiling, reading the errors and building again. That loop, not the code generation, is what lifts the outputNATIVE — What comes out is pure Swift and SwiftUI, not React Native. Reaching AR, Metal graphics and widgets that React Native cannot touch is the real gap between this and other buildersPLATFORMS — Coverage spans iPhone, iPad, Apple Watch, Apple TV and Vision Pro, plus iMessage. Worth a look if you want to start from a watch app or an extension rather than a phone screenCOMPANION — The Rork Companion app lets you check a generated build on a real iPhone without a paid Apple Developer account, lowering the bar for trying a first project end to endPRICING — Free to start, paid plans from $25 a month, and Rork Max on the $200 Max plan. Worth working out up front how many projects it takes to earn that backDEADLINE — From August 31, 2026, Google Play requires target API level 36 or higher for new apps and updates alike. Ten days out, and the targetSdkVersion of what you generate is yours to verify
Articles/AI Models
AI Models/2026-06-24Advanced

Receiving On-Device AI Output as Typed Data with Foundation Models Guided Generation

How to receive Foundation Models output as typed Swift structs instead of free text, with working code for Guided Generation and Tool Calling on-device.

Foundation Models4On-Device AI6Rork Max233SwiftUI64Guided Generation

Premium Article

When I hand an app feature to on-device AI, the part I have worried about most as an indie developer is reshaping the model's free-text reply into structured data inside the app. Even when I ask the prompt to "return JSON," the model sometimes wraps it in a sentence of explanation, drifts on a key name, or changes shape only when an array is empty. Each time, I add another regular expression or try? to absorb it, and that becomes the most fragile place in the codebase.

The iOS 26 Foundation Models framework ships two mechanisms that remove this problem at the root: Guided Generation, which binds output to a Swift type, and Tool Calling, which lets the model call your own functions. Because Rork Max generates native Swift, you can drop these APIs straight into the generated code. This article walks through moving from free-text parsing to receiving AI output as a type, with code that actually runs.

Why parsing free-text replies is so fragile

With a typical cloud-LLM integration, you tend to write code like this.

// Fragile: take free text and reshape it into JSON yourself
let text = try await callCloudLLM(prompt: "Return 3 recommended meditation themes as a JSON array")
 
// text may include a preamble like "Sure, here you go: [...]"
guard let jsonStart = text.firstIndex(of: "["),
      let data = String(text[jsonStart...]).data(using: .utf8),
      let themes = try? JSONDecoder().decode([String].self, from: data) else {
    // You hit this branch more often than you'd expect
    return fallbackThemes
}

The trouble is that text is prose meant for humans, so its format is never guaranteed. When I added an AI feature to one of my own apps, it was stable during testing, yet after release I got reports that for certain inputs a preamble appeared and parsing failed. The more often you fall back to fallbackThemes, the thinner the value of the AI feature becomes.

Guided Generation removes the "reshape the prose" step entirely. You hand the model a type up front, and it generates only values that conform to that type.

Binding output to a type with @Generable

First, declare the data you want back as a Swift struct and annotate it with @Generable.

import FoundationModels
 
@Generable
struct MeditationTheme {
    @Guide(description: "Session title. Short, under 6 words")
    var title: String
 
    @Guide(description: "What to focus on, in one sentence")
    var focus: String
 
    @Guide(description: "Recommended length in minutes", .range(3...30))
    var durationMinutes: Int
}

A type marked @Generable becomes a blueprint that tells the model "produce output in this shape." Then you just ask the session to generate it.

let session = LanguageModelSession()
 
let response = try await session.respond(
    to: "Suggest one meditation theme for a beginner",
    generating: MeditationTheme.self
)
 
// response.content is a MeditationTheme. There is no parsing step
let theme = response.content
print(theme.title)            // e.g. "Return to the Breath"
print(theme.durationMinutes)  // e.g. 5

This is the heart of it. response.content is not a string; it is a MeditationTheme. No JSONDecoder, no regular expressions. Even if the model tries to produce a value that violates a constraint (say, 40 for durationMinutes), the framework steers generation to respect the type and the @Guide constraints, so by the time it reaches your app it already sits within 3...30.

Here is why this is more trustworthy than regex parsing: hand-rolled parsing is a reactive defense that inspects the string after it is generated, while Guided Generation is a forward constraint that shapes the output during generation. The difference is between fixing something broken and never letting it be built broken.

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
You can replace fragile free-text parsing with @Generable, receiving AI output as type-safe Swift structs
You'll learn how to let the model call your own app functions with Tool Calling, building assistant features that work without a network
You'll be able to ship production-grade on-device AI, including streamed partial generation and graceful fallback when the model is unavailable
Secure payment via Stripe · Cancel anytime

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Rork Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • Copy-paste ready implementation code
  • New advanced guides published daily
  • $5/mo or $10 for lifetime access
View Membership →

Related Articles

AI Models2026-06-14
On-Device Image Tagging in Rork Max Swift Apps with Foundation Models Image Input
WWDC26 gave the on-device Foundation Models model image input. Here is how to add image tagging and captioning to a Rork Max Swift app entirely on-device, including the availability gate, structured output, and Vision interop.
AI Models2026-07-15
On-Device Image Classification: TFLite on React Native or Core ML on Rork Max — How I Chose After Building Both
Adding on-device image classification means choosing between TFLite on React Native and Core ML on Rork Max. I built the same feature both ways, measured the end-to-end breakdown, and worked out what the decision actually hinges on.
AI Models2026-07-01
Long-Form On-Device Transcription with SpeechAnalyzer in Rork Max's Native Swift
Implementation notes on rebuilding long-form, offline transcription with iOS 26's SpeechAnalyzer and SpeechTranscriber after hitting the walls of SFSpeechRecognizer. Covers model asset downloads, feeding audio through an AsyncStream, drawing volatile vs. final results, and the boundary design for Rork Max native code and bridging from Expo — with the pitfalls I actually hit.
📚RECOMMENDED BOOKS
Build a Large Language Model (From Scratch)
Sebastian Raschka
LLM Dev
Prompt Engineering for LLMs
Berryman & Ziegler
Prompting
AI Engineering
Chip Huyen
AI Eng
* Contains affiliate links
See all →