Skip to content

Commit 58d9d76

Browse files
committed
Guard against unsupported image types
1 parent b911d5d commit 58d9d76

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

packages/vertexai/src/methods/chrome-adapter.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,33 @@ describe('ChromeAdapter', () => {
153153
})
154154
).to.be.false;
155155
});
156+
it('returns true if request has image with supported mime type', async () => {
157+
const adapter = new ChromeAdapter(
158+
{
159+
availability: async () => Availability.available
160+
} as LanguageModel,
161+
'prefer_on_device'
162+
);
163+
for (const mimeType of ChromeAdapter.SUPPORTED_MIME_TYPES) {
164+
expect(
165+
await adapter.isAvailable({
166+
contents: [
167+
{
168+
role: 'user',
169+
parts: [
170+
{
171+
inlineData: {
172+
mimeType,
173+
data: ''
174+
}
175+
}
176+
]
177+
}
178+
]
179+
})
180+
).to.be.true;
181+
}
182+
});
156183
it('returns true if model is readily available', async () => {
157184
const languageModelProvider = {
158185
availability: () => Promise.resolve(Availability.available)

packages/vertexai/src/methods/chrome-adapter.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import {
3535
* and encapsulates logic for detecting when on-device is possible.
3636
*/
3737
export class ChromeAdapter {
38+
// Visible for testing
39+
static SUPPORTED_MIME_TYPES = ['image/jpeg', 'image/png', 'image/webp'];
3840
private isDownloading = false;
3941
private downloadPromise: Promise<LanguageModel | void> | undefined;
4042
private oldSession: LanguageModel | undefined;
@@ -142,6 +144,18 @@ export class ChromeAdapter {
142144
if (content.role !== 'user') {
143145
return false;
144146
}
147+
148+
// Returns false if request contains an image with an unsupported mime type.
149+
for (const part of content.parts) {
150+
if (
151+
part.inlineData &&
152+
ChromeAdapter.SUPPORTED_MIME_TYPES.indexOf(
153+
part.inlineData.mimeType
154+
) === -1
155+
) {
156+
return false;
157+
}
158+
}
145159
}
146160

147161
return true;

0 commit comments

Comments
 (0)