Pinecone Python Client (Legacy)

6.0.0 · deprecated · verified Sat Apr 11

The `pinecone-client` library is the **DEPRECATED** Python client for the Pinecone vector database, which allows users to store, search, and manage high-dimensional vectors. This package is no longer maintained, and its last version is 6.0.0. All users should migrate to the new `pinecone` package for continued support, updates, and new features. The new `pinecone` package follows a quarterly release cadence aligned with Pinecone's API versioning.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the **new** `pinecone` client, create a serverless index, upsert vectors, and query the index. It is crucial to use the `pinecone` package for all new development as `pinecone-client` is deprecated. Replace `YOUR_API_KEY` with your actual Pinecone API key and adjust the dimension and region as needed.

import os
from pinecone import Pinecone, ServerlessSpec, CloudProvider, AwsRegion

# Initialize the Pinecone client (using the new 'pinecone' package)
api_key = os.environ.get('PINECONE_API_KEY', 'YOUR_API_KEY')
# Ensure you have a Pinecone API Key set as an environment variable or replace 'YOUR_API_KEY'

pc = Pinecone(api_key=api_key)

index_name = 'my-first-index'
dimension = 1536  # Example dimension, choose based on your embedding model

# Create a serverless index (if it doesn't exist)
if index_name not in pc.list_indexes().names():
    pc.create_index(
        name=index_name,
        dimension=dimension,
        metric='cosine', # or 'euclidean', 'dotproduct'
        spec=ServerlessSpec(
            cloud=CloudProvider.AWS, 
            region=AwsRegion.US_EAST_1
        )
    )
print(f"Index '{index_name}' created or already exists.")

# Connect to the index
index = pc.Index(name=index_name)

# Example: Upserting data (replace with your actual vectors/text)
vectors_to_upsert = [
    {"id": "vec1", "values": [0.1]*dimension, "metadata": {"genre": "fiction"}},
    {"id": "vec2", "values": [0.2]*dimension, "metadata": {"genre": "non-fiction"}}
]
index.upsert(vectors=vectors_to_upsert)
print(f"Upserted {len(vectors_to_upsert)} vectors to '{index_name}'.")

# Example: Querying the index
query_vector = [0.15]*dimension
query_results = index.query(vector=query_vector, top_k=1, include_metadata=True)
print("Query results:", query_results)

# Example: Deleting the index (uncomment to run)
# pc.delete_index(index_name)
# print(f"Index '{index_name}' deleted.")

view raw JSON →