Swift AI SDK

repository·main·Indexed 41 days ago

https://github.com/teunlao/swift-ai-sdk

A unified, provider-agnostic library for Swift designed for full API parity with the Vercel AI SDK. It supports streaming chat and completions, structured outputs via Codable types, and tool/MCP calling across providers like OpenAI and Anthropic.

Tokens
128.7K
Snippets
372
Records
516
Agent score
97%

What's inside Swift AI SDK

  1. Overview of Swift AI SDK components

    main

    The Swift AI SDK is a toolkit for building AI applications in Swift across Apple platforms and server-side Swift. It ports the AI SDK Core from the TypeScript version, providing a unified API for text generation, structured data, and tool calling.

    Included Components

    • SwiftAISDK (Core): The main API for generateText, streamText, and generateObject.
    • Provider System: Integrations for 38+ providers (e.g., OpenAIProvider, AnthropicProvider, GoogleProvider).
    • AISDKProvider: Foundation types and protocols like LanguageModelV3.
    • AISDKProviderUtils: Utilities for HTTP, JSON, schema validation, and retries.
    • AISDKZodAdapter: A Zod-like DSL for defining structured data schemas.

    Not Included

    Unlike the TypeScript SDK, this does not include web-specific UI libraries (like useChat or useCompletion) or React Server Components (RSC) utilities. For hybrid apps, use the Swift AI SDK on the server and the TypeScript AI SDK UI on the frontend.

  2. Mock providers and test helpers in Swift AI SDK

    main

    To unit test code that uses the AI SDK without the cost, latency, or non-determinism of real language models, you can use mock providers and test helpers imported from SwiftAISDK.

    Available Mock Providers

    • MockLanguageModelV3: A mock language model using the language model v3 specification.
    • MockImageModelV3: A mock image model for testing image generation.

    Available Test Helpers

    • mockValues: Iterates over an array of values with each call. Returns the last value when the array is exhausted.
    • simulateReadableStream: Simulates a readable stream with configurable delays.
  3. Key Features of Swift AI SDK

    main

    The Swift AI SDK is designed to bring Vercel AI SDK capabilities to Apple platforms. Core features include:

    • Text Generation: Supports both streaming and non-streaming text generation.
    • Structured Outputs: Enables type-safe object generation using schemas.
    • Tool Calling: Supports function calling and Model Context Protocol (MCP) tools.
    • Multi-Provider Support: Compatible with OpenAI, Anthropic, Google, Groq, and more.
    • Middleware System: Provides an extensible system for processing requests and responses.
    • Telemetry: Includes built-in observability and monitoring.
    • Cross-Platform Support: Works on iOS 16+, macOS 13+, tvOS 16+, and watchOS 9+.
  4. Use the Google Provider for AI tasks

    main

    The Google Provider implementation in Swift AI SDK provides parity with the Vercel AI SDK's Google implementation. It supports Gemini models, tool calling, embeddings, and multimodal inputs (images, video, files).

    Key Capabilities

    • Tool Calling: Tools are encoded as an array under functionDeclarations to ensure Gemini compatibility.
    • Built-in Provider Tools: Supports specialized tools like google_search, url_context, code_execution, enterprise_web_search, file_search, vertex_rag_store, and google_maps.
    • Multimodal Support: Handles images (including aspect-ratio configuration like 21:9), video (mapping n to sampleCount), and file URLs (Google Files/YouTube).
    • Embeddings: Supports embedding requests with configurable outputDimensionality (accepts any numeric value).
    • Grounding: Extracts grounding sources from web, retrievedContext (HTTP URLs, file URIs, fileSearchStore), and maps chunks.

    Configuration & Requirements

    • API Key: Requires the GOOGLE_GENERATIVE_AI_API_KEY environment variable. If missing, the SDK throws a LoadAPIKeyError at request-time.
    • Vertex AI: Note that google-vertex progress is tracked separately; this provider focuses on the standard Google Generative AI implementation.
    // Note: Actual initialization code depends on the specific SDK entry point,
    // but ensure GOOGLE_GENERATIVE_AI_API_KEY is set in your environment.
  5. OpenAI Provider Verification Status

    main
    The OpenAI provider in the Swift AI SDK is currently classified as verified/current. This means the implementation has achieved parity with the upstream Vercel AI SDK across several key surfaces, including Chat, Completion, Responses, and Tools. Verification is confirmed via a comprehensive suite of Swift Testing tests covering usage, metadata, and schema parity.
  6. OpenAI Provider Capabilities and Parity

    main

    The OpenAIProvider in the Swift AI SDK implements a native V4 REST implementation designed to match the capabilities of the Vercel AI SDK (upstream).

    Supported features include:

    • Transcription: Native V4 REST transcription support.
    • Completion: Native V4 completion (text generation).
    • Chat: Native V4 chat (message-based interaction).
    • Files & Skills: Support for file management and skill uploads (including binary upload parity).
    • Realtime: Support for the OpenAI Realtime V4 model.
    • Core DX: Support for core developer experience features like generateText, streamText, generateObject, embed, and agent patterns, as well as middleware and provider registry management.
  7. What is AISDKZodAdapter?

    main

    Overview

    AISDKZodAdapter is a Swift-native Domain Specific Language (DSL) inspired by TypeScript's Zod library. It provides an ergonomic way to define JSON schemas for AI tools without the verbosity of manual JSON Schema construction.

    Key Features

    • Syntactic Sugar: It acts as a cleaner interface over manual JSON Schema.
    • Internal Conversion: It automatically converts your Zod-like syntax into standard JSON Schema used by the AI SDK.
    • Pure Swift: No TypeScript or JavaScript dependencies required.
    • Type-Safe: Designed to improve developer experience when defining tool schemas.
  8. What is a tool in Swift AI SDK?

    main

    A tool is an object that allows a Large Language Model (LLM) to perform discrete tasks or interact with the outside world (e.g., fetching weather or performing math). Tools are passed to generateText or streamText via the tools parameter.

    A tool consists of three main components:

    • description: An optional string that helps the LLM decide when to use the tool.
    • inputSchema: A Codable type that defines the parameters the tool accepts. The SDK automatically derives the JSON Schema from this type.
    • execute: An optional asynchronous function that receives the validated arguments and returns a result.

    If a tool includes an execute function, it runs automatically when the LLM emits a tool call. The results are returned to the LLM as tool-result parts, enabling multi-step reasoning.

    // Conceptual structure of a tool
    let myTool = tool(
      description: "Description of what the tool does",
      inputSchema: MyCodableInput.self
    ) { input, _ in
      // Execution logic
      return MyResult()
    }
  9. Use reasoning models with Moonshot AI

    main

    Models like kimi-k2-thinking generate intermediate reasoning tokens. You can configure these via providerOptions using the moonshotai key.

    Reasoning Configuration Options

    thinking (object)

    • type: 'enabled' | 'disabled'
    • budgetTokens: number (minimum 1024)

    reasoningHistory (string)

    • 'disabled': Remove reasoning from history.
    • 'interleaved': Include reasoning between tool calls within a single turn.
    • 'preserved': Keep all reasoning in history.
    import SwiftAISDK
    import MoonshotAIProvider
    
    let result = try await generateText(
      model: try moonshotai.chatModel(modelId: "kimi-k2-thinking"),
      providerOptions: [
        "moonshotai": [
          "thinking": [
            "type": "enabled",
            "budgetTokens": 2048
          ],
          "reasoningHistory": "interleaved"
        ]
      ],
      prompt: "How many \"r\"s are in the word \"strawberry\"?"
    )
    
    print(result.reasoningText ?? "")
    print(result.text)
  10. Handle reasoning summaries in streams and non-streaming calls

    main

    For reasoning models (like gpt-5), you can enable reasoning summaries via reasoningSummary in providerOptions.

    Streaming: Reasoning summaries appear as events with type .reasoning(delta) in the stream.

    Non-streaming: Reasoning summaries are available in the reasoning field of the response object.

    // Streaming example
    let result = try streamText(
      model: openai("gpt-5"),
      prompt: "Tell me about the Mission burrito debate in San Francisco.",
      providerOptions: [
        "openai": [
          "reasoningSummary": "detailed"
        ]
      ]
    )
    
    for try await part in result.fullStream {
      switch part {
      case .reasoning(let delta):
        print("Reasoning: \(delta)")
      case .textDelta(let delta):
        print(delta, terminator: "")
      default:
        break
      }
    }
    
    // Non-streaming example
    let result = try await generateText(
      model: openai("gpt-5"),
      prompt: "...",
      providerOptions: ["openai": ["reasoningSummary": "auto"]]
    )
    print("Reasoning:", result.reasoning ?? "")
  11. Use wildcard patterns for test filtering

    main

    When defining include or exclude lists, you can use wildcard syntax to match test suites and individual tests:

    • *: Matches any sequence of characters.
    • ?: Matches any single character.
    • .: The dot is automatically escaped.

    Pattern Examples

    • SwiftAISDKTests.HandleUIMessageStreamFinishTests/*: Matches all tests within a specific suite.
    • *.CreateUIMessageStreamTests/*: Matches all tests in any package containing CreateUIMessageStreamTests.
    • SwiftAISDKTests.DelayTests/delayWithSignal: Matches one specific test.
    • *Finish*/*: Matches all tests in any suite that has "Finish" in its name.
    • AISDKProviderTests.*: Matches all tests in the AISDKProviderTests package.
    {
      "mode": "include",
      "include": [
        "SwiftAISDKTests.UIMessage*",
        "AISDKProviderTests.*
      ]
    }