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.
What’s Included
Section titled “What’s Included”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
What’s Not Included
Section titled “What’s Not Included”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.
Package Structure
Section titled “Package Structure”The Swift AI SDK is organized into focused packages:
| Package | Purpose | Dependencies |
|---|---|---|
| AISDKProvider | Foundation types and protocols (LanguageModelV3, etc.) | None |
| AISDKProviderUtils | HTTP, JSON, schema, validation, retry utilities | AISDKProvider |
| SwiftAISDK | Core API (generateText, streamText, generateObject) | Provider, ProviderUtils |
| OpenAIProvider | OpenAI integration (GPT-4, GPT-3.5, embeddings, etc.) | Provider, ProviderUtils |
| AnthropicProvider | Anthropic Claude integration | Provider, ProviderUtils |
| GoogleProvider | Google Gemini integration | Provider, ProviderUtils |
| GroqProvider | Groq integration | Provider, ProviderUtils |
| OpenAICompatibleProvider | OpenAI-compatible API integration | Provider, ProviderUtils |
| AISDKZodAdapter | Zod-like schema DSL for structured data | AISDKProvider |
| EventSourceParser | Server-Sent Events (SSE) parsing (internal utility) | None |
Platform Compatibility
Section titled “Platform Compatibility”Swift AI SDK works across all Apple platforms and server-side Swift:
| Platform | Support |
|---|---|
| iOS / iPadOS | ✓ |
| macOS | ✓ |
| watchOS | ✓ |
| tvOS | ✓ |
| Server-side Swift | ✓ |
| Linux (Swift on Linux) | ✓ |
Minimum versions:
- macOS 13+
- iOS 16+
- watchOS 9+
- tvOS 16+
Core Features
Section titled “Core Features”Text Generation
Section titled “Text Generation”import SwiftAISDKimport OpenAIProvider
let result = try await generateText( model: openai("gpt-4o"), prompt: "Write a short story about a cat.")print(result.text)Streaming
Section titled “Streaming”let stream = try await streamText( model: openai("gpt-4o"), prompt: "Count to 10.")
for try await textPart in stream.textStream { print(textPart, terminator: "")}Structured Data
Section titled “Structured Data”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)Tool Calling
Section titled “Tool Calling”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?")Embeddings
Section titled “Embeddings”let result = try await embed( model: openai.embedding("text-embedding-3-small"), value: "Hello, world!")print(result.embedding) // [0.123, -0.456, ...]Getting Started
Section titled “Getting Started”Choose a quickstart based on your environment:
- iOS & macOS Apps - Building native apps with SwiftUI or UIKit
- Server (Vapor) - Building server-side Swift APIs
- CLI - Building command-line tools
Integration with Web Frontends
Section titled “Integration with Web Frontends”If you’re building a web application with a Swift backend:
- Backend (Swift): Use Swift AI SDK Core for LLM calls, embeddings, and structured data generation
- 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.
Documentation Structure
Section titled “Documentation Structure”- 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
Next Steps
Section titled “Next Steps”Ready to start building? Pick a quickstart guide:
Or explore the AI SDK Core overview to learn about core concepts.