Cannot load .mtlpackage to MTLLibrary

After watching WWDC 2025 session "Combine Metal 4 machine learning and graphics", I have decided to give it a shot to integrate the latest MTL4MachineLearningCommandEncoder to my existing render pipeline. After a lot of trial and errors, I managed to set up the pipeline and have the app compiled.

However, I am now stuck on creating a MTLLibrary with .mtlpackage.

Here is the code I have to create a MTLLibrary according the WWDC session https://developer.apple.com/videos/play/wwdc2025/262/?time=550:

        let coreMLFilePath = bundle.path(forResource: "my_model", ofType: "mtlpackage")!
       let coreMLURL = URL(string: coreMLFilePath)!
       do {
             metalDevice.makeLibrary(URL: coreMLURL)
       } catch {
             print("error: \(error)")
       }

With the above code, I am getting error:


Error Domain=MTLLibraryErrorDomain Code=1 "Invalid metal package" UserInfo={NSLocalizedDescription=Invalid metal package}

What is the correct way to create a MTLLibrary with .mtlpackage? Do I see this error because the .mtlpackage I am using is incorrect? How should I go with debugging this? I'd really appreciate if I could get some help on this as I have been stuck with it for some time now. Thanks in advance!

Answered by DTS Engineer in 884988022

Your approach of loading the .mtlpackage with makeLibrary(URL:) is correct — this matches the workflow shown in the WWDC session you referenced.

The issue is how you're creating the URL. URL(string:) expects a URL-formatted string (like file:///path/to/file), not a raw file path. When you pass a file path from bundle.path(forResource:ofType:) to URL(string:), the resulting URL is malformed, which is why Metal reports "Invalid metal package."

Replace:

let coreMLURL = URL(string: coreMLFilePath)!

with:

let coreMLURL = URL(fileURLWithPath: coreMLFilePath)

Or alternatively, use bundle.url(forResource:withExtension:) which returns a properly formed file URL directly:

let coreMLURL = bundle.url(forResource: "my_model", withExtension: "mtlpackage")!
let library = try metalDevice.makeLibrary(URL: coreMLURL)

Your approach of loading the .mtlpackage with makeLibrary(URL:) is correct — this matches the workflow shown in the WWDC session you referenced.

The issue is how you're creating the URL. URL(string:) expects a URL-formatted string (like file:///path/to/file), not a raw file path. When you pass a file path from bundle.path(forResource:ofType:) to URL(string:), the resulting URL is malformed, which is why Metal reports "Invalid metal package."

Replace:

let coreMLURL = URL(string: coreMLFilePath)!

with:

let coreMLURL = URL(fileURLWithPath: coreMLFilePath)

Or alternatively, use bundle.url(forResource:withExtension:) which returns a properly formed file URL directly:

let coreMLURL = bundle.url(forResource: "my_model", withExtension: "mtlpackage")!
let library = try metalDevice.makeLibrary(URL: coreMLURL)
Cannot load .mtlpackage to MTLLibrary
 
 
Q