1. Overview
LangChain Infinispan provides LangChain integration with Infinispan, offering a vector store for similarity search in AI applications.
1.1. What is LangChain Infinispan?
LangChain Infinispan is a community integration that connects LangChain with Infinispan.
It provides an InfinispanVectorStore that implements the LangChain VectorStore interface, enabling:
-
Store and search document embeddings using Infinispan vector search capabilities with Ickle query language.
-
Metadata filtering with support for typed fields (strings, integers, floats).
-
Multiple distance strategies: Cosine, L2, Inner Product, and Max Inner Product.
-
Automatic Protobuf schema registration and indexed cache creation.
-
Three authentication methods: username/password, bearer token, or pre-built client.
The integration uses the Infinispan REST API for all operations.
2. Getting started
Install LangChain Infinispan and connect to an Infinispan Server instance.
2.1. Installing LangChain Infinispan
Install the LangChain Infinispan package from PyPI.
-
Python 3.10 or later
-
Infinispan 15 or later with vector search support
-
Install the package:
pip install langchain-infinispan -
Verify the installation:
from langchain_infinispan import InfinispanVectorStore print("langchain-infinispan installed successfully")
2.2. Starting Infinispan Server
Start an Infinispan Server instance for your LangChain application to connect to.
-
Run Infinispan Server with Docker:
docker run -p 11222:11222 -e USER="admin" -e PASS="password" quay.io/infinispan/server:latest
The server starts with default settings and creates an admin user.
The REST API is available at http://localhost:11222.
3. Vector store
Use Infinispan as a vector store in your LangChain applications for storing embeddings and performing similarity search.
3.1. How the vector store works
The Infinispan vector store uses Protobuf schemas to define the structure of stored embeddings. When the vector store is first used, it:
-
Builds a Protobuf schema with dimension-specific message names (for example,
LangChainItem1536for 1536-dimensional embeddings). -
Registers the schema with Infinispan Server.
-
Creates an indexed distributed cache configured for vector search.
Similarity search uses the Infinispan Ickle query language with the <→ vector distance operator.
The store supports metadata filtering through Ickle query joins on embedded metadata fields.
Each document is stored as a Protobuf message containing:
-
id- unique identifier -
text- the document text -
embedding- the vector representation -
metadata- repeated embedded messages with typed fields for strings, integers, and floats
3.2. Creating the vector store
Create an InfinispanVectorStore instance to connect to Infinispan Server.
Three authentication methods are available.
from langchain_infinispan import InfinispanVectorStore
from langchain_openai import OpenAIEmbeddings
vector_store = InfinispanVectorStore(
embedding=OpenAIEmbeddings(),
cache_name="my_vectors",
ispn_url="http://localhost:11222",
ispn_user="admin",
ispn_password="password",
)
vector_store = InfinispanVectorStore(
embedding=OpenAIEmbeddings(),
cache_name="my_vectors",
ispn_url="http://localhost:11222",
ispn_bearer_token="my-token",
)
from langchain_infinispan import InfinispanVectorStore, InfinispanClient
client = InfinispanClient(
url="http://localhost:11222",
username="admin",
password="password",
)
vector_store = InfinispanVectorStore(
embedding=OpenAIEmbeddings(),
cache_name="my_vectors",
client=client,
)
Using a pre-built client allows you to share a single HTTP session across multiple vector store instances.
3.3. Vector store parameters
The following table describes the parameters for InfinispanVectorStore.
| Parameter | Type | Default | Description |
|---|---|---|---|
|
|
(required) |
The embedding model to use for vectorizing text. |
|
|
|
Name of the Infinispan cache to store vectors. |
|
|
|
Pre-built client instance. If provided, connection parameters are ignored. |
|
|
Infinispan Server URL. |
|
|
|
|
Username for HTTP Digest authentication. |
|
|
|
Password for HTTP Digest authentication. |
|
|
|
Bearer token for authentication. |
|
|
|
Whether to verify TLS certificates. |
|
|
|
Embedding dimension. Auto-detected from the embedding model if not set. |
|
|
|
Distance strategy for similarity search. |
|
|
|
Custom cache configuration XML. Uses a default indexed distributed cache if not set. |
|
|
|
Whether to auto-create the cache if it does not exist. |
|
|
|
Whether to auto-register the Protobuf schema. |
3.4. Distance strategies
The following distance strategies are available for similarity search.
| Strategy | Description |
|---|---|
|
Cosine similarity. Default strategy. Works with any vectors. |
|
Euclidean (squared L2) distance. |
|
Dot product similarity. Requires unit-length (normalized) vectors. On Infinispan 15.1+, the server rejects non-normalized vectors. |
|
Dot product similarity that allows non-normalized vectors. Relevance scores are normalized using |
from langchain_infinispan import InfinispanVectorStore, DistanceStrategy
vector_store = InfinispanVectorStore(
embedding=my_embeddings,
similarity=DistanceStrategy.L2,
ispn_url="http://localhost:11222",
ispn_user="admin",
ispn_password="password",
)
3.5. Adding documents
Add documents to the Infinispan vector store. Documents are automatically embedded and stored with their metadata.
ids = vector_store.add_texts(
texts=["Hello world", "Infinispan is fast"],
metadatas=[{"source": "greeting"}, {"source": "fact"}],
)
from langchain_core.documents import Document
docs = [
Document(page_content="Hello world", metadata={"source": "greeting"}),
Document(page_content="Infinispan is fast", metadata={"source": "fact"}),
]
ids = vector_store.add_documents(docs)
vector_store = InfinispanVectorStore.from_texts(
texts=["Hello world", "Infinispan is fast"],
embedding=my_embeddings,
metadatas=[{"source": "greeting"}, {"source": "fact"}],
ispn_url="http://localhost:11222",
ispn_user="admin",
ispn_password="password",
)
Unique IDs are auto-generated for each document. You can also provide custom IDs with the ids parameter.
3.6. Performing similarity search
Search for documents similar to a query using the Infinispan vector store.
results = vector_store.similarity_search("hello", k=5)
for doc in results:
print(doc.page_content, doc.metadata)
results = vector_store.similarity_search_with_score("hello", k=5)
for doc, score in results:
print(f"{doc.page_content} (score: {score})")
Relevance scores are normalized to [0, 1] for all distance strategies.
results = vector_store.similarity_search_with_relevance_scores("hello", k=5)
for doc, score in results:
print(f"{doc.page_content} (relevance: {score})")
embedding = my_embeddings.embed_query("hello")
results = vector_store.similarity_search_by_vector(embedding, k=5)
results = vector_store.similarity_search(
"hello",
k=5,
filter={"source": "greeting"},
)
3.7. Managing documents
Delete, retrieve, and clear documents in the Infinispan vector store.
vector_store.delete(ids=["id1", "id2"])
docs = vector_store.get_by_ids(["id1", "id2"])
vector_store.clear()
Close the HTTP session when you are done with the vector store.
vector_store.close()
4. Metadata filtering
Filter similarity search results based on document metadata using Infinispan Ickle queries.
4.1. How metadata filtering works
The Infinispan vector store supports filtering similarity search results based on document metadata.
Metadata values are stored with type-aware fields in the Protobuf schema:
-
String values are stored in the
valuefield. -
Integer values are stored in both
valueandvalue_intfields. -
Float values are stored in both
valueandvalue_floatfields.
Two filter formats are supported:
Simple key-value equality matching. List values are translated to IN clauses.
results = vector_store.similarity_search(
"hello",
filter={"source": "greeting"},
)
results = vector_store.similarity_search(
"hello",
filter={"category": ["news", "tech"]},
)
Use Comparison and Operation objects from langchain_core.structured_query for complex filter expressions.
from langchain_core.structured_query import Comparison, Comparator
results = vector_store.similarity_search(
"hello",
filter=Comparison(
comparator=Comparator.GT,
attribute="year",
value=2020,
),
)
Filter expressions are translated to Infinispan Ickle queries with metadata joins for each filtered field.
4.2. Supported filter operations
The Infinispan vector store supports the following filter operations.
| Comparator | Description | Example |
|---|---|---|
|
Equals |
|
|
Not equals |
|
|
Greater than |
|
|
Greater than or equal |
|
|
Less than |
|
|
Less than or equal |
|
|
In list |
|
|
Not in list |
|
|
Contains substring |
|
|
Pattern match |
|
| Operator | Description |
|---|---|
|
All conditions must match. |
|
At least one condition must match. |
|
Negates the condition. |
from langchain_core.structured_query import (
Comparator, Comparison, Operation, Operator,
)
filter_expr = Operation(
operator=Operator.AND,
arguments=[
Comparison(comparator=Comparator.EQ, attribute="country", value="BG"),
Comparison(comparator=Comparator.GT, attribute="year", value=2020),
],
)
results = vector_store.similarity_search("hello", filter=filter_expr)