Unified AI SDK for Swift - native port of Vercel AI SDK with a focus on API parity
Hi!
I'm working on Swift AI SDK - a port of Vercel AI SDK. The goal is to preserve the developer experience and features of the original through native Swift APIs
Where did the project come from?
Swift AI SDK started as a Vercel SDK port for my personal project - needed to swap providers/models dynamically.
After living inside my codebase for a while, its maintenance started overshadowing the main product. I extracted it into a separate open-source project.
Core idea:
// Unified API for all providers
let result = try await generateText(
model: openai("gpt-4o"), // change one line
// model: anthropic("claude"), // and everything works
prompt: "Explain quantum computing"
)
Instead of learning different APIs, their specifics and nuances - you learn one.
API Parity with Vercel AI SDK: Examples
Below are short "TypeScript → Swift" pairs showing that code porting is mechanical.
Structured response
// TypeScript
const result = await generateObject({
model: openai("gpt-4o"),
schema: z.object({
recipe: z.object({
name: z.string(),
ingredients: z.array(z.object({ name: z.string(), amount: z.string() })),
steps: z.array(z.string())
})
}),
prompt: "Generate a lasagna recipe."
});
console.log(result.object.recipe.name);
// Swift
struct Ingredient: Codable, Sendable {
let name: String
let amount: String
}
struct Recipe: Codable, Sendable {
let name: String
let ingredients: [Ingredient]
let steps: [String]
}
let result = try await generateObject(
model: openai("gpt-4o"),
schema: Recipe.self,
prompt: "Generate a lasagna recipe."
)
print(result.object.recipe.name)
Tool calling
// TypeScript
const weather = tool({
inputSchema: z.object({ city: z.string() })
}, async ({ city }) => ({ city, temperature: 21 }));
await generateText({
model: openai("gpt-4o"),
tools: { weather }
});
// Swift
let weather = tool(
inputSchema: WeatherQuery.self
) { query, _ in WeatherReport(city: query.city, temperature: 21) }
let _ = try await generateText(
model: openai("gpt-4o"),
tools: ["weather": weather.tool]
)
Features
- Text:
generateText/streamTextwith streaming - Tool calling: automatic execution, approvals
- Multi-step: generation via
stopWhen - Structured:
generateObject/streamObject(Codable schemas) - Multimodal: image generation, speech synthesis
2200+ tests verify behavior against TypeScript version. 40+ examples demonstrate core scenarios.
Known Gaps
Providers: OpenAI, Anthropic, Google are well-tested. Others need deeper testing - working through them.
Schema validation: Limited compared to JS ecosystem - no zod equivalent in Swift. Currently use Schema.codable or explicit JSON Schema.
- Multimodal capabilities still have edge cases and inconsistencies
- Normalizing retry/timeout behavior across providers
- Some streaming edge cases still differ
Next Steps
- Complete examples and provider-specific features (expand provider coverage, add more scenarios)
- Improve and expand documentation so all Swift scenarios are well-described and easy to find
- Deep-test providers, add new examples and reveal provider-specific settings
What I'm particularly interested to learn
- Would this be useful for your projects? What use cases would you have?
- Which providers/models or scenarios are missing for production use?
- If you've used Vercel AI SDK - what should the Swift port handle differently or better?
Links:
Thanks for stopping by - I'd appreciate any feedback!