mastra-ai/mastra · error · MastraError

INVALID_DATA_URL_FORMAT

INVALID_DATA_URL_FORMAT

Error message

Invalid data URL format in content ${content.toString()}

What it means

When converting AI SDK v5 content parts to Mastra's format, a data: URL must contain both a media type and base64 content. splitDataUrl returned null for one of these, meaning the URL is a malformed data URL (e.g. 'data:' with no media type or no comma-separated payload). MastraError with id INVALID_DATA_URL_FORMAT is thrown as a USER-category error.

Source

Thrown at packages/core/src/stream/aisdk/v5/compat/content.ts:72

    return { data: new Uint8Array(content), mediaType: undefined };
  }

  // Attempt to create a URL from the data. If it fails, we can assume the data
  // is not a URL and likely some other sort of data.
  if (typeof content === 'string') {
    try {
      content = new URL(content);
    } catch {
      // ignored
    }
  }

  // Extract data from data URL:
  if (content instanceof URL && content.protocol === 'data:') {
    const { mediaType: dataUrlMediaType, base64Content } = splitDataUrl(content.toString());

    if (dataUrlMediaType == null || base64Content == null) {
      throw new MastraError({
        id: 'INVALID_DATA_URL_FORMAT',
        text: `Invalid data URL format in content ${content.toString()}`,
        domain: ErrorDomain.LLM,
        category: ErrorCategory.USER,
      });
    }

    return { data: base64Content, mediaType: dataUrlMediaType };
  }

  return { data: content, mediaType: undefined };
}

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Format the data URL fully: data:<mimeType>;base64,<base64Payload> (e.g. 'data:image/png;base64,iVBORw0...')
  2. Use Buffer.from(bytes).toString('base64') and prefix the header, instead of string concatenation with the raw bytes
  3. Validate with a regex before sending: /^data:[\w.+-]+\/[\w.+-]+;base64,[A-Za-z0-9+/=]+$/
  4. If the source is a remote file, pass an https:// URL or Blob instead of a data URL

Example fix

// before
new URL(`data:${mimeType},${base64}`);
// after
new URL(`data:${mimeType};base64,${base64}`);
Defensive patterns

Strategy: validation

Validate before calling

const DATA_URL = /^data:[\w.+-]+\/[\w.+-]+(;base64)?,[\s\S]+$/;
function assertValidDataUrl(url: URL) {
  if (url.protocol === 'data:' && !DATA_URL.test(url.toString())) {
    throw new Error(`Malformed data URL: ${url}`);
  }
}

Type guard

function isWellFormedDataUrl(u: unknown): u is URL & { toString(): `data:${string};base64,${string}` } {
  return u instanceof URL && /^data:[^,]+,[\s\S]+$/.test(u.toString());
}

Try / catch

try {
  return await agent.stream({ messages: withMedia(dataUrl) });
} catch (e) {
  if (e instanceof MastraError && e.id === 'INVALID_DATA_URL_FORMAT') {
    logger.error('Fix data URL format: data:<mime>;base64,<payload>', { url: dataUrl });
  }
  throw e;
}

Prevention

When it happens

Trigger: Passing new URL('data:image/png') or 'data:base64,xxx' (missing '/' in mime) or 'data:text/plain,' (empty payload) as a URL-backed media part in a message to an agent/stream.

Common situations: Hand-building data URLs with template strings and forgetting the comma or mime type; encoding empty buffers; receiving truncated data URLs from external systems.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/8e2f608022ce2e9a. Report an issue: GitHub.