Repository navigation
Description
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.becomesAT&T.;,?key=abc&format=json&lang=enbecomes?key=abc&format;=json⟨=en, and?api=1&map_action=mapbecomes?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). Soif a < b and c > 0:readsif 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 there is lost.
- This is how line-numbered code is laid out by Pygments' table formatter (MkDocs with pymdownx.highlight), Rouge (Jekyll
- 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 (©Copyright). 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.
- On Python 3.13 and 3.14, this happens after a decimal reference that has no
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 < b && c > 0:</pre></td></tr></table>",
'<pre>in <a href="./rfc5741#section-2">Section 2 of RFC 5741</a>.</pre>',
'<pre>fn new() -> <a href="/std/vec">Vec<T></a></pre>',
"<p>©Copyright 2026</p><h2>Next</h2><p>More & 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=enif a < b && c > 0:inside its fencein Section 2 of RFC 5741.with its no-break spacefn 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 '©Copyright 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
↦_actionand 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]readsitems[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:
- The stray
;.HTMLParsercallshandle_entityref(name)for&namefollowed by any character that is not a letter or digit, whether or not that character is;. The handler always decodeshtml.unescape(f"&{name};"). That adds a;the page never had:AT&T.becomesAT&T.;, because a name may contain.. It also decodes names such aslangandmap, which are references only with their;. - Code leaving its block. The decoded text goes through
_emit(), whose target is an open link or table cell before an open<pre>. Meanwhilehandle_dataappends<pre>text straight to the<pre>buffer. So inside<td><pre>or<pre><a>, the decoded characters land away from the text around them. - 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,
©Copyrighttakes 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&#.
- On 3.13.11 and 3.14.7,
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
danielhanchen commentedon Nov 10, 2026
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, usedocker exec <container> unsloth-studio-update. Reopen if it still happens.[CS146S Stanford] Studio web fetch: a bare & stays as written, and es…