LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
  • LangGraph Checkpoint
    LangGraph Store
    Checkpoint Postgres
    Store Postgres
    Checkpoint SQLite
    LangGraph Prebuilt
    LangGraph CLI
    LangGraph SDK
    LangGraph Supervisor
    LangGraph Swarm
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    LangGraph Checkpoint
    LangGraph Store
    Checkpoint Postgres
    Store Postgres
    Checkpoint SQLite
    LangGraph Prebuilt
    LangGraph CLI
    LangGraph SDK
    LangGraph Supervisor
    LangGraph Swarm
    Language
    Theme
    Pythonlanggraph.storebaseembedensure_embeddings
    Function●Since v2.0

    ensure_embeddings

    Ensure that an embedding function conforms to LangChain's Embeddings interface.

    This function wraps arbitrary embedding functions to make them compatible with LangChain's Embeddings interface. It handles both synchronous and asynchronous functions.

    Copy
    ensure_embeddings(
      embed: Embeddings | EmbeddingsFunc | AEmbeddingsFunc | str | None
    ) -> Embeddings

    Wrap a synchronous embedding function:

    def my_embed_fn(texts):
        return [[0.1, 0.2] for _ in texts]
    
    embeddings = ensure_embeddings(my_embed_fn)
    result = embeddings.embed_query("hello")  # Returns [0.1, 0.2]

    Wrap an asynchronous embedding function:

    async def my_async_fn(texts):
        return [[0.1, 0.2] for _ in texts]
    
    embeddings = ensure_embeddings(my_async_fn)
    result = await embeddings.aembed_query("hello")  # Returns [0.1, 0.2]

    Initialize embeddings using a provider string:

    # Requires langchain>=0.3.9 and langgraph-checkpoint>=2.0.11
    embeddings = ensure_embeddings("openai:text-embedding-3-small")
    result = embeddings.embed_query("hello")

    Parameters

    NameTypeDescription
    embed*Embeddings | EmbeddingsFunc | AEmbeddingsFunc | str | None

    Either an existing Embeddings instance, or a function that converts text to embeddings. If the function is async, it will be used for both sync and async operations.

    View source on GitHub