swift-openai

0.1.0

WIP Swift OpenAI API wrapper using the OpenAPI docs
ajevans99/swift-openai

What's New

v0.1.0

2025-06-08T21:51:34Z

First release

Swift OpenAI

Important

This project is in active development and does not support many endpoints yet. Public API is likely to have major breaking changes.

CI

A modern Swift package for interacting with OpenAI’s API, built on top of the official OpenAI OpenAPI spec.

Library Structure

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 use OpenAPIAsyncHTTPClient, OpenAPIURLSession, or bring your own.

OpenAIKit

  1. Define your tools, conforming to the Tool protocol.

  2. Register your tools

  3. Setup a ResponseSession

  4. Start streaming (or send async request)

OpenAICore

OpenAIFoundation

Endpoint Supported?
/responses [x]
/images [x]

Examples

Checkout the Example CLI Project for some more sample usages.

Code Genertion

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

Description

  • Swift Tools 5.10.0
View More Packages from this Author

Dependencies

Last updated: Sun Jun 15 2025 20:50:23 GMT-0900 (Hawaii-Aleutian Daylight Time)