Documentation Index: Fetch llms.txt first to discover every published page. This page is also available as Markdown at /memory-json-value-helper.md.
Verified · 8/11/2026

Memory Value Validation Helper

isMemoryJsonValue is a type-guard function that checks whether a value satisfies the JSON constraints Primitive Memories accepts, letting you validate data before calling client.memories.set.

What it does#

isMemoryJsonValue is a TypeScript type-guard function that checks whether an arbitrary JavaScript value conforms to the MemoryJsonValue type Primitive Memories accepts. Call it before writing a value with client.memories.set to fail fast on invalid input instead of getting a rejected API request.

It is defined in the workspace-internal api-core package and reaches consumers through @primitivedotdev/sdk/api:

import { isMemoryJsonValue } from "@primitivedotdev/sdk/api";

isMemoryJsonValue({ step: 2, done: false }); // true
isMemoryJsonValue(undefined); // false
Note

Primitive Memories itself, the durable JSON key-value store behind client.memories, is documented on Generated API Client and Primitive Memories. This page covers only the validation helper.

Signature#

function isMemoryJsonValue(value: unknown): value is MemoryJsonValue;
ParameterTypeDescription
valueunknownThe value to check.

Returns: boolean. When true, TypeScript narrows value to MemoryJsonValue in the calling scope.

The MemoryJsonValue type#

type MemoryJsonValue =
  | null
  | string
  | number
  | boolean
  | MemoryJsonValue[]
  | { [key: string]: MemoryJsonValue };

This is the exact type the generated OpenAPI client uses for the Memories API's value field. It is recursive: array elements and object property values must themselves be valid MemoryJsonValues.

What is accepted#

InputAccepted?
stringYes
Finite numberYes
booleanYes
nullYes
Array of valid MemoryJsonValuesYes
Plain object with valid MemoryJsonValue valuesYes

What is rejected#

InputRejected?Why
undefinedYesNot representable in JSON
bigintYesNot a JSON type
symbolYesNot a JSON type
functionYesNot a JSON type
NaN / Infinity / -InfinityYesNot finite numbers
Sparse arrays (holes)YesA hole is not a valid element
Class instances (e.g. Date, Map, custom classes)YesNot plain objects
Cyclic structuresYesCannot serialize to JSON

Where it's used internally#

The Node SDK's client.memories.set calls the same validation logic on its value field before sending the request, so a value that fails isMemoryJsonValue also fails at client.memories.set with a TypeError:

client.memories.set value must be a JSON value: string, finite number, boolean,
null, array, or plain object. Undefined, bigint, symbol, function, NaN, Infinity,
sparse arrays, class instances, and cyclic values are not valid memory values.

Calling isMemoryJsonValue yourself lets you validate a value earlier in your code path, for example, before constructing the object you plan to store, or when accepting arbitrary data from an upstream source.

Example: guarding a value before storing it#

import {
  createPrimitiveClient,
  isMemoryJsonValue,
} from "@primitivedotdev/sdk/api";

const client = createPrimitiveClient({ apiKey: process.env.PRIMITIVE_API_KEY! });


function toStorableState(candidate: unknown): Record<string, unknown> {
  if (!isMemoryJsonValue(candidate)) {
    throw new TypeError("state is not a valid Primitive Memories JSON value");
  }
  return candidate as Record<string, unknown>;
}

const state = toStorableState({ step: 2, lastEmailId: "em_123" });

await client.memories.set({ key: "thread:latest", value: state });

The generated MemoryJsonValue TypeScript type required a post-processing repair step during codegen, because @hey-api/openapi-ts widens the recursive type: "null" branch of the schema to unknown. See Generated TypeScript Client Fixups for how fix-generated-api-imports.ts patches this.

Next steps#

Was this page helpful?

© Primitive SDKs

Powered by Browzer