Skip to content

teunlao/swift-ai-sdk

Swift AI SDK

Tests codecov License Release Documentation

A unified AI SDK for Swift: streaming chat/completions, structured outputs, tool/function/MCP calling, middleware, and telemetry — with 28+ providers via one API (OpenAI, Anthropic, Google, Groq, xAI). Based on the Vercel AI SDK with a focus on full API parity.

Documentation | Getting Started | Examples | Discussions

Table of Contents

Features

  • Text Generation - Streaming and non-streaming
  • Structured Outputs - Type-safe object generation with schemas
  • Tool Calling - Function calling and MCP tools
  • Multi-Provider - OpenAI, Anthropic, Google (Gemini), Groq, xAI, and 28+ providers
  • Middleware System - Extensible request/response processing
  • Telemetry - Built-in observability

Packages

  • SwiftAISDK - Main SDK with text generation, streaming, tools
  • AISDKProvider - Foundation types and interfaces
  • AISDKProviderUtils - Provider utilities
  • *Provider - 28+ provider packages (see full list)

Implementation Status

Updated: 2025-10-26 Upstream: Based on Vercel AI SDK 6.0.0-beta.42

Category Tests Coverage
Core SDK 1598 100%
Providers 645 69.2%
Overall 2243 79.5%
Provider Details
Provider Impl Tests Upstream Swift Coverage
openai 290 290 100%
anthropic 114 114 100%
google 155 155 100%
groq 58 58 100%
xai 50 50 100%
mistral 44 44 100%
azure 26 26 100%
baseten 25 25 100%
openai-compatible 128 128 100%
deepseek 13 13 100%
replicate 11 11 100%
lmnt 9 9 100%
cerebras 7 7 100%
perplexity 19 0 0%
cohere 48 0 0%
deepinfra 18 0 0%
elevenlabs 15 0 0%
deepgram 6 0 0%
assemblyai 6 0 0%
fal 26 0 0%
fireworks 23 0 0%
gladia 6 0 0%
huggingface 32 0 0%
google-vertex 78 0 0%
amazon-bedrock 152 0 0%
gateway 9 0 0%
togetherai 17 0 0%
luma 16 0 0%
hume 9 0 0%
revai 6 0 0%
vercel 4 0 0%
TOTAL 28/32 13/32 1520 930 61.2%

Installation (SwiftPM)

Available on Swift Package Index.

Add the package to your Package.swift:

// Package.swift
dependencies: [
  .package(url: "https://github.com/teunlao/swift-ai-sdk.git", from: "0.5.0")
],
targets: [
  .target(
    name: "YourApp",
    dependencies: [
      .product(name: "SwiftAISDK", package: "swift-ai-sdk"),
      .product(name: "OpenAIProvider", package: "swift-ai-sdk")
    ]
  )
]

Quickstart

Minimal text generation and streaming with OpenAI:

import SwiftAISDK
import OpenAIProvider

@main
struct Demo {
  static func main() async throws {
    // Set OPENAI_API_KEY in your environment (loaded lazily by the provider).

    // Streaming text
    let stream = try streamText(
      model: openai("gpt-5"),
      prompt: "Stream one sentence about structured outputs."
    )
    for try await delta in stream.textStream {
      print(delta, terminator: "")
    }
  }
}

More examples (tools, structured output, telemetry, middleware) are available in the documentation.

Unified Provider Architecture

Switch providers without changing code — the function signature stays the same regardless of provider.

import SwiftAISDK
import OpenAIProvider
import AnthropicProvider
import GoogleProvider

let models: [LanguageModel] = [
  openai("gpt-5"),
  anthropic("claude-4.5-sonnet"),
  google("gemini-2.5-pro")
]

for model in models {
  let result = try await generateText(
    model: model,
    prompt: "Invent a new holiday and describe its traditions."
  )
  print(result.text)
}

Usage: Structured Data (generateObject)

Generate structured data validated by JSON Schema or Codable.

Example: extract a release summary into a Release type using Schema.codable.

import SwiftAISDK
import OpenAIProvider

struct Release: Codable, Sendable {
  let name: String
  let version: String
  let changes: [String]
}

let summary = try await generateObject(
  model: openai("gpt-5"),
  schema: Release.self,
  schemaName: "release_summary",
  prompt: "Summarize Swift AI SDK 0.1.0: streaming + tools."
).object

print("Release: \\(summary.name) (\\(summary.version))")
summary.changes.forEach { print("- \($0)") }

Notes: use generateObjectNoSchema(...) for raw JSONValue; arrays/enums via generateObjectArray / generateObjectEnum.

Usage: Agents & Tools

Models can call tools. Typed weather example:

import SwiftAISDK
import OpenAIProvider
import Foundation

struct WeatherQuery: Codable, Sendable {
  let location: String
}

struct WeatherReport: Codable, Sendable {
  let location: String
  let temperatureFahrenheit: Int
}

let weatherTool = tool(
  description: "Get the weather in a location",
  inputSchema: WeatherQuery.self,
  execute: { (query, _) in
    WeatherReport(
      location: query.location,
      temperatureFahrenheit: Int.random(in: 62...82)
    )
  }
)

let result = try await generateText(
  model: openai("gpt-4.1"),
  tools: ["weather": weatherTool.tool],
  prompt: "Use the weather tool to fetch the weather for San Francisco."
)

if let toolResult = result.toolResults.first {
  let report = try weatherTool.decodeOutput(from: toolResult)
  print(report)
}

Notes: tool(...) auto-generates schemas from Codable types. For streaming, use streamText(..., tools: ...) and consume textStream/toolResults.

Templates & Examples

See examples/ in this repo and the docs site under apps/docs.

Upstream & Parity

Based on Vercel AI SDK 6.0.0-beta.42 (commit 77db222ee).

Contributing

Contributions welcome. See CONTRIBUTING.md for guidelines.

License

Apache 2.0. Portions adapted from the Vercel AI SDK under Apache 2.0.

About

Unified AI SDK for Swift, bringing the power of Vercel AI SDK to Apple platforms

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •