SwiftyAISwiftyAI

Search documentation

Find a docs page by title or section

1

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

AreaWhat you useBest for
Text generationgenerateText, AIResponse, GenerationOptionsSummaries, classifiers, rewrite jobs, background tasks, simple assistants
StreamingstreamText, AIStreamChunk, onChunk, onFinishChat UIs, long answers, progress output, cancellation-aware flows
Structured outputgenerateObject, streamObject, AISchema, OutputJSON extraction, typed app state, validation-style model responses
Tools and agentstool, dynamicTool, generateWithTools, streamWithToolsRetrieval, app actions, live data, multi-step reasoning loops
SwiftUI stateAIChat, AICompletion, SwiftyChatObservable chat and completion state without prebuilt UI components
MediagenerateImage, transcribe, generateSpeech, generateVideoImage, audio, transcription, speech, and video model calls
ProvidersDirect providers, AI.configure, ProviderRegistrySwapping providers, local models, tests, private gateways
ExtensionsMiddleware, MCP, utilitiesRequest 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.