Integrating ChatGPT in Your iOS Apps - Non-sendable/nonisolated error in tutorial

Performing the tutorial in Lesson 1 Demo playground I get the following issue on client.sendChats…:

Non-sendable result type ā€˜GPTChatResponse’ cannot be sent from non isolated context in call to instance method ā€˜sendChats’

Does anyone know why this is happening and how to fix it.

My develop environment is Sequoia 15.4.1 / Xcode 16.3 on a Mac mini.

I resolved the issue.

When I performed the Lesson 1 Demo I cut and pasted the code from the video transcript because I didn’t see any kind of of starter project link. I got the

Non-sendable result type ā€˜GPTChatResponse’ cannot… error

which I couldn’t resolve. I have since found the link to the project materials. On comparison of the code sources I found no significant differences between my Demo code and the project materials, but the Demo Final playground works as advertised!

I then found that the Playground Settings/Swift version is Swift 5 for the downloaded materials, but for my Demo playground is Swift 6. Changing my Demo playground setting to Swift 5 eliminates the error and allows the Demo to work.

Conclusion: Apparently changes in Swift 6 to concurrent environment sendable/non-sendable and isolated/non-isolated classes handling breaks Swift 5 code. The code would still need to be modified in some way in order to compile if Swift 6.

Hello!
The ā€œNon-sendable result type ā€˜GPTChatResponse’ cannot be sent from non isolated contextā€ error in Xcode 16.3 on Sequoia indicates a Swift Concurrency issue. It means your GPTChatResponse type, returned by the async client.sendChats method, isn’t marked as Sendable, preventing it from safely crossing between different concurrency contexts (like background to main thread). To fix this, make GPTChatResponse conform to the Sendable protocol. If it’s a struct, simply add : Sendable to its definition; if it contains custom types, ensure those are also Sendable. If GPTChatResponse is a class, consider refactoring it to a struct or ensuring its thread safety.

Hello ryan247zook!

Thank you for your response.

I did as you suggested and made GPTChatResponse conform to Sendable, but it just changes the error to:

Sending main actor-isolated ā€˜client’ to nonisolated instance method ā€˜sendChats’ risks causing data races between nonisolated and main actor-isolated uses

My attempts to make ā€˜sendChats’ isolated only led to a cascade of errors. I suspect that a more serious reworking of the code is required for Swift 6.