On-Device Text Embeddings in React Native With Apple NLP Framework

At Callstack, we believe that using AI, whether on-device or in the cloud, should be simple and consistent for React and React Native developers. Guided by this principle of a unified API, we are expanding our on-device AI SDK provider to include support for Embeddings.

This new feature, powered by Apple's built-in Natural Language framework, unlocks crucial capabilities like semantic search and content classification without requiring any additional model downloads for your users. Here's how to get started.

import { embed } from 'ai'
import { apple } from '@react-native-ai/apple'

const { embedding } = await embed({
  model: apple.textEmbeddingModel(),
  value: 'React Universe Conf is next month, go get your ticket now!',
})

Read on to learn more about the model's architecture and the advantages of this on-device approach.

What are embeddings?

In the context of Natural Language Processing (NLP), an embedding is a numerical representation of a piece of text, be it a single word, a sentence, or an entire document. This representation takes the form of a vector, a list of numbers, that captures the text's semantic meaning.

vector embeddings

Think of it as a coordinate system for language. Words and sentences with similar meanings will have vectors that are "close" to each other in this high-dimensional space. This concept unlocks a range of applications, such as:

  • Semantic Search: Finding documents that are conceptually similar to a query, not just those that share keywords.
  • Text Classification: Categorizing articles, emails, or user feedback based on their content.
  • Clustering: Grouping similar pieces of text together without prior labels.
  • Recommendation Engines: Suggesting items based on textual descriptions.

On-Device vs On-Demand

The rise of powerful AI models has led to a common challenge: model distribution. Most third-party models require developers to bundle a large model file (often several hundred megabytes) within their app or force the user to download it upon first launch. While feasible for specific use cases, this approach is unsustainable for broader market adoption, or even impossible to make the app grow quickly in popularity in some cases. If every app that wants to use AI downloads its own separate model, users' devices will quickly fill up.

Apple takes a more integrated and efficient approach. Many of its advanced AI models, including the one we're discussing today, are treated as shared assets.

When an application requests a model for the first time, the operating system downloads it to a shared asset catalog. This model is then available not only to the requesting app but to any other app on the system that needs it. If the model is already present, it's available instantly.

This system-level management means:

  • Reduced App Size: Your app bundle doesn't need to include the model.
  • Efficient Storage: A single model download serves all apps.
  • Instant Access: If another app has already downloaded the model, yours can use it immediately.

It's the same foundation behind many other on-device models from Apple, which we will be exploring in the coming weeks.

Overview of Apple's Embeddings API

For years, developers had access to NLEmbedding within the Natural Language framework. This API provided word embeddings, generating a vector for each individual word.

While useful, NLEmbedding had a fundamental limitation: it lacked contextual understanding. The vector for the word "bank" would be the same in "river bank" and "investment bank." True semantic understanding requires comprehending the entire sentence.

This is where the new API comes in. NLContextualEmbedding is a modern, sentence-based transformer model. Instead of looking at words in isolation, it analyzes the entire string to produce a single vector that represents its collective meaning. This provides a significant improvement in semantic representation.

Overall, NLContextualEmbedding is:

  • Built on a BERT (Bidirectional Encoder Representations from Transformers) architecture, a model type that excels at understanding relationships between words in a sentence.
  • Not a single model. There are three different underlying models based on the language being processed, ensuring high-quality results across multiple languages.
  • Producing a 512-dimensional vector and can process up to 256 tokens per request.
The vector's dimension describes the richness of its semantic representation. Higher number allows the model to capture more subtle nuances in meaning. While many popular compact models designed for mobile use, like all-MiniLM-L6-v2, use a smaller dimension (e.g., 384), Apple's choice of 512 dimensions is a promising indicator of the model's capacity for high-fidelity language understanding. The 256-token sequence length is a standard for many BERT-based models of this class, balancing performance with the ability to process meaningful chunks of text.

From token vectors to a single sentence embedding

A critical detail of how NLContextualEmbedding works is that it doesn't return one vector for an entire sentence directly. Instead, it generates a distinct, context-aware vector for each token (a word or sub-word piece). This provides a rich, granular output, but for many applications and APIs, including the Vercel AI SDK's function, a single vector representing the entire sentence is required.

Mean pooling of multiple word vectors into sentence embedding

To bridge this gap, we aggregate these token vectors into a single sentence embedding using mean pooling, a technique that calculates the element-wise average of all vectors.

To ensure this operation is performed with maximum efficiency, we leverage Apple's low-level Accelerate framework, specifically the vDSP library. This allows us to offload the complex math to highly-optimized code that runs directly on the CPU's specialized hardware, computing the final sentence embedding with negligible overhead.

First-class Vercel AI SDK support

We've made integrating these capabilities as straightforward as possible. With the react-native-ai library, you can generate an embedding from a string by using either embed or embedMany from AI SDK.

import { embedMany } from 'ai'
import { apple } from '@react-native-ai/apple'

const { embeddings } = await embedMany({
  model: apple.textEmbeddingModel(),
  values: ['React Universe Conf', '3-4 September 2025', 'Wroclaw'],
})

The result is a high-dimensional vector that you can then use for a variety of on-device AI tasks, such as finding the similarity between two pieces of text using cosine similarity calculations.

Conclusion

Our goal is to bring the power of native, on-device frameworks from Apple (and soon, Android) to help you build intelligent and personalized apps, without you having to deal with the complexities of each platform's specific APIs. This is where React Native truly shines, abstracting away platform differences under a unified interface, in this case, the Vercel AI SDK.

We encourage you to explore the documentation, experiment with the new embeddings API, and see what you can build. We look forward to seeing the innovative solutions that emerge from the community.

‍

Insights

Learn more about AI

Building a Software Factory: Write Specs an Agent Can Build From — cover

Nov 6Article

Building a Software Factory: Write Specs an Agent Can Build From

Learn how to research requirements, resolve open decisions, and write testable specs that AI coding agents can implement without the planning conversation.

 Large-Scale Migrations With Coding Agents: Planning, Verification, and Rollback — cover

Nov 4Article

Large-Scale Migrations With Coding Agents: Planning, Verification, and Rollback

Plan codebase migrations with coding agents using reviewable requirements, verification checks, staged rollouts, and defined rollback criteria.

Conversational Banking: From Customer Questions to Authorized Actions — cover

Nov 2Article

Conversational Banking: From Customer Questions to Authorized Actions

Explore conversational banking architecture through a card-dispute example, covering tool access, authorization, execution, observability, and human handoff.

Building a Software Factory: The Night My Agents Waited for Me — cover

Oct 28Article

Building a Software Factory: The Night My Agents Waited for Me

Learn how permissions, review limits, and missing product decisions stalled two overnight coding agent runs, and how their maintainer revised the workflow.

Your AI Coding Agent Still Needs an Architecture — cover

Oct 22Article

Your AI Coding Agent Still Needs an Architecture

Use explicit contracts, behavior tests, and dependency checks to guide AI coding agents and review their changes through a TypeScript favorites example.

cover

Oct 15Live Stream

Who Left the Agents Running? React Native Software Factories

Join Kewin Wereszczyński and Szymon Chmal to explore what can go wrong in a React Native software factory overnight and the controls that keep agents on track.

cover

Oct 14Podcast

When Coding Gets Faster, What Breaks Next?

Matt Hargett explains how AI moves bottlenecks across testing, deployment, infrastructure, support, and the rest of software delivery.

cover

Oct 12Podcast

AI Coding Agents and Hermes With Tzvetan Mikov

Tzvetan Mikov discusses AI coding agents, context switching, low-level engine optimization, reviewing specs and tests, and his plans for Hermes X.

cover

Oct 12Podcast

State Machines for AI Agents With David Khourshid

David Khourshid explains state machines for AI agents, human approval steps, tracing, and handling invalid model outputs in agent workflows.

cover

Oct 9Article

How to Maximize AI SDLC? Measure the Outer Business Loop, Not the Inner Developer Loop

Connect AI-assisted development to measurable business outcomes. Matt Hargett shares a checklist for assessing user exposure, results, and delivery costs.

cover

Oct 7Talk

Automating Mobile QA with Cloud Agents

Michał Pierzchała shows how cloud-based QA agents can test real iOS and Android user flows and bring screenshots, recordings, and logs into pull requests.

cover

Oct 1Live Stream

Can Jev Make AI Agent Decisions Faster?

Explore Jev with Kewin Wereszczyński and Szymon Chmal: how it works, its approach to faster agent decisions, and possible local alternatives.

cover

Mar 19Tutorial

The Hard Way vs. The React Native AI SDK Way: Stop Writing Custom Modules for Every Model

Learn how React Native AI uses a unified provider model to switch between cloud and on-device LLMs without rewriting application logic, enabling reliable AI experiences online and offline.

cover

Mar 12Tutorial

Build Smarter Apps: Tool Calling & AI Orchestration Explained

Learn how on-device models in React Native can call tools, interact with external APIs, and return structured outputs you can use directly in application logic.

cover

Mar 5Tutorial

MLC LLM + React Native: On-Device AI Without the Pain

Learn how to run third-party on-device LLMs in React Native using MLC LLM. Choose your models, run them efficiently across platforms, and keep a unified JavaScript API with React Native AI.

cover

Feb 24Tutorial

Intelligent Fallbacks with Apple Intelligence in React Native

Learn how to use Apple’s built-in Foundation Models in React Native apps, reduce memory usage, and enable fast, fully on-device AI with React Native AI Apple.

cover

Feb 10Tutorial

What Is the React Native AI SDK? A Complete Intro & Quickstart

Learn why on-device AI matters for React Native apps, how local LLMs behave offline, and what problems React Native AI is designed to solve from day one.

cover

Jan 29Tutorial

Implementing an Android TurboModule with Kotlin

Learn how to implement a React Native TurboModule on Android, from the TypeScript spec and Codegen to the Kotlin implementation and package registration.

cover

Jan 19Tutorial

Working with Different Threads in Swift TurboModules

Learn how JavaScript, native module, background, and main threads interact in React Native TurboModules, and how to use GCD queues to run async work safely in Swift.

cover

Jan 9Tutorial

How to Add Type-Safe Constants to Swift TurboModules

Learn how to expose typed, immutable constants from a Swift TurboModule using Codegen, and get full end-to-end type safety between native and JavaScript.

cover

Dec 30Tutorial

Adding Event Emitters to Your TurboModule in Swift

Learn how to emit native events from a Swift TurboModule and subscribe to them in JavaScript, including proper memory management and Codegen integration.

cover

Dec 22Tutorial

Writing Your First TurboModule in Swift

Learn how to build a fully working TurboModule in Swift, integrate it with Codegen, and bridge it through Objective‑C inside an Expo app.