Skip to content

Commit

Permalink
fix (ai/core): prefer auto-detected image mimetype (#4172)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrammel authored Dec 20, 2024
1 parent 6fb3e91 commit a8669a2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-tools-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': patch
---

fix (ai/core): prefer auto-detected image mimetype
33 changes: 30 additions & 3 deletions packages/ai/core/prompt/convert-to-language-model-prompt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ describe('convertToLanguageModelMessage', () => {
content: [
{
type: 'image',
image: 'data:image/jpg;base64,dGVzdA==',
image: 'data:image/jpg;base64,/9j/3Q==',
},
],
},
Expand All @@ -572,8 +572,35 @@ describe('convertToLanguageModelMessage', () => {
content: [
{
type: 'image',
image: new Uint8Array([116, 101, 115, 116]),
mimeType: 'image/jpg',
image: new Uint8Array([255, 216, 255, 221]),
mimeType: 'image/jpeg',
},
],
});
});

it('should prefer detected mimetype', async () => {
const result = convertToLanguageModelMessage(
{
role: 'user',
content: [
{
type: 'image',
// incorrect mimetype:
image: 'data:image/png;base64,/9j/3Q==',
},
],
},
{},
);

expect(result).toEqual({
role: 'user',
content: [
{
type: 'image',
image: new Uint8Array([255, 216, 255, 221]),
mimeType: 'image/jpeg',
},
],
});
Expand Down
18 changes: 11 additions & 7 deletions packages/ai/core/prompt/convert-to-language-model-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,20 +284,23 @@ function convertPartToLanguageModelPart(
// Now that we have the normalized data either as a URL or a Uint8Array,
// we can create the LanguageModelV1Part.
switch (type) {
case 'image':
// We give a best effort to detect the mime type if it is not provided.
// otherwise, we use the provided mime type.
if (mimeType == null && normalizedData instanceof Uint8Array) {
mimeType = detectImageMimeType(normalizedData);
}
case 'image': {
// When possible, try to detect the mimetype automatically
// to deal with incorrect mimetype inputs.
// When detection fails, use provided mimetype.

if (normalizedData instanceof Uint8Array) {
mimeType = detectImageMimeType(normalizedData) ?? mimeType;
}
return {
type: 'image',
image: normalizedData,
mimeType,
providerMetadata: part.experimental_providerMetadata,
};
case 'file':
}

case 'file': {
// We should have a mimeType at this point, if not, throw an error.
if (mimeType == null) {
throw new Error(`Mime type is missing for file part`);
Expand All @@ -312,5 +315,6 @@ function convertPartToLanguageModelPart(
mimeType,
providerMetadata: part.experimental_providerMetadata,
};
}
}
}

0 comments on commit a8669a2

Please sign in to comment.