feat(core): add stringify helper and make AI-tracing serializers safe#22163
Draft
logaretm wants to merge 1 commit into
Draft
feat(core): add stringify helper and make AI-tracing serializers safe#22163logaretm wants to merge 1 commit into
logaretm wants to merge 1 commit into
Conversation
safeJsonStringify and normalize usage
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.
b4c66d5 to
2d78382
Compare
safeJsonStringify and normalize usage
Contributor
size-limit report 📦
|
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.stringifycoerces a value to a string without ever throwing: strings pass through unchanged (so an already-serialized value isn't double-encoded and a plain string like a model name isn't wrapped in quotes), anything else isJSON.stringify-ed, and a circular ref orBigIntyields'[unserializable]'rather than throwing. It's a stringify, not a JSON-stringify, which is why strings aren't quoted.The real motivation is a latent bug:
getJsonStringandgetTruncatedJsonStringthrew 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 (tool args, usage metadata, etc.) could throw out of the instrumentation hook and disrupt the wrapped call.Consolidates
safeStringify(server-utils) and langchain's privateasStringintostringify, deletes the throwing internalgetJsonString(its 8 call sites now use the safe helper), and wrapsgetTruncatedJsonStringin a try/catch. Note the throw there can also originate in the media-stripping/truncation step, not justJSON.stringify, so the try/catch covers the whole non-string path. Success paths are unchanged.