RORK LABJP
FUNDING — Rork raised a $15M seed led by Left Lane Capital, with Peak XV, True Ventures, Goodwater, and a16z Speedrun joiningENGINE — Rork Max runs on Claude Code and Claude Opus 4.6; it drew 8M+ views on X and doubled annual revenue in two weeksSWIFT — Rork Max is the first web-based Swift app builder, positioned to replace Apple's traditional XcodePRODUCT — Rork Max covers the whole Apple ecosystem: iPhone, iPad, Apple Watch, Apple TV, Vision Pro, and iMessageCLASSIC — The original Rork uses React Native (Expo), building iOS/Android apps from a plain-English descriptionPRICING — Start free; paid plans begin at $25/mo, and Rork Max is $200/moFUNDING — Rork raised a $15M seed led by Left Lane Capital, with Peak XV, True Ventures, Goodwater, and a16z Speedrun joiningENGINE — Rork Max runs on Claude Code and Claude Opus 4.6; it drew 8M+ views on X and doubled annual revenue in two weeksSWIFT — Rork Max is the first web-based Swift app builder, positioned to replace Apple's traditional XcodePRODUCT — Rork Max covers the whole Apple ecosystem: iPhone, iPad, Apple Watch, Apple TV, Vision Pro, and iMessageCLASSIC — The original Rork uses React Native (Expo), building iOS/Android apps from a plain-English descriptionPRICING — Start free; paid plans begin at $25/mo, and Rork Max is $200/mo
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 AI3Rork Max182SwiftUI51Guided 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-06-14
Calling Apple Foundation Models from a Rork (Expo) App: Bridging On-Device AI Through a Native Module
Rork generates Expo (React Native) apps, but Apple Foundation Models ships as a Swift framework you can't touch from JavaScript. Here's how to write an Expo Modules API bridge, gate it by availability, and fall back to the cloud on unsupported devices.
AI Models2026-05-04
What Can Rork Max Actually Generate in SwiftUI? — Real-Device Testing in 2026
An honest assessment of Rork Max's SwiftUI native app generation — what it handles well, where it struggles, and what that means for your App Store submission. Based on real-device testing.
📚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 →