sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[BUG] web_fetch drops image alt text during HTML-to-text extraction (Python)

Open ryanfoxtyler opened this issue 6 days ago • 2 comments

Human Overview

The web_fetch tool drops both images and alt text removing the ability for the agent to understand that content. In this case, it was unable to parse docs and understand whether something was supported.

Checks

  • [x] I have updated to the lastest minor and patch version of Strands
  • [x] I have checked the documentation and this is not expected behavior
  • [x] I have searched ./issues and there are no duplicates of my issue

SDK Language

Python

Strands Version

strands-harness 0.3.3, strands-agents 1.66.0

Language Runtime Version

Python 3.12.13

Operating System

macOS 27.2.1, arm64

Installation Method

pip

Steps to Reproduce

from strands_harness.tools.web_fetch import _html_to_text

html = """
<table>
  <tr><th>Capability</th><th>bedrock-runtime</th><th>bedrock-mantle</th></tr>
  <tr><td>Cross-region inference</td><td><img alt="supported" src="yes.png"></td><td><img alt="not-supported" src="no.png"></td></tr>
</table>
"""
print(_html_to_text(html))

This also occurs with AWS's Bedrock inference endpoint comparison, where support indicators are images with alt="supported" and alt="not-supported".

Expected Behavior

Image alternative text should survive HTML-to-text conversion. The extracted text should preserve both support states, for example:

Cross-region inference | supported | not-supported

Without the alt values, the model receives the capability name but loses the information that distinguishes the two endpoints.

Actual Behavior

The output is:

Capability bedrock-runtime bedrock-mantle
Cross-region inference

The cause is the generic tag-stripping expression in _html_to_text():

text = re.sub(r"(?s)<[^>]+>", " ", html)

This removes the complete <img> element, including its alt attribute. The same implementation is present on current main at commit 8e3884657b62c6b989230265307794a5b1352e93, in harness-py/src/strands_harness/tools/web_fetch.py, line 52.

Both curl and direct transports pass HTML through _html_to_text() via _to_text(), so the information is lost before the summarizer model receives it.

Possible Solution

Preserve img[alt] before the generic tag-removal pass, or replace the regex-based reduction with an HTML parser that retains image alternative text.

For example, convert an image to text such as [image: supported] before removing other tags. Add a regression test asserting that meaningful alt values survive extraction while decorative images with empty alt remain omitted.

Additional Context

Why this matters

AWS's Bedrock endpoint comparison uses image alt attributes as the support indicators in a capability matrix. Dropping them leaves the row label but removes the values that answer whether each endpoint supports the capability.

Related Issues

#6243 concerns escaping image alt text in the separate SDK vended web_fetch implementations; this report is about the harness tool dropping alt text entirely. #6296 concerns Python/TypeScript image rendering differences, not alt-text loss.

ryanfoxtyler avatar Nov 25 '26 22:11 ryanfoxtyler

May I take this one? I would preserve meaningful image alt text in the Python harness extractor, keep decorative images empty, and add offline HTML regressions without fetching external pages.

widechaos avatar Nov 25 '26 23:11 widechaos

I've had a look at this one. The cause is exactly the generic tag-stripping step: <img> is removed before anything reads its alt, and the value is never carried into the text.

I have a fix that substitutes each <img> with its alt first (double-quoted, single-quoted and unquoted forms; a tag with no alt still collapses to a space), then unescapes entities after all tags are gone — so an alt containing < cannot re-open a tag and swallow what follows. PR incoming.

lkuko avatar Nov 28 '26 06:11 lkuko