Concurrency warning in Translation API

I encountered a concurrency compilation warning when calling the TranslationSession.translations(from: [TranslationSession.Request]) API, and I'm don't know how to resolve it. I reviewed the official demo, but it appears identical.

Answered by DTS Engineer in 868311022

This is tricky. My best guess is that translationTask(_:action:) is missing all the magic concurrency decorations required to make this work nicely. To get this to build right now you can mark the closure as sendable, like so:

.translationTask(configuration) { @Sendable session in
    do {
        try await session.prepareTranslation()
    } catch {
        // Handle any errors.
    }
}

This compiles in Xcode 26.1 using the Swift 6 language mode and:

Note I’m using prepareTranslation() in my examples because it’s simpler, and that makes it easier to focus on the core issue.

The drawback to this is that you can no longer access main-actor-isolated state from the closure. You can work around that using MainActor.run(…):

try await session.prepareTranslation()
… no isolation …
await MainActor.run {
    … main-actor isolated …
}

Clearly this is less than ideal and I encourage you to file a bug against… actually, I think it’d be best to file a bug against the Translating text within your app sample code. We can use that to either update the sample or drive improvements in the API.

Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

This is tricky. My best guess is that translationTask(_:action:) is missing all the magic concurrency decorations required to make this work nicely. To get this to build right now you can mark the closure as sendable, like so:

.translationTask(configuration) { @Sendable session in
    do {
        try await session.prepareTranslation()
    } catch {
        // Handle any errors.
    }
}

This compiles in Xcode 26.1 using the Swift 6 language mode and:

Note I’m using prepareTranslation() in my examples because it’s simpler, and that makes it easier to focus on the core issue.

The drawback to this is that you can no longer access main-actor-isolated state from the closure. You can work around that using MainActor.run(…):

try await session.prepareTranslation()
… no isolation …
await MainActor.run {
    … main-actor isolated …
}

Clearly this is less than ideal and I encourage you to file a bug against… actually, I think it’d be best to file a bug against the Translating text within your app sample code. We can use that to either update the sample or drive improvements in the API.

Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Concurrency warning in Translation API
 
 
Q