fix(postgres): handle edge cases in getMessages payload parsing
SA
5d ago+91+91+01234567890123456789-12-12-01234567890123456789
PR Checklist
Please check if your PR fulfills the following requirements:
- The commit message follows our guidelines: https://voltagent.dev/docs/community/contributing/#commit-convention
Bugs / Features
- Related issue(s) linked
- Tests for the changes have been added
- Docs have been added / updated
- Changesets have been added https://voltagent.dev/docs/community/contributing/#creating-a-changeset
What is the current behavior?
In PostgreSQLMemoryAdapter.getMessages (packages/postgres/src/memory-adapter.ts):
- When
row.partscontains"null"or a non-array JSON value (e.g.{}),JSON.parseassigns a non-array value toparts, causing aTypeError: Cannot read properties of null (reading 'length')crash or returning an invalidUIMessage.partsshape. - When falling back to legacy
row.contentcontaining plain text (whereJSON.parsethrows), an earlyreturnbypasses themetadata.createdAtnormalization at the end of the mapper. - When legacy
row.contentcontains a JSON primitive string such as"123"or"true",JSON.parsereturns anumberorboolean, which fails thetypeof content === "string"andArray.isArray(content)checks and silently drops the message content (parts = []).
What is the new behavior?
row.partsis validated withArray.isArray(...)both afterJSON.parseand when already parsed bypg, falling back safely to[]if it is not an array.- Legacy plain-text
row.contentno longer returns early in thecatchblock, ensuringmetadata.createdAtis consistently attached as aDateinstance while preserving existing metadata fields. - Legacy
row.contentvalues that parse asnumberorbooleanprimitives are preserved and converted into text parts ([{ type: "text", text: String(rawContent) }]).
fixes #1560
Notes for reviewers
- Added unit regression tests in
packages/postgres/src/memory-adapter.spec.tscovering all three edge cases. - Verified with
pnpm --filter @voltagent/postgres test:coverage,pnpm --filter @voltagent/postgres build, andpnpm lint:ci.
Summary by cubic
Fixes edge cases in PostgreSQLMemoryAdapter.getMessages where malformed or legacy payloads caused crashes or lost content. Fixes #1560.
Bug Fixes
- Non-array
partsvalues (like"null"or{}) now safely fall back to[]. - Legacy plain-text
contentno longer returns early, sometadata.createdAtis always normalized to aDate. - Legacy
contentstoring JSON primitives (numbers, booleans) is preserved as text parts instead of being dropped.
Summary by CodeRabbit
- Bug Fixes
- Fixed message retrieval so invalid or non-array message parts are returned as empty arrays.
- Preserved legacy plain-text content—including text that resembles a number or boolean—and populated its creation-time metadata.