recouse/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
Xcode Package Dependency
From Xcode menu: File > Swift Packages > Add Package Dependency
https://github.com/Recouse/OpenAISwift Package Manager
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:
- [x] Models
- [x] Completions (Deprecated)
- [x] Chat Completions
- [x] Responses
- [ ] Images
- [ ] Embeddings
- [ ] Audio
- [ ] Files
- [ ] Fine-tunes
- [ ] Moderations
Dependencies
- EventSource https://github.com/Recouse/EventSource
- JSONSchema https://github.com/mattt/JSONSchema
- swift-log https://github.com/apple/swift-log
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.
Package Metadata
Repository: recouse/openai
Default branch: main
README: README.md