Non-base64 data URLs in file parts throw InvalidCharacterError or send the wrong bytes
Description
File and image parts accept a data URL, but the SDK assumes every data URL is base64. A data URL without ;base64 (valid under RFC 2397, and common for SVGs and small text files) either throws a raw InvalidCharacterError from atob, or is sent to the provider as if its text were base64, so the model receives different bytes with no error.
| File part data | Result |
|---|---|
new URL('data:text/plain;base64,SGVsbG8sIHdvcmxkIQ==') |
works ("Hello, world!") |
new URL('data:text/plain,Hello%2C%20world!') |
throws InvalidCharacterError: Invalid character |
new URL('data:text/csv,a,b,c%0A1,2,3') |
throws InvalidCharacterError |
new URL('data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%2F%3E') |
throws InvalidCharacterError |
new URL('data:text/plain,abcd') |
no error, but the provider receives { type: 'data', data: 'abcd' }, which is decoded as base64 to the bytes 0x69 0xb7 0x1d instead of "abcd" |
Expected: the content is percent-decoded and the model receives the actual bytes ("Hello, world!", the CSV, the SVG, "abcd"), the same as for the ;base64 form. If non-base64 data URLs are not meant to be supported, a clear InvalidDataContentError would be better than a raw DOMException or silently wrong data.
Cause: packages/ai/src/prompt/split-data-url.ts:
const [header, base64Content] = dataUrl.split(',');
return { mediaType: header.split(';')[0].split(':')[1], base64Content };
It never checks the header for ;base64, and splitting on every comma drops anything after a second comma (the CSV case). convertUrlToFilePartData (prompt/file-part-data.ts) then passes the content on as { type: 'data', data: base64Content }, and convertBase64ToUint8Array calls atob on it. The same helper is used by generateImage and generateVideo, and useChat file parts with such URLs go through the same path via convertToModelMessages.
Possible fix: split at the first comma only; if the header ends with ;base64, keep the current behaviour; otherwise percent-decode the content to bytes and pass those on.
Reproduction
import { generateText } from 'ai';
import { MockLanguageModelV4 } from 'ai/test';
let prompt: any;
const model = new MockLanguageModelV4({
async doGenerate(options) {
prompt = options.prompt;
return {
content: [{ type: 'text', text: 'ok' }],
finishReason: { unified: 'stop', raw: 'stop' },
usage: {
inputTokens: { total: 1, noCache: 1, cacheRead: 0, cacheWrite: 0 },
outputTokens: { total: 1, text: 1, reasoning: 0 },
},
warnings: [],
};
},
});
await generateText({
model,
messages: [
{
role: 'user',
content: [
{ type: 'file', mediaType: 'text/plain', data: new URL('data:text/plain,Hello%2C%20world!') },
],
},
],
});
// -> throws DOMException [InvalidCharacterError]: Invalid character (from atob)
// With data: new URL('data:text/plain,abcd') there is no error, but
// prompt[0].content[0].data is { type: 'data', data: 'abcd' }, which is
// treated as base64 and decodes to different bytes.
AI SDK Version
- ai: 7.0.152 (main @ e729d58)
Code of Conduct
- [x] I agree to follow this project's Code of Conduct
Identify issue type
Identified as Bug
Confidence: High
Rationale
This is a concrete failure in existing data URL handling: accepted file/image input either throws unexpectedly or is corrupted instead of preserving its actual bytes.
Evidence
- Valid non-base64 RFC 2397 data URLs cause a raw
InvalidCharacterErrorduring file-part processing. - Some payloads such as
data:text/plain,abcdare silently treated as base64, causing providers to receive incorrect bytes. - The report includes a reproducible
generateTextexample and identifies parsing that assumes;base64and splits incorrectly on commas.
Identify bug discovery
Discovery source: discovery:audit (medium confidence).
The report appears to originate from targeted source-code inspection and edge-case testing rather than a failure in a real production application. It traces the defect through specific internal helpers and tests it with a mock model, though the author does not explicitly state how they first discovered it.
- The description identifies the faulty implementation in
packages/ai/src/prompt/split-data-url.tsand explains both parsing defects. - It traces the affected data through
convertUrlToFilePartData,convertBase64ToUint8Array, and related image, video, and chat paths. - The reproduction uses
MockLanguageModelV4to inspect SDK behavior rather than describing an actual deployed application. - The report systematically tests several RFC 2397 edge cases against a specific main-branch commit.
I'd like to work on this.
Plan: detect base64 vs non-base64 data URLs per RFC 2397; percent-decode the non-base64 form so providers get the real bytes; keep existing base64 path unchanged; add tests for the cases in the table (plain text, CSV, SVG, and the silent "abcd" corruption case).
Will open a PR referencing #23943.