The operation couldn’t be completed. (FoundationModels.LanguageModelSession.GenerationError error 4.)

Is there anywhere we can reference error codes? I'm getting this error: "The operation couldn’t be completed. (FoundationModels.LanguageModelSession.GenerationError error 4.)" and I have no idea of what it means or what to attempt to fix.

You try catach the error, like:

catch LanguageModelSession.GenerationError.guardrailViolation {...}
catch LanguageModelSession.GenerationError.exceededContextWindowSize { ... }
catch { answer = error.localizedDescription }

If I am not wrong, this is theguardrail violation (prompt too sensitive). Change the prompt.

You should be able to catch the error with the following code:

do {
    let response = try await session.respond(to: prompt)
} catch let error {
    handleGeneratedError(error as! LanguageModelSession.GenerationError)
}

private func handleGeneratedError(_ error: LanguageModelSession.GenerationError) {
    switch error {
    case .exceededContextWindowSize(let context):
        presentGeneratedError(error, context: context)
        
    case .assetsUnavailable(let context):
        presentGeneratedError(error, context: context)

    case .guardrailViolation(let context):
        presentGeneratedError(error, context: context)

    case .unsupportedGuide(let context):
        presentGeneratedError(error, context: context)

    case .unsupportedLanguageOrLocale(let context) :
        presentGeneratedError(error, context: context)
        
    case .decodingFailure(let context) :
        presentGeneratedError(error, context: context)
        
    case .rateLimited(let context) :
        presentGeneratedError(error, context: context)
        
    default:
        print("Failed to respond: \(error.localizedDescription)")
    }
}

private func presentGeneratedError(_ error: LanguageModelSession.GenerationError,
                                   context: LanguageModelSession.GenerationError.Context) {
    print("""
        Failed to respond: \(error.localizedDescription).
        Failure reason: \(String(describing: error.failureReason)).
        Recovery suggestion: \(String(describing: error.recoverySuggestion)).
        """)
    print("Failed to respond: \(context)")
}

FoundationModels.LanguageModelSession.GenerationError error 4 points to .unsupportedLanguageOrLocale, which occurs "if the model is prompted to respond in a language that it does not support."

If you can share your system language and locale , and the instructions and prompt you use, I'd try to reproduce the error

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

The operation couldn’t be completed. (FoundationModels.LanguageModelSession.GenerationError error 4.)
 
 
Q