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
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;
| Parameter | Type | Description |
|---|---|---|
value | unknown | The 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#
| Input | Accepted? |
|---|---|
string | Yes |
Finite number | Yes |
boolean | Yes |
null | Yes |
Array of valid MemoryJsonValues | Yes |
Plain object with valid MemoryJsonValue values | Yes |
What is rejected#
| Input | Rejected? | Why |
|---|---|---|
undefined | Yes | Not representable in JSON |
bigint | Yes | Not a JSON type |
symbol | Yes | Not a JSON type |
function | Yes | Not a JSON type |
NaN / Infinity / -Infinity | Yes | Not finite numbers |
| Sparse arrays (holes) | Yes | A hole is not a valid element |
Class instances (e.g. Date, Map, custom classes) | Yes | Not plain objects |
| Cyclic structures | Yes | Cannot 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 });
Related fix in codegen#
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#
Store, search, and delete durable JSON records with client.memories.
Generated TypeScript Client FixupsSee the post-processing pass that repairs the generated MemoryJsonValue type.
Operation Manifest ReferenceLook up the Memories API operations and their request/response schemas.
PrimitiveApiClientConstruct the host-aware client that exposes memory operations directly.
Was this page helpful?