How to create a Pinecone index
Quick answer
Use the
Pinecone SDK v3+ to create an index by initializing the client with your API key, then calling pc.Index.create() with your index name, dimension, and metric. This sets up a vector index ready for storing and querying embeddings.PREREQUISITES
Python 3.8+Pinecone API keypip install pinecone-client>=3.0
Setup
Install the Pinecone Python SDK and set your API key as an environment variable before creating an index.
pip install pinecone-client>=3.0 Step by step
This example shows how to create a Pinecone index named example-index with 1536 dimensions and cosine similarity metric.
import os
from pinecone import Pinecone
# Initialize Pinecone client
pc = Pinecone(api_key=os.environ["PINECONE_API_KEY"])
# Define index parameters
index_name = "example-index"
dimension = 1536
metric = "cosine"
# Create the index
pc.Index.create(name=index_name, dimension=dimension, metric=metric)
print(f"Index '{index_name}' created successfully.") output
Index 'example-index' created successfully.
Common variations
You can create indexes with different metrics like euclidean or dotproduct, and adjust the dimension to match your embedding size. The Pinecone SDK also supports async usage with asyncio if needed.
import asyncio
from pinecone import Pinecone
async def create_index_async():
pc = Pinecone(api_key=os.environ["PINECONE_API_KEY"])
await pc.Index.create(name="async-index", dimension=512, metric="euclidean")
print("Async index created.")
asyncio.run(create_index_async()) output
Async index created.
Troubleshooting
- If you see
IndexAlreadyExistsException, the index name is taken; choose a unique name. - Ensure your
PINECONE_API_KEYenvironment variable is set correctly. - Check your network connection if the client cannot reach Pinecone servers.
Key Takeaways
- Initialize the Pinecone client with your API key from environment variables.
- Use
pc.Index.create()with name, dimension, and metric to create an index. - Match the dimension to your embedding vector size for proper indexing.
- Choose the similarity metric based on your use case: cosine, euclidean, or dotproduct.
- Handle common errors like duplicate index names and API key misconfiguration.