Skip to content

SDK Fails to Parse NDJSON Format in Vectorize Upsert, Works with Fetch #2646

@bimsina

Description

@bimsina

Confirm this is a Typescript library issue and not an underlying Cloudflare API issue

  • This is an issue with the Typescript library

Describe the bug

When using the Cloudflare TypeScript SDK to perform a Cloudflare Vectorize upsert operation, I encounter the following error:

Error: 400 {"result":null,"success":false,"errors":[{"code":40023,"message":"vectorize.upstream_error - failed to parse upsert vectors request in ndjson format: line Some(0) was not expected format"}],"messages":[]}

This error happens when I call the client.vectorize.indexes.upsert method. However, the same request works fine when using the fetch method directly, with the same body and headers.

It seems that the SDK might not be properly formatting the request in the expected ndjson format.

To Reproduce

  1. Use the Cloudflare TypeScript SDK to perform a vectorization upsert using the client.vectorize.indexes.upsert method.
  2. See the error failed to parse upsert vectors request in ndjson format: line Some(0) was not expected format.
  3. Switch to using the fetch method with the same body and headers.
  4. The request works as expected without the error.

Code snippets

const vectors = [
    { id: "1", values: [...], metadata: { columnId: 1 } },
    { id: "2", values: [...], metadata: { columnId: 1 } },
  ];

  const body = vectors
    .map((vector) => JSON.stringify(vector))
    .join("\n")
    .trim();

  const client = new Cloudflare({
    apiToken: env.CLOUDFLARE_VECTORIZE_API_TOKEN,
  });

  const insertUsingSdk = await client.vectorize.indexes.upsert(
    index.name,
    {
      account_id: env.CLOUDFLARE_ACC_ID,
      body: body,
    },
    {
      headers: {
        "Content-Type": "application/x-ndjson",
      },
    },
  );

  const insertUsingFetch = await fetch(
    `https://api.cloudflare.com/client/v4/accounts/${env.CLOUDFLARE_ACC_ID}/vectorize/v2/indexes/${index.name}/insert`,
    {
      method: "POST",
      body: body,
      headers: {
        Authorization: `Bearer ${env.CLOUDFLARE_VECTORIZE_API_TOKEN}`,
        "Content-Type": "application/x-ndjson",
      },
    },
  );

OS

macOS

Runtime version

Typescript 5.6.3

Library version

v4.3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions