react-native-foundation-models
v0.1.2
Published
React Native Nitro module for Apple's Foundation Models (Apple Intelligence). Enables on-device AI with tool calling and streaming responses on iOS 26.0+.
Readme
react-native-foundation-models
React Native Nitro module for Apple's Foundation Models (Apple Intelligence). Enables AI capabilities in React Native applications using Apple's on-device model, with tool calling support and streaming responses on iOS 26.0+.
Requirements
- iOS 26.0 or later
- Apple Intelligence enabled in Settings > Apple Intelligence & Siri
- Compatible Apple devices (Apple Silicon Macs, recent iPhones/iPads)
- React Native with Nitro modules support
Installation
npm install react-native-foundation-models react-native-nitro-modules
yarn add react-native-foundation-models react-native-nitro-modules
bun add react-native-foundation-models react-native-nitro-modulesUsage
Basic Chat Session
import { LanguageModelSession } from 'react-native-foundation-models';
const session = new LanguageModelSession({
instructions: 'You are a helpful assistant',
useCase: 'general',
guardrails: 'default',
});
const response = await session.respond('Hello, how are you?');
console.log('Response:', response);Streaming Chat Session
import { LanguageModelSession } from 'react-native-foundation-models';
const session = new LanguageModelSession({
instructions: 'You are a helpful assistant'
});
await session.streamResponse('Hello, how are you?', (responseSoFar) => {
console.log('Streaming response:', responseSoFar);
});Using React Hooks
import { useLanguageModel } from 'react-native-foundation-models';
function ChatComponent() {
const { send, response, loading, error, isSessionReady } = useLanguageModel({
instructions: 'You are a helpful coding assistant',
onResponse: (response) => console.log('Got response:', response),
onError: (error) => console.error('Error:', error)
});
const handleSendMessage = async () => {
if (isSessionReady) {
await send('Explain React hooks');
}
};
return (
<View>
<Text>{response}</Text>
<Button onPress={handleSendMessage} disabled={loading} title="Send" />
</View>
);
}Tool calling
import { createTool, LanguageModelSession } from 'react-native-foundation-models';
import { z } from 'zod';
const weatherTool = createTool({
name: 'weather_tool',
description: 'Get current weather for a city',
arguments: z.object({
city: z.string(),
}),
handler: async (args) => {
const response = await fetch(`/weather?city=${args.city}`);
const res = await response.json();
return res.data
},
});
const session = new LanguageModelSession({
instructions: 'You are a weather assistant',
tools: [weatherTool]
});API Reference
LanguageModelSession
Core class for managing AI conversations.
constructor(config?: {
instructions?: string;
tools?: Tool[];
useCase?: 'general' | 'contentTagging';
guardrails?: 'default' | 'permissiveContentTransformations';
})The library now creates sessions with an explicit SystemLanguageModel, which lets you opt into Foundation Models use cases and guardrails from React Native.
Methods:
respond(prompt)- Generate a complete response and resolve when finishedstreamResponse(prompt, onChunk)- Stream the response progressively
useLanguageModel(config)
React hook for session management with automatic lifecycle handling.
Returns:
session- The current session instanceresponse- Latest AI responseloading- Whether a request is in progresserror- Any error that occurredsend(prompt)- Send a message to the AIreset()- Reset the conversation stateisSessionReady- Whether the session is ready to use
useStreamingResponse(session)
Lower-level hook for streaming responses.
checkFoundationModelsAvailability()
Check if Apple Intelligence is available on the device.
The returned object also includes:
contextSize: current library context budget in tokensmodelFamily:'26.0-26.3'or'26.4+'based on the OS version
getFoundationModelsModelFamily()
Returns the Foundation Models family for the current OS version: '26.0-26.3' or '26.4+'.
getFoundationModelsContextSize()
Returns the library's current context budget in tokens. Apple documents a 4,096-token context window for the on-device model in iOS 26.0 through 26.3, so the library currently surfaces that value until it can be wired to the native SystemLanguageModel.contextSize API with an iOS 26.4 SDK.
Foundation Models 26.4 Notes
- Apple’s current docs split the on-device model into two version families:
26.0-26.3and26.4+. The library exposes this asmodelFamilyso apps can version prompts when model behavior changes. - Exact native token measurement with
SystemLanguageModel.tokenCount(for:)is not compiled into this branch yet because the installed Xcode SDK here isiPhoneOS26.2, which does not expose that symbol. Once the project moves to an SDK that includes the 26.4 APIs, the JS surface can adopt the native token counter without another breaking API change.
Development
This project uses a workspace structure with:
package/- The nitro module source codeexample/- Example app demonstrating usage
Setup
bun install
bun run buildRunning the example
cd example
bun start # Start Expo dev server
bun ios # Run on iOSNote: Android is not supported as this module requires Apple's Foundation Models framework.
