how to use foundationmodels?
Asked on 06/12/2025
1 search
To use the Foundation Models framework, you can follow these steps:
-
Import the Framework: Start by importing the Foundation Models framework into your project. This gives you access to the on-device large language model with a convenient Swift API.
-
Create a Session: The framework is built around the concept of a stateful session. You create a session to interact with the model. This session can be customized with instructions that guide the model's behavior.
-
Send a Prompt: Once you have a session, you can send prompts to the model. The model processes these prompts and generates responses. You can use the
session.respond
method to send your prompt and receive a response. -
Use Generable for Structured Output: If you need structured output, you can use the
Generable
macro to define types that the model can generate. This allows you to get structured data directly from the model, which can be more reliable than parsing unstructured text. -
Tool Calling: The framework supports tool calling, which allows the model to autonomously call functions you define. This can be used to fetch additional information or perform actions based on the model's processing.
-
Prompt Engineering: Crafting effective prompts is crucial. You can use Xcode's Playground to experiment with different prompts and refine them to get the desired output.
For a more detailed exploration, you can refer to the session Deep dive into the Foundation Models framework (00:00:00) and Code-along: Bring on-device AI to your app using the Foundation Models framework (00:01:09). These sessions provide comprehensive guidance on using the framework effectively.

Deep dive into the Foundation Models framework
Level up with the Foundation Models framework. Learn how guided generation works under the hood, and use guides, regexes, and generation schemas to get custom structured responses. We’ll show you how to use tool calling to let the model autonomously access external information and perform actions, for a personalized experience. To get the most out of this video, we recommend first watching “Meet the Foundation Models framework”.

Code-along: Bring on-device AI to your app using the Foundation Models framework
Develop generative AI features for your SwiftUI apps using the Foundation Models framework. Get started by applying the basics of the framework to create an awesome feature. Watch step-by-step examples of how to complement the models with tools you build, stream results, and apply further optimizations for great performance.

Platforms State of the Union
Discover the newest advancements on Apple platforms.