perf(core): Reuse unchanged Vercel AI messages JSON#21354
Open
AyaanFaisal21 wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 that case, this reuses the original ai.prompt.messages string instead of serializing the same large payload again.
The existing behavior is preserved when:
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
ai.prompt.messagesJSON string when no system message is removed and truncation is disabled, avoiding another full-size serialization of the same payload.Verification
Verified locally:
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
Related to #21235