feat(core): add stringify helper and make AI-tracing serializers safe#22163
Merged
Conversation
safeJsonStringify and normalize usage
b4c66d5 to
2d78382
Compare
safeJsonStringify and normalize usage
Contributor
size-limit report 📦
|
Adds an exported stringify to core (string passthrough, else JSON.stringify, never throws) and routes the SDK's span-attribute serializers through it. getJsonString and getTruncatedJsonString previously threw on circular refs / BigInt straight into span.setAttribute with no try/catch, which could crash instrumentation; the safe path returns '[unserializable]' instead. Consolidates safeStringify (server-utils) and langchain's asString too.
2d78382 to
b6cc26f
Compare
JSON.stringify returns undefined (not a throw) for top-level undefined, functions, and symbols, so stringify could return undefined despite its string annotation. Widen the return type to string | undefined and let it propagate: undefined means "nothing to serialize", which naturally omits the attribute, while the fallback stays reserved for real throws (circular refs, BigInt). Propagates through the langchain attribute builders and swaps the vercel-ai subscriber's bespoke Attributes type for the shared SpanAttributes, which already permits undefined. No runtime behavior change.
The array case routes through truncateGenAiMessages (a different code path from a bare object) and only asserted it didn't throw, leaving the actual fallback value unchecked. Assert it returns '[unserializable]' too.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 4ab1ca0. Configure here.
andreiborza
approved these changes
Jul 14, 2026
andreiborza
left a comment
Member
There was a problem hiding this comment.
Nice, thanks for making this better.
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.

Adds an exported
stringifyto@sentry/coreand routes the SDK's "serialize a value for a span attribute" helpers through it.We had a few possible gaps in
getJsonStringandgetTruncatedJsonStringwhere they throw on circular refs /BigInt, and every call site feeds their result straight intospan.setAttributewith notry/catch. So a non-serializable value in an LLM message could throw out of the instrumentation and disrupt the wrapped call.Regarding the naming, I decided to name it
stringifybecause it neither JUST casts to strings nor it just string JSONfies. naming it as either will be confusing. I dropped thesafeprefix because I don't think we ever want an unsafe option.One change that needed to propagate is type accuracy. The old functions did return
undefinedbut never typed it, it mostly was safe to do so because it often ended up in attributes but type accuracy here is safer.