Closed steveoon closed 3 months ago
After analyzing error log, here are some findings:
Error Type:
AI_InvalidDataContentError: Invalid data content. Content string is not a base64-encoded media.
This indicates that the system expects to receive base64-encoded media content, but the actual content received doesn't match this format.
Error Location: The error stack shows that this issue occurs within the internal processing of the AI library, specifically involving Next.js route modules and Edge Functions.
Root Cause:
The deeper cause of the error is a TypeError
suggesting an "Illegal invocation". This typically means a function was called with an incorrect context (this reference).
Environment: The error is happening in a Cloudflare Workers environment, as evident from the URL link in the error message.
Data Content: The error message ends with a long string that appears to be base64 encoded. This might be the actual content causing the error.
Could be a variant of this: https://github.com/vercel/ai/issues/1333
@steveoOn I suspect that we are using some function that is not available in the cloudflare workers environment, thus the type error. could you identify which call the type error happens on?
this is the problematic instruction:
It might be the invocation with this.
@steveoOn can you try out the following 2 code pieces on cloudflare workers if you get a chance?
console.log(globalThis.atob("dGVzdA=="));
const atob = globalThis.atob;
console.log(atob("dGVzdA=="));
I suspect cloudflare does some extra checks for the this context. Thus the first code block would throw an error, and the 2nd would succeed.
@lgrammel I've tried this on Cloudflare Worker, and shows all passed.
@steveoOn thanks. this is strange because the stack trace
cause: TypeError: Illegal invocation: function called with incorrect `this` reference. See https://developers.cloudflare.com/workers/observability/errors/#illegal-invocation-errors for details.
at (node_modules/.pnpm/@ai-sdk+provider-utils@1.0.9_zod@3.23.6/node_modules/@ai-sdk/provider-utils/dist/index.mjs:509:0)
points to exactly this location
double checked the location by going up the stack to the 2nd line of the cause (742):
Therefore we have a confirmed location of the issue, but we were unable to reproduce it. The only difference that I'm aware of is that we use different data in the test.
@steveoOn can you use the data that caused the failure in the test that you just ran? also, are there any other differences between the worker env where the issue occurred and your test env (versions, settings, etc)?
Therefore we have a confirmed location of the issue, but we were unable to reproduce it. The only difference that I'm aware of is that we use different data in the test.
@steveoOn can you use the data that caused the failure in the test that you just ran? also, are there any other differences between the worker env where the issue occurred and your test env (versions, settings, etc)?
@lgrammel I've deployed a testing Demo on Vercel: https://multi-modal-chatbot-demo.vercel.app
This is the Demo Github Repository: https://github.com/steveoOn/multi-modal-chatbot-demo.git
The testing Data:
[
{
"id": "1",
"role": "user",
"content": "describe image",
"experimental_attachments": [
{
"name": "iShot_2024-01-14_11.45.50.png",
"contentType": "image/png",
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcIAAAHCCAYAAAB8GMlFAAAZEElEQVR4nO3d3XoaSbIFUHy+ef9X1rmY1thtISkRWeSOiLUubTp/qypIA7t/vb29vd0AYKj/Oz0AADhJIQRgNIUQgNEUQgBGUwgBGE0hBGA0hRCA0RRCAEZTCAEYTSEEYDSFEIDRFEIARlMIARhNIQRgNIUQgNEUQgBGUwgBGE0hBGA0hRCA0RRCAEZTCAEYTSEEYDSFEIDRFEIARlMIARhNIQRgNIUQgNEUQgBGUwgBGE0hBGA0hRCA0RRCAEZTCAEYTSEEYDSFEIDRFEIARlMIARhNIQRgNIUQgNH+c3oAz/j169fpIVzq7e3t9BCATTyvcv16Kzj67hfU33Zt0d/r9my7q+2l97t7fLt1mceqLvN453mVr1QhnHZB/e2Zrfps7X7a5mp76f3uHt9uXeaxqss8bp5XpfaszGeE0y+q2xNr8NV/95M2V9tL73f3+HbrMo9VXeZxKzjeK1RagzKFEACuUKIQVnpncTVrAdnco79VWYsShRAArhJfCKu8o3ilR9fkqw+tf/KB9mp76f3uHt9uXeaxqsM8PK8+qrAm8YWQPe49SJ55uKy2l97v7vHt1mUeq7rMg1rifz5R4d3ECeHbBiN5Xt2X/rwqnSyzKm0T3CzAZzyvXq91IUy7oN69j2vCBfaILkk16f3u1iXp5/Q6p+3ruwnPq5afEb69vcVeVH+qMs5XuHeTPfNn+n2N3fM4Nd+T61zlOVBlnD/RrhBW3KiKY96pS1JNer+7dUn6ObnOFe/9imP+TrtCCACPaFUIK79TqTx24HGV7/nKY7+nVSEEgEe1KYQd3qF0mMNPdEmqSe93ty5JPyf6Tdi/Z3WYw7s2hZDauiTVpPe7W5ekn/R15lptkmXCp7Fs2nyhk2n3b5f5tv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UwgBGE0hBGA0hRCA0RRCAEZTCAEY7f8B76TmAmbOw/YAAAAASUVORK5CYII="
}
]
}
]
The result:
It also got the same Error when I called this DEMO API hosting online from my local env in another project.
The log form API hosting demo:
Error details: AI_InvalidDataContentError: Invalid data content. Content string is not a base64-encoded media.
at (node_modules/.pnpm/ai@3.3.5_react@18.3.1_sswr@2.1.0_svelte@4.2.18__svelte@4.2.18_vue@3.4.37_typescript@5.5.4__zod@3.23.8/node_modules/ai/dist/index.mjs:744:0)
at (node_modules/.pnpm/ai@3.3.5_react@18.3.1_sswr@2.1.0_svelte@4.2.18__svelte@4.2.18_vue@3.4.37_typescript@5.5.4__zod@3.23.8/node_modules/ai/dist/index.mjs:3288:0)
at (node_modules/.pnpm/ai@3.3.5_react@18.3.1_sswr@2.1.0_svelte@4.2.18__svelte@4.2.18_vue@3.4.37_typescript@5.5.4__zod@3.23.8/node_modules/ai/dist/index.mjs:3330:0)
at (lib/utils/index.ts:147:33)
at (app/api/chat/route.ts:12:55)
at (node_modules/.pnpm/next@14.2.5_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/esm/server/future/route-modules/app-route/module.js:195:0)
at (node_modules/.pnpm/next@14.2.5_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/esm/server/future/route-modules/app-route/module.js:124:0)
at (node_modules/.pnpm/next@14.2.5_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/esm/server/future/route-modules/app-route/module.js:257:0)
at (node_modules/.pnpm/next@14.2.5_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/esm/server/web/edge-route-module-wrapper.js:81:0)
at (node_modules/.pnpm/next@14.2.5_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/esm/server/web/adapter.js:158:0) {
name: 'AI_InvalidDataContentError',
cause: TypeError: Illegal invocation: function called with incorrect `this` reference. See https://developers.cloudflare.com/workers/observability/errors/#illegal-invocation-errors for details.
at (node_modules/.pnpm/@ai-sdk+provider-utils@1.0.9_zod@3.23.8/node_modules/@ai-sdk/provider-utils/dist/index.mjs:509:0)
at (node_modules/.pnpm/ai@3.3.5_react@18.3.1_sswr@2.1.0_svelte@4.2.18__svelte@4.2.18_vue@3.4.37_typescript@5.5.4__zod@3.23.8/node_modules/ai/dist/index.mjs:742:38)
at (node_modules/.pnpm/ai@3.3.5_react@18.3.1_sswr@2.1.0_svelte@4.2.18__svelte@4.2.18_vue@3.4.37_typescript@5.5.4__zod@3.23.8/node_modules/ai/dist/index.mjs:3288:0)
at (node_modules/.pnpm/ai@3.3.5_react@18.3.1_sswr@2.1.0_svelte@4.2.18__svelte@4.2.18_vue@3.4.37_typescript@5.5.4__zod@3.23.8/node_modules/ai/dist/index.mjs:3330:0)
at (lib/utils/index.ts:147:33)
at (app/api/chat/route.ts:12:55)
at (node_modules/.pnpm/next@14.2.5_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/esm/server/future/route-modules/app-route/module.js:195:0)
at (node_modules/.pnpm/next@14.2.5_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/esm/server/future/route-modules/app-route/module.js:124:0)
at (node_modules/.pnpm/next@14.2.5_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/esm/server/future/route-modules/app-route/module.js:257:0)
at (node_modules/.pnpm/next@14.2.5_@opentelemetry+api@1.9.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/esm/server/web/edge-route-module-wrapper.js:81:0),
content: 'iVBORw0KGgoAAAANSUhEUgAABYQAAALwCAIAAACoXeZMAAAAAXNSR0IArs4c6QAAIABJREFUeJzs3XFYVPedL/7v1tnHTNZIpnMlZFIaqme1RUppwHHMwnKNkM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0hyLb3DlypWcnJyVK1fysEIwGOzq6nrrrbcYY93d3Yyx9PT00D0wxnw+39WrV9WPKorS29s7NjbW3d2dkpKyYsWKtLQ0TSqH+ul0aowxGlOgycKQJKm/v39gYGDdunVh99PT0/Phhx8ODAwYDIa0tDT1WYTl9/sHBgbS09MTExMHBwc7OzsTEhJ4I2VZvnDhwqlTp3JycnJzc9XxIDq1K1euSJJEp6aOAvBLsWDBguzsbPWz6LwEQVBvHBaNwamurq6rq1Ovl2WZAkOVlZW0Rq/X19bWpqSkuFyus2fP8j0HAgHKZLmjARqSJNH+8/LyxnulAOLNrVu36uvrL168GOuG3OMuXrzocDhqamqWLl0a67YAwLhu3rzZ0NCAj8SZ1tvb29vbW19fv2jRoli3BQDuBXHX9bLZbG63u7OzUx2MaG1tZYxVVVWJoqgZxMHjFOpu5MjIyIsvvsgzDtxutyAIzc3NvHft9/uLior49qIoMsaMRiMtqG/Ri6IoiqLT6QwbjDh27Jj6UUmSKioqeIVOxpjL5TIajSdPngzby6Wn0zIdUR2MOHTokMvl4s0wGo179+7ll0VRlF27dvGnc01NTepLpzEwMOBwOGw22+XLl/n1cblcNptt06ZNNBSCX5CGhgYeWVAUpbS0VJ0tQm1rb2+nc09MTCwvL1evYYz19PQ4HA6j0XjixInxmkRaWlq8Xq/NZsvJydEEI9555x0
It seems to point to the same reason:
AI_InvalidDataContentError
: This is a custom error type indicating that the data content being processed is invalid. Specifically, the log mentions that the content string is not a base64-encoded media.index.mjs
: The error originates from the index.mjs
file located within the node_modules
directory. This file likely belongs to a third-party library, potentially one used for AI model processing.cause: TypeError: Illegal invocation
: The direct cause of the error is an illegal invocation, meaning a function was called with an incorrect reference or in an improper context. This typically happens when a function is invoked with an incorrect this
reference, possibly related to the execution environment, such as Cloudflare Workers.ai/dist/index.mjs
file and then propagates through several other files. It eventually affects the API route in lib/utils/index.ts
and app/api/chat/route.ts
, indicating that the problem ultimately impacts the API routing.This is complete Cloudflare worker testing log:
{
"truncated": false,
"outcome": "ok",
"scriptVersion": {
"id": "40c59538-4294-41a5-a300-e048b1c2f82b"
},
"scriptName": "polished-math-96b3",
"diagnosticsChannelEvents": [],
"exceptions": [],
"logs": [
{
"message": [
"Testing global atob:"
],
"level": "log",
"timestamp": 1723384322695
},
{
"message": [
"test"
],
"level": "log",
"timestamp": 1723384322695
},
{
"message": [
"Testing local atob:"
],
"level": "log",
"timestamp": 1723384322695
},
{
"message": [
"test"
],
"level": "log",
"timestamp": 1723384322695
},
{
"message": [
"Testing convertBase64ToUint8Array:"
],
"level": "log",
"timestamp": 1723384322695
},
{
"message": [
"116,101,115,116"
],
"level": "log",
"timestamp": 1723384322695
}
],
"eventTimestamp": 1723384322695,
"event": {
"request": {
"url": "https://polished-math-96b3.rsw87621.workers.dev/favicon.ico",
"method": "GET",
"headers": {
"accept": "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8",
"accept-encoding": "gzip, br",
"accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,ru;q=0.6,zh-TW;q=0.5",
"cf-connecting-ip": "xxx.xxx.xxx.xxx",
"cf-ipcountry": "US",
"cf-ray": "xxx",
"cf-visitor": "{\"scheme\":\"https\"}",
"connection": "Keep-Alive",
"cookie": "xxx",
"host": "polished-math-96b3.rsw87621.workers.dev",
"priority": "u=1, i",
"referer": "https://polished-math-96b3.rsw87621.workers.dev/",
"sec-ch-ua": "\"Not)A;Brand\";v=\"99\", \"Google Chrome\";v=\"127\", \"Chromium\";v=\"127\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"macOS\"",
"sec-fetch-dest": "image",
"sec-fetch-mode": "no-cors",
"sec-fetch-site": "same-origin",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36",
"x-forwarded-proto": "https",
"x-real-ip": "xxx.xxx.xxx.xxx"
},
"cf": {
"clientTcpRtt": 1,
"longitude": "-97.82200",
"httpProtocol": "HTTP/2",
"tlsCipher": "AEAD-AES128-GCM-SHA256",
"continent": "NA",
"asn": 21859,
"clientAcceptEncoding": "gzip, deflate, br, zstd",
"country": "US",
"tlsClientAuth": {
"certIssuerDNLegacy": "",
"certIssuerSKI": "",
"certSubjectDNRFC2253": "",
"certSubjectDNLegacy": "",
"certFingerprintSHA256": "",
"certNotBefore": "",
"certSKI": "",
"certSerial": "",
"certIssuerDN": "",
"certVerified": "NONE",
"certNotAfter": "",
"certSubjectDN": "",
"certPresented": "0",
"certRevoked": "0",
"certIssuerSerial": "",
"certIssuerDNRFC2253": "",
"certFingerprintSHA1": ""
},
"tlsExportedAuthenticator": {
"clientFinished": "xxx",
"clientHandshake": "xxx",
"serverHandshake": "xxx",
"serverFinished": "xxx"
},
"tlsVersion": "TLSv1.3",
"colo": "LAX",
"timezone": "America/Chicago",
"verifiedBotCategory": "",
"edgeRequestKeepAliveStatus": 1,
"tlsClientRandom": "xxx",
"tlsClientExtensionsSha1": "xxx",
"tlsClientHelloLength": "2075",
"asOrganization": "Zenlayer",
"requestPriority": "weight=220;exclusive=1",
"latitude": "37.75100"
}
},
"response": {
"status": 200
}
},
"id": 1
}
Hi,@lgrammel
I try to copy convertToCoreMessages
logic and test if this issue occurs inside the Function:
function base64ToUint8Array(base64: string): Uint8Array {
try {
const parts = base64.split(',');
if (parts.length !== 2) {
throw new Error('Invalid base64 string format');
}
const binaryString = atob(parts[1]);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes;
} catch (error) {
console.error('Error in base64ToUint8Array:', error);
throw new Error(
`Failed to convert base64 to Uint8Array: ${(error as Error).message}`
);
}
}
export function copyConvertToCoreMessages(
messages: InputMessages
): CoreMessage[] {
if (!Array.isArray(messages)) {
throw new TypeError('Input must be an array of messages');
}
return messages.map((message, index): CoreMessage => {
try {
if (typeof message !== 'object' || message === null) {
throw new TypeError(
`Invalid message at index ${index}: message must be an object`
);
}
if (!('role' in message) || typeof message.role !== 'string') {
throw new TypeError(
`Invalid message at index ${index}: message must have a 'role' property of type string`
);
}
switch (message.role) {
case 'system':
case 'assistant':
return {
role: message.role,
content: message.content ?? '',
};
case 'user':
if (
message.experimental_attachments &&
Array.isArray(message.experimental_attachments) &&
message.experimental_attachments.length > 0
) {
const content: Array<TextPart | ImagePart> = [
{ type: 'text', text: message.content ?? '' },
];
message.experimental_attachments.forEach(
(attachment: Attachment, attachmentIndex: number) => {
if (attachment?.contentType?.startsWith('image/')) {
try {
content.push({
type: 'image',
image: base64ToUint8Array(attachment.url),
mimeType: attachment.contentType,
});
} catch (error) {
console.error(
`Error processing attachment ${attachmentIndex} in message ${index}:`,
error
);
// Optionally, you can choose to skip this attachment or throw an error
}
}
}
);
return {
role: 'user',
content: content,
};
} else {
return {
role: 'user',
content: message.content ?? '',
};
}
default:
throw new Error(`Unsupported message role: ${message.role}`);
}
} catch (error) {
console.error(`Error processing message at index ${index}:`, error);
throw new Error(
`Failed to convert message at index ${index}: ${
(error as Error).message
}`
);
}
});
}
To run this code all works fine on Local env. but Edge Runtime fail with those Error logs:
Error in aiChat: TypeError: Illegal invocation: function called with incorrect `this` reference. See https://developers.cloudflare.com/workers/observability/errors/#illegal-invocation-errors for details.
at (node_modules/.pnpm/@ai-sdk+provider-utils@1.0.9_zod@3.23.6/node_modules/@ai-sdk/provider-utils/dist/index.mjs:517:0)
at (node_modules/.pnpm/@ai-sdk+openai@0.0.44_zod@3.23.6/node_modules/@ai-sdk/openai/dist/index.mjs:52:169)
at (node_modules/.pnpm/@ai-sdk+openai@0.0.44_zod@3.23.6/node_modules/@ai-sdk/openai/dist/index.mjs:42:0)
at (node_modules/.pnpm/@ai-sdk+openai@0.0.44_zod@3.23.6/node_modules/@ai-sdk/openai/dist/index.mjs:257:0)
at (node_modules/.pnpm/@ai-sdk+openai@0.0.44_zod@3.23.6/node_modules/@ai-sdk/openai/dist/index.mjs:383:29)
at (node_modules/.pnpm/ai@3.3.4_openai@4.42.0_react@18.3.1_sswr@2.1.0_svelte@4.2.18__svelte@4.2.18_vue@3.4.36_typescript@5.3.3__zod@3.23.6/node_modules/ai/dist/index.mjs:2870:0)
at (node_modules/.pnpm/ai@3.3.4_openai@4.42.0_react@18.3.1_sswr@2.1.0_svelte@4.2.18__svelte@4.2.18_vue@3.4.36_typescript@5.3.3__zod@3.23.6/node_modules/ai/dist/index.mjs:251:0)
at (node_modules/.pnpm/ai@3.3.4_openai@4.42.0_react@18.3.1_sswr@2.1.0_svelte@4.2.18__svelte@4.2.18_vue@3.4.36_typescript@5.3.3__zod@3.23.6/node_modules/ai/dist/index.mjs:180:0)
at (node_modules/.pnpm/ai@3.3.4_openai@4.42.0_react@18.3.1_sswr@2.1.0_svelte@4.2.18__svelte@4.2.18_vue@3.4.36_typescript@5.3.3__zod@3.23.6/node_modules/ai/dist/index.mjs:249:0)
Even try to not use atob
also did not work on Edge:
function base64ToUint8Array(base64: string): Uint8Array {
const base64Chars =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
function decode(encoded: string): string {
let decoded = '';
encoded = encoded.replace(/[^A-Za-z0-9+/]/g, '');
for (let i = 0; i < encoded.length; i += 4) {
const v =
(base64Chars.indexOf(encoded[i]) << 18) |
(base64Chars.indexOf(encoded[i + 1]) << 12) |
(base64Chars.indexOf(encoded[i + 2]) << 6) |
base64Chars.indexOf(encoded[i + 3]);
decoded += String.fromCharCode((v >> 16) & 255, (v >> 8) & 255, v & 255);
}
return decoded;
}
try {
const parts = base64.split(',');
if (parts.length !== 2) {
throw new Error('Invalid base64 string format');
}
const binaryString = decode(parts[1]);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes;
} catch (error) {
console.error('Error in base64ToUint8Array:', error);
throw new Error(
`Failed to convert base64 to Uint8Array: ${(error as Error).message}`
);
}
}
and got same Error logs:
Error in aiChat: TypeError: Illegal invocation: function called with incorrect `this` reference. See https://developers.cloudflare.com/workers/observability/errors/#illegal-invocation-errors for details.
at (node_modules/.pnpm/@ai-sdk+provider-utils@1.0.9_zod@3.23.6/node_modules/@ai-sdk/provider-utils/dist/index.mjs:517:0)
at (node_modules/.pnpm/@ai-sdk+anthropic@0.0.39_zod@3.23.6/node_modules/@ai-sdk/anthropic/dist/index.mjs:76:55)
at (node_modules/.pnpm/@ai-sdk+anthropic@0.0.39_zod@3.23.6/node_modules/@ai-sdk/anthropic/dist/index.mjs:263:0)
at (node_modules/.pnpm/@ai-sdk+anthropic@0.0.39_zod@3.23.6/node_modules/@ai-sdk/anthropic/dist/index.mjs:356:29)
at (node_modules/.pnpm/ai@3.3.4_openai@4.42.0_react@18.3.1_sswr@2.1.0_svelte@4.2.18__svelte@4.2.18_vue@3.4.36_typescript@5.3.3__zod@3.23.6/node_modules/ai/dist/index.mjs:2870:0)
at (node_modules/.pnpm/ai@3.3.4_openai@4.42.0_react@18.3.1_sswr@2.1.0_svelte@4.2.18__svelte@4.2.18_vue@3.4.36_typescript@5.3.3__zod@3.23.6/node_modules/ai/dist/index.mjs:251:0)
at (node_modules/.pnpm/ai@3.3.4_openai@4.42.0_react@18.3.1_sswr@2.1.0_svelte@4.2.18__svelte@4.2.18_vue@3.4.36_typescript@5.3.3__zod@3.23.6/node_modules/ai/dist/index.mjs:180:0)
at (node_modules/.pnpm/ai@3.3.4_openai@4.42.0_react@18.3.1_sswr@2.1.0_svelte@4.2.18__svelte@4.2.18_vue@3.4.36_typescript@5.3.3__zod@3.23.6/node_modules/ai/dist/index.mjs:249:0)
at (node_modules/.pnpm/ai@3.3.4_openai@4.42.0_react@18.3.1_sswr@2.1.0_svelte@4.2.18__svelte@4.2.18_vue@3.4.36_typescript@5.3.3__zod@3.23.6/node_modules/ai/dist/index.mjs:2842:0)
at (node_modules/.pnpm/ai@3.3.4_openai@4.42.0_react@18.3.1_sswr@2.1.0_svelte@4.2.18__svelte@4.2.18_vue@3.4.36_typescript@5.3.3__zod@3.23.6/node_modules/ai/dist/index.mjs:83:0)
I suspect that some code in @ai-sdk+provider-utils
causes this issue?
@steveoOn
"url": "data:image/jpeg;base64,/9j/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnK..."
This is not a valid image base64 (...
at end). Where the 3 dots in the data that you used or did you just remove it in this issue report?
@steveoOn
"url": "data:image/jpeg;base64,/9j/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnK..."
This is not a valid image base64 (
...
at end). Where the 3 dots in the data that you used or did you just remove it in this issue report? @lgrammel[ { "id": "1", "role": "user", "content": "1+4=?" }, { "id": "2", "role": "assistant", "content": "1 + 4 equals 5." }, { "id": "3", "role": "user", "content": "能看到图片吗", "experimental_attachments": [ { "name": "IMG_6957.jpg", "contentType": "image/jpeg", "url": "data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/wcAAgEBAUKFVv8AAAAASUVORK5CYII=" } ] } ]
Try this test data, remove the
...
at end.
The base64 image data not include ...
at the end while it transfers through my code context.
you can also try this with the API endpoint.
curl -X POST https://multi-modal-chatbot-demo.vercel.app/api/chat \
-H "Content-Type: application/json" \
-d '{
"messages": [
{
"id": "1",
"role": "user",
"content": "1+4=?"
},
{
"id": "2",
"role": "assistant",
"content": "1 + 4 equals 5."
},
{
"id": "3",
"role": "user",
"content": "能看到图片吗",
"experimental_attachments": [
{
"name": "IMG_6957.jpg",
"contentType": "image/jpeg",
"url": "data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/wcAAgEBAUKFVv8AAAAASUVORK5CYII="
}
]
}
]
}'
@lgrammel
Just reproduced and verified myself. My initial assessment was correct. The bug is indeed in globalThis.atob
Proposed fix: https://github.com/vercel/ai/pull/2635
Just reproduced and verified myself. My initial assessment was correct. The bug is indeed in
globalThis.atob
I'm curious why I didn't catch this issue when testing on Cloudflare Worker before, it's weird 🤔
@steveoOn please try with the latest versions of the ai sdk libs (just released)
(my reproduction / fix was for a vercel edge deployment fwiw)
The ai@3.3.6
test passed, and Edge Runtime works as expected.🎉🎉
I upgraded to the latest "3.4.9" version and still get this error in vercel edge environment. An error occurs when processing images, but there is no problem with text input.
{"stack":"Error: Illegal invocation: function called with incorrect `this` reference. See https://developers.cloudflare.com/workers/observability/errors/#illegal-invocation-errors for details.\n at (vc/edge/function:647:562)\n at (vc/edge/function:647:1430)","message":"Illegal invocation: function called with incorrect `this` reference. See https://developers.cloudflare.com/workers/observability/errors/#illegal-invocation-errors for details."}
This is the source code:
Description
When using the AI SDK in a Vercel Edge Runtime environment, there's an issue with handling image attachments in the
convertToCoreMessages()
function. This function, provided by the SDK, appears to fail when processing base64-encoded image data in the Edge Runtime, whereas it works correctly in local development and Node.js Runtime environments.This issue occurs specifically when:
The error message received in the Edge Runtime is: "Error converting messages to core messages: Invalid data content. Content string is not a base64-encoded media."
Importantly, this issue does not occur when using Node.js Runtime on Vercel or during local development.
Key Observations:
In local development,
convertToCoreMessages()
successfully processes the image data and converts it to a Uint8Array:This log is from the local development environment in an API route, printed after image uploading.
In the Vercel Edge Runtime, the function seems to throw an error before reaching this point, as we don't see this log output.
The base64 data in
experimental_attachments
before the conversion appears correct.The error message suggests that the
data:image/webp;base64,
prefix is missing from the base64 string when the error occurs.Steps to reproduce:
Code example
Additional context
Additional context:
data:image/webp;base64,
prefix in the error message might indicate that the Edge Runtime is stripping this information or handling the data differently before it reaches the conversion function.We would appreciate any insights into why this conversion is failing specifically in the Edge Runtime and how we can ensure consistent behavior across different environments. Additionally, any guidance on how to properly handle or preprocess the base64 data to make it compatible with the Edge Runtime would be highly valuable.
The logs
Original Messages
package.json