Apple Foundation Models

Oct 2 2025 · Swift 6.2, iOS 26, macOS 26, iPadOS 26, XCode 26

Lesson 03: Extending Apple Foundation Models

Streaming Guided Generation Data

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Streaming Guided Generation

Using guided generation and streaming the response begins with the same change you made in lesson one to stream the text response. Replace the current call to respond(to:) in generateLunchMenu() with:

let streamedResponse = session.streamResponse(to: prompt, generating: RestaurantMenu.self)
do {
  for try await partialResponse in streamedResponse {
    menu = partialResponse.content
  }
} catch {
  print(error.localizedDescription)
}
@State var menu: RestaurantMenu.PartiallyGenerated?
if let menu = menu {
  if let menuItems = menu.menu {
    ScrollView {
      ForEach(menuItems, id: \.name) { item in
        MenuItemView(menuItem: item)
        Divider()
      }
    }
  }
}
var menuItem: MenuItem.PartiallyGenerated
HStack {
  Text(menuItem.name ?? "")
  Spacer()
  if let cost = menuItem.cost {
    Text(cost, format: .currency(code: "USD"))
  }
}
.font(.title)
Text(menuItem.description ?? "")
  .frame(maxWidth: .infinity, alignment: .leading)
  .padding(.leading, 15.0)
  .font(.headline)
if let ingredients = menuItem.ingredients {
  Text(ingredients.joined(separator: " • "))
    .font(.subheadline)
}
MenuItemView(
  menuItem: item.asPartiallyGenerated()
)
See forum comments
Cinema mode Download course materials from Github
Previous: Generating Custom Data Structures Next: Dynamic Guided Generation