Skip to content

Attaching a text file that starts with "BM" fails with "Image could not be decoded" #59245

Closed

Description

@andreivince

Description

A text file whose content starts with the letters "BM" is detected as a BMP image when it is attached to a prompt. A CSV with a "BMI" column or notes that start with "BMW" are enough. The server then tries to decode it as an image and rejects the whole prompt with HTTP 400 Image could not be decoded. Text that starts with "GIF8" fails the same way, and the read tool returns such a file to the model as a base64 GIF image instead of text.

Mime.detect in packages/core/src/mime.ts only checks the first two bytes for BMP ("BM") and the first four for GIF ("GIF8"). Attachments get their type from it, including the file:// URIs that TUI and app file mentions send.

Plugins

None

OpenCode version

dev at ed74ec8 (2.1.4)

Steps to reproduce

  1. Create bmi.csv with:
    BMI,age
    22.5,31
    
  2. Start opencode serve and create a session for that folder.
  3. Send a prompt with the file attached:
    POST /api/session/<id>/prompt with {"text": "Inspect this", "files": [{"uri": "file:///path/to/bmi.csv"}]}
  4. The server answers 400: {"_tag":"InvalidRequestError","message":"Image could not be decoded: file:///path/to/bmi.csv","field":"files"}

Expected: the file is attached as text/plain, like any other text file.

Screenshot

Not applicable, API response above.

Operating System

macOS 26.6.2 (arm64)

Terminal

Not terminal specific

Activity

  1. opencode-agent commented on Oct 26, 2026

    @opencode-agent
    Contributor

    Thanks for the clear report, I can confirm this on 2.1.4.

    I started opencode serve, created a session and sent a prompt with one file attached by file:// URI each time:

    • plain,age\n22.5,31 is accepted and attached as text/plain.
    • BMI,age\n22.5,31 gets HTTP 400 Image could not be decoded: file:///…/bmi.csv.
    • GIF8 notes\nhello gets the same 400.

    It still happens on the latest dev (ed74ec8) run from source. I didn't separately check the read tool returning a GIF8 file as a GIF image. It goes through the same Mime.detect, though, which only looks at the first 2 bytes (BM) or 4 bytes (GIF8), as you pointed out.

    The script below reproduces it against the API.

    Reproduction script (repro.sh)
    #!/usr/bin/env bash
    # Reproduces #59245: text files starting with "BM" / "GIF8" are sniffed as images
    # and the prompt is rejected with "Image could not be decoded".
    # Usage: repro.sh [opencode command...]   (defaults to `opencode` on PATH)
    set -u
    OC=("${@:-opencode}")
    PORT=4211
    PROJ=$(mktemp -d)
    cd "$PROJ"
    printf 'BMI,age\n22.5,31\n' > bmi.csv
    printf 'GIF8 notes\nhello\n' > gif.txt
    printf 'plain,age\n22.5,31\n' > ok.csv
    export OPENCODE_SERVER_PASSWORD=repro-pass
    "${OC[@]}" serve --port $PORT > "$PROJ/serve.log" 2>&1 &
    PID=$!
    trap 'kill $PID 2>/dev/null' EXIT
    for i in $(seq 1 60); do curl -s -o /dev/null localhost:$PORT/api/session -u opencode:x && break; sleep 1; done
    PASS=$(grep -o 'server password .*' serve.log | awk '{print $3}'); PASS=${PASS:-$OPENCODE_SERVER_PASSWORD}
    api() { curl -s -u "opencode:$PASS" -H 'content-type: application/json' "$@"; echo; }
    SID=$(api -X POST localhost:$PORT/api/session -d "{\"location\":{\"directory\":\"$PROJ\"}}" | sed -E 's/.*"id":"([^"]+)".*/\1/')
    echo "session: $SID"
    for f in ok.csv bmi.csv gif.txt; do
      echo "--- prompt with $f attached"
      api -w 'HTTP %{http_code}' -X POST localhost:$PORT/api/session/$SID/prompt \
        -d "{\"text\":\"Inspect this\",\"files\":[{\"uri\":\"file://$PROJ/$f\"}]}"
    done

    Run it: bash repro.sh

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions