Skip to content

Commit fd1924b

Browse files
authored
chore (ai): remove redundant mimeType property (#6070)
## Background The `mimeType` property was still used on messages which should be migrated. ## Summary Remove `mimeType` property since we are using `mediaType`.
1 parent fafc3f2 commit fd1924b

11 files changed

Lines changed: 34 additions & 43 deletions

.changeset/nasty-lobsters-shave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ai': major
3+
---
4+
5+
chore (ai): remove redundant `mimeType` property

packages/ai/core/generate-speech/generated-audio-file.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,22 @@ export class DefaultGeneratedAudioFile
2929
super({ data, mediaType });
3030
let format = 'mp3';
3131

32-
// If format is not provided, try to determine it from the mimeType
32+
// If format is not provided, try to determine it from the media type
3333
if (mediaType) {
34-
const mimeTypeParts = mediaType.split('/');
34+
const mediaTypeParts = mediaType.split('/');
3535

36-
if (mimeTypeParts.length === 2) {
36+
if (mediaTypeParts.length === 2) {
3737
// Handle special cases for audio formats
3838
if (mediaType !== 'audio/mpeg') {
39-
format = mimeTypeParts[1];
39+
format = mediaTypeParts[1];
4040
}
4141
}
4242
}
4343

4444
if (!format) {
45+
// TODO this should be an AI SDK error
4546
throw new Error(
46-
'Audio format must be provided or determinable from mimeType',
47+
'Audio format must be provided or determinable from media type',
4748
);
4849
}
4950

packages/ai/core/generate-text/__snapshots__/stream-text.test.ts.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4028,11 +4028,11 @@ exports[`streamText > result.pipeDataStreamToResponse > should write file conten
40284028
[
40294029
"f:{"messageId":"msg-0"}
40304030
",
4031-
"k:{"mimeType":"text/plain","url":"data:text/plain;base64,Hello World"}
4031+
"k:{"mediaType":"text/plain","url":"data:text/plain;base64,Hello World"}
40324032
",
40334033
"0:"Hello!"
40344034
",
4035-
"k:{"mimeType":"image/jpeg","url":"data:image/jpeg;base64,QkFVRw=="}
4035+
"k:{"mediaType":"image/jpeg","url":"data:image/jpeg;base64,QkFVRw=="}
40364036
",
40374037
"e:{"finishReason":"stop","usage":{"promptTokens":3,"completionTokens":10},"isContinued":false}
40384038
",
@@ -4525,11 +4525,11 @@ exports[`streamText > result.toDataStream > should send file content 1`] = `
45254525
[
45264526
"f:{"messageId":"msg-0"}
45274527
",
4528-
"k:{"mimeType":"text/plain","url":"data:text/plain;base64,Hello World"}
4528+
"k:{"mediaType":"text/plain","url":"data:text/plain;base64,Hello World"}
45294529
",
45304530
"0:"Hello!"
45314531
",
4532-
"k:{"mimeType":"image/jpeg","url":"data:image/jpeg;base64,QkFVRw=="}
4532+
"k:{"mediaType":"image/jpeg","url":"data:image/jpeg;base64,QkFVRw=="}
45334533
",
45344534
"e:{"finishReason":"stop","usage":{"promptTokens":3,"completionTokens":10},"isContinued":false}
45354535
",

packages/ai/core/generate-text/stream-text.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1612,7 +1612,7 @@ However, the LLM results are expected to be small enough to not cause issues.
16121612
controller.enqueue(
16131613
// TODO update protocol to v2 or replace with event stream
16141614
formatDataStreamPart('file', {
1615-
mimeType: chunk.file.mediaType, // TODO mediaType
1615+
mediaType: chunk.file.mediaType,
16161616
url: `data:${chunk.file.mediaType};base64,${chunk.file.base64}`,
16171617
}),
16181618
);

packages/ai/core/prompt/append-response-messages.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ Internal. For test use only. May change without notice.
115115
}
116116
parts.push({
117117
type: 'file' as const,
118-
mediaType: part.mediaType ?? part.mimeType,
119-
url: `data:${part.mediaType ?? part.mimeType};base64,${convertDataContentToBase64String(part.data)}`,
118+
mediaType: part.mediaType,
119+
url: `data:${part.mediaType};base64,${convertDataContentToBase64String(part.data)}`,
120120
});
121121
break;
122122
}

packages/ai/core/prompt/content-part.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ Optional IANA media type of the image.
5858
*/
5959
mediaType?: string;
6060

61-
/**
62-
@deprecated Use `mediaType` instead.
63-
*/
64-
mimeType?: string;
65-
6661
/**
6762
Additional provider-specific metadata. They are passed through
6863
to the provider from the AI SDK and enable provider-specific
@@ -78,7 +73,6 @@ export const imagePartSchema: z.ZodType<ImagePart> = z.object({
7873
type: z.literal('image'),
7974
image: z.union([dataContentSchema, z.instanceof(URL)]),
8075
mediaType: z.string().optional(),
81-
mimeType: z.string().optional(),
8276
providerOptions: providerMetadataSchema.optional(),
8377
});
8478

@@ -108,11 +102,6 @@ IANA media type of the file.
108102
*/
109103
mediaType: string;
110104

111-
/**
112-
@deprecated Use `mediaType` instead.
113-
*/
114-
mimeType?: string;
115-
116105
/**
117106
Additional provider-specific metadata. They are passed through
118107
to the provider from the AI SDK and enable provider-specific
@@ -129,7 +118,6 @@ export const filePartSchema: z.ZodType<FilePart> = z.object({
129118
data: z.union([dataContentSchema, z.instanceof(URL)]),
130119
filename: z.string().optional(),
131120
mediaType: z.string(),
132-
mimeType: z.string().optional(),
133121
providerOptions: providerMetadataSchema.optional(),
134122
});
135123

packages/ai/core/prompt/convert-to-language-model-prompt.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export function convertToLanguageModelMessage(
115115
type: 'file',
116116
data,
117117
filename: part.filename,
118-
mediaType: mediaType ?? part.mediaType ?? part.mimeType,
118+
mediaType: mediaType ?? part.mediaType,
119119
providerOptions,
120120
};
121121
}
@@ -194,9 +194,7 @@ async function downloadAssets(
194194
)
195195
.map(part => {
196196
const mediaType =
197-
part.mediaType ??
198-
part.mimeType ??
199-
(part.type === 'image' ? 'image/*' : undefined);
197+
part.mediaType ?? (part.type === 'image' ? 'image/*' : undefined);
200198

201199
let data = part.type === 'image' ? part.image : part.data;
202200
if (typeof data === 'string') {
@@ -275,8 +273,7 @@ function convertPartToLanguageModelPart(
275273
const { data: convertedData, mediaType: convertedMediaType } =
276274
convertToLanguageModelV2DataContent(originalData);
277275

278-
let mediaType: string | undefined =
279-
convertedMediaType ?? part.mediaType ?? part.mimeType;
276+
let mediaType: string | undefined = convertedMediaType ?? part.mediaType;
280277
let data: Uint8Array | string | URL = convertedData; // binary | base64 | url
281278

282279
// If the content is a URL, we check if it was downloaded:

packages/ai/core/util/data-stream-parts.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ describe('data-stream-parts', () => {
363363
it('should format a file stream part', () => {
364364
const file = {
365365
url: 'data:text/plain;base64,SGVsbG8gV29ybGQ=',
366-
mimeType: 'text/plain',
366+
mediaType: 'text/plain',
367367
};
368368

369369
expect(formatDataStreamPart('file', file)).toEqual(
@@ -374,7 +374,7 @@ describe('data-stream-parts', () => {
374374
it('should parse a file stream part', () => {
375375
const file = {
376376
url: 'data:text/plain;base64,SGVsbG8gV29ybGQ=',
377-
mimeType: 'text/plain',
377+
mediaType: 'text/plain',
378378
};
379379

380380
const input = `k:${JSON.stringify(file)}`;
@@ -387,15 +387,15 @@ describe('data-stream-parts', () => {
387387
it('should throw an error if the file value is not an object', () => {
388388
const input = 'k:"not an object"';
389389
expect(() => parseDataStreamPart(input)).toThrow(
390-
'"file" parts expect an object with a "url" and "mimeType" property.',
390+
'"file" parts expect an object with a "url" and "mediaType" property.',
391391
);
392392
});
393393

394394
it('should throw an error if the file object is missing required properties', () => {
395395
const invalidFile = { name: 'test.txt' };
396396
const input = `k:${JSON.stringify(invalidFile)}`;
397397
expect(() => parseDataStreamPart(input)).toThrow(
398-
'"file" parts expect an object with a "url" and "mimeType" property.',
398+
'"file" parts expect an object with a "url" and "mediaType" property.',
399399
);
400400
});
401401
});

packages/ai/core/util/data-stream-parts.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ const fileStreamPart: DataStreamPart<
402402
'file',
403403
{
404404
url: string;
405-
mimeType: string; // TODO mediaType
405+
mediaType: string;
406406
}
407407
> = {
408408
code: 'k',
@@ -413,14 +413,14 @@ const fileStreamPart: DataStreamPart<
413413
typeof value !== 'object' ||
414414
!('url' in value) ||
415415
typeof value.url !== 'string' ||
416-
!('mimeType' in value) ||
417-
typeof value.mimeType !== 'string'
416+
!('mediaType' in value) ||
417+
typeof value.mediaType !== 'string'
418418
) {
419419
throw new Error(
420-
'"file" parts expect an object with a "url" and "mimeType" property.',
420+
'"file" parts expect an object with a "url" and "mediaType" property.',
421421
);
422422
}
423-
return { type: 'file', value: value as { url: string; mimeType: string } };
423+
return { type: 'file', value: value as { url: string; mediaType: string } };
424424
},
425425
};
426426

@@ -533,7 +533,7 @@ export const parseDataStreamPart = (line: string): DataStreamPartType => {
533533
};
534534

535535
/**
536-
Prepends a string with a prefix from the `StreamChunkPrefixes`, JSON-ifies it,
536+
Prepends a string with a prefix from the `StreamChunkPrefixes`, converts it to JSON,
537537
and appends a new line.
538538
539539
It ensures type-safety for the part type and value.

packages/ai/core/util/process-chat-response.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,12 +713,12 @@ describe('scenario: server provides file parts', () => {
713713
formatDataStreamPart('text', 'Here is a file:'),
714714
formatDataStreamPart('file', {
715715
url: 'data:text/plain;base64,SGVsbG8gV29ybGQ=',
716-
mimeType: 'text/plain',
716+
mediaType: 'text/plain',
717717
}),
718718
formatDataStreamPart('text', 'And another one:'),
719719
formatDataStreamPart('file', {
720720
url: 'data:application/json;base64,eyJrZXkiOiJ2YWx1ZSJ9',
721-
mimeType: 'application/json',
721+
mediaType: 'application/json',
722722
}),
723723
formatDataStreamPart('finish_step', {
724724
finishReason: 'stop',

0 commit comments

Comments
 (0)