Skip to content

Commit 5c5fbd1

Browse files
rasmusfabergithub-actions[bot]Wauplin
authored
Make requests decode content (#3271)
* Request that requests decode content * Fix decode_content * Add decode_content = True to retry case as well. Add repro test. * Apply style fixes * Update tests/test_hf_file_system.py * fix tests * fix --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Lucain <[email protected]>
1 parent ddfeff2 commit 5c5fbd1

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/huggingface_hub/hf_file_system.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,7 @@ def read(self, length: int = -1):
10691069
)
10701070
hf_raise_for_status(self.response)
10711071
try:
1072+
self.response.raw.decode_content = True
10721073
out = self.response.raw.read(*read_args)
10731074
except Exception:
10741075
self.response.close()
@@ -1091,6 +1092,7 @@ def read(self, length: int = -1):
10911092
)
10921093
hf_raise_for_status(self.response)
10931094
try:
1095+
self.response.raw.decode_content = True
10941096
out = self.response.raw.read(*read_args)
10951097
except Exception:
10961098
self.response.close()

tests/test_hf_file_system.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@
1111
import fsspec
1212
import pytest
1313

14-
from huggingface_hub import hf_file_system
14+
from huggingface_hub import constants, hf_file_system
1515
from huggingface_hub.errors import RepositoryNotFoundError, RevisionNotFoundError
16-
from huggingface_hub.hf_file_system import HfFileSystem, HfFileSystemFile, HfFileSystemStreamFile
16+
from huggingface_hub.hf_file_system import (
17+
HfFileSystem,
18+
HfFileSystemFile,
19+
HfFileSystemStreamFile,
20+
)
1721

1822
from .testing_constants import ENDPOINT_STAGING, TOKEN
19-
from .testing_utils import repo_name
23+
from .testing_utils import repo_name, with_production_testing
2024

2125

2226
class HfFileSystemTests(unittest.TestCase):
@@ -612,3 +616,13 @@ def test_exists_after_repo_deletion():
612616
api.delete_repo(repo_id=repo_id, repo_type="model")
613617
# Verify that the repo no longer exists.
614618
assert not hffs.exists(repo_id, refresh=True)
619+
620+
621+
@with_production_testing
622+
def test_hf_file_system_file_can_handle_gzipped_file():
623+
"""Test that HfFileSystemStreamFile.read() can handle gzipped files."""
624+
fs = HfFileSystem(endpoint=constants.ENDPOINT)
625+
# As of July 2025, the math_qa.py file is gzipped when queried from production:
626+
with fs.open("datasets/allenai/math_qa/math_qa.py", "r", encoding="utf-8") as f:
627+
out = f.read()
628+
assert "class MathQa" in out

0 commit comments

Comments
 (0)