G
YouTube TikTok Games
Skip to content

webfetch text output includes inert HTML template contents #64068

Open

Description

@Harshil-Mittal

Description

webfetch with format: "text" includes the contents of inert HTML <template> elements. The same response in Markdown correctly omits them. Template boilerplate therefore becomes part of the text returned to the model.

OpenCode version

dev at d432a06 (Bun 1.4.4).

Steps to reproduce

Serve this as text/html, then fetch it with webfetch in text and Markdown formats:

<p>Before</p><template id="card"><div>Hidden<template>Nested</template><span>Also hidden</span></div></template><p>After</p>
  • Text currently returns BeforeHiddenNestedAlso hiddenAfter; expected BeforeAfter.
  • Markdown already returns Before\n\nAfter.

Reproduced through the registered tool in packages/core/test/tool-webfetch.test.ts, with the HTTP test layer returning the HTML above. The new regression fails on dev.

Ordinary template content is not rendered. The text extractor's omitted-element list lacks template; the Markdown converter already includes it. I have a small fix and regression test ready.

Plugins

Built-in WebFetch only.

Operating System

macOS.

Prepared and reproduced with OpenAI Codex.

Activity

  1. opencodejr commented on Nov 15, 2026

    @opencodejr
    Contributor

    Seen with a simulated model or a mock server, not a real provider; a maintainer should confirm.

    Thanks for the precise report. I reproduced this on 2.2.7, and it still happens on the latest dev (d432a06) run from source.

    I ran the real opencode run against a local mock model that calls webfetch twice, once with format: "text" and once with format: "markdown", on a local page serving your HTML. The tool results sent back to the model were:

    • text: BeforeHiddenNestedAlso hiddenAfter
    • markdown: Before\n\nAfter

    Only the model was mocked; the webfetch tool and its HTTP fetch were real. The script below reproduces it.

    Reproduction script (repro.sh)
    #!/usr/bin/env bash
    # Reproduces #64068: webfetch format "text" keeps <template> contents, "markdown" drops them.
    # Runs the real `opencode run` against a mock OpenAI-compatible model that calls webfetch twice
    # (text + markdown) on a local HTML page, then prints the tool results OpenCode sent back.
    # Usage: ./repro.sh            (opencode from PATH)
    #        OC_DEV=/path/to/opencode ./repro.sh   (run from a source checkout)
    set -euo pipefail
    WORK=$(mktemp -d); cd "$WORK"
    cat > mock.ts <<'TS'
    const HTML = `<p>Before</p><template id="card"><div>Hidden<template>Nested</template><span>Also hidden</span></div></template><p>After</p>`
    const sse = (chunks: unknown[]) => new Response(chunks.map((c) => `data: ${JSON.stringify(c)}\n\n`).join("") + "data: [DONE]\n\n", { headers: { "content-type": "text/event-stream" } })
    const base = { id: "c1", object: "chat.completion.chunk", created: 0, model: "mock" }
    const s = Bun.serve({ port: 4555, hostname: "127.0.0.1", async fetch(req) {
      const u = new URL(req.url)
      if (u.pathname === "/page") return new Response(HTML, { headers: { "content-type": "text/html; charset=utf-8" } })
      if (!u.pathname.endsWith("/chat/completions")) return new Response("{}", { status: 404 })
      const body: any = await req.json()
      const tools = body.messages.filter((m: any) => m.role === "tool")
      if (tools.length === 0) {
        if (!body.tools?.length) return sse([{ ...base, choices: [{ index: 0, delta: { role: "assistant", content: "title" }, finish_reason: "stop" }] }])
        const url = "http://127.0.0.1:4555/page"
        const call = (i: number, id: string, format: string) => ({ index: i, id, type: "function", function: { name: "webfetch", arguments: JSON.stringify({ url, format }) } })
        return sse([{ ...base, choices: [{ index: 0, delta: { role: "assistant", tool_calls: [call(0, "call_text", "text"), call(1, "call_md", "markdown")] }, finish_reason: null }] },
                    { ...base, choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] }])
      }
      for (const t of tools) console.log(`TOOL RESULT ${t.tool_call_id}: ${JSON.stringify(t.content)}`)
      return sse([{ ...base, choices: [{ index: 0, delta: { role: "assistant", content: "done" }, finish_reason: "stop" }] }])
    } })
    TS
    mkdir -p .opencode
    cat > .opencode/opencode.jsonc <<'JSON'
    { "permission": { "webfetch": "allow" },
      "providers": { "mock": { "name": "Mock", "env": ["MOCK_API_KEY"], "package": "@opencode/ai/providers/openai-compatible",
        "settings": { "baseURL": "http://127.0.0.1:4555/v1" }, "models": { "m": { "name": "M" } } } } }
    JSON
    bun mock.ts > mock.log 2>&1 & MOCK=$!; trap 'kill $MOCK' EXIT; sleep 1
    export MOCK_API_KEY=x
    if [ -n "${OC_DEV:-}" ]; then
      # the spawned server process reads bunfig.toml from the cwd; give it the Solid JSX preload
      echo "preload = [\"$OC_DEV/packages/tui/node_modules/@opentui/solid/scripts/preload.js\"]" > bunfig.toml
      OC=(bun --conditions=browser --preload="$OC_DEV/packages/tui/node_modules/@opentui/solid/scripts/preload.js" "$OC_DEV/packages/cli/src/index.ts")
    else OC=(opencode); fi
    "${OC[@]}" --version
    timeout 120 "${OC[@]}" run -m mock/m "fetch the page" || true
    grep "TOOL RESULT" mock.log

    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