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

# Upsert vectors

> The `upsert` operation writes vectors into a namespace. If a new value is upserted for an existing vector ID, it will overwrite the previous value.

For guidance, examples, and limits, see [Upsert data](https://docs.pinecone.io/guides/index-data/upsert-data).

<RequestExample>
  ```python Python theme={null}
  # pip install "pinecone[grpc]"
  from pinecone.grpc import PineconeGRPC as Pinecone

  pc = Pinecone(api_key="YOUR_API_KEY")
  index = pc.Index("docs-example")

  upsert_response = index.upsert(
      vectors=[
          {
              "id": "vec1",
              "values": [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8],
              "sparse_values": {
                  "indices": [1, 5],
                  "values": [0.5, 0.5]
              },
              "metadata": {
                  "genre": "drama"
              }
          },
          {
              "id": "vec2",
              "values": [0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],
              "sparse_values": {
                  "indices": [5, 6],
                  "values": [0.4, 0.5]
              },
              "metadata": {
                  "genre": "action"
              }
          }
      ],
      namespace="example-namespace"
  )
  ```

  ```javascript JavaScript theme={null}
  // npm install @pinecone-database/pinecone
  import { Pinecone } from '@pinecone-database/pinecone'

  const pc = new Pinecone({ apiKey: "YOUR_API_KEY" })
  const index = pc.index("docs-example")

  await index.namespace('example-namespace').upsert([
    {
      id: 'vec1',
      values: [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8],
      sparseValues: {
          indices: [1, 5],
          values: [0.5, 0.5]
      },
      metadata: {'genre': 'drama'},
    },
    {
      id: 'vec2',
      values: [0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],
      metadata: {'genre': 'action'},
      sparseValues: {
          indices: [5, 6],
          values: [0.4, 0.5]
      }
    }
  ])
  ```

  ```java Java theme={null}
  import com.google.protobuf.Struct;
  import com.google.protobuf.Value;
  import io.pinecone.clients.Index;
  import io.pinecone.clients.Pinecone;

  import java.util.Arrays;
  import java.util.List;

  public class UpsertExample {
      public static void main(String[] args) {
          Pinecone pc = new Pinecone.Builder("YOUR_API_KEY").build();
          String indexName = "docs-example";
          Index index = pc.getIndexConnection(indexName);
          List<Float> values1 = Arrays.asList(0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f, 0.8f);
          List<Float> values2 = Arrays.asList(0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f, 0.8f, 0.9f);
          Struct metaData1 = Struct.newBuilder()
                  .putFields("genre", Value.newBuilder().setStringValue("drama").build())
                  .build();
          Struct metaData2 = Struct.newBuilder()
                  .putFields("genre", Value.newBuilder().setStringValue("action").build())
                  .build();

          index.upsert("A", values1, null, null, metaData1, null);
          index.upsert("B", values2, null, null, metaData2, null);
      }
  }
  ```

  ```go Go theme={null}
  package main

  import (
      "context"
      "fmt"
      "log"

      "github.com/pinecone-io/go-pinecone/v2/pinecone"
      "google.golang.org/protobuf/types/known/structpb"
  )

  func main() {
      ctx := context.Background()

      pc, err := pinecone.NewClient(pinecone.NewClientParams{
          ApiKey: "YOUR_API_KEY",
      })
      if err != nil {
          log.Fatalf("Failed to create Client: %v", err)
      }

      idx, err := pc.DescribeIndex(ctx, indexName)
      if err != nil {
          log.Fatalf("Failed to describe index \"%v\": %v", idx.Name, err)
      }

      idxConnection, err := pc.Index(pinecone.NewIndexConnParams{Host: idx.Host})
      if err != nil {
          log.Fatalf("Failed to create IndexConnection for Host %v: %v", idx.Host, err)
  	  }

      sparseValues1 := pinecone.SparseValues{
          Indices: []uint32{1, 5},
          Values:  []float32{0.5, 0.5},
      }

      sparseValues2 := pinecone.SparseValues{
          Indices: []uint32{5, 6},
          Values:  []float32{0.4, 0.5},
      }

      metadataMap1 := map[string]interface{}{
          "genre": "comedy",
          "year": 2020,
      }

      metadata1, err := structpb.NewStruct(metadataMap1)
      if err != nil {
          log.Fatalf("Failed to create metadata map: %v", err)
      }

      metadataMap2 := map[string]interface{}{
          "genre": "documentary",
          "year": 2019,
      }

      metadata2, err := structpb.NewStruct(metadataMap2)
      if err != nil {
          log.Fatalf("Failed to create metadata map: %v", err)
      }

      vectors := []*pinecone.Vector{
          {
              Id:     "vec1",
              Values: []float32{0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8},
              SparseValues: &sparseValues1,
              Metadata: metadata1,
          },
          {
              Id:     "vec2",
              Values: []float32{0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9},
              SparseValues: &sparseValues2,
              Metadata: metadata2,
          },
      }

      count, err := idxConnection.UpsertVectors(ctx, vectors)
      if err != nil {
  	      log.Fatalf("Failed to upsert vectors: %v", err)
      } else {
  	      fmt.Printf("Successfully upserted %d vector(s)!\n", count)
      }
  }
  ```

  ```csharp C# theme={null}
  using Pinecone;

  var pinecone = new PineconeClient("YOUR_API_KEY");

  var index = pinecone.Index("docs-example");

  var upsertResponse = await index.UpsertAsync(new UpsertRequest {
      Vectors = new[]
      {
          new Vector
          {
              Id = "vec1",
              Values = new[] { 1.0f, 1.5f },
              SparseValues = new SparseValues
              {
                  Indices = [1, 5],
                  Values = new[] { 0.5f, 0.5f },
              },
              Metadata = new Metadata {
                  ["genre"] = new("comedy"),
                  ["year"] = new(2020),
              }
          },
          new Vector
          {
              Id = "vec2",
              Values = new[] { 2.0f, 1.0f },
              SparseValues = new SparseValues
              {
                  Indices = [5, 6],
                  Values = new[] { 0.4f, 0.5f },
              },
              Metadata = new Metadata {
                  ["genre"] = new("documentary"),
                  ["year"] = new(2019),
              },
          }
      },
      Namespace = "example-namespace",
  });
  ```

  ```shell curl theme={null}
  PINECONE_API_KEY="YOUR_API_KEY"
  INDEX_HOST="INDEX_HOST"

  curl "https://$INDEX_HOST/vectors/upsert" \
    -H "Api-Key: $PINECONE_API_KEY" \
    -H "content-type: application/json" \
    -H "X-Pinecone-Api-Version: 2024-07" \
    -d '{
          "vectors": [
            {
              "id": "vec1",
              "values": [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8],
              "sparseValues": {
                "indices": [2, 7],
                "values": [0.4, 0.5]
              },
              "metadata": {
                "genre": "drama"
              }
            },
            {
              "id": "vec2",
              "values": [0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],
              "sparseValues": {
                "indices": [1, 4],
                "values": [0.1, 0.2]
              },
              "metadata": {
                "genre": "action"
              }
            }
          ],
          "namespace": "example-namespace"
        }'
  ```
</RequestExample>

<ResponseExample>
  ```json curl theme={null}
  {"upsertedCount":2}
  ```
</ResponseExample>


## OpenAPI

````yaml https://raw.githubusercontent.com/pinecone-io/pinecone-api/refs/heads/main/2024-07/data_2024-07.oas.yaml post /vectors/upsert
openapi: 3.0.3
info:
  title: Pinecone Data Plane API
  description: >-
    Pinecone is a vector database that makes it easy to search and retrieve
    billions of high-dimensional vectors.
  contact:
    name: Pinecone Support
    url: https://support.pinecone.io
    email: support@pinecone.io
  license:
    name: Apache 2.0
    url: https://www.apache.org/licenses/LICENSE-2.0
  version: 2024-07
servers:
  - url: https://{index_host}
    variables:
      index_host:
        default: unknown
        description: host of the index
security:
  - ApiKeyAuth: []
tags:
  - name: Data Plane
externalDocs:
  description: More Pinecone.io API docs
  url: https://docs.pinecone.io/introduction
paths:
  /vectors/upsert:
    post:
      tags:
        - Data Plane
      summary: Upsert vectors
      description: >-
        The `upsert` operation writes vectors into a namespace. If a new value
        is upserted for an existing vector ID, it will overwrite the previous
        value.


        For guidance, examples, and limits, see [Upsert
        data](https://docs.pinecone.io/guides/index-data/upsert-data).
      operationId: upsert
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/UpsertRequest'
        required: true
      responses:
        '200':
          description: A successful response.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/UpsertResponse'
        '400':
          description: Bad request. The request body included invalid request parameters.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/rpcStatus'
        default:
          description: An unexpected error response.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/rpcStatus'
components:
  schemas:
    UpsertRequest:
      description: The request for the `upsert` operation.
      type: object
      properties:
        vectors:
          description: >-
            An array containing the vectors to upsert. Recommended batch limit
            is up to 1000 vectors.
          type: array
          required:
            - vectors
          items:
            $ref: '#/components/schemas/Vector'
          minLength: 1
          maxLength: 1000
        namespace:
          example: example-namespace
          description: The namespace where you upsert vectors.
          type: string
      required:
        - vectors
    UpsertResponse:
      description: The response for the `upsert` operation.
      type: object
      properties:
        upsertedCount:
          example: 2
          description: The number of vectors upserted.
          type: integer
          format: int64
    rpcStatus:
      type: object
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string
        details:
          type: array
          items:
            $ref: '#/components/schemas/protobufAny'
    Vector:
      type: object
      properties:
        id:
          example: example-vector-1
          description: This is the vector's unique id.
          type: string
          required:
            - id
          minLength: 1
          maxLength: 512
        values:
          example:
            - 0.1
            - 0.2
            - 0.3
            - 0.4
            - 0.5
            - 0.6
            - 0.7
            - 0.8
          description: This is the vector data included in the request.
          type: array
          required:
            - values
          items:
            type: number
            format: float
          minLength: 1
          maxLength: 20000
        sparseValues:
          $ref: '#/components/schemas/SparseValues'
        metadata:
          example:
            genre: documentary
            year: 2019
          description: This is the metadata included in the request.
          type: object
      required:
        - id
        - values
    protobufAny:
      type: object
      properties:
        typeUrl:
          type: string
        value:
          type: string
          format: byte
    SparseValues:
      description: >-
        Vector sparse data. Represented as a list of indices and a list of 
        corresponded values, which must be with the same length.      
      type: object
      properties:
        indices:
          example:
            - 1
            - 312
            - 822
            - 14
            - 980
          description: The indices of the sparse data.
          type: array
          required:
            - indices
          items:
            type: integer
            format: int64
          minLength: 1
          maxLength: 1000
        values:
          example:
            - 0.1
            - 0.2
            - 0.3
            - 0.4
            - 0.5
          description: >-
            The corresponding values of the sparse data, which must be with the
            same length as the indices.
          type: array
          required:
            - values
          items:
            type: number
            format: float
          minLength: 1
          maxLength: 1000
      required:
        - indices
        - values
  securitySchemes:
    ApiKeyAuth:
      type: apiKey
      in: header
      name: Api-Key
      description: >-
        An API Key is required to call Pinecone APIs. Get yours from the
        [console](https://app.pinecone.io/).

````