Skip to content

perf(core): Reuse unchanged Vercel AI messages JSON#21354

Open
AyaanFaisal21 wants to merge 1 commit into
getsentry:developfrom
AyaanFaisal21:perf-vercel-ai-reuse-messages-json
Open

perf(core): Reuse unchanged Vercel AI messages JSON#21354
AyaanFaisal21 wants to merge 1 commit into
getsentry:developfrom
AyaanFaisal21:perf-vercel-ai-reuse-messages-json

Conversation

@AyaanFaisal21

@AyaanFaisal21 AyaanFaisal21 commented Jun 7, 2026

Copy link
Copy Markdown

Summary

This PR reduces unnecessary work in the Vercel AI tracing path when ai.prompt.messages is already provided as a JSON string.

When truncation is disabled and extractSystemInstructions does not remove a system message, the parsed message array is unchanged. In the previous path, that left three prompt-sized representations alive during processing: the original ai.prompt.messages string, the parsed message array, and a newly serialized JSON string. This PR reuses the original string in the unchanged case, reducing that path from three prompt-sized representations to two while preserving the existing truncation and system-extraction behavior.

The existing behavior is preserved when:

  • truncation is enabled
  • system instructions are extracted
  • the messages payload is malformed or not an array

Background

This is related to #21235.

For large Vercel AI prompts, the current string branch parses ai.prompt.messages and then serializes the parsed messages again, even when the output is effectively the same as the input. That can create an avoidable full-size copy of a large prompt payload in memory-constrained runtimes.

This change keeps the existing parse step so the SDK can still validate the payload, extract system instructions, and record the message count. It only skips the final re-serialization when the parsed messages were not transformed and truncation is off.

Changes

  • Reuse the original ai.prompt.messages JSON string when no system message is removed and truncation is disabled, avoiding another full-size serialization of the same payload.
  • Preserve the existing getTruncatedJsonString(...) path when truncation is enabled.
  • Preserve the existing getJsonString(...) path when system-message extraction changes the message array.
  • Add regression tests for the ai.prompt.messages string branch.

Verification

Verified locally:

  • yarn build:dev:filter @sentry/core
  • yarn workspace @sentry/core test vercel-ai-request-messages
  • yarn workspace @sentry/core test
    • 158 test files passed
    • 3024 tests passed
    • 1 test skipped
  • OXLINT_TSGOLINT_DANGEROUSLY_SUPPRESS_PROGRAM_DIAGNOSTICS=true ./node_modules/.bin/oxlint packages/core/src/tracing/vercel-ai/utils.ts packages/core/test/lib/tracing/vercel-ai-request-messages.test.ts
  • ./node_modules/.bin/oxfmt packages/core/src/tracing/vercel-ai/utils.ts packages/core/test/lib/tracing/vercel-ai-request-messages.test.ts --check
  • git diff --check

I verified the affected package and touched files locally rather than using root yarn lint / yarn test as the final signal. In this local checkout, package/root lint did not provide a clean repo-level signal: @sentry/core:lint failed before checking the changed code due to an oxlint type-aware config parsing issue, and root lint surfaced broader monorepo noise unrelated to these files. To keep the verification scoped and accurate, I ran the same linter/formatter directly against the two touched files, where oxlint reported 0 warnings and 0 errors.

Checklist

  • If you've added code that should be tested, please add tests.
  • Ensure your code lints and the test suite passes (yarn lint) & (yarn test).
  • Link an issue if there is one related to your pull request. If no issue is linked, one will be auto-generated and linked.

Related to #21235

@isaacs

isaacs commented Jun 8, 2026

Copy link
Copy Markdown
Member

Good patch, thanks. Avoiding extra work when it would definitely produce a known output, is always a win.

Will be merged if CI feels the same :)

@isaacs isaacs enabled auto-merge (rebase) June 8, 2026 17:56
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.

2 participants