perf(core): Reuse unchanged Vercel AI messages JSON#21354
Merged
isaacs merged 2 commits intoJun 10, 2026
Conversation
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
approved these changes
Jun 8, 2026
nicohrubec
pushed a commit
that referenced
this pull request
Jun 10, 2026
This PR adds the external contributor to the CHANGELOG.md file, so that they are credited for their contribution. See #21354 Co-authored-by: isaacs <9287+isaacs@users.noreply.github.com>
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
extractSystemInstructionsdoes 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 originalai.prompt.messagesstring, 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:
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