jamesrochabrun/SwiftOpenAI
The most complete open-source Swift package for interacting with OpenAI's public API.
SwiftOpenAI – Swift‑native client for OpenAI’s API
What it is
SwiftOpenAI is a Swift package that wraps every public OpenAI endpoint (Chat, Audio, Images, Embeddings, Fine‑tuning, etc.) and a handful of compatible services such as Azure OpenAI, Anthropic, Gemini, Ollama, Groq, xAI, OpenRouter, DeepSeek and AIProxy. It lets iOS, macOS, watchOS, and Linux apps call the API with native Swift types, async/await, and built‑in support for streaming, function calling, vision, and the new Realtime voice‑chat API.
Who it’s for
- Swift developers building AI‑enabled apps – chat assistants, voice agents, image generators, transcription tools, etc.
- Teams that need a type‑safe, async‑first wrapper instead of hand‑crafting HTTP requests.
- Projects that run on Apple platforms or Linux (e.g., Vapor back‑ends).
Key features (as described in the README)
| Area | What the package provides |
|---|---|
| Core API coverage | All OpenAI endpoints: Audio (transcriptions, translations, speech synthesis, Realtime), Chat (function calling, structured outputs, vision), Embeddings, Fine‑tuning, Batch, Files, Images, Models, Moderations. |
| Beta/Assistants | Assistant objects, threads, messages, runs, streaming deltas, vector stores, agents – mirroring OpenAI’s Assistants API. |
| Realtime voice | WebSocket‑based low‑latency bidirectional audio, VAD, tool calling, custom voice selection, configurable audio formats. |
| Streaming | Server‑sent‑event style streaming for chat, audio, assistants, etc., exposed as async sequences. |
| Cross‑platform | Works on iOS 15+, macOS 13+, watchOS 9+, and Linux (uses AsyncHTTPClient to avoid URLSession bugs). |
| OpenAI‑compatible providers | Helper initialisers for Azure OpenAI, Anthropic, Gemini, Ollama, Groq, xAI, OpenRouter, DeepSeek, AIProxy, and custom URLs via OpenAIServiceFactory. |
| Error handling | APIError.responseUnsuccessful gives you the HTTP status code (e.g., 429 rate‑limit). |
| Installation | Swift Package Manager – add via Xcode or swift package. The README warns about Xcode’s default upper version bound and how to set it correctly. |
| CLI & MCP | Separate repositories provide a command‑line interface and a “MCP” (presumably a macOS‑compatible package) built on top of the core library. |
How you would use it (quick example from the README)
import SwiftOpenAI
let apiKey = "sk‑…"
let service = OpenAIServiceFactory.service(apiKey: apiKey)
let chatParams = ChatCompletionParameters(
messages: [.init(role: .user, content: .text("Hello, world!"))],
model: .gpt4o)
do {
let response = try await service.startChat(parameters: chatParams)
print(response.choices.first?.message.content?.text ?? "no reply")
} catch APIError.responseUnsuccessful(let desc, let code) {
print("Error \(code): \(desc)")
}
The same pattern applies to audio transcription, speech synthesis, image generation, etc., by swapping the parameter struct and calling the corresponding method on service.
When to pick this library
- You already work in Swift and want a single dependency that stays up‑to‑date with OpenAI’s evolving API surface.
- You need streaming or Realtime voice support, which many generic HTTP wrappers lack.
- Your app targets Apple platforms and you prefer native async/await rather than Combine or callbacks.
- You also want the flexibility to point the client at Azure or other OpenAI‑compatible back‑ends without changing code.
Things to watch out for
- The README mentions a beta Assistants API – those types may change as OpenAI finalises the spec.
- Realtime voice requires AVFoundation and microphone permissions; it is not available on Linux.
- The package versioning may exceed Xcode’s default upper bound (2.0.0), so you must manually set the lower bound when adding the package.
All information above is taken directly from the repository’s README; no additional features have been assumed.
Related
- Project
- Dispatch
- Project
- Project
- Project