SwiftData crashes on insert (Swift 6, Xcode 16, macOS 15)

I'm trying to insert values into my SwiftData container but it crashes on insert context.insert(fhirObject) and the only error I get from Xcode is:

@Transient
private var _hkID: _SwiftDataNoType?
// original-source-range: /Users/cyril/Documents/GitHub/MyApp/MyApp/HealthKit/FHIR/FHIRModels.swift:35:20-35:20

configured as follows:

import SwiftData
import SwiftUI

@main
struct MyApp: App {
    
    var container: ModelContainer
    
    init() {
        do {
            let config = ModelConfiguration(cloudKitDatabase: .private("iCloud.com.author.MyApp"))
            container = try ModelContainer(for: FHIRObservation.self, configurations: config)
            UserData.shared = UserData(modelContainer: container)
        } catch {
            fatalError("Failed to configure SwiftData container.")
        }
    }
    
    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(UserData.shared) 
        }
        .modelContainer(container)
    }
}

My UserData and DataStore are configured as follows:

import SwiftUI
import SwiftData
import os

/// `FHIRDataStore` is an actor responsible for updating the SwiftData db as needed on the background thread.
private actor FHIRDataStore {
    let logger = Logger(
        subsystem:
            "com.author.MyApp.FHIRDataStore",
        category: "ModelIO")
    
    private let container: ModelContainer
    
    init(container: ModelContainer) {
        self.container = container
    }
    
    func update(newObservations: [FHIRObservationWrapper], deletedObservations: [UUID]) async throws {
        let context = ModelContext(container)
        
        // [FHIRObservationWrapper] -> [FHIRObservation]
//        let modelUpdates = newObservations.lazy.map { sample in
//            FHIRObservation(hkID: sample.hkID, fhirID: sample.fhirID, name: sample.name, status: sample.status, code: sample.code, value: sample.value, range: sample.range, lastUpdated: sample.lastUpdated)
//        }
        
        do {
            try context.transaction {
                for sample in newObservations {
                    let fhirObject = FHIRObservation(hkID: sample.hkID, fhirID: sample.fhirID, name: sample.name, status: sample.status, code: sample.code, value: sample.value, range: sample.range, lastUpdated: sample.lastUpdated)
                    // App crashes here
                    context.insert(fhirObject)
                }
//                for obj in modelUpdates {
//
//                }
                //            try context.delete(model: FHIRObservation.self, where: #Predicate { sample in
                //                deletedObservations.contains(sample.hkID!)
                //            })
                //            try context.save()
                
                logger.debug("Database updated successfully with new and deleted observations.")
            }
        } catch {
            logger.debug("Catch me bro: \(error.localizedDescription)")
        }
    }
}

@MainActor
public class UserData: ObservableObject {
    
    let userDataLogger = Logger(
        subsystem:
            "com.author.MyApp.UserData",
        category: "Model")
    
    public static var shared: UserData!
    
    // MARK: - Properties
    public lazy var healthKitManager = HKManager(withModel: self)
    
    let modelContainer: ModelContainer
        private var store: FHIRDataStore
        
        init(modelContainer: ModelContainer) {
            self.modelContainer = modelContainer
            self.store = FHIRDataStore(container: modelContainer)
        }
    
    // MARK: - Methods
    func updateObservations(newObservations: [FHIRObservationWrapper], deletedObservations: [UUID]) {
        Task {
            do {
                try await store.update(newObservations: newObservations, deletedObservations: deletedObservations)
                userDataLogger.debug("Observations updated successfully.")
            } catch {
                userDataLogger.error("Failed to update observations: \(error.localizedDescription)")
            }
        }
    }
    
}

My @Model is configured as follows:

public struct FHIRObservationWrapper: Sendable {
    let hkID: UUID
    let fhirID: String
    var name: String
    var status: String
    var code: FHIRCodeableConcept
    var value: FHIRLabValueType
    var range: FHIRLabRange?
    var lastUpdated: Date?
}

@Model
public final class FHIRObservation {
    let hkID: UUID?
    let fhirID: String?
    @Attribute(.allowsCloudEncryption) var name: String?
    @Attribute(.allowsCloudEncryption) var status: String?
    @Attribute(.allowsCloudEncryption) var code: FHIRCodeableConcept?
    @Attribute(.allowsCloudEncryption) var value: FHIRLabValueType?
    @Attribute(.allowsCloudEncryption) var range: FHIRLabRange?
    @Attribute(.allowsCloudEncryption) var lastUpdated: Date?
    
    init(hkID: UUID?, fhirID: String?, name: String? = nil, status: String? = nil, code: FHIRCodeableConcept? = nil, value: FHIRLabValueType? = nil, range: FHIRLabRange? = nil, lastUpdated: Date? = nil) {
        self.hkID = hkID
        self.fhirID = fhirID
        self.name = name
        self.status = status
        self.code = code
        self.value = value
        self.range = range
        self.lastUpdated = lastUpdated
    }
}

Any help would be greatly appreciated!

It seems the crash only occurs if I use:

 try context.transaction { }

I wonder why

The app now crashes at context.save() with exception: this class is not key value coding-compliant for the key _buffer."

SwiftData crashes on insert (Swift 6, Xcode 16, macOS 15)
 
 
Q