Skip to content

teunlao/swift-ai-sdk

Swift AI SDK

Swift AI SDK

License Release Documentation

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

Documentation | Getting Started | Examples | Discussions

Features

  • Streaming and non-streaming text generation
  • Structured outputs (typed Codable + JSON Schema)
  • Tool/function calling + MCP tools
  • Provider-agnostic API (swap providers without changing call sites)
  • Middleware hooks
  • Experimental video generation

Installation (SwiftPM)

Available on Swift Package Index.

Add the package to your Package.swift:

// Package.swift
dependencies: [
  // Use the latest release tag (e.g. "0.17.6").
  .package(url: "https://github.com/teunlao/swift-ai-sdk.git", from: "0.17.6")
],
targets: [
  .target(
    name: "YourApp",
    dependencies: [
      .product(name: "SwiftAISDK", package: "swift-ai-sdk"),
      .product(name: "OpenAIProvider", package: "swift-ai-sdk")
    ]
  )
]

Quickstart (Streaming)

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, middleware) are in the docs and examples/.

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)
}

Structured Outputs (Typed Codable)

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.17.6: 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.

Tools (Typed)

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.

Providers

Upstream & Parity

This project ports the Vercel AI SDK with a focus on behavior/API parity.

  • Upstream reference (pinned commit): upstream/UPSTREAM.md

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

 
 
 

Contributors