Skip to content

feat(core): add stringify helper and make AI-tracing serializers safe#22163

Open
logaretm wants to merge 4 commits into
developfrom
feat/consolidate-safe-json-stringify
Open

feat(core): add stringify helper and make AI-tracing serializers safe#22163
logaretm wants to merge 4 commits into
developfrom
feat/consolidate-safe-json-stringify

Conversation

@logaretm

@logaretm logaretm commented Jul 9, 2026

Copy link
Copy Markdown
Member

Adds an exported stringify to @sentry/core and routes the SDK's "serialize a value for a span attribute" helpers through it.

We had a few possible gaps in getJsonString and getTruncatedJsonString where they throw on circular refs / BigInt, and every call site feeds their result straight into span.setAttribute with no try/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 stringify because it neither JUST casts to strings nor it just string JSONfies. naming it as either will be confusing. I dropped the safe prefix 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 undefined but never typed it, it mostly was safe to do so because it often ended up in attributes but type accuracy here is safer.

@logaretm logaretm changed the title feat(core): add safeJsonStringify and make AI-tracing serializers safe feat(core): add safeJsonStringify and normalize usage Jul 9, 2026
@logaretm logaretm force-pushed the feat/consolidate-safe-json-stringify branch from b4c66d5 to 2d78382 Compare July 9, 2026 20:09
@logaretm logaretm changed the title feat(core): add safeJsonStringify and normalize usage feat(core): add stringify helper and make AI-tracing serializers safe Jul 9, 2026
@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 27.61 kB - -
@sentry/browser - with treeshaking flags 26.05 kB - -
@sentry/browser (incl. Tracing) 46.36 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 48.16 kB - -
@sentry/browser (incl. Tracing, Profiling) 51.15 kB - -
@sentry/browser (incl. Tracing, Replay) 85.63 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 75.27 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 90.34 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 103 kB - -
@sentry/browser (incl. Feedback) 44.79 kB - -
@sentry/browser (incl. sendFeedback) 32.41 kB - -
@sentry/browser (incl. FeedbackAsync) 37.53 kB - -
@sentry/browser (incl. Metrics) 28.69 kB - -
@sentry/browser (incl. Logs) 28.93 kB - -
@sentry/browser (incl. Metrics & Logs) 29.62 kB - -
@sentry/react 29.4 kB - -
@sentry/react (incl. Tracing) 48.63 kB - -
@sentry/vue 33.04 kB - -
@sentry/vue (incl. Tracing) 48.34 kB - -
@sentry/svelte 27.63 kB - -
CDN Bundle 30.01 kB - -
CDN Bundle (incl. Tracing) 48.34 kB - -
CDN Bundle (incl. Logs, Metrics) 31.59 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 49.65 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 70.82 kB - -
CDN Bundle (incl. Tracing, Replay) 85.84 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 87.16 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 91.65 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 92.92 kB - -
CDN Bundle - uncompressed 89.37 kB - -
CDN Bundle (incl. Tracing) - uncompressed 146.12 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 94.07 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 150.09 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 218.8 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 265.32 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 269.28 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 279.02 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 282.97 kB - -
@sentry/nextjs (client) 51.18 kB - -
@sentry/sveltekit (client) 46.81 kB - -
@sentry/core/server 78.45 kB +0.05% +35 B 🔺
@sentry/core/browser 64.81 kB +0.07% +39 B 🔺
@sentry/node-core 62.73 kB - -
@sentry/node 125.08 kB -0.01% -11 B 🔽
@sentry/node (incl. diagnostics channel injection) 139.34 kB -0.01% -1 B 🔽
@sentry/node/import (ESM hook with diagnostics-channel injection) 69.96 kB - -
@sentry/node/light 50.73 kB +0.01% +1 B 🔺
@sentry/node - without tracing 74.1 kB - -
@sentry/aws-serverless 83.32 kB - -
@sentry/cloudflare (withSentry) - minified 181.58 kB - -
@sentry/cloudflare (withSentry) 449.49 kB - -

View base workflow run

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.
@logaretm logaretm force-pushed the feat/consolidate-safe-json-stringify branch from 2d78382 to b6cc26f Compare July 13, 2026 15:22
@logaretm logaretm marked this pull request as ready for review July 13, 2026 16:00
@logaretm logaretm requested a review from a team as a code owner July 13, 2026 16:00
@logaretm logaretm requested review from Lms24, chargome and s1gr1d and removed request for a team July 13, 2026 16:00
Comment thread packages/core/src/utils/string.ts
Comment thread packages/core/src/utils/string.ts
Comment thread packages/core/src/utils/string.ts
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.
Comment thread packages/core/test/lib/tracing/ai-message-truncation.test.ts
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.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ 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.

Comment thread packages/core/test/lib/utils/string.test.ts
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.

1 participant