huggingface /AnyLanguageModel
An API-compatible, drop-in replacement for Apple's Foundation Models framework with support for custom language model providers.
★ 993⑂ 111👁 12⑃ 352● 87▤ 41
+0
日增长
+3
周增长
993
stars
111
forks
12
watchers
352
prs
87
issues
41
releases
README
AnyLanguageModel
A Swift package that provides a drop-in replacement for
Apple's Foundation Models framework
with support for custom language model providers.
All you need to do is change your import statement:
diff
- import FoundationModels
+ import AnyLanguageModel
Anything AnyLanguageModel adds beyond Apple's API
is marked as an extension in its documentation.
Differences from Foundation Models
summarizes these extensions.
swift
struct WeatherTool: Tool {
let name = "getWeather"
let description = "Retrieve the latest weather information for a city"
@Generable
struct Arguments {
@Guide(description: "The city to fetch the weather for")
var city: String
}
func call(arguments: Arguments) async throws - String {
"The weather in \(arguments.city) is sunny and 72°F / 23°C"
}
}
let model = SystemLanguageModel.default
let session = LanguageModelSession(model: model, tools: [WeatherTool()])
let response = try await session.respond {
Prompt("How's the weather in Cupertino?")
}
print(response.content)
To observe or control tool execution, assign a delegate on the session:
swift
actor ToolExecutionObserver: ToolExecutionDelegate {
func didGenerateToolCalls(_ toolCalls: [Transcript.ToolCall], in session: LanguageModelSession) async {
print("Generated tool calls: \(toolCalls)")
}
func toolCallDecision(
for toolCall: Transcript.ToolCall,
in session: LanguageModelSession
) async - ToolExecutionDecision {
// Return .stop to halt after tool calls, or .provideOutput(...) to bypass execution.
// This is a good place to ask the user for confirmation (for example, in a modal dialog).
.execute
}
func didExecuteToolCall(
_ toolCall: Transcript.ToolCall,
output: Transcript.ToolOutput,
in session: LanguageModelSession
) async {
print("Executed tool call: \(toolCall)")
}
}
let session = LanguageModelSession(model: model, tools: [WeatherTool()])
session.toolExecutionDelegate = ToolExecutionObserver()
Features
Supported Providers
- [x] Apple Foundation Models
- [x] Apple Private Cloud Compute and any FoundationModels.LanguageModel conformer, including Core AI models (OS 27)
- [x] Core ML models
- [x] MLX models
- [x] llama.cpp (GGUF models)
- [x] Ollama HTTP API
- [x] Anthropic Messages API
- [x] Google Gemini API
- [x] OpenAI Chat Completions API
- [x] OpenAI Responses API
- [x] Open Responses (multi-provider Responses API–compatible endpoints)
Requirements
- Swift 6.3+ (Xcode 26.4+);
PrivateCloudComputeLanguageModel and FoundationLanguageModel
compile only with Swift 6.4+ (Xcode 27+) and aren't available on tvOS
- iOS 17.0+ / macOS 14.0+ / tvOS 17.0+ / watchOS 10.0+ / visionOS 1.0+ / Linux / WASI
- SystemLanguageModel isn't available on watchOS,
and is available on tvOS only with Swift 6.3
- On WASI, remote models need a transport that you provide
- CI builds watchOS with XcodTop issues
#210Roadmap: 1.0 (Foundation Models 26) and 2.0 (Foundation Models 27)7 comments · 2026-09-02#114Feature Request: Automatic Context Window Management / Trimming5 comments · 2026-02-05#302Support the Gemini Interactions API in GeminiLanguageModel2 comments · 2026-10-06#38Audio input support?2 comments · 2025-11-23
Latest release
1.4.1 1.4.1 2026-11-20This release fixes how AnyLanguageModel sends instructions to Anthropic and Gemini. Both providers used to get a session's instructions as a user message, so the model read them as something the user said. AnthropicLanguageModel now sends them in the top-level system field (433), and GeminiLanguageModel sends them in systemInstruction (436). Sessions with instructions can respond differently than they did in 1.4.0. For Anthropic, a system value that you set in extraBody still wins. OpenAILanguageModel now sorts the keys of every request body at every level, so the same request always encodes