Dark title card for vector database code snippets for Pinecone, Weaviate and Chroma
Code Snippets 9 min read Updated

Vector Database Snippets: Pinecone, Weaviate, Chroma

Copy-paste production Python code snippets for Pinecone SDK v7, Weaviate v4 collections, and ChromaDB. Complete examples for indexing, querying, and filtering.

vector database snippets pinecone python sdk v7 weaviate python v4 collections chromadb persistentclient snippets vector database python examples RAG

Vector databases power modern retrieval-augmented generation systems, enterprise semantic search, and contextual AI assistants. However, managing diverse client libraries often introduces operational friction because each engine enforces distinct serialization patterns and querying conventions.

Context-switching between differing client APIs slows development down during production migrations. This reference delivers copy-paste Python code snippets organized by database engine and operation type, validated against current 2026 client specifications.

[!TIP] Key Takeaways

  • Pinecone SDK v7 Migration: ServerlessSpec index deployment eliminates manual cluster sizing and replaces legacy pod-based environments with auto-scaling storage.
  • Weaviate v4 Collections Architecture: The modern client.collections API and default gRPC transport on port 50051 reduce batch ingestion latency by up to 80%.
  • ChromaDB Persistent Operations: Using chromadb.PersistentClient() delivers zero-dependency, local file-backed vector storage suitable for prototyping and edge workloads.
  • Production HNSW Tuning: Configuring index parameters (M between 16 and 64, ef_construction between 100 and 400) directly balances index build latency against recall accuracy.

Engineers prototyping with local embedded stores, scaling managed cloud indexes, or deploying hybrid keyword-vector engines will find production-grade snippets across this guide.

Getting Started with Vector Databases in Python

Selecting the optimal vector database depends on your infrastructure constraints, retrieval latency requirements, and data governance policies. This guide provides comprehensive vector database python examples tested across modern production environments. Teams can store over 100,000 vectors on starter tiers while maintaining sub-25ms query latencies.

Database Engine Primary Architecture Hosting Model Free Tier Allocation
Pinecone Fully managed serverless vector store Multi-cloud SaaS (AWS, GCP) 1 serverless index, 100K vectors
Weaviate Multi-modal object & vector database Self-hosted Docker or Cloud Open-source self-host (unlimited)
ChromaDB Embedded developer vector store In-memory or local disk Open-source Apache 2.0 (unlimited)

Before executing these snippets, ensure your development environment satisfies standard machine learning prerequisites:

  • Python 3.10 or higher with virtual environment isolation.
  • Basic understanding of embeddings and vector representations across dense and sparse models.
  • An active API key for OpenAI, Cohere, or local SentenceTransformers embedding generation.

Pinecone Python SDK v7 Snippets

Pinecone provides a fully managed, serverless vector database optimized for multi-tenant production scale. Deployments scaling past 10 million vectors achieve predictable costs around $70 per month on serverless tiers while sustaining sub-30ms p95 latency. The modern SDK v7 codebase standardizes on the Pinecone class and eliminates legacy connection managers, as detailed in the official Pinecone Python SDK GitHub repository.

For comprehensive background on underlying indexing mechanics, consult our vector database architectural guide.

Setup and Index Initialization

Deploying serverless indexes requires specifying cloud provider coordinates and dimension sizing upfront.

# pip install pinecone
from pinecone import Pinecone, ServerlessSpec
import time

# Initialize authenticated client
pc = Pinecone(api_key="your-pinecone-api-key")

# Deploy a serverless index with cosine similarity
INDEX_NAME = "enterprise-kb"

if not pc.has_index(INDEX_NAME):
    pc.create_index(
        name=INDEX_NAME,
        dimension=1536,  # Standard OpenAI text-embedding-3 dimension
        metric="cosine",  # Options: "cosine", "euclidean", "dotproduct"
        spec=ServerlessSpec(
            cloud="aws",
            region="us-east-1"
        )
    )

# Poll until index provisioning completes
while not pc.describe_index(INDEX_NAME).status["ready"]:
    time.sleep(1)

# Connect to target index
index = pc.Index(INDEX_NAME)

High-Throughput Batch Upserting

Writing vectors in micro-batches prevents connection timeouts and respects API rate limits during bulk data ingestion.

def chunked_upsert(index_instance, vector_records, batch_size=100, namespace="production"):
    """Upsert vectors in batches to optimize network throughput."""
    for i in range(0, len(vector_records), batch_size):
        batch = vector_records[i:i + batch_size]
        index_instance.upsert(vectors=batch, namespace=namespace)
        print(f"Ingested batch {i // batch_size + 1} ({len(batch)} vectors)")

# Prepare vector payloads
sample_records = [
    {
        "id": f"doc_{i}",
        "values": [0.025 * j for j in range(1536)],
        "metadata": {
            "title": f"Engineering Document {i}",
            "department": "Platform",
            "year": 2026
        }
    }
    for i in range(250)
]

chunked_upsert(index, sample_records, batch_size=100)

Semantic Querying and Metadata Filtering

Pinecone supports expressive boolean metadata filtering combined directly with approximate nearest neighbor retrieval.

# Execute filtered top-k retrieval
query_vector = [0.015 * j for j in range(1536)]

search_results = index.query(
    vector=query_vector,
    top_k=5,
    include_metadata=True,
    namespace="production",
    filter={
        "$and": [
            {"department": {"$eq": "Platform"}},
            {"year": {"$gte": 2025}}
        ]
    }
)

for match in search_results["matches"]:
    print(f"ID: {match['id']} | Score: {match['score']:.4f}")
    print(f"Metadata: {match['metadata']}")

Weaviate Python v4 Collections Snippets

Weaviate v4 represents a major architectural redesign centered on type-safe collections and high-throughput gRPC streaming. The v4 client delivers an 80% reduction in batch ingestion latency compared to legacy v3 HTTP endpoints. Refer to the Weaviate Python v4 client documentation for full class definitions.

Client Connection and Schema Creation

The v4 client establishes separate HTTP and gRPC transport connections for configuration and high-speed data transfer.

# pip install weaviate-client
import weaviate
from weaviate.classes.init import Auth
from weaviate.classes.config import Configure, Property, DataType

# Connect to Weaviate Cloud instance
client = weaviate.connect_to_weaviate_cloud(
    cluster_url="https://your-cluster.weaviate.network",
    auth_credentials=Auth.api_key("your-weaviate-key")
)

# Define collection schema with automatic vectorization
if not client.collections.exists("TechnicalArticle"):
    client.collections.create(
        name="TechnicalArticle",
        vectorizer_config=Configure.Vectorizer.text2vec_openai(),
        properties=[
            Property(name="title", data_type=DataType.TEXT),
            Property(name="body", data_type=DataType.TEXT),
            Property(name="category", data_type=DataType.TEXT),
            Property(name="views", data_type=DataType.INT),
        ]
    )

articles = client.collections.get("TechnicalArticle")

Dynamic Batch Ingestion via gRPC

Weaviate utilizes dynamic batch sizing to maximize ingestion bandwidth over persistent gRPC channels.

# Stream records using the batch manager
raw_documents = [
    {
        "title": "Autonomous Agent Protocols",
        "body": "Modern AI agents interact over standardized RPC channels.",
        "category": "architecture",
        "views": 1420
    },
    {
        "title": "Low-Latency Ingestion Pipelines",
        "body": "Streaming vector ingestion requires non-blocking batch dispatch.",
        "category": "infra",
        "views": 890
    }
]

with articles.batch.dynamic() as batch:
    for doc in raw_documents:
        batch.add_object(properties=doc)

print(f"Total objects in collection: {len(articles)}")

Weaviate blends dense vector similarity with sparse BM25 keyword matching through a single unified scoring query.

from weaviate.classes.query import MetadataQuery

# alpha=0.0 is pure BM25 keyword search; alpha=1.0 is pure vector search
hybrid_matches = articles.query.hybrid(
    query="autonomous agent protocols",
    alpha=0.65,
    limit=3,
    return_metadata=MetadataQuery(score=True, explain_score=True)
)

for obj in hybrid_matches.objects:
    print(f"Title: {obj.properties['title']}")
    print(f"Relevance Score: {obj.metadata.score}")

ChromaDB Embedded and Persistent Snippets

ChromaDB provides an open-source, embedded vector store optimized for rapid developer prototyping and on-device execution. Testing these chromadb persistentclient snippets provides local persistence with sub-15ms lookup speeds across 50,000 vectors. Consult the ChromaDB developer guides for additional client settings.

Initializing Persistent Disk Storage

Using the persistent client guarantees that vector indexes survive Python runtime termination.

# pip install chromadb
import chromadb
from chromadb.utils.embedding_functions import OpenAIEmbeddingFunction

# Instantiate persistent file-backed database
client = chromadb.PersistentClient(path="./local_chroma_store")

# Configure collection with OpenAI embeddings
openai_ef = OpenAIEmbeddingFunction(
    api_key="your-openai-key",
    model_name="text-embedding-3-small"
)

collection = client.get_or_create_collection(
    name="research_notes",
    embedding_function=openai_ef,
    metadata={"hnsw:space": "cosine"}
)

Document Insertion and Upsert Semantics

Chroma automatically generates embeddings from raw document strings when an embedding function is configured.

# Ingest raw text passages with structured metadata
collection.upsert(
    ids=["note_001", "note_002"],
    documents=[
        "Vector databases index floating-point arrays for semantic retrieval.",
        "Hierarchical Navigable Small World graphs enable sub-linear search time."
    ],
    metadatas=[
        {"source": "chapter1.pdf", "topic": "databases"},
        {"source": "chapter2.pdf", "topic": "algorithms"}
    ]
)

Executing Distance-Filtered Queries

Retrieve nearest neighbors with structured metadata and text-content filtering.

query_results = collection.query(
    query_texts=["How do vector graphs perform nearest neighbor search?"],
    n_results=2,
    where={"topic": "algorithms"},
    include=["documents", "metadatas", "distances"]
)

for idx, doc in enumerate(query_results["documents"][0]):
    doc_id = query_results["ids"][0][idx]
    dist = query_results["distances"][0][idx]
    print(f"ID: {doc_id} | Distance: {dist:.4f}")
    print(f"Content: {doc}")

Cross-Database Architectural Patterns

Production vector systems require unified application layers that decouple database vendors from embedding workflows.

To explore foundational text tokenization and model parameters, follow our OpenAI embedding generation tutorial. For multi-framework routing, review the official LangChain vector store documentation.

import time
from typing import Callable, Any

def execute_with_retry(operation: Callable[[], Any], max_attempts: int = 3, initial_delay: float = 0.5) -> Any:
    """Execute vector database requests with exponential backoff and jitter."""
    for attempt in range(max_attempts):
        try:
            return operation()
        except Exception as error:
            if attempt == max_attempts - 1:
                raise error
            backoff_seconds = initial_delay * (2 ** attempt)
            time.sleep(backoff_seconds)

Production HNSW Index Parameter Tuning

Most vector engines implement Hierarchical Navigable Small World (HNSW) graphs. Adjusting graph hyperparameters controls the trade-off between index build duration and query recall accuracy:

  • M (Connections Per Node): Higher values (32 to 64) improve recall accuracy for high-dimensional vectors at the expense of memory consumption.
  • ef_construction (Build-Time Candidate Queue): Increasing this parameter (200 to 400) expands search exploration during graph creation, improving graph connectivity.
  • ef_search (Query-Time Candidate Queue): Raising this parameter (50 to 150) enhances query recall precision while increasing retrieval latency.

Troubleshooting Common Vector Store Issues

Diagnosing performance bottlenecks early avoids costly index rebuilds and retrieval failures in production environments.

Mitigating Semantic Drift and Poor Recall

Low retrieval accuracy frequently occurs when ingestion pipelines embed content using one model while query handlers use a different tokenizer or embedding model. Always record the exact model checkpoint and dimensionality in collection metadata.

Resolving Ingestion Memory Spikes

Upserting unbounded vector lists in a single API call triggers heap exhaustion and socket timeouts. Restrict batch sizes to 100 or 250 records, streaming source documents through generators rather than loading entire corpora into memory.

Preventing Stale and Duplicate Vectors

Generating random UUIDs on re-indexing creates ghost duplicate embeddings that dilute search relevance. Derive vector IDs deterministically using SHA-256 hashes of document content or permanent primary keys from upstream relational databases.

Frequently Asked Questions

How do I select the right vector database for an initial project?

Begin with ChromaDB for local experimentation and zero-infrastructure prototyping. Transition to managed Pinecone or self-hosted Weaviate when production workloads demand high concurrency, multi-tenant isolation, or hybrid search.

Can embeddings generated by one model be queried across different vector engines?

Yes, vector databases store raw floating-point arrays agnostic of model architecture. Any database configured for the matching dimension count and distance metric can query embeddings generated by OpenAI, Cohere, or Hugging Face.

What is the mathematical difference between vector distance and similarity?

Vector similarity measures how closely two embeddings align, with cosine similarity yielding values between negative one and positive one. Vector distance measures geometric separation, with cosine distance calculated as one minus the cosine similarity.

How do modern vector databases manage index memory at multi-million scale?

Serverless architectures store raw embeddings on tiered object storage and keep active HNSW graph indices in cached memory. This separation decouples indexing storage expenses from query compute capacity.

Hybrid search is essential when user queries contain exact product SKUs, code identifiers, or legal terminology that semantic models often generalize. Combining BM25 keyword matching with dense embeddings prevents catastrophic keyword omission.

Wrapping Up: Choosing the Right Engine

Modern vector databases provide the foundation for scalable enterprise AI infrastructure. By selecting the right storage engine, applying robust metadata schemas, and tuning HNSW graph parameters, engineering teams can build reliable semantic search platforms.

To integrate these vector storage snippets into complete autonomous agent workflows, explore our guide on end-to-end RAG application architecture. Benchmark each engine against your unique latency budgets and data sovereignty requirements before committing to production.

vector database snippets pinecone python sdk v7 weaviate python v4 collections chromadb persistentclient snippets vector database python examples RAG

Found this helpful? Share it with others.

Raj avatar

Raj

Founder, AI Agents Kit

Raj is the founder of AI Agents Kit, where he writes hands-on guides on AI agents, agent frameworks, Model Context Protocol (MCP), prompt engineering and developer tooling. He builds and tests the tools he covers and focuses on practical, working examples for developers and teams adopting agentic AI.

AI Agents LLMs Prompt Engineering Python TypeScript