SwiftyAI
SwiftyAI is a Swift SDK for building AI features without turning your app into provider glue. The same application code can generate text, stream text, parse structured output, run tool loops, accept multimodal input, call media models, and manage SwiftUI state.
The SDK stays close to Swift. Models are protocols, responses are plain structs, streams are AsyncThrowingStream, and UI helpers are optional state objects rather than prebuilt screens.
The SDK Shape
Most features start with a model and one function call:
import SwiftyAI
let model = OpenAICompatibleProvider(
baseURL: "https://api.openai.com/v1",
apiKey: ProcessInfo.processInfo.environment["OPENAI_API_KEY"]!,
model: "gpt-4o-mini"
)
let response = try await generateText(
model: model,
prompt: "Explain Swift actors in three practical bullets."
)
print(response.text)That same model can usually be reused for streaming, object generation, tools, and SwiftUI hooks if it conforms to the matching protocol.
Core Features
| Area | What you use | Best for |
|---|---|---|
| Text generation | generateText, AIResponse, GenerationOptions | Summaries, classifiers, rewrite jobs, background tasks, simple assistants |
| Streaming | streamText, AIStreamChunk, onChunk, onFinish | Chat UIs, long answers, progress output, cancellation-aware flows |
| Structured output | generateObject, streamObject, AISchema, Output | JSON extraction, typed app state, validation-style model responses |
| Tools and agents | tool, dynamicTool, generateWithTools, streamWithTools | Retrieval, app actions, live data, multi-step reasoning loops |
| SwiftUI state | AIChat, AICompletion, SwiftyChat | Observable chat and completion state without prebuilt UI components |
| Media | generateImage, transcribe, generateSpeech, generateVideo | Image, audio, transcription, speech, and video model calls |
| Providers | Direct providers, AI.configure, ProviderRegistry | Swapping providers, local models, tests, private gateways |
| Extensions | Middleware, MCP, utilities | Request policy, response cleanup, external tool servers |
Choosing A Path
Use direct provider instances when you want explicit dependencies and simple tests. Use model strings when your app has central configuration and wants calls like "openai/gpt-4o-mini" or "ollama/llama3.2".
AI.configure { ai in
ai.openAI(apiKey: ProcessInfo.processInfo.environment["OPENAI_API_KEY"]!)
ai.ollama()
}
let cloud = try await generateText(
model: "openai/gpt-4o-mini",
prompt: "Write a one-line release note."
)
let local = try await generateText(
model: "ollama/llama3.2",
prompt: "Summarize this log file."
)Call AI.configure during app startup before user-driven requests. For code that configures and calls a model immediately, or for library code, tests, and dependency injection, prefer direct model instances or a local ProviderRegistry.
Next Reads
Start with installation, then provider setup and quickstart. After that, follow the path your app needs: text, streaming, objects, tools, SwiftUI hooks, middleware, or MCP.