Skip to content

Commit 9b8a7be

Browse files
pmartinsdevAbhiPrasad
authored andcommitted
fix(ai-sdk): log AI SDK v7 inline file data as attachments
1 parent 753d396 commit 9b8a7be

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"braintrust": patch
3+
---
4+
5+
fix(ai-sdk): log AI SDK v7 inline file data as attachments instead of copying the bytes into the span

‎js/src/instrumentation/plugins/ai-sdk-plugin.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
BRAINTRUST_AI_SDK_V7_OPERATION_KEY as AI_SDK_V7_OPERATION_KEY,
3333
} from "../../vendor-sdk-types/ai-sdk-v7-telemetry";
3434
import { serializeAISDKToolsForLogging } from "../../wrappers/ai-sdk/tool-serialization";
35+
import { Attachment } from "../../logger";
3536
import {
3637
AISDKPlugin,
3738
DEFAULT_DENY_OUTPUT_PATHS,
@@ -221,6 +222,28 @@ describe("AISDKPlugin", () => {
221222
expect(processed.model).not.toHaveProperty("doStream");
222223
expect(processed.model.config).not.toHaveProperty("url");
223224
});
225+
226+
it("logs AI SDK v7 inline file data as attachments", () => {
227+
const processed = processAISDKCallInput({
228+
prompt: [
229+
{
230+
role: "user",
231+
content: [
232+
{
233+
type: "file",
234+
mediaType: "image/png",
235+
data: {
236+
type: "data",
237+
data: new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]),
238+
},
239+
},
240+
],
241+
},
242+
],
243+
}).input as Record<string, any>;
244+
245+
expect(processed.prompt[0].content[0].data).toBeInstanceOf(Attachment);
246+
});
224247
});
225248

226249
describe("enable/disable", () => {

‎js/src/instrumentation/plugins/ai-sdk-plugin.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1939,7 +1939,7 @@ const processContentPart = (part: any): any => {
19391939
(part.mimeType || part.mediaType)
19401940
) {
19411941
const fileAttachment = convertDataToAttachment(
1942-
part.data,
1942+
unwrapInlineFileData(part.data),
19431943
part.mimeType || part.mediaType,
19441944
part.name || part.filename,
19451945
);
@@ -1972,6 +1972,10 @@ const processContentPart = (part: any): any => {
19721972
return part;
19731973
};
19741974

1975+
// AI SDK v7 tags inline file bytes as { type: "data", data }.
1976+
const unwrapInlineFileData = (data: unknown): unknown =>
1977+
isObject(data) && data.type === "data" && "data" in data ? data.data : data;
1978+
19751979
const convertImageToAttachment = (
19761980
image: any,
19771981
explicitMimeType?: string,

0 commit comments

Comments
 (0)