Swift 6 concurrency w/ Translation framework

I’m trying to integrate Apple’s Translation framework in a Swift 6 project with Approachable Concurrency enabled.

I’m following the code here: Translating text within your app | Apple Developer Documentation

And, specifically, inside the following code

.translationTask(configuration) { session in
            do {
                // Use the session the task provides to translate the text.
                let response = try await session.translate(sourceText)


                // Update the view with the translated result.
                targetText = response.targetText
            } catch {
                // Handle any errors.
            }
        }

On the try await session.translate(…) line, the compiler complains that “Sending ‘session’ risks causing data races”.

Extended error message:

Sending main actor-isolated 'session' to @concurrent instance method 'translate' risks causing data races between @concurrent and main actor-isolated uses

I’ve downloaded Apple’s sample code (at the top of linked webpage), it compiles fine as-is on Xcode 26.4, but fails with the same error as soon as I switch the Swift Language Mode to Swift 6 in the project.

How can I fix this?

Hmmm, this rings some bells…

Yeah, it’s not really an answer, but see Concurrency warning in Translation API on Apple Developer Forums.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

Hey, thanks for your reply!

I managed to get around simply by adding @preconcurrency import Translation instead of just import Translation, and boom, it works in Swift 6 mode without changing the code.

Does this have any side-effect, compared to your solution with marking the closure @Sendable and using await MainActor.run {} to access main-actor-isolated state from inside the closure? (Which works fine, too, but leads to a more verbose code with actor-jumping calls)

May I ask which one seems the most correct, safe, and clean to you?