Skip to content

[Bug] Studio web fetch: a bare & gains a stray ;, escaped code leaves its code block, and a malformed &# can leave the rest of the page as raw HTML #17199

Closed

Description

@mrityu75

When the model fetches a page with the web search tool, Studio's HTML-to-Markdown converter (studio/backend/core/inference/_html_to_md.py) mishandles character references and bare &:

  • Stray ; and wrongly decoded names. A bare & followed by a letter gains a ;, and a name that is a reference only with its ; is decoded anyway. For example, AT&T. becomes AT&T.;, ?key=abc&format=json&lang=en becomes ?key=abc&format;=json⟨=en, and ?api=1&map_action=map becomes ?api=1↦_action=map.
  • Escaped characters leave their code block. In a <pre> inside a table cell, escaped characters are moved out of the code block.
    • This is how line-numbered code is laid out by Pygments' table formatter (MkDocs with pymdownx.highlight), Rouge (Jekyll linenos) and Chroma (Hugo). So if a < b and c > 0: reads if a b and c 0: inside the fence, with <> ahead of it.
    • Inside a link in a <pre>, the escaped characters become the whole Markdown link text, the rest of the link text moves in front of it, and a &nbsp; there is lost.
  • The rest of the page can come out as raw HTML.
    • On Python 3.13 and 3.14, this happens after a decimal reference that has no ; and runs into a letter a–f (&#169Copyright). On 3.11 and 3.12 that reference stays undecoded instead.
    • On 3.11 and 3.12, it happens after a &# with no digits, when no ; follows it anywhere in the page or once a second such &# appears.

Environment

Where are you using Unsloth?

  • Unsloth web UI (unsloth studio)

Operating system and version: macOS 26. The converter is pure Python, so this is not OS-specific.

GPU model(s) and accelerator backend: None needed.

Versions: main at 3329d6d. The converter is unchanged since dc75b66 (#16286). Reproduced on Python 3.11.16, 3.12.7, 3.13.11 and 3.14.7.

What happened?

Steps to reproduce: From studio/backend (the converter needs only the standard library), run with PYTHONPATH=.:

from core.inference._html_to_md import html_to_markdown

for page in [
    "<p>R&D and Q&A at AT&T.</p>",
    "<p>See https://example.com/v1?key=abc&format=json&lang=en</p>",
    "<table><tr><td><pre>1</pre></td><td><pre>if a &lt; b &amp;&amp; c &gt; 0:</pre></td></tr></table>",
    '<pre>in <a href="./rfc5741#section-2">Section&nbsp;2 of RFC 5741</a>.</pre>',
    '<pre>fn new() -&gt; <a href="/std/vec">Vec&lt;T&gt;</a></pre>',
    "<p>&#169Copyright 2026</p><h2>Next</h2><p>More &amp; text.</p>",
]:
    print(repr(html_to_markdown(page, main_content = True)))

Expected behavior: The text a browser shows:

  • R&D and Q&A at AT&T.
  • ?key=abc&format=json&lang=en
  • if a < b && c > 0: inside its fence
  • in Section 2 of RFC 5741. with its no-break space
  • fn new() -> Vec<T>
  • ©Copyright 2026, with the heading and paragraph after it rendered as Markdown

Actual behavior on Python 3.13.11 (on 3.12.7 the last line is '&#169Copyright 2026\n\n## Next\n\nMore & text.' instead):

'R&D; and Q&A; at AT&T.;'
'See https://example.com/v1?key=abc&format;=json⟨=en'
'| ``` 1 ``` | <&&>  ``` if a  b  c  0: ``` |\n| --- | --- |'
'```\nin Section2 of RFC 5741.\n```'
'```\nfn new() -> VecT[<>](/std/vec)\n```'
'©Copyright 2026</p><h2>Next</h2><p>More & text.</p>;'

Live pages converted with html_to_markdown(main_content=True) on main:

  • Alpha Vantage's API documentation: 396 &symbol;= and 332 &interval;= in its code samples.
  • Google's Maps URLs documentation: 13 ↦_action and 7 &query;=.
  • OpenStreetMap's Overpass API wiki, whose query examples are <pre> blocks in table cells: no <osm-script> survives (osm-scriptunion ...).
  • MkDocs Material's code-blocks reference: items[j] > items[j + 1] reads items[j] items[j + 1] 3 times.
  • A Jekyll Minimal Mistakes page: () => { reads () = {.
  • RFC 6749 on rfc-editor.org: Section2 of RFC 5741.
  • Procter & Gamble's homepage: P&G;’s.

Model and operation involved: Any chat model calling web_search with a url. _render_page passes the page body to html_to_markdown(main_content=True). The reproduction needs no model.

Diagnostics and logs

_MarkdownRenderer runs HTMLParser with convert_charrefs=False and decodes each reference itself in handle_entityref and handle_charref:

  1. The stray ;. HTMLParser calls handle_entityref(name) for &name followed by any character that is not a letter or digit, whether or not that character is ;. The handler always decodes html.unescape(f"&{name};"). That adds a ; the page never had: AT&T. becomes AT&T.;, because a name may contain .. It also decodes names such as lang and map, which are references only with their ;.
  2. Code leaving its block. The decoded text goes through _emit(), whose target is an open link or table cell before an open <pre>. Meanwhile handle_data appends <pre> text straight to the <pre> buffer. So inside <td><pre> or <pre><a>, the decoded characters land away from the text around them.
  3. The raw-HTML tail. With convert_charrefs=False, how a malformed &# ends depends on the Python version:
    • On 3.13.11 and 3.14.7, &#169Copyright takes everything after it into the reference.
    • On 3.11.16 and 3.12.7, a &# that starts no numeric reference passes the rest of the page through as text if no ; follows it, or at the second such &#.

No converter test checks the rendered text for a bare & before a letter, or for a &# without digits.

Additional context

HTMLParser(convert_charrefs=True) is the stdlib default. It decodes each text run with html.unescape, which applies the HTML5 rules a browser uses for text, and passes the decoded run to handle_data, so the run is routed with the text around it. A PR with that fix and regression tests follows.

Activity

  1. danielhanchen commented on Nov 10, 2026

    @danielhanchen
    Member

    Fixed! The web fetch converter decoded character references on its own, so a bare & gained a stray ;, escaped characters inside line-numbered code tables and linked code jumped out of their code block, and a malformed &# could leave the rest of the page as raw HTML. It now decodes references inside their text run the way a browser does, fixed in #17200. Thanks for the detailed report and the fix!
    To update: Unsloth Desktop will have an update banner. For Studio, use unsloth studio update. For Docker, use docker exec <container> unsloth-studio-update. Reopen if it still happens.

  2. added a commit that references this issue on Nov 10, 2026
    025f8bb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions