wrapAISDK copies AI SDK v7 file bytes one key per byte instead of turning them into attachments

Offen
#2,750 0 Kommentare 0 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

@AbhiPrasad arbeitet bereits daran.

Seit 06.11.2026.

  • #2751 von @pmartinsdev — ohne Merge geschlossen
  • #2752 von @AbhiPrasad — offen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Beschreibung

typescript

What happens

With ai 7.0.145 and braintrust 3.32.0, file parts reach the doStream model span as { type: 'file', mediaType, data: { type: 'data', data: Uint8Array } }, the AI SDK 7 shape (SharedV4FileData). processContentPart passes part.data, the wrapper object, to convertDataToAttachment, which only handles a string, Uint8Array, Buffer or Blob, so no attachment is made. deepCopyEvent then runs JSON.stringify and JSON.parse on the raw Uint8Array, which becomes an object with one key per byte.

https://github.com/braintrustdata/braintrust-sdk-javascript/blob/braintrust%403.32.0/js/src/instrumentation/plugins/ai-sdk-plugin.ts#L1297-L1301

The same code is in 3.43.0 and on main as of e0b7107dc7.

Impact

1 MB of image bytes becomes 1,000,000 keys and about 120 MB of heap on every model call. We call Claude through Vertex, which takes no image URLs, so the AI SDK downloads every image in the conversation and each call carries all of them. A chat with 25 MB of images pushed the heap to about 3.1 GB, and our 4 GB serverless instances were killed for memory.

On a single 38-image call (24.5 MB of images), the heap peaked at 2,651 MB with wrapAISDK and at 117 MB without it, and deepCopyEvent alone allocated 5.4 GB.

Reproduction

processContentPart leaves this shape untouched, so the model span event is copied like this.

import { deepCopyEvent } from 'braintrust';

const copy = deepCopyEvent({
  input: [{ type: 'file', mediaType: 'image/png', data: { type: 'data', data: new Uint8Array(1_000_000) } }],
});
// copy.input[0].data.data is a plain object with 1,000,000 keys, about 11 MB of JSON

Workaround we use

A one-line change in processContentPart, applied with pnpm patch to every build in dist, fixes it for us.

       const fileAttachment = convertDataToAttachment(
-        part.data,
+        part.data.type === "data" ? part.data.data : part.data,
         part.mimeType || part.mediaType,
         part.name || part.filename
       );

With it, the same 38-image call peaked at 130 MB of heap with tracing on. A complete fix could also handle { type: 'url' } and the other SharedV4FileData variants in convertDataToAttachment.

Environment

  • braintrust 3.32.0, also seen at runtime on 3.42.0 and in the source of 3.43.0
  • ai 7.0.145 with @ai-sdk/google-vertex 5.0.117 (Anthropic on Vertex)
  • Node 22
Vorherrschende Sprache
TypeScript
Sterne
29
Forks
22
Ø Merge
1 T. 25 Min.
Gemergte PRs (30 T.)
80

Entwicklungsumgebung

  • Enthält ein Dockerfile oder eine Docker-Compose-Datei
  • Keine Pull-Request-Vorlage
  • Kein Beitragsleitfaden

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus braintrustdata/braintrust-sdk-javascript

Alle Issues in braintrustdata/braintrust-sdk-javascript

Ähnliche Issues

Weitere Issues zu TypeScript

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.