Skip to content

V2 media normalization double-prefixes uppercase DATA: URLs #47538

Closed

Description

@sudoSwami

Description

While testing V2's OpenAI Chat request compilation, I found that an uppercase DATA: URL gets a second data-URL prefix:

Input:  DATA:image/png;base64,AAECAw==
Output: data:image/png;base64,DATA:image/png;base64,AAECAw==

Lowercase data: and raw base64 both work. Existing data URLs should be recognized regardless of scheme casing, as described in RFC 3986 §3.1.

The case-sensitive part.data.startsWith("data:") check in packages/ai/src/protocols/shared.ts sends the uppercase URL down the raw-base64 path.

I can contribute a focused fix and regression tests if this is not already being addressed.

Plugins

None; reproduced in the provider test harness.

OpenCode version

V2 source at 7a4ad68af6; Bun 1.4.2.

Steps to reproduce

In packages/ai/test/provider/openai-chat.test.ts, using the existing model, imports, and Effect test harness, compile:

const prepared = yield* compileRequest(
  LLM.request({
    model,
    messages: [
      Message.user({
        type: "media",
        mediaType: "image/png",
        data: "DATA:image/png;base64,AAECAw==",
      }),
    ],
  }),
)

The image URL in prepared.body.messages contains the double prefix shown above. Repeating with AAECAw== or data:image/png;base64,AAECAw== produces the expected URL.

This checks request encoding only: the bytes are a small test fixture, not a PNG, and no provider call is needed.

Operating System

macOS arm64.

Activity

  1. opencodejr commented on Oct 28, 2026

    @opencodejr
    Contributor

    Thanks for the clear report. I reproduced this.

    • At 7a4ad68af6: with your OpenAI Chat test, AAECAw== and data:image/png;base64,AAECAw== both produce data:image/png;base64,AAECAw==. DATA:image/png;base64,AAECAw== produces data:image/png;base64,DATA:image/png;base64,AAECAw==.
    • On the latest dev (b6c43a524): the media API has been refactored, so your exact { type: "media", data: "..." } input no longer applies. The same bug is still there:
      • Media.parseDataUrl("DATA:image/png;base64,AAECAw==") returns undefined.
      • A tool-result file with uri DATA:image/png;base64,AAECAw== compiles to the same double-prefixed image_url for OpenAI Chat.

    The likely cause now is the case-sensitive DATA_URL regex in packages/ai/src/media.ts, which ProviderShared.toolFileMedia relies on. imageOutput in protocols/utils/media-input.ts also uses a case-sensitive startsWith("data:"). Your earlier PR #47648 targeted the old packages/llm code and was auto-closed. A fix now would need to go in packages/ai.

    The script below writes and runs a bun test that reproduces this on dev.

    Related, but not the same problem: #47583 (uppercase BASE64 flag in server-side text attachments) and #58937 (uppercase image MIME types in the Cohere route).

    Reproduction script (repro-47538.sh)
    #!/usr/bin/env bash
    # Reproduces anomalyco/opencode#47538 on current dev: uppercase DATA: URLs get a second data: prefix.
    # Usage: REPO=/path/to/opencode-checkout bash repro-47538.sh   (after `bun install` in the checkout)
    set -euo pipefail
    cd "${REPO:-/workspace/repo}/packages/ai"
    cat > test/provider/repro-47538.test.ts <<'EOF'
    // Reproduction for anomalyco/opencode#47538 on current dev (packages/ai).
    // Run from packages/ai: bun test test/provider/repro-47538.test.ts
    import { describe, expect } from "bun:test"
    import { Effect } from "effect"
    import { LLM, Media, Message, ToolCallPart } from "../../src/effect.js"
    import * as OpenAIChat from "../../src/protocols/openai-chat.js"
    import { Auth } from "../../src/route.js"
    import { compileRequest } from "../../src/route/client.js"
    import { it } from "../lib/effect.js"
    
    const model = OpenAIChat.route
      .with({ endpoint: { baseURL: "https://api.openai.test/v1/" }, auth: Auth.bearer("test") })
      .model({ id: "gpt-4o-mini" })
    
    describe("#47538", () => {
      for (const uri of ["data:image/png;base64,AAECAw==", "DATA:image/png;base64,AAECAw=="]) {
        it.effect(`parseDataUrl ${uri}`, () =>
          Effect.sync(() => {
            const parsed = Media.parseDataUrl(uri)
            console.log("parseDataUrl", uri, "=>", parsed?.source)
            expect(parsed).toBeDefined()
          }),
        )
        it.effect(`tool-result file ${uri}`, () =>
          Effect.gen(function* () {
            const prepared: any = yield* compileRequest(
              LLM.request({
                model,
                messages: [
                  Message.user("Show me the screenshot."),
                  Message.assistant([ToolCallPart.make({ id: "call_1", name: "read", input: { filePath: "shot.png" } })]),
                  Message.tool({
                    id: "call_1",
                    name: "read",
                    resultType: "content",
                    result: [{ type: "file", uri, mime: "image/png" }],
                  }),
                ],
              }),
            )
            const body = JSON.stringify(prepared.body.messages)
            const urls = body.match(/"url":"[^"]*"/g)
            console.log("tool file", uri, "=>", urls)
            expect(body).toContain('"url":"data:image/png;base64,AAECAw=="')
            expect(body).not.toContain("DATA:")
          }),
        )
      }
    })
    EOF
    bun test test/provider/repro-47538.test.ts

    Run it: bash repro-47538.sh

  2. neriousy commented on Nov 19, 2026

    @neriousy
    Member

    Fixed by #60963, which recognizes data URLs regardless of scheme case, so uppercase DATA: URLs are no longer double-prefixed. If you still see this on the latest version, comment here and we'll reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions