v5-mirror: serialize UInt8Arrays as base64 for inner telemetry spans #6377
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.
Background
Clone of #6357 but for v5.
Description below is copy-pasted from there
generateObject
,generateText
,streamText
, andstreamObject
currently callJSON.stringify
on the input messages. If the input messages contain an image, it is most likely normalized into aUint8Array
.JSON.stringify
does not the most obvious things to TypedArrays includingUint8Array
.In practice, this results in bloating images by about 5-15x depending on the original image size. For Laminar, for example, a span with 3 avg sized images will not be able to be sent as it is larger than the (reasonably high) gRPC payload size for our traces endpoint.
From MDN docs:
Summary
Added a function that maps over messages in a
LanguageModelV2Prompt
and maps over content parts in each message, replacingUint8Array
s with raw base64 strings instead.Call this function when calling
recordSpan
for the inner (doStream/doGenerate) span ingenerateObject
,generateText
,streamText
, andstreamObject
.Verification
Ran this small script against a local instance of Laminar and logged the Telemetry payloads (span attributes) on the backend to verify that they are indeed base64.
Tasks
pnpm changeset
in the project root)pnpm prettier-fix
in the project root)Future Work
Related Issues
Fixes #6210 for v5