npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@ratley/react-native-apple-foundation-models

v0.1.1

Published

Access Apple’s on-device Foundation Models (text + image AI)

Readme

react-native-apple-foundation-models

Access Apple's on-device Foundation Models for text on iOS.

Installation

Install the module:

npx expo install @ratley/react-native-apple-foundation-models

Rebuild your native project:

  • Expo (managed): run npx expo prebuild and rebuild your iOS app (EAS Build or expo run:ios).
  • Bare React Native: run npx pod-install after installing the package.

Platform requirements

  • iOS: Requires iOS 26+ and Apple Intelligence‑capable hardware.
  • Web: Not supported; calls will throw/return unavailable.
  • Android: Not supported for on‑device text models.

API quickstart

Availability

Always check availability before using the on-device model and handle the specific reason:

import { getTextModelAvailability } from "apple-foundation-models";

const availability = await getTextModelAvailability();
if (availability.status === "available") {
  // Ready to use
} else {
  switch (availability.reasonCode) {
    case "deviceNotEligible":
      // Device doesn't support Apple Intelligence
      break;
    case "appleIntelligenceNotEnabled":
      // Ask the user to enable Apple Intelligence in Settings
      break;
    case "modelNotReady":
      // Model is downloading or initializing; show a spinner/backoff
      break;
    case "unknown":
    case "unsupported":
      // Provide a non‑AI fallback UX
      break;
  }
}

Notes:

  • Mirrors SystemLanguageModel.default.availability on iOS 26+. On unsupported platforms, the module reports { status: "unavailable", reasonCode: "unsupported" }.
  • A boolean isTextModelAvailable() is also exposed for simple checks.

Text generation (one‑shot)

import { generateText } from "apple-foundation-models";

const { text, sessionId } = await generateText({
  prompt: "Summarize this paragraph in 2 sentences",
  instructions: "Be concise.", // optional system instructions
  temperature: 0.2,              // optional; keep conservative on‑device
  maxOutputTokens: 256,          // optional; bound output length
});

Sessions (object API and React hook)

For multiple related turns, use a reusable session. You don't need to manage IDs manually unless you want to.

import { LLMSession } from "apple-foundation-models";

const s = await LLMSession.create({ instructions: "Be concise." });
const a = await s.ask({ prompt: "Summarize apple vs orange" });
s.reset({ instructions: "Reply in pirate speak" });
const b = await s.ask({ prompt: "Say hi" });

React hook (UI ergonomics):

import { useLLMSession } from "apple-foundation-models";

const s = useLLMSession({ instructions: "Be concise." });
// await s.ask("Summarize apple vs orange")

Structured output (JSON) with generateObject

Ask the model to return JSON that matches a small schema. The library prefers native guided generation on iOS 26+, and falls back to prompt‑then‑parse otherwise.

import { generateObject } from "apple-foundation-models";

const { object } = await generateObject<{ items: { name: string; quantity: number }[]}>({
  prompt: 'From: "We need 2 cartons of milk and a dozen eggs" create a shopping list',
  schema: {
    type: "object",
    required: ["items"],
    properties: {
      items: {
        type: "array",
        items: {
          type: "object",
          required: ["name", "quantity"],
          properties: {
            name: { type: "string", minLength: 1 },
            quantity: { type: "number", minimum: 0 },
          },
        },
      },
    },
  },
});
// object: { items: [ { name: "milk", quantity: 2 }, { name: "eggs", quantity: 12 } ] }

Schema support is a minimal JSON Schema subset:

  • string (minLength, maxLength, enum)
  • number (minimum, maximum)
  • boolean
  • array (items, minItems, maxItems)
  • object (properties, required)

Guidelines:

  • Keep prompts focused and outputs small/bounded for best on‑device performance.
  • Prefer low temperature (e.g., 0.2) for deterministic structure.

Errors

Thrown errors are normalized:

  • Text: TextGenerationError with codes like ERR_TEXT_GENERATION_UNSUPPORTED, ERR_TEXT_PROMPT_INVALID, ERR_TEXT_GENERATION_TIMEOUT, ERR_TEXT_GENERATION_MODEL_UNAVAILABLE, and model‑availability specific codes (ERR_TEXT_MODEL_DEVICE_NOT_ELIGIBLE, ERR_TEXT_MODEL_NOT_ENABLED, ERR_TEXT_MODEL_NOT_READY, ERR_TEXT_MODEL_UNKNOWN).
  • Object: codes like ERR_OBJECT_PROMPT_INVALID, ERR_OBJECT_SCHEMA_INVALID, ERR_OBJECT_GENERATION_DECODE_FAILED, ERR_OBJECT_GENERATION_UNSUPPORTED.

Handle with toTextGenerationError(error) or your own guards.