AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(gmail): fallback to raw HTML when html2text conversion fails (#12369)
Shunyu Wuc2e79fa5e1fb
diff --git a/autogpt_platform/backend/backend/blocks/google/gmail.py b/autogpt_platform/backend/backend/blocks/google/gmail.pyindex 2051f86b9e..0a4e92611c 100644--- a/autogpt_platform/backend/backend/blocks/google/gmail.py+++ b/autogpt_platform/backend/backend/blocks/google/gmail.py@@ -241,8 +241,8 @@ class GmailBase(Block, ABC):h.ignore_links = Falseh.ignore_images = Truereturn h.handle(html_content)- except ImportError:- # Fallback: return raw HTML if html2text is not available+ except Exception:+ # Keep extraction resilient if html2text is unavailable or fails.return html_content# Handle content stored as attachmentdiff --git a/autogpt_platform/backend/test/blocks/test_gmail.py b/autogpt_platform/backend/test/blocks/test_gmail.pyindex 5c086c6e0b..569e0d37b8 100644--- a/autogpt_platform/backend/test/blocks/test_gmail.py+++ b/autogpt_platform/backend/test/blocks/test_gmail.py@@ -84,6 +84,27 @@ class TestGmailReadBlock:assert "Hello World" in resultassert "This is HTML content" in result+ @pytest.mark.asyncio+ async def test_html_fallback_when_html2text_conversion_fails(self):+ """Fallback to raw HTML when html2text converter raises unexpectedly."""+ html_text = "<html><body><p>Broken <b>HTML</p></body></html>"++ msg = {+ "id": "test_msg_html_error",+ "payload": {+ "mimeType": "text/html",+ "body": {"data": self._encode_base64(html_text)},+ },+ }++ with patch("html2text.HTML2Text") as mock_html2text:+ mock_converter = Mock()+ mock_converter.handle.side_effect = ValueError("conversion failed")+ mock_html2text.return_value = mock_converter++ result = await self.gmail_block._get_email_body(msg, self.mock_service)+ assert result == html_text+@pytest.mark.asyncioasync def test_html_fallback_when_html2text_unavailable(self):"""Test fallback to raw HTML when html2text is not available."""