Uh oh!
There was an error while loading. Please reload this page.
feat(core): add stringify helper and make AI-tracing serializers safe - #22163
Conversation
safeJsonStringify and normalize usageb4c66d5 to
2d78382ComparesafeJsonStringify and normalize usagesize-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
b6cc26fCompareUh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.
andreiborza
left a comment
There was a problem hiding this comment.
Nice, thanks for making this better.
Uh oh!
There was an error while loading. Please reload this page.
…22370) Adds a item to `AGENTS.md` (`CLAUDE.md` symlinks to it) telling agents to search for an existing util before writing a new one, pointing at where shared helpers actually live (`packages/core/src/utils/`, `packages/browser-utils/`). Motivation: LLM-assisted changes have a habit of introducing near-duplicate helpers instead of reusing what's already there. This is a passive nudge rather than an enforced check. I did a quick check spawned a couple of agents on bogus tasks that needed a couple of utils, and watched its reasoning and it picked up on this, so while it doesn't enforce it, it may help us curb the util soup we get. This is a follow up to my previous util cleanup PRs #22155#22163#22154

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.