wavetermdev/waveterm · error

invalid data URL format

Error message

invalid data URL format

What it means

For image/* media types, a data: URL must contain a comma separating the "data:mediatype;base64" header from the payload; SplitN(URL, ",", 2) must yield 2 pieces. A data URL without a comma is malformed, so conversion fails with 'invalid data URL format' while building the Anthropic image block.

Source

Thrown at pkg/aiusechat/anthropic/anthropic-convertmessage.go:378

		return nil, errors.New("file part missing mediaType")
	}

	// Validate URL protocol - only allow data:, http:, https:
	if !strings.HasPrefix(p.URL, "data:") &&
		!strings.HasPrefix(p.URL, "http://") &&
		!strings.HasPrefix(p.URL, "https://") {
		return nil, fmt.Errorf("unsupported URL protocol in file part: %s", p.URL)
	}

	// Branch on mediaType first to determine block type and constraints
	switch {
	case strings.HasPrefix(p.MediaType, "image/"):
		// image/* (jpeg, png, gif, webp) → Anthropic image block
		if strings.HasPrefix(p.URL, "data:") {
			// Data URL → base64 source
			parts := strings.SplitN(p.URL, ",", 2)
			if len(parts) != 2 {
				return nil, errors.New("invalid data URL format")
			}
			return &anthropicMessageContentBlock{
				Type: "image",
				Source: &anthropicSource{
					Type:      "base64",
					Data:      parts[1],
					MediaType: p.MediaType,
				},
			}, nil
		} else {
			// HTTP/HTTPS URL → url source (no media_type for image URLs)
			return &anthropicMessageContentBlock{
				Type: "image",
				Source: &anthropicSource{
					Type: "url",
					URL:  p.URL,
				},
			}, nil

View on GitHub (pinned to a4447c1563)

Solutions

  1. Build the data URL as "data:<mediatype>;base64,<payload>" including the comma.
  2. Validate with a regex like ^data:[^,]+,[\s\S]+$ before sending.
  3. If you only have raw bytes, base64-encode them and construct the full data URL client-side.

Example fix

// before
url := "data:image/png;base64" // missing comma + data

// after
url := "data:image/png;base64," + base64.StdEncoding.EncodeToString(pngBytes)
Defensive patterns

Strategy: validation

Validate before calling

var dataURLRe = regexp.MustCompile(`^data:[^,]+,[\s\S]+$`)
if strings.HasPrefix(p.URL, "data:") && !dataURLRe.MatchString(p.URL) {
    return fmt.Errorf("malformed data URL for image part")
}

Type guard

func isWellFormedImageDataURL(url string) bool {
    return strings.HasPrefix(url, "data:image/") && strings.Contains(url, ",")
}

Try / catch

block, err := convertFileUIMessagePart(part)
if err != nil && strings.Contains(err.Error(), "invalid data URL format") {
    return fmt.Errorf("re-encode the image as data:<type>;base64,<data>: %w", err)
}

Prevention

When it happens

Trigger: Passing a file part with MediaType like "image/png" and URL starting with "data:" but missing the comma — e.g. "data:image/png" or "data:image/png;base64" with no ",<data>" suffix.

Common situations: String-concatenation bugs building data URLs (forgot the comma); truncated URLs from clipboard/DB storage; URL-encoding or a middleware stripping part of the data URL.

Related errors


AI-assisted analysis of wavetermdev/waveterm@a4447c1563 (2026-09-01). Data as JSON: /api/errors/7e7ec959f7954ec6. Report an issue: GitHub.