A breaking change in KV API?

The project I’ve built and is run in production for a few months now, is using KV api. To make it strongly typed, it uses @cloudflare/workers-types in version 2.2.2 that was released a few months back. A few hours ago two workers started returning errors. The exception was traced to a line similar to:

const value = await kv.get<number>(key, "json");

Before yesterday, it was handling perfectly fine keys that didn’t exist. The value returned was null. Yesterday evening it started failing informing about a JSON.parse problem with a wrongly formatted JSON.

Has something changed? All workers use a compatibility date set to '2021-09-21'.

Nothing has intentionally changed about KV. I tested the following

async function handleRequest(event) {
  try {
    const value = await KV.get("key", "json")
    return new Response(`${value}`)
  } catch (e) {
    return new Response(`Uh oh ${e}`)

If key is nonexistant:

bash-3.2$ curl  https://hello-workers.cody.workers.dev

If key is valid json:

bash-3.2$ curl  https://hello-workers.cody.workers.dev

If key is invalid json (this is probably the case you’re hitting)

bash-3.2$ curl  https://hello-workers.cody.workers.dev
Uh oh SyntaxError: Unexpected token h in JSON at position 1

I’d suggest that you directly check what the value of the key in question currently is, using wrangler or the cloudflare dashboard.

This is interesting. I’ll close this as if there was no change in the behavior, it’s probably me. If I find the reason, will share.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.