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

# Bulk upsert data

> Upload large batches of documents using the LambdaDB bulk upsert workflow with presigned URLs. Includes SDK and API code examples.

Use bulk upsert for batches that exceed the regular [request-body limit](https://docs.lambdadb.ai/guides/get-started/limits.md#requests-and-documents). It uploads documents separately through a presigned URL. Enforce the returned `sizeLimitBytes`; the individual document limit still applies.

> [!WARNING]
>
> Bulk upsert never generates [native embeddings](https://docs.lambdadb.ai/guides/collections/native-embeddings.md#bulk-upsert). For each native embedding field, a document with no source values can omit the vector. A document with a present source field must supply the vector directly or the whole request fails before any documents are committed; empty image arrays are skipped. To generate embeddings, use the regular [upsert](https://docs.lambdadb.ai/guides/documents/upsert-data.md) or [update](https://docs.lambdadb.ai/guides/documents/update-data.md) flow.

## Recommended: One-step bulk upsert

The easiest way to bulk upsert is to use the SDK's one-step method. The client uploads your documents to the presigned URL and completes the bulk upsert for you.

Document field names are preserved as supplied in every SDK. The examples use `dense_vector` and `sparse_vector`; configure indexes with those same names to search these values. The dense examples use synthetic 64-dimensional vectors; configure `dense_vector` with `dimensions: 64`. For real data, send the full vectors from your embedding model, matching the field's dimensions within the [supported range](https://docs.lambdadb.ai/guides/get-started/limits.md#collections-and-schema).

```python Python theme={null}
from lambdadb import LambdaDB

with LambdaDB(
    project_api_key="YOUR_API_KEY",
    base_url="YOUR_BASE_URL",
    project_name="YOUR_PROJECT_NAME",
) as client:
    coll = client.collection("my_collection")
    docs = [
        {"id": "bulk_1", "url": "https://en.wikipedia.org/wiki/LambdaDB", "title": "LambdaDB", "text": "LambdaDB is a serverless search database ... ", "dense_vector": [(i + 1) / 64 for i in range(64)], "sparse_vector": {"LambdaDB": 0.83, "is": 0.1, "a": 0.1, "AI": 0.7}},
        {"id": "bulk_2", "url": "https://en.wikipedia.org/wiki/Winamp", "title": "Winamp", "text": "Winamp is a media player for Windows, macOS and Android ...", "dense_vector": [(i + 11) / 64 for i in range(64)]},
    ]
    coll.docs.bulk_upsert_docs(docs=docs)
```

> [!NOTE]
>
> Python: `LambdaDB` supports context manager usage. `__enter__` returns the client, and `__exit__` calls `client.close()` (closing the SDK-owned HTTP client) and makes the client unusable after the `with` block. If you don't use `with`, call `client.close()` when you're done. If you pass a custom `client=`/`async_client=`, you own closing it.

```typescript TypeScript theme={null}
import { LambdaDBClient } from "@functional-systems/lambdadb";

const client = new LambdaDBClient({
  projectApiKey: "YOUR_API_KEY",
  baseUrl: "YOUR_BASE_URL",
  projectName: "YOUR_PROJECT_NAME",
});
const collection = client.collection("my_collection");
const docs = [
  { id: "bulk_1", url: "https://en.wikipedia.org/wiki/LambdaDB", title: "LambdaDB", text: "LambdaDB is a serverless search database ... ", dense_vector: Array.from({ length: 64 }, (_, i) => (i + 1) / 64), sparse_vector: { LambdaDB: 0.83, is: 0.1, a: 0.1, AI: 0.7 } },
  { id: "bulk_2", url: "https://en.wikipedia.org/wiki/Winamp", title: "Winamp", text: "Winamp is a media player for Windows, macOS and Android ...", dense_vector: Array.from({ length: 64 }, (_, i) => (i + 11) / 64) },
];
await collection.docs.bulkUpsertDocs({ docs });
```

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

import (
  "context"
  "log"
  lambdadb "github.com/lambdadb/go-lambdadb"
)

func main() {
  ctx := context.Background()
  client := lambdadb.New(
    lambdadb.WithBaseURL("YOUR_BASE_URL"),
    lambdadb.WithProjectName("YOUR_PROJECT_NAME"),
    lambdadb.WithAPIKey("YOUR_API_KEY"),
  )
  coll := client.Collection("my_collection")
  demoVector := func(offset int) []float64 {
    values := make([]float64, 64)
    for i := range values { values[i] = float64(i + offset) / 64 }
    return values
  }
  docs := []map[string]interface{}{
    {"id": "bulk_1", "url": "https://en.wikipedia.org/wiki/LambdaDB", "title": "LambdaDB", "text": "LambdaDB is a serverless search database ... ", "dense_vector": demoVector(1)},
    {"id": "bulk_2", "url": "https://en.wikipedia.org/wiki/Winamp", "title": "Winamp", "text": "Winamp is a media player for Windows, macOS and Android ...", "dense_vector": demoVector(11)},
  }
  _, err := coll.Docs().BulkUpsertDocuments(ctx, lambdadb.UpsertDocsInput{Docs: docs})
  if err != nil {
    log.Fatal(err)
  }
}
```

After a successful request you'll receive a message such as `"Upsert request is accepted"`. Documents are processed asynchronously and become searchable after indexing completes.

***

## Two-step: Get presigned URL, then upload and complete

If you need to control the upload yourself (e.g. from a different process or storage), use this flow: (1) get bulk upsert info, (2) upload the payload with every returned signed header, then (3) call the bulk-upsert API with the `objectKey`.

Both REST control calls accept a branch. Use the same branch for the upload-URL request and the completion request; omitting it in both places writes to `main`.

The uploaded object is bound to that branch's write stream. Do not reuse its key for another branch or after deleting and recreating the original branch. The REST completion request's `type` field is optional; the examples use `"application/json"`. The server validates the uploaded object's Content-Type, not this request field. Setting `type` does not replace sending the required `Content-Type` header during upload.

### Step 1: Get bulk upsert information

```python Python theme={null}
from lambdadb import LambdaDB

with LambdaDB(
    project_api_key="YOUR_API_KEY",
    base_url="YOUR_BASE_URL",
    project_name="YOUR_PROJECT_NAME",
) as client:
    coll = client.collection("my_collection")
    get_bulk_upsert = coll.docs.get_bulk_upsert()
    # get_bulk_upsert.url, get_bulk_upsert.type, get_bulk_upsert.headers,
    # get_bulk_upsert.object_key, get_bulk_upsert.size_limit_bytes
```

```typescript TypeScript theme={null}
import { LambdaDBClient } from "@functional-systems/lambdadb";

const client = new LambdaDBClient({
  projectApiKey: "YOUR_API_KEY",
  baseUrl: "YOUR_BASE_URL",
  projectName: "YOUR_PROJECT_NAME",
});
const info = await client.collection("my_collection").docs.getBulkUpsert();
// info.url, info.type, info.headers, info.objectKey, info.sizeLimitBytes
```

```go Go theme={null}
info, err := client.Collection("my_collection").Docs().GetBulkUpsertInfo(ctx)
if err != nil {
  log.Fatal(err)
}
// info.URL, info.Type, info.Headers, info.ObjectKey, info.SizeLimitBytes
```

```bash cURL theme={null}
BASE_URL="YOUR_BASE_URL"
PROJECT_NAME="YOUR_PROJECT_NAME"
COLLECTION_NAME="my_collection"
LAMBDADB_PROJECT_API_KEY="YOUR_API_KEY"

curl -i -X GET \
  "$BASE_URL/projects/$PROJECT_NAME/collections/$COLLECTION_NAME/docs/bulk-upsert" \
  -H "x-api-key: $LAMBDADB_PROJECT_API_KEY"
```

Response:

```json theme={null}
{
  "url": "<string>",
  "type": "application/json",
  "httpMethod": "PUT",
  "objectKey": "<string>",
  "sizeLimitBytes": 209715200,
  "headers": {
    "If-None-Match": "*"
  }
}
```

### Step 2: Upload to presigned URL and call bulk-upsert

Upload the document list as JSON to the `url`. Set `Content-Type` to the returned `type` and include every entry in the returned `headers` map, then call the bulk-upsert API with the `objectKey` from step 1. Do not drop, rename, or recompute signed headers.

The Python tab below is a complete alternative to its Step 1 snippet: it obtains a fresh upload URL, uploads, and completes the import inside one client context. Run it on its own so the client remains open through completion. The TypeScript, Go, and cURL tabs continue from Step 1.

For the manual Python upload, also install `requests` with `pip install requests`.

```python Python theme={null}
import json
import requests
from lambdadb import LambdaDB

docs = [
    {"id": "bulk_33201222", "url": "https://en.wikipedia.org/wiki/LambdaDB", "title": "LambdaDB", "text": "LambdaDB is a serverless search database ... ", "dense_vector": [(i + 1) / 64 for i in range(64)], "sparse_vector": {"LambdaDB": 0.83, "is": 0.1, "a": 0.1, "AI": 0.7}},
]
with LambdaDB(
    project_api_key="YOUR_API_KEY",
    base_url="YOUR_BASE_URL",
    project_name="YOUR_PROJECT_NAME",
) as client:
    coll = client.collection("my_collection")
    get_bulk_upsert = coll.docs.get_bulk_upsert()
    upload_headers = {"Content-Type": get_bulk_upsert.type.value, **get_bulk_upsert.headers}
    # Upload to presigned URL
    resp = requests.put(
        get_bulk_upsert.url,
        data=json.dumps({"docs": docs}),
        headers=upload_headers,
    )
    resp.raise_for_status()
    # Complete bulk upsert while the client is still open
    coll.docs.bulk_upsert(object_key=get_bulk_upsert.object_key)
```

```typescript TypeScript theme={null}
const docs = [
  { id: "bulk_33201222", url: "https://en.wikipedia.org/wiki/LambdaDB", title: "LambdaDB", text: "LambdaDB is a serverless search database ... ", dense_vector: Array.from({ length: 64 }, (_, i) => (i + 1) / 64), sparse_vector: { LambdaDB: 0.83, is: 0.1, a: 0.1, AI: 0.7 } },
];
// Upload to presigned URL
const uploadRes = await fetch(info.url, {
  method: "PUT",
  headers: {
    "Content-Type": info.type,
    ...info.headers,
  },
  body: JSON.stringify({ docs }),
});
if (!uploadRes.ok) throw new Error(`Upload failed: ${uploadRes.status}`);
// Complete bulk upsert
await client.collection("my_collection").docs.bulkUpsert({ objectKey: info.objectKey });
```

```go Go theme={null}
// Assume bytes, encoding/json, net/http are imported
demoVector := func(offset int) []float64 {
  values := make([]float64, 64)
  for i := range values { values[i] = float64(i + offset) / 64 }
  return values
}
docs := []map[string]interface{}{
  {"id": "bulk_33201222", "url": "https://en.wikipedia.org/wiki/LambdaDB", "title": "LambdaDB", "text": "LambdaDB is a serverless search database ... ", "dense_vector": demoVector(1)},
}
payload, _ := json.Marshal(map[string]interface{}{"docs": docs})
req, _ := http.NewRequestWithContext(ctx, "PUT", info.URL, bytes.NewReader(payload))
req.Header.Set("Content-Type", string(*info.Type))
for name, value := range info.Headers {
  req.Header.Set(name, value)
}
uploadRes, err := http.DefaultClient.Do(req)
if err != nil {
  log.Fatal(err)
}
uploadRes.Body.Close()
if uploadRes.StatusCode < 200 || uploadRes.StatusCode >= 300 {
  log.Fatalf("upload failed: %s", uploadRes.Status)
}
// Complete bulk upsert
_, err = client.Collection("my_collection").Docs().BulkUpsert(ctx, lambdadb.BulkUpsertInput{ObjectKey: info.ObjectKey})
if err != nil {
  log.Fatal(err)
}
```

```bash cURL theme={null}
# Reuse the connection variables from step 1; both control calls target main.
# 1. Upload to the presigned URL from step 1
curl --fail -X PUT "<provided_url>" \
  -H 'Content-Type: application/json' \
  -H 'If-None-Match: *' \
  -d '{"docs": [{"id": "bulk_33201222", "url": "https://en.wikipedia.org/wiki/LambdaDB", "title": "LambdaDB", "text": "LambdaDB is a serverless search database ... ", "dense_vector": [
      0.015625, 0.03125, 0.046875, 0.0625, 0.078125, 0.09375, 0.109375, 0.125,
      0.140625, 0.15625, 0.171875, 0.1875, 0.203125, 0.21875, 0.234375, 0.25,
      0.265625, 0.28125, 0.296875, 0.3125, 0.328125, 0.34375, 0.359375, 0.375,
      0.390625, 0.40625, 0.421875, 0.4375, 0.453125, 0.46875, 0.484375, 0.5,
      0.515625, 0.53125, 0.546875, 0.5625, 0.578125, 0.59375, 0.609375, 0.625,
      0.640625, 0.65625, 0.671875, 0.6875, 0.703125, 0.71875, 0.734375, 0.75,
      0.765625, 0.78125, 0.796875, 0.8125, 0.828125, 0.84375, 0.859375, 0.875,
      0.890625, 0.90625, 0.921875, 0.9375, 0.953125, 0.96875, 0.984375, 1.0
    ]}]}' &&

# 2. Complete bulk upsert with the objectKey from step 1
curl -i -X POST \
  "$BASE_URL/projects/$PROJECT_NAME/collections/$COLLECTION_NAME/docs/bulk-upsert" \
  -H 'content-type: application/json' \
  -H "x-api-key: $LAMBDADB_PROJECT_API_KEY" \
  -d '{
    "objectKey": "<objectKey from step 1>",
    "type": "application/json"
  }'
```

The cURL headers show the current `type` and `headers` response values. Always set `Content-Type` from `type` and use the exact signed header names and values returned for that upload URL.

### Upload retries

The signed upload is create-only (`If-None-Match: *`). Uploading again to an object that already exists can return `412` from object storage. This is not a LambdaDB JSON error. Acquire a new URL for a new upload attempt rather than blindly repeating the same PUT. If the upload or completion outcome is uncertain, verify the prior outcome before automatically submitting another import.

For browser uploads, the target deployment's CORS configuration must allow the returned signed headers. Use the returned `httpMethod` and enforce `sizeLimitBytes`.

## Response

After successful bulk upsert initiation you'll receive:

```json theme={null}
{
  "message": "Bulk upsert request is accepted"
}
```

> [!NOTE]
>
> Bulk upsert operations are processed asynchronously in the background.
> Consequently, newly uploaded documents may not be immediately available for search or fetch requests,
> **even if `consistentRead` (Python: `consistent_read`) is set to `true`**.
> The documents will become available only after the indexing process is fully complete.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.