◉RORK LABJP
●GPT-6.1 SOL — Added to Rork's model menu (09-29) with Low, Medium and High effort; included on Pro and Max●SONNET 5.5 — Claude Sonnet 5.5 is selectable: strong at UI design and faster than Sonnet 5 (09-28)●SDK 58 — Beta since 09/15 with no stable date yet; running projects in Expo Go has required login since 09/03●WIDGET — A write-up traces an iOS widget that renders black only in production builds●OBSERVE — EAS Observe now records native crashes too (SDK 57.0.21 or later, 10/07)●NEW — My App Open Ad Only Showed When the App…●GPT-6.1 SOL — Added to Rork's model menu (09-29) with Low, Medium and High effort; included on Pro and Max●SONNET 5.5 — Claude Sonnet 5.5 is selectable: strong at UI design and faster than Sonnet 5 (09-28)●SDK 58 — Beta since 09/15 with no stable date yet; running projects in Expo Go has required login since 09/03●WIDGET — A write-up traces an iOS widget that renders black only in production builds●OBSERVE — EAS Observe now records native crashes too (SDK 57.0.21 or later, 10/07)●NEW — My App Open Ad Only Showed When the App…
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 Max236SwiftUI66Guided 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-05-02
Benchmarking Rork Max SwiftUI Native Generation Across 30 Features — What to Delegate and When to Step In
A systematic evaluation of Rork Max's SwiftUI AI generation capabilities across 30 feature categories, rated S through C. Includes practical prompt patterns and code fixes to elevate C-rated features to production quality.
◉ AI Models2026-09-28
Keep Fixing in the Same Thread, or Restore and Rephrase? What I Learned Fixing One Rork Bug Both Ways
When a Rork fix request fails for the third time, do you keep going or use Restore and ask differently? I fixed the same bug both ways and compared round trips, leftover code, and where I now draw the line.
◉ AI Models2026-09-12
Ask Rork for an iOS 27 feature and you may get last year's code back, with no error at all
iOS 27 ships on September 14. When a model is asked for an API it has never seen, there are two kinds of failure: the kind that stops your build, and the kind that does not. Here is why standard Rork and Rork Max differ, and the order I check generated code in.