> ## Documentation Index
> Fetch the complete documentation index at: https://docs.getnetra.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Pinecone

> Monitor Pinecone vector database with automatic query and upsert tracking

<img src="https://mintcdn.com/netra/IXT7TOAHn4HQhvyF/images/integration-logos/vector-databases/pinecone.png?fit=max&auto=format&n=IXT7TOAHn4HQhvyF&q=85&s=08805855a7d4cbe7af6108fcad5c5881" alt="Pinecone" width="391" height="80" data-path="images/integration-logos/vector-databases/pinecone.png" />

## Installation

Install both the Netra SDK and Pinecone:

<CodeGroup>
  ```bash Python theme={null}
  pip install netra-sdk pinecone-client
  ```

  ```bash Typescript theme={null}
  npm install netra-sdk @pinecone-database/pinecone
  ```
</CodeGroup>

## Usage

Initialize the Netra SDK to automatically trace all Pinecone operations:

<CodeGroup>
  ```python Python theme={null}
  from netra import Netra
  from pinecone import Pinecone
  import os

  # Initialize Netra
  Netra.init(
      headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}",
      trace_content=True
  )

  # Create Pinecone client - automatically traced
  pc = Pinecone(api_key=os.environ.get('PINECONE_API_KEY'))
  index = pc.Index("my-index")

  # Upsert vectors
  index.upsert(vectors=[{
      "id": "vec1",
      "values": [0.1, 0.2, 0.3],
      "metadata": {"text": "Sample document"}
  }])
  ```

  ```typescript Typescript theme={null}
  import { Netra } from "netra-sdk";
  import { Pinecone } from "@pinecone-database/pinecone";

  // Initialize Netra
  await Netra.init({
    headers: `x-api-key=${process.env.NETRA_API_KEY}`,
    traceContent: true
  });

  // Create Pinecone client - automatically traced
  const pinecone = new Pinecone({
    apiKey: process.env.PINECONE_API_KEY
  });

  const index = pinecone.index("my-index");

  // Upsert vectors
  await index.upsert([{
    id: "vec1",
    values: [0.1, 0.2, 0.3],
    metadata: { text: "Sample document" }
  }]);
  ```
</CodeGroup>

### Index Operations

Trace index creation and management:

<CodeGroup>
  ```python Python theme={null}
  from netra.decorators import task
  from netra import SpanWrapper

  @task()
  def create_index(pc: Pinecone, name: str, dimension: int):
      span = SpanWrapper("pinecone-create-index", {
          "index.name": name,
          "index.dimension": dimension
      }).start()
      
      pc.create_index(
          name=name,
          dimension=dimension,
          metric="cosine",
          spec={"serverless": {"cloud": "aws", "region": "us-west-2"}}
      )
      
      span.end()
  ```

  ```typescript Typescript theme={null}
  import { task, SpanWrapper } from "netra-sdk";

  @task()
  async function createIndex(pinecone: Pinecone, name: string, dimension: number) {
    const span = new SpanWrapper("pinecone-create-index", {
      "index.name": name,
      "index.dimension": dimension
    }).start();
    
    await pinecone.createIndex({
      name,
      dimension,
      metric: "cosine",
      spec: {
        serverless: {
          cloud: "aws",
          region: "us-west-2"
        }
      }
    });
    
    span.end();
  }
  ```
</CodeGroup>

### Vector Upsert

Trace vector insertions:

<CodeGroup>
  ```python Python theme={null}
  from netra.decorators import task
  from netra import SpanWrapper, ActionModel

  @task()
  def upsert_vectors(index, vectors: list):
      span = SpanWrapper("pinecone-upsert", {
          "vectors.count": len(vectors)
      }).start()
      
      result = index.upsert(vectors=vectors)
      
      span.set_action([ActionModel(
          action="upsert",
          action_type="database.upsert",
          success=True,
          affected_records=[{"id": v["id"]} for v in vectors],
          metadata={"index": index._index_name, "upserted": result["upserted_count"]}
      )])
      span.set_attribute("upserted.count", result["upserted_count"])
      span.end()
      
      return result
  ```

  ```typescript Typescript theme={null}
  import { task, SpanWrapper, ActionModel } from "netra-sdk";

  @task()
  async function upsertVectors(index: any, vectors: any[]) {
    const span = new SpanWrapper("pinecone-upsert", {
      "vectors.count": vectors.length
    }).start();
    
    const result = await index.upsert(vectors);
    
    span.setAction([{
      action: "upsert",
      action_type: "database.upsert",
      success: true,
      affected_records: vectors.map(v => ({ id: v.id })),
      metadata: { index: index.name, upserted: result.upsertedCount }
    }]);
    span.setAttribute("upserted.count", result.upsertedCount);
    span.end();
    
    return result;
  }
  ```
</CodeGroup>

### Vector Search

Trace similarity searches:

<CodeGroup>
  ```python Python theme={null}
  from netra.decorators import workflow
  from netra import SpanWrapper

  @workflow()
  def search_vectors(index, query: list[float], top_k: int = 5):
      span = SpanWrapper("pinecone-search", {
          "query.dimension": len(query),
          "top_k": top_k
      }).start()
      
      results = index.query(
          vector=query,
          top_k=top_k,
          include_metadata=True
      )
      
      span.set_attribute("results.count", len(results["matches"]))
      span.end()
      
      return results
  ```

  ```typescript Typescript theme={null}
  import { workflow, SpanWrapper } from "netra-sdk";

  @workflow()
  async function searchVectors(index: any, query: number[], topK: number = 5) {
    const span = new SpanWrapper("pinecone-search", {
      "query.dimension": query.length,
      "top_k": topK
    }).start();
    
    const results = await index.query({
      vector: query,
      topK,
      includeMetadata: true
    });
    
    span.setAttribute("results.count", results.matches.length);
    span.end();
    
    return results;
  }
  ```
</CodeGroup>

### Namespace Operations

Trace namespace-specific operations:

<CodeGroup>
  ```python Python theme={null}
  from netra.decorators import task
  from netra import SpanWrapper

  @task()
  def query_namespace(index, namespace: str, query: list[float]):
      span = SpanWrapper("pinecone-namespace-query", {
          "namespace": namespace
      }).start()
      
      results = index.query(
          vector=query,
          top_k=10,
          namespace=namespace
      )
      
      span.set_attribute("results.count", len(results["matches"]))
      span.end()
      
      return results
  ```

  ```typescript Typescript theme={null}
  import { task, SpanWrapper } from "netra-sdk";

  @task()
  async function queryNamespace(index: any, namespace: string, query: number[]) {
    const span = new SpanWrapper("pinecone-namespace-query", {
      "namespace": namespace
    }).start();
    
    const results = await index.namespace(namespace).query({
      vector: query,
      topK: 10
    });
    
    span.setAttribute("results.count", results.matches.length);
    span.end();
    
    return results;
  }
  ```
</CodeGroup>

## Next Steps

* [Quick Start Guide](https://docs.getnetra.ai/quick-start/python) - Complete setup and configuration
* [Decorators](https://docs.getnetra.ai/tracing/decorators) - Add custom tracing with `@workflow`, `@agent`, and `@task` decorators
* [Pinecone Documentation](https://docs.pinecone.io/) - Official Pinecone documentation
