Important
This project is in active development and does not support many endpoints yet. Public API is likely to have major breaking changes.
A modern Swift package for interacting with OpenAI’s API, built on top of the official OpenAI OpenAPI spec.
Layer | Package | Description | Use Case |
---|---|---|---|
🚀 1 — High-level API | OpenAIKit |
Opinionated wrapper for conversational agents using tools, including the Tool protocol, ResponseSession actor, error policies, and streaming support. |
Build assistants, chatbots, and AI-powered features with minimal setup. |
🧠 2 — Swifty OpenAI Client | OpenAICore |
Lightweight async wrapper around the OpenAI API with ergonomic Swift types and methods like createResponse(...) . Built on top of OpenAICore . |
Interact directly with OpenAI endpoints in a clean, modern Swift style. |
🛠️ 3 — Low-level Generated Layer | OpenAIFoundation |
Fully autogenerated models and API client from the OpenAPI spec. No opinions, no wrappers—just raw access. | Advanced integrations, custom wrappers, or OpenAPI-driven workflows. |
💡 All layers rely on a pluggable transport layer conforming to
ClientTransport
protocol. You can useOpenAPIAsyncHTTPClient
,OpenAPIURLSession
, or bring your own.
-
Define your tools, conforming to the
Tool
protocol. -
Register your tools
-
Setup a
ResponseSession
-
Start streaming (or send async request)
Endpoint | Supported? |
---|---|
/responses |
[x] |
/images |
[x] |
Checkout the Example CLI Project for some more sample usages.
Note
This section is only relevant for library maintainers. If you're just using the package, you can skip this.
Patches are messed up currently!
swift-openai
uses swift-openapi-generator
to generate models and endpoint definitions directly from OpenAI’s official openapi.yaml. This ensures maximum compatibility and future-proofing as the spec evolves.
To ensure the generated code compiles and behaves correctly in Swift, we apply a series of patch files located in the Patches/
directory to the swift-openapi.yaml
spec. These are applied before generation via a custom script. This workaround is necessary because the raw OpenAPI spec includes some constructs that are either unsupported or problematic for the Swift OpenAPI toolchain.
Task | Command |
---|---|
To check for spec changes | make check |
To fetch the latest openapi.yaml |
make fetch |
To apply the necessary patches | make patches |
To generate the Swift types | make generate |
To fetch, patch, and generate | make all |
To generate patches, make changes to the yaml and run:
git diff Sources/OpenAIFoundation/openapi.yaml > Patches/<two-digit-sort-number-here>-<patch-description-here>.patch