DS DevShelfHub Projects · AI tools
Tutorials / LangChain / Reference / Classes / FakeEmbeddings
Embeddings langchain-core Beginner

FakeEmbeddings: Reference Guide

By DevShelfHub

Generate random embeddings for testing.

What is FakeEmbeddings?

FakeEmbeddings is a test double that implements the LangChain Embeddings interface but returns random vectors of a fixed size instead of calling a real model. Its entire purpose is to let you exercise the rest of a RAG or retrieval pipeline — document loading, splitting, vector-store wiring, retriever plumbing — without making a network call, burning API credits, or needing an API key. It lives in langchain-core, so it is always available with no extra install.

You construct it with a size equal to the embedding dimension your code expects, then call embed_query or embed_documents exactly as you would with OpenAIEmbeddings. Because the vectors are random and unrelated to the text, similarity search results are meaningless — FakeEmbeddings is for verifying that code runs and shapes line up, not for asserting that the right document came back. Use it to keep unit tests and CI fast and hermetic.

A key subtlety: plain FakeEmbeddings produces different vectors on every call, which makes tests that compare or cache vectors flaky. When you need stability, use the sibling DeterministicFakeEmbedding, which seeds its output from a hash of the input text so the same string always yields the same vector. Reach for the deterministic variant whenever a test persists vectors, dedupes by embedding, or asserts on retrieval order; reach for plain FakeEmbeddings when you only need a cheap stand-in. Never ship either to production.

When to Use

You're testing without API calls. Use FakeEmbeddings for unit tests and rapid development.

Use Cases

  • Unit testing
  • Development without APIs
  • Mock testing
  • CI/CD pipelines
  • Quick prototyping
  • Test fixtures

Key Features

  • No API calls
  • Deterministic
  • Fast
  • Easy testing
  • No setup
  • Lightweight

When NOT to Use

For any production or real semantic search.

Notes

Vectors are random and meaningless

FakeEmbeddings returns random numbers unrelated to the input text, so similarity search returns arbitrary documents. Use it to assert that your pipeline runs and shapes match — never to assert that the correct document was retrieved.

Use DeterministicFakeEmbedding for stable tests

Plain FakeEmbeddings yields different vectors on every call, which makes tests that persist or compare vectors flaky. DeterministicFakeEmbedding hashes the input so the same text always maps to the same vector — reach for it whenever a test caches embeddings or asserts on retrieval order.

Match size to the real model

Set size to the dimension your production model uses (1536 for text-embedding-3-small, 768 for many open models) so vector-store schemas and downstream code see realistic shapes. A size mismatch can hide dimension bugs that only surface with the real model.

Never ship it

FakeEmbeddings is a test fixture, not a fallback. If a real embeddings provider is misconfigured, fail loudly rather than silently substituting fake vectors — otherwise retrieval quietly returns garbage in production with no error.

Import

python
from langchain_core.embeddings.fake import FakeEmbeddings

Configuration

Parameter Type Default Purpose
size int None Embedding dimension

Usage Examples

Create a random fake embedding

python
from langchain_core.embeddings import FakeEmbeddings

embeddings = FakeEmbeddings(size=768)
vec = embeddings.embed_query('Test')
print(len(vec))  # 768

Test a retriever pipeline without API calls

python
from langchain_core.embeddings import FakeEmbeddings
from langchain_chroma import Chroma

embeddings = FakeEmbeddings(size=1536)
store = Chroma.from_documents(docs, embeddings)
hits = store.similarity_search('anything', k=2)
assert len(hits) == 2

Stable vectors with DeterministicFakeEmbedding

python
from langchain_core.embeddings import DeterministicFakeEmbedding

embeddings = DeterministicFakeEmbedding(size=256)
a = embeddings.embed_query('same text')
b = embeddings.embed_query('same text')
assert a == b  # stable across calls

Common Pitfalls

❌ Use for semantic search

✅ For testing only

Alternative Embedding Models

Model When to Use
Any real embedding model For production use

Browse the full LangChain API reference index to explore more classes, methods, and decorators, or start with the LangChain introduction tutorial for end-to-end context on building with FakeEmbeddings and the wider framework.

FakeEmbeddings FAQ

What is FakeEmbeddings in LangChain?

Generate random embeddings for testing. FakeEmbeddings is a test double that implements the LangChain Embeddings interface but returns random vectors of a fixed size instead of calling a real model. Its entire purpose is to let you exercise the rest of a RAG or retrieval pipeline — document loading, splitting, vector-store wiring, retriever plumbing — without making a network call, burning API credits, or needing an API key. It lives in langchain-core, so it is always available with no extra install. You construct…

Which package provides FakeEmbeddings?

DevShelfHub documents FakeEmbeddings from the langchain-core package. Pin your installed LangChain version and match imports to the snippet on this page.

When should I use FakeEmbeddings?

You're testing without API calls. Use FakeEmbeddings for unit tests and rapid development.

When should I avoid using FakeEmbeddings?

For any production or real semantic search.

How do I import FakeEmbeddings in Python?

from langchain_core.embeddings.fake import FakeEmbeddings

Where can I explore more LangChain API reference pages?

Open the LangChain API reference index on DevShelfHub to browse classes, methods, and decorators, each with runnable examples, parameters, common mistakes, and cross-links.