Apple Developer onepage/WWDC26
#WWDC26#FoundationModels#LLM#SwiftPackage2026-06-08 · 20:41

讓任意 LLM 變成 Foundation Models 的可替換模型

這場 session 面向模型供應商與進階 app 團隊:Apple 把 on-device model、Private Cloud Compute、Core AI、MLX 和第三方模型放進同一個 LanguageModel protocol,重點不只是「換模型」,而是如何正確實作 executor、streaming、state、認證與自訂能力。

// 一句話結論

Foundation Models 正從單一 Apple model API 變成 Swift 生態的 LLM adapter layer;模型供應商真正要交付的是可靠的 LanguageModelExecutor,而不是只包一個 HTTP client。

Apple Developer · WWDC26 · Video ID: u06ZVpSl0J4Apple Developer 官方頁
Content map

這支影片在拆四個 provider integration 問題

封裝

模型要以 Swift Package 形式被採用

Apple 建議用 Swift Package Manager 發佈,支援 iOS、macOS、visionOS、watchOS,並考慮 Foundation Models open source 後的 Linux server-side Swift 用途。

Protocol

Model 輕、Executor 重

LanguageModel 宣告 capabilities 與 executor configuration;LanguageModelExecutor 才負責載入權重、管理連線、轉 transcript、串流 token 與處理錯誤。

狀態

Configuration 是 executor cache key

同一個 session 會用 configuration 查 executor store。若 integration 有 KV cache、persistent session 或 server connection,這個 lifecycle 決定能否避免重做昂貴工作。

擴充

Metadata、custom segments、server-side tools 是差異化點

Provider 可以在不離開 LanguageModelSession 的前提下暴露引用、效能資訊、新 modality 或工具過程,但要明確處理隱私與 developer 可理解性。

Implementation flow

把一個模型接進 Foundation Models 的導入路徑

1

先設計 Swift package 邊界

把 public model conformance、runtime、tests 拆清楚;dependencies 會變成 app shipping bytes,不能把 provider SDK 的複雜度全部丟給 app。

2

實作 LanguageModel 與 executor configuration

capabilities 要誠實描述 tool calling、guided generation、reasoning 等能力;configuration 必須 Hashable,因為它控制 executor reuse。

3

在 executor 轉換 transcript 與 options

把 instructions、prompt、tool calls、tool output、response、reasoning 映射到模型原生 message role;同時讀取 context options 與 generation options。

4

用 streaming channel 回傳可觀測事件

先送 metadata 和 usage,再送 text delta、tool call delta、reasoning 或 custom segment。one-shot API 只是收集 stream,實作仍應以 streaming 為核心。

5

補齊認證、錯誤與隱私契約

不要只收字串 API key;提供 token provider 或 sign-in flow,用 Keychain 保存 token,並用內建 LanguageModelError 或清楚 custom error 告知限制。

重點 1:LanguageModel protocol 是替換模型的表層,executor 才是產品品質

影片一開始展示同一段 app code 可以換成 System Language Model、Private Cloud Compute、Core AI local model 或 MLX Hugging Face model。這個便利的背後,是所有模型都要遵守同一個 LanguageModel protocol。

對 app 開發者而言,這代表 model choice 不應散落在業務邏輯裡;對 provider 而言,真正要寫好的是 executor:載入成本、streaming latency、usage accounting、error semantics 與 state invalidation 都在這裡。

Capabilities 要保守且可測如果宣告支援 guided generation 或 tool calling,executor 就要能處理相關 transcript entry、options、streaming event 與錯誤。
Configuration 不是 metadata它是 executor store 的 lookup key;相同 configuration 會 reuse executor,不同 configuration 會建新 executor。
Prewarm 只能加速,不能當保證prewarm 可提前載入權重或開連線,但 respond 必須在 prewarm 沒跑過時仍能正確 lazy load。
Transcript mapping 需明確策略沒有 dedicated tool role 的模型可以把 tool calls / outputs / reasoning 映射到 assistant,但這是 provider 的相容性決策。

重點 2:狀態與 cache 的難點在 transcript divergence

executor 每次 respond 都會收到完整 transcript。若新的 transcript 只是 append prompt,就能保留既有 KV cache 或 server session,只處理新增內容;若開發者 trim 或修改舊 entry,executor 必須找出 divergence point,回退或重建狀態。

01
保存上一輪可比較的 transcript snapshot
不要只保存模型原生 messages;要能回頭判斷 Foundation Models transcript 哪裡變了。
02
定義 entry match 規則
同樣內容、metadata 變化、custom segment 更新是否算相同,會影響 KV cache 是否可重用。
03
在無法誠實 approximation 時 throw
例如 token limit 與 required schema 衝突時,不要假裝成功;使用內建 LanguageModelError 或清楚的 provider-specific error。

重點 3:認證 API shape 會塑造開發者是否犯錯

Apple 明確提醒:如果 initializer 只吃 API key string,開發者容易把密鑰硬塞進 app。比較好的 provider package 應提供 token provider 或 sign-in flow;若 package 替開發者取 token,應存入 Keychain。雲端模型還應評估 App Attest,避免 tampered builds 或濫用流量。

風險與遷移注意事項

不要把所有錯誤都包成 unknowncontext overflow、rate limit、refusal 等應優先用內建錯誤,降低 app 端學習成本。
Streaming 順序會影響 observability先送 model/request metadata 和 prompt token usage,app 才能即時做 logging、成本估算與 UI feedback。
Custom segments 是 protocol 擴充,不是自由文字新 modality、server-side tool result 或結構化附件都要 typed,否則 app 很難穩定呈現。
模型隱私特性要透明on-device、PCC、third-party cloud 的資料流不同;provider package 與 app 都要讓使用者理解實際使用的模型。

對開發者的實作啟發

01
把模型切換封裝在 composition root
app feature 只依賴 LanguageModelSession;on-device、PCC、MLX、provider package 的選擇留給設定層。
02
為 provider package 建 transcript fixture tests
至少覆蓋 tool calls、tool outputs、reasoning、schema output、trimmed transcript、stream cancellation 與 error mapping。
03
把 response metadata 變成 typed helper
tokensPerSecond、timeToFirstToken、citations、request ID 不應只靠 dictionary key 字串散落在 app code。

來源

主要來源為 Apple Developer 官方影片頁、transcript、chapters 與 code snippets;YouTube 影片頁用於 iframe 與發布 metadata。官方頁:Bring an LLM provider to the Foundation Models framework - WWDC26。YouTube:WWDC26: Bring an LLM provider to the Foundation Models framework | Apple。

// TL;DR