Repository navigation
fix(web): return non-HTML text responses from fetch_web_page - #102
Open
mayuriphad wants to merge 1 commit into
Open
mayuriphad wants to merge 1 commit into
mayuriphad wants to merge 1 commit into
Conversation
fetch_web_page passed every response body to trafilatura.extract, which only handles HTML and returns None for JSON, CSV, XML or plain text. The tool then reported success with an empty body, so agents could not read API responses or data files and were not told anything went wrong. Branch on the response's media type: text types are returned as is, HTML (or a response without a content type) is still extracted with trafilatura, and other types such as PDFs return an error naming the content type. Fixes ArtificialAnalysis#101 Signed-off-by: mayuriphad <mayuriphad656@gmail.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #101
Problem
fetch_web_page_executorpassed every response body totrafilatura.extract(..., output_format="markdown"), whatever theContent-Type. trafilatura only extracts HTML and returnsNonefor JSON, CSV, XML and plain text. The executor turned that into""and returnedsuccess=Truewith<body></body>, so an agent couldn't read API responses or data files and wasn't told that anything went wrong.Fix
_fetch_with_redirectsnow returns the finalhttpx.Responseinstead of only its bytes, so the executor can see the headers.text/*other than HTML,application/json,application/xml,+json/+xmlsuffixes, CSV, NDJSON, YAML, JavaScript) are returned asresponse.text, truncated toMAX_LENGTH_WEB_FETCH_HTMLas before;text/html,application/xhtml+xml, or a response with noContent-Typestill go through trafilatura, as now;application/pdf) returnssuccess=FalsewithUnsupported content type: <type>.Tests
test_web_fetch_returns_text_responses_as_is(JSON, a+jsonvendor type with charset, CSV, plain text, XML),test_web_fetch_extracts_html_responsesandtest_web_fetch_reports_unsupported_content_types. The text and unsupported-type cases fail onmain.html_responsetest helper now sendsContent-Type: text/html. Before,httpx.Response(text=...)defaulted it totext/plain, which didn't match a real HTML page.tests/test_web_fetch.pyandtests/test_web_search.py: 61 passed locally (Python 3.12, trafilatura 2.3.1).ruff format --checkandruff checkare clean on both files.