How to Intermediate · 3 min read

How to use Pinecone with LlamaIndex

Quick answer
Use the pinecone Python SDK to create and manage a vector index, then connect it with LlamaIndex by initializing a PineconeVectorStore as the vector store backend. This enables efficient similarity search and retrieval within LlamaIndex using Pinecone's managed vector database.

PREREQUISITES

  • Python 3.8+
  • Pinecone API key (free tier available)
  • pip install pinecone-client llama-index
  • Set environment variables PINECONE_API_KEY

Setup

Install the required Python packages and set your Pinecone API key environment variable.

  • Install packages: pip install pinecone-client llama-index
  • Export environment variable in your shell:
    export PINECONE_API_KEY='your-pinecone-api-key'
bash
pip install pinecone-client llama-index

Step by step

This example demonstrates creating a Pinecone index, initializing LlamaIndex with Pinecone as the vector store, inserting documents, and querying for similar content.

python
import os
from pinecone import Pinecone
from llama_index import GPTVectorStoreIndex, SimpleDirectoryReader, PineconeVectorStore

# Initialize Pinecone client
pc = Pinecone(api_key=os.environ["PINECONE_API_KEY"])

# Create Pinecone index if it doesn't exist
index_name = "llamaindex-example"
if index_name not in pc.list_indexes():
    pc.create_index(index_name, dimension=1536, metric="cosine")

# Connect to Pinecone index
pinecone_index = pc.Index(index_name)

# Initialize PineconeVectorStore for LlamaIndex
vector_store = PineconeVectorStore(pinecone_index)

# Load documents (replace with your own data source)
docs = SimpleDirectoryReader("./data").load_data()

# Create LlamaIndex with Pinecone vector store
index = GPTVectorStoreIndex(docs, vector_store=vector_store)

# Query the index
query_str = "What is the capital of France?"
response = index.query(query_str)
print("Query response:", response.response)
output
Query response: Paris is the capital of France.

Common variations

  • Use different embedding models by passing a custom embed_model to GPTVectorStoreIndex.
  • Use async Pinecone client with pinecone.AsyncIndex for asynchronous operations.
  • Switch to other vector stores like FAISS or Chroma by changing the vector store initialization.
python
from llama_index import LangchainEmbedding
from langchain_openai import OpenAI

# Example: Using OpenAI embeddings with LlamaIndex and Pinecone
embed_model = LangchainEmbedding(OpenAI(model_name="gpt-4o"))
index = GPTVectorStoreIndex(docs, vector_store=vector_store, embed_model=embed_model)

Troubleshooting

  • If you see IndexNotFoundError, ensure the Pinecone index is created and the name matches.
  • For authentication errors, verify your PINECONE_API_KEY environment variable is set correctly.
  • If queries return empty results, check that documents were properly indexed and embeddings generated.

Key Takeaways

  • Initialize Pinecone client with API key before use.
  • Use PineconeVectorStore to integrate Pinecone with LlamaIndex seamlessly.
  • Create the Pinecone index with the correct vector dimension matching your embeddings.
  • Load and index documents before querying to get meaningful results.
  • Verify environment variables and index existence to avoid common errors.
Verified 2026-04 · gpt-4o
Verify ↗