Swift Language Models

A Swift package that plugs other providers' models into Apple's Foundation Models API, so OpenRouter and OpenAI models run in the same LanguageModelSession as Apple's on-device model.

View on GitHub ↗
Stars
★ 24
Last commit
2 Dec 2026
Maintenance
Active
Licence
Apache-2.0
Category
On-device AI SDKs · Cloud-capable
GitHub
ironsparrows/swift-language-models

Overview

Swift Language Models lets an app keep Apple's LanguageModelSession, with its transcripts, tools, @Generable guided generation, Dynamic Profiles and reasoning levels, and use models from other services inside it. Switching between Apple's on-device model and, say, Claude, GPT, Gemini, DeepSeek, Qwen, Llama or Mistral changes only the model: argument.

There are now two provider modules: OpenRouterLanguageModels (one key reaches many hosts) and OpenAILanguageModels (OpenAI's Responses API directly). Each provider module re-exports the shared core, LanguageModels.

Swift Language Models is licensed under the Apache License 2.0.

Features

  • Two provider modules: OpenRouter and OpenAI, each with a typed model catalogue.
  • Typed reasoning levels, checked when the code compiles.
  • Routing, fallback models and prompt caching (OpenRouter).
  • Server tools: web search, web fetch, advisors and subagents on OpenRouter; web search, image generation and file search on OpenAI.
  • Attachments: images, and from OS 27.2 also PDFs, text, and audio and video through OpenRouter. Generated images come back as data.
  • Errors map to Apple's LanguageModelError (rate limits, oversized contexts, guardrails, refusals).
  • Works with Apple's Evaluations framework, and includes a scripted model for offline tests.
  • A showcase app in the Example folder.
  • Docs on GitHub Pages for each module.

Requirements

  • iOS, macOS, visionOS or watchOS 27.0.
  • Xcode 27.0, with Swift 6.4.
  • OS 27.2 and Xcode 27.2 for PDF, audio, video and text attachments, and for generated images.
  • An OpenRouter and/or OpenAI API key, depending on the module.
  • It doesn't support tvOS or Linux, because Foundation Models' LanguageModel isn't available there.

Install

  1. Xcode. Choose File > Add Package Dependencies and enter https://github.com/ironsparrows/swift-language-models.

  2. Package.swift. Add the package from version 2.0.0, then add OpenRouterLanguageModels and/or OpenAILanguageModels to each target that uses them.

Example

Import FoundationModels and OpenRouterLanguageModels. Create an OpenRouterLanguageModel with a catalogue model, such as .anthropicClaudeHaiku5_5, and your key. Pass it to LanguageModelSession(model:) and call respond(to:) as you would with SystemLanguageModel.default.

The README warns never to ship an API key inside an app, and shows how to route requests through your own server instead.

Works with

Not stated by the source

Listed in

Read the code before you install anything. SwiftAgents doesn't review or endorse the tools listed here. Active: a commit in the last 90 days. Slowing: last commit within the past year. Stale: older than a year, or archived.

Report a correction

More in this category