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

**URL:** <https://forums.swift.org/t/openfoundationmodels-apple-compatible-foundation-models-api-with-multi-provider-support/82168>\
**Category:** Community Showcase\
**Created:** [September 17, 2025, 4:06am UTC](https://forums.swift.org/t/openfoundationmodels-apple-compatible-foundation-models-api-with-multi-provider-support/82168 "2025-09-17T04:06:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![1amageek](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/1amageek/32/9952_2.png) [@1amageek](https://forums.swift.org/u/1amageek)\
**Post date:** [September 17, 2025, 4:06am UTC](https://forums.swift.org/t/openfoundationmodels-apple-compatible-foundation-models-api-with-multi-provider-support/82168/1 "2025-09-17T04:06:17Z")

</div>

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

```swift
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

```swift
@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

📂 GitHub Repository: [1amageek / OpenFoundationModels](https://github.com/1amageek/OpenFoundationModels)

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