OpenAI

0.1.4

Swift package to interact with OpenAI API
Recouse/OpenAI

What's New

0.1.4

2025-08-10T12:26:19Z

What's Changed

  • Added helper methods in Responses input object
  • Now only Sendable model types are accepted in the request handler

Full Changelog: 0.1.3...0.1.4

OpenAI

A modern, type-safe Swift package for seamless integration with OpenAI's API across all Apple platforms. Built with async/await support and streaming capabilities.

Features

  • 🛡️ Type Safety: Comprehensive Swift types for all API endpoints
  • ⚡ Modern Swift: Built with async/await, Codable, and Swift concurrency
  • 🔄 Streaming Support: Chat completions with async sequences
  • 🌐 Cross-Platform: Works on Apple platforms (iOS, macOS, tvOS, watchOS, visionOS), Linux and Android
  • 🎯 Comprehensive API Coverage: Support for all major OpenAI endpoints

Compatibility

  • macOS 10.15+
  • iOS 13.0+
  • tvOS 13.0+
  • watchOS 6.0+
  • visionOS 1.0+

Installation

From Xcode menu: File > Swift Packages > Add Package Dependency

https://github.com/Recouse/OpenAI

In your Package.swift file, first add the following to the package dependencies:

.package(url: "https://github.com/Recouse/OpenAI.git", from: "0.1.0"),

And then, include "OpenAI" as a dependency for your target:

.target(
    name: "<target>",
    dependencies: [
        .product(name: "OpenAI", package: "OpenAI"),
    ]
),

Basic Usage

import OpenAI

// Initialize client
let client = OpenAI(apiKey: "your-api-key-here")

// Simple chat completion
let response = try await client.chat.completions(
    model: .gpt4_1_mini,
    messages: [.user("Explain quantum computing in simple terms")]
)

print(response.choices.first?.message.content ?? "No response")

Streaming Responses

Chat Completions

// Stream chat completions
let stream = client.chat.completionsStream(
    model: .gpt4_1_nano,
    messages: [.user("Write a short story about space exploration")]
)

for try await chunk in stream {
    print(chunk.choices.first?.delta.content ?? "")
}

Responses

// Stream responses
let responses = openAI.responses.createStream(
    input: .text("Explain teleportation in simple terms"),
    model: .gpt4_1_nano
)

for try await response in responses {
    // Filter text deltas
    guard response.type == .outputTextDelta else { continue }
    print(response.delta ?? "")
}

Examples

Check out the Examples directory for a complete sample application demonstrating how to use this package in an app.

What's Implemented:

Dependencies

Contributing

Contributions to are always welcomed! If you'd like to contribute, please fork this repository and submit a pull request with your changes.

License

OpenAI is released under the MIT license. See LICENSE for more information.

Description

  • Swift Tools 6.1.0
View More Packages from this Author

Dependencies

Last updated: Sun Aug 10 2025 04:51:13 GMT-0900 (Hawaii-Aleutian Daylight Time)