OpenFoundationModels — Apple-Compatible Foundation Models API with Multi-Provider Support

Hello Swift community!
I’d like to introduce a new open-source project: OpenFoundationModels.


What is OpenFoundationModels?

OpenFoundationModels (OFM) is an open-source implementation of Apple’s Foundation Models API. Its primary goal is to provide 100% API compatibility with Apple’s design, while removing platform and provider limitations.

Key Highlights

  • Beyond Apple’s models: Use OpenAI, Anthropic, local models, or your own provider with the same high-level API you’d use for Apple’s built-in models.

  • @Generable macro: Enables type-safe structured generation, so model responses can be parsed directly into Swift types, instead of manually decoding JSON.

  • Streaming and function calling: Support for streaming responses, tool/function calls, and fine-grained transcript handling.

  • Transcript-centric design: Conversations are represented as transcripts (prompt, response, tool calls) for clarity and flexibility.


Example: Basic Usage with OpenAI

import OpenFoundationModels
import OpenFoundationModelsOpenAI  // OpenAI provider module

let provider = OpenAIProvider(apiKey: "your_api_key")
let session = LanguageModelSession(model: provider.gpt4o)

let response = try await session.respond {
    Prompt("Explain Swift concurrency")
}
print(response.content)


Example: Structured Generation with @Generable

@Generable
struct ProductReview {
    @Guide(description: "Product name", .pattern("^[A-Za-z0-9\\s]+$"))
    let productName: String

    @Guide(description: "Rating from 1 to 5", .range(1...5))
    let rating: Int

    @Guide(description: "Review between 50–500 characters", .count(50...500))
    let comment: String
}

// Type-safe response
let review = try await session.respond(generating: ProductReview.self) {
    Prompt("Generate a product review for the iPhone 15 Pro")
}

print("Product: \(review.content.productName)")
print("Rating: \(review.content.rating)")
print("Comment: \(review.content.comment)")


Notes & Limitations

  • While OFM provides the same API surface as Apple’s Foundation Models, actual Apple system models (e.g. SystemLanguageModel.default) are only available when running on supported devices.

  • Provider modules need to be imported explicitly (e.g. OpenFoundationModelsOpenAI, OpenFoundationModelsAnthropic), not everything is bundled into the core.


Links

:open_file_folder: GitHub Repository: 1amageek / OpenFoundationModels

We’d love to hear your feedback, ideas, and contributions from the Swift community :rocket:

1 Like