Install
Add SwiftAISDK to your package manifest:
dependencies: [ .package(url: "https://github.com/SwiftAISDK/SwiftAISDK.git", from: "2.8.1"),],targets: [ .target( name: "YourApp", dependencies: [ .product(name: "SwiftAISDK", package: "SwiftAISDK"), ] ),]For an application that must stay on a reviewed SDK build, replace from: with
SwiftPM’s exact: requirement for that release. Production applications should
not depend on the moving main branch.
Then import the library:
import SwiftAISDKCredentials
Section titled “Credentials”Provider factories read upstream-style environment variables by default:
| Provider | Default environment variable |
|---|---|
| OpenAI | OPENAI_API_KEY |
| Anthropic | ANTHROPIC_API_KEY |
| Google Generative AI | GOOGLE_GENERATIVE_AI_API_KEY or GEMINI_API_KEY |
You can also pass credentials explicitly:
let provider = try AIProviders.openAI( settings: ProviderSettings(apiKey: "your-api-key"))Use explicit settings when you need custom headers, a custom base URL, a mock transport, or per-environment credentials.
Independent modular packages
Section titled “Independent modular packages”Core, Anthropic, OpenAI-compatible and OpenAI are available as separate Swift
packages, initially released at 1.0.0. Other providers, MCP and UI remain in the
2.x aggregate above while migration continues.
For a modular OpenAI client:
dependencies: [ .package(url: "https://github.com/SwiftAISDK/swift-ai-core.git", from: "1.0.0"), .package(url: "https://github.com/SwiftAISDK/swift-ai-openai.git", from: "1.0.0"), ], targets: [ .target(name: "YourApp", dependencies: [ .product(name: "SwiftAI", package: "swift-ai-core"), .product(name: "SwiftAIOpenAI", package: "swift-ai-openai"), ]), ]import SwiftAIimport SwiftAIOpenAI
let provider = try AIProviders.openAI( configuration: OpenAIProviderSettings(apiKey: "your-api-key"))let model = try provider.languageModel("gpt-4.1")let result = try await AI.generateText( model: model, request: .init(messages: [.user("Hello")]))Anthropic uses SwiftAIAnthropic and AnthropicProviderSettings. A generic
compatible endpoint uses SwiftAIOpenAICompatible with
OpenAICompatibleProviderSettings(name:baseURL:...). Native factories accept
configuration:; the released compatibility API continues to accept settings:.
Native clients pass model objects or use explicit registries; core does not
initialize an implicit Gateway.
The canonical repository contains complete offline AnthropicOnly,
OpenAICompatibleOnly, OpenAIOnly and MixedProviders examples. OpenAI shares
chat/completion, embedding and image wire code with the compatible package;
selecting it does not require unrelated providers, MCP or UI. See the
migration report
for extraction evidence. The M5 release report records the public tags, hosted compiler checks and clean clients.
Provider versions can be selected independently as long as their shared dependency
requirements intersect. SwiftPM resolves one version per package identity. Swift
release numbers do not track npm release numbers. Applications should commit their
Package.resolved; reusable libraries should normally express compatible ranges.
See the verified mixed-provider lockfile.
Tagged 2.x and the new modular packages own different model/request types. Their values are not interchangeable. The development compatibility facade shares the modular core types, but its versioned aggregate release belongs to the later 3.0 migration. Use each published API with its own model and request values.