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.
- 🛡️ 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
- macOS 10.15+
- iOS 13.0+
- tvOS 13.0+
- watchOS 6.0+
- visionOS 1.0+
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"),]),import OpenAI
// Initialize client
letclient=OpenAI(apiKey:"your-api-key-here")
// Simple chat completion
letresponse=tryawait client.chat.completions(
model:.gpt4_1_mini,
messages:[.user("Explain quantum computing in simple terms")])print(response.choices.first?.message.content ??"No response")// Stream chat completions
letstream= client.chat.completionsStream(
model:.gpt4_1_nano,
messages:[.user("Write a short story about space exploration")])fortryawaitchunkin stream {print(chunk.choices.first?.delta.content ??"")}// Stream responses
letresponses= openAI.responses.createStream(
input:.text("Explain teleportation in simple terms"),
model:.gpt4_1_nano
)fortryawaitresponsein responses {
// Filter text deltas
guard response.type ==.outputTextDelta else{continue}print(response.delta ??"")}Check out the Examples directory for a complete sample application demonstrating how to use this package in an app.
- Models
- Completions (Deprecated)
- Chat Completions
- Responses
- Images
- Embeddings
- Audio
- Files
- Fine-tunes
- Moderations
- EventSource https://github.com/Recouse/EventSource
- JSONSchema https://github.com/mattt/JSONSchema
- swift-log https://github.com/apple/swift-log
Contributions to are always welcomed! If you'd like to contribute, please fork this repository and submit a pull request with your changes.
OpenAI is released under the MIT license. See LICENSE for more information.