Skip to content

Navigating the Library

This page describes the Swift AI SDK structure. It differs from the original TypeScript SDK which includes web-specific UI libraries.

The Swift AI SDK is a comprehensive toolkit for building AI applications in Swift across all Apple platforms and server-side Swift. This page explains the structure and available components.

The Swift AI SDK ports the AI SDK Core from the TypeScript AI SDK, providing:

  • AI SDK Core (SwiftAISDK): Unified API for generating text, structured objects, and tool calls with LLMs
  • Provider System: Pre-built integrations for OpenAI, Anthropic, Google, Groq, and OpenAI-compatible providers
  • Foundation Types (AISDKProvider): Language model interfaces, provider protocols, and shared types
  • Utilities (AISDKProviderUtils): HTTP clients, JSON handling, schema validation, retry logic
  • Zod Adapter (AISDKZodAdapter): Zod-like schema DSL for structured data validation

The Swift AI SDK focuses on core AI functionality and does not include web-specific libraries:

  • AI SDK UI (React/Svelte/Vue hooks like useChat, useCompletion) - JavaScript/TypeScript only
  • AI SDK RSC (React Server Components utilities) - Next.js specific

If you’re building hybrid applications with Swift backends and JavaScript frontends, you can use the TypeScript AI SDK UI with Swift AI SDK Core on the server.

The Swift AI SDK is organized into focused packages:

PackagePurposeDependencies
AISDKProviderFoundation types and protocols (LanguageModelV3, etc.)None
AISDKProviderUtilsHTTP, JSON, schema, validation, retry utilitiesAISDKProvider
SwiftAISDKCore API (generateText, streamText, generateObject)Provider, ProviderUtils
OpenAIProviderOpenAI integration (GPT-4, GPT-3.5, embeddings, etc.)Provider, ProviderUtils
AnthropicProviderAnthropic Claude integrationProvider, ProviderUtils
GoogleProviderGoogle Gemini integrationProvider, ProviderUtils
GroqProviderGroq integrationProvider, ProviderUtils
OpenAICompatibleProviderOpenAI-compatible API integrationProvider, ProviderUtils
AISDKZodAdapterZod-like schema DSL for structured dataAISDKProvider
EventSourceParserServer-Sent Events (SSE) parsing (internal utility)None

Swift AI SDK works across all Apple platforms and server-side Swift:

PlatformSupport
iOS / iPadOS
macOS
watchOS
tvOS
Server-side Swift
Linux (Swift on Linux)

Minimum versions:

  • macOS 13+
  • iOS 16+
  • watchOS 9+
  • tvOS 16+
import SwiftAISDK
import OpenAIProvider
let result = try await generateText(
model: openai("gpt-4o"),
prompt: "Write a short story about a cat."
)
print(result.text)
let stream = try await streamText(
model: openai("gpt-4o"),
prompt: "Count to 10."
)
for try await textPart in stream.textStream {
print(textPart, terminator: "")
}
import AISDKZodAdapter
let schema = z.object([
"name": z.string(),
"age": z.number()
])
let result = try await generateObject(
model: openai("gpt-4o"),
schema: schema,
prompt: "Generate a person profile."
)
print(result.object)
let result = try await generateText(
model: openai("gpt-4o"),
tools: [
"getWeather": tool(
description: "Get weather for a location",
parameters: z.object(["location": z.string()])
) { args in
return "Sunny, 72°F"
}
],
prompt: "What's the weather in San Francisco?"
)
let result = try await embed(
model: openai.embedding("text-embedding-3-small"),
value: "Hello, world!"
)
print(result.embedding) // [0.123, -0.456, ...]

Choose a quickstart based on your environment:

If you’re building a web application with a Swift backend:

  1. Backend (Swift): Use Swift AI SDK Core for LLM calls, embeddings, and structured data generation
  2. Frontend (JavaScript/TypeScript): Use AI SDK UI for React/Vue/Svelte chat interfaces

The two SDKs are designed to work together seamlessly through HTTP APIs.

  • Getting Started: Platform-specific quickstarts and setup guides
  • AI SDK Core: Core functions (generateText, streamText, generateObject, tools, embeddings)
  • Agents: Building autonomous AI agents with workflow patterns
  • Zod Adapter: Zod-like schema DSL for structured data validation
  • Foundations: Understanding providers, models, prompts, tools, and streaming

Ready to start building? Pick a quickstart guide:

Or explore the AI SDK Core overview to learn about core concepts.