{"record":{"id":"1a4ce8eb18b039a7","repo":"vercel/ai","slug":"you-can-t-get-the-string-key-field-from-the","errorCode":null,"errorMessage":"You can't get the \"${String(key)}\" field from the AI state because it's not an object.","messagePattern":"You can't get the \"(.+?)\" field from the AI state because it's not an object\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/rsc/src/ai-state.tsx","lineNumber":84,"sourceCode":" * @example const field = getAIState('key') // Get the value of the key\n */\nfunction getAIState<AI extends AIProvider = any>(): Readonly<\n  InferAIState<AI, any>\n>;\nfunction getAIState<AI extends AIProvider = any>(\n  key: keyof InferAIState<AI, any>,\n): Readonly<InferAIState<AI, any>[typeof key]>;\nfunction getAIState<AI extends AIProvider = any>(\n  ...args: [] | [key: keyof InferAIState<AI, any>]\n) {\n  const store = getAIStateStoreOrThrow(\n    '`getAIState` must be called within an AI Action.',\n  );\n\n  if (args.length > 0) {\n    const key = args[0];\n    if (typeof store.currentState !== 'object') {\n      throw new Error(\n        `You can't get the \"${String(\n          key,\n        )}\" field from the AI state because it's not an object.`,\n      );\n    }\n    return store.currentState[key as keyof typeof store.currentState];\n  }\n\n  return store.currentState;\n}\n\n/**\n * Get the mutable AI state. Note that you must call `.done()` when finishing\n * updating the AI state.\n *\n * @example\n * ```tsx\n * const state = getMutableAIState()","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/vercel/ai/blob/69428b1f8b037e4d118fb4853428d5c4e620493c/packages/rsc/src/ai-state.tsx#L66-L102","documentation":"In the RSC package, getAIState(key) accepts an optional key to read a single field of the AI state. If a key is passed but the current AI state is not an object (e.g. null, a primitive, or an array held as scalar), reading a field is meaningless, so the library throws with a message naming the offending key.","triggerScenarios":"Calling getAIState('someKey') inside an AI Action when the state was initialized as a non-object (e.g. getAIState() of a number/string/null state set via an initial AIState of a primitive) — or after an action replaced the whole state with a non-object.","commonSituations":"Initializing useAIState/AI state with a scalar or null and later reading a named field; a setter action overwrote state with a primitive; typo assuming state shape differs from what was stored.","solutions":["Initialize the AI state as an object, e.g. { messages: [] } rather than [] or a scalar.","Call getAIState() without a key if you want the whole state, then narrow it yourself.","Check that no action replaces the entire state with a non-object via setAIState/mutable update.","Add a runtime check `typeof state === 'object' && state !== null` before accessing keyed state."],"exampleFix":"// before\n// initial AI state: 0\nconst score = getAIState('score');\n\n// after\n// initial AI state: { score: 0 }\nconst { score } = getAIState();","handlingStrategy":"type-guard","validationCode":"const state = getAIState();\nif (state === null || typeof state !== 'object') {\n  throw new Error('AI state must be an object before keyed access');\n}\nconst value = (state as Record<string, unknown>).myKey;","typeGuard":"function isObjectState(s: unknown): s is Record<string, unknown> {\n  return typeof s === 'object' && s !== null && !Array.isArray(s);\n}","tryCatchPattern":"let value: unknown;\ntry {\n  value = getAIState('myKey');\n} catch (e) {\n  if (e instanceof Error && e.message.includes(\"from the AI state because it's not an object\")) {\n    value = getAIState(); // fall back to whole-state read\n  } else throw e;\n}","preventionTips":["Always initialize AI state as a plain object (e.g. { messages: [] }).","Never assign scalars/null as the whole AI state in setAIState or initial state.","Read the full state with getAIState() and destructure instead of keyed access when the shape is uncertain."],"tags":["rsc","ai-state","state-shape","react-server-components"],"backgroundTag":"ai-state-not-an-object","analyzedSha":"69428b1f8b037e4d118fb4853428d5c4e620493c","analyzedAt":"2026-08-30T12:32:21.016Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}