Skip to content

fix(postgres): handle edge cases in getMessages payload parsing - #1561

Open
SanemIzci wants to merge 1 commit into
VoltAgent:mainfrom
SanemIzci:fix/postgres-get-messages-parsing
Open

SanemIzci wants to merge 1 commit into
VoltAgent:mainfrom
SanemIzci:fix/postgres-get-messages-parsing

Conversation

@SanemIzci

@SanemIzci SanemIzci commented Nov 22, 2026 •

Copy link
Copy Markdown

PR Checklist

Please check if your PR fulfills the following requirements:

Bugs / Features

What is the current behavior?

In PostgreSQLMemoryAdapter.getMessages (packages/postgres/src/memory-adapter.ts):

  1. When row.parts contains "null" or a non-array JSON value (e.g. {}), JSON.parse assigns a non-array value to parts, causing a TypeError: Cannot read properties of null (reading 'length') crash or returning an invalid UIMessage.parts shape.
  2. When falling back to legacy row.content containing plain text (where JSON.parse throws), an early return bypasses the metadata.createdAt normalization at the end of the mapper.
  3. When legacy row.content contains a JSON primitive string such as "123" or "true", JSON.parse returns a number or boolean, which fails the typeof content === "string" and Array.isArray(content) checks and silently drops the message content (parts = []).

What is the new behavior?

  1. row.parts is validated with Array.isArray(...) both after JSON.parse and when already parsed by pg, falling back safely to [] if it is not an array.
  2. Legacy plain-text row.content no longer returns early in the catch block, ensuring metadata.createdAt is consistently attached as a Date instance while preserving existing metadata fields.
  3. Legacy row.content values that parse as number or boolean primitives 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.ts covering all three edge cases.
  • Verified with pnpm --filter @voltagent/postgres test:coverage, pnpm --filter @voltagent/postgres build, and pnpm 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 parts values (like "null" or {}) now safely fall back to [].
  • Legacy plain-text content no longer returns early, so metadata.createdAt is always normalized to a Date.
  • Legacy content storing JSON primitives (numbers, booleans) is preserved as text parts instead of being dropped.

Written for commit 46a5bc6. Summary will update on new commits.

View guided diff

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.

@changeset-bot

changeset-bot Bot commented Nov 22, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 46a5bc6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@voltagent/postgres Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

PostgreSQLMemoryAdapter.getMessages now normalizes non-array parts to empty arrays and preserves legacy primitive content as text parts. Regression tests cover these cases and verify metadata.createdAt for legacy plain-text content.

Changes: PostgreSQL message parsing

Layer / File(s) Summary
Normalize message payloads
packages/postgres/src/memory-adapter.ts, packages/postgres/src/memory-adapter.spec.ts, .changeset/fix-postgres-get-messages-parsing.md
getMessages accepts parts only when the parsed value is an array. Legacy string, numeric, and boolean content becomes a text part. Tests cover these cases and metadata.createdAt normalization. The changeset declares a patch release.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: omeraplak

Merge Risk: Minimal · up to 46a5b

This fix makes PostgreSQL message retrieval handle malformed parts and legacy content more robustly. No actionable merge risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the package (postgres), the change type (fix), and the area (getMessages payload parsing). It matches the main change: three edge cases in PostgreSQLMemoryAdapter.getMessages for parts…
Description check Passed The description follows the repository template. It includes the checklist, the current behavior, the new behavior, and the issue reference (fixes #1560). It explains the three edge cases and lists th…
Linked Issues check Passed Directly linked issue #1560 has three coding requirements in PostgreSQLMemoryAdapter.getMessages. (1) Parts normalization: the PR description and the change summary for packages/postgres/src/memory-ad…
Out of Scope Changes check Passed The PR changes four areas. (a) packages/postgres/src/memory-adapter.ts: the getMessages parsing fixes that implement #1560. (b) packages/postgres/src/memory-adapter.spec.ts: regression tests for the t…
Full details: Title check

Explanation

The title names the package (postgres), the change type (fix), and the area (getMessages payload parsing). It matches the main change: three edge cases in PostgreSQLMemoryAdapter.getMessages for parts and legacy content. It is specific enough for a teammate scanning history.

Full details: Description check

Explanation

The description follows the repository template. It includes the checklist, the current behavior, the new behavior, and the issue reference (fixes #1560). It explains the three edge cases and lists the tests and verification steps in the reviewer notes. The author left the docs checkbox unchecked, which the template allows. The description does not state whether the changeset was reviewed, but the checklist marks it complete and the changeset file is present in the summary.

Full details: Linked Issues check

Explanation

Directly linked issue #1560 has three coding requirements in PostgreSQLMemoryAdapter.getMessages. (1) Parts normalization: the PR description and the change summary for packages/postgres/src/memory-adapter.ts state that row.parts is accepted only when it is an array, whether it is parsed from JSON or already parsed by pg. Otherwise parts falls back to []. The spec file adds a test for the "null" and object cases. (2) createdAt on legacy plain-text content: the summary states that the early return in the catch block was removed, so the mapper's metadata.createdAt normalization runs. The spec adds a test asserting metadata.createdAt is a Date from created_at. (3) Legacy primitive content: the summary states that numeric or boolean results (for example "123" or "true") become a text part using String(rawContent). The spec adds a test for these values. The changes address all three requirements. Limitation: the getMessages body was truncated in the repository read at the reviewed head (16000-byte limit), so the verdict relies on the change summary and the PR description, not a direct read of the final code. Tests were not executed.

Full details: Out of Scope Changes check

Explanation

The PR changes four areas. (a) packages/postgres/src/memory-adapter.ts: the getMessages parsing fixes that implement #1560. (b) packages/postgres/src/memory-adapter.spec.ts: regression tests for the three #1560 cases. Tests support the objective. (c) .changeset/fix-postgres-get-messages-parsing.md: a patch changeset for @voltagent/postgres that describes the same fix. This is release metadata for the change. No public declarations changed, per the summary. No unrelated files or behavior changes were reported. The direct diff of memory-adapter.ts could not be read in full (truncated at 16000 bytes), so the absence of other edits rests on the change summary.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c89e3201-4a40-44f6-aaa0-823632bb7581
Commits

Reviewing files that changed from the base of the PR and between e9fc599 and 46a5bc6.

Files selected for processing: 3

  • .changeset/fix-postgres-get-messages-parsing.md
  • packages/postgres/src/memory-adapter.spec.ts
  • packages/postgres/src/memory-adapter.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 3 files

View guided diff | Re-trigger cubic

@SanemIzci

Copy link
Copy Markdown
Author

@omeraplak
It looks like the CI jobs failed during the initial pnpm install step due to an upstream issue in onnxruntime-node@1.21.0's postinstall script on Linux x64 (https://github.com/microsoft/onnxruntime/releases/download/v1.21.0/onnxruntime-linux-x64-gpu-1.21.0.tgz now returns 404 Not Found).

Setting ONNXRUNTIME_NODE_INSTALL_CUDA: "skip" in .github/workflows/pull-request.yml (next to PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: "1") should resolve the pnpm install failure across CI. Let me know if you'd like me to open a separate PR for that CI fix!

@SanemIzci

Copy link
Copy Markdown
Author

I opened #1563 to set ONNXRUNTIME_NODE_INSTALL_CUDA: "skip" across the CI workflows (fixing the fresh pnpm install 404 after the pnpm cache expired). Once #1563 is verified and merged, #1561 should pass cleanly on a re-run.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] PostgreSQLMemoryAdapter.getMessages crashes on non-array parts and drops legacy primitive/createdAt data

1 participant