Repository: huggingface/huggingface_hub

Issue:
HfFileSystemStreamFile.read() does not handle Content-Encoding: gzip
### Describe the bug

Sometimes responses are returned from the server as gzip encoded:
```
{'Content-Type': 'text/plain; charset=utf-8', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Date': 'Fri, 25 Jul 2025 16:51:10 GMT', 'Content-Security-Policy': "default-src 'none'; sandbox", 'Content-Disposition': 'inline; filename*=UTF-8\'\'math_qa.py; filename="math_qa.py";', 'X-Powered-By': 'huggingface-moon', 'cross-origin-opener-policy': 'same-origin', 'Referrer-Policy': 'strict-origin-when-cross-origin', 'X-Request-Id': 'Root=1-6883b5fe-41f5f016508b317149da7784;a76a214e-6e20-4ff6-8111-bc889e55b574', 'Access-Control-Allow-Origin': 'https://huggingface.co', 'Access-Control-Expose-Headers': 'X-Repo-Commit,X-Request-Id,X-Error-Code,X-Error-Message,X-Total-Count,ETag,Link,Accept-Ranges,Content-Range,X-Linked-Size,X-Linked-ETag,X-Xet-Hash', 'Set-Cookie': 'token=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Secure; SameSite=None, token=; Domain=huggingface.co; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Secure; SameSite=Lax', 'X-Repo-Commit': 'c4f1cc784c04c4957b50c97858f23893b633eea6', 'Content-Encoding': 'gzip', 'ETag': 'W/"c7ec157d2c79e30553b7a65a9549277b34ad7d17"', 'Vary': 'Origin,accept-encoding', 'X-Cache': 'Hit from cloudfront', 'Via': '1.1 b9ba4bda57a72c60657b278a2341fc54.cloudfront.net (CloudFront)', 'X-Amz-Cf-Pop': 'CPH50-C1', 'X-Amz-Cf-Id': '8UQpDQ7v-q8ZEguQ5gqSrr6z_BK5IfCsYT3FoX4H6CSVZ_m3M3Vz8g==', 'Age': '233163'}
```

I suspect Cloudfront might be the cause.

In this case the `self.response.raw.read()` in [HfFileSystemStreamFile.read()](https://github.com/huggingface/huggingface_hub/blob/main/src/huggingface_hub/hf_file_system.py#L1072) will return the gzipped data instead of the actual data. This will result in an error like:
```
Failed to read the dataset script at datasets/math_qa/math_qa.py: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte
```
in consumers. (If you google this error, you will see that others have encountered the same error and given up. Perhaps because it went away after some time, when Cloudfront changed it's cache.)

The easy fix is to add `self.response.raw.decode_content = True` just before `self.response.raw.read()`


### Reproduction

```
    fs = HfFileSystem()
    with fs.open(f"datasets/math_qa/math_qa.py", "r", encoding="utf-8") as f:
        print(f.read())
```

but note that this might vary depending on which exact cache Cloudfront edge server handles the request and what is has in its cache.

### Logs

```shell

```

### System info

```shell
- huggingface_hub version: 0.25.1
- Platform: Linux-6.14.6-061406-generic-x86_64-with-glibc2.41
- Python version: 3.12.10
- Running in iPython ?: No
- Running in notebook ?: No
- Running in Google Colab ?: No
- Running in Google Colab Enterprise ?: No
- Token path ?: /home/faber/.cache/huggingface/token
- Has saved token ?: False
- Configured git credential helpers: 
- FastAI: N/A
- Tensorflow: N/A
- Torch: 2.9.0.dev20250727+cu128
- Jinja2: 3.1.6
- Graphviz: N/A
- keras: N/A
- Pydot: N/A
- Pillow: 11.3.0
- hf_transfer: N/A
- gradio: N/A
- tensorboard: N/A
- numpy: 1.26.4
- pydantic: N/A
- aiohttp: 3.10.8
- ENDPOINT: https://huggingface.co
- HF_HUB_CACHE: /home/faber/.cache/huggingface/hub
- HF_ASSETS_CACHE: /home/faber/.cache/huggingface/assets
- HF_TOKEN_PATH: /home/faber/.cache/huggingface/token
- HF_HUB_OFFLINE: False
- HF_HUB_DISABLE_TELEMETRY: False
- HF_HUB_DISABLE_PROGRESS_BARS: None
- HF_HUB_DISABLE_SYMLINKS_WARNING: False
- HF_HUB_DISABLE_EXPERIMENTAL_WARNING: False
- HF_HUB_DISABLE_IMPLICIT_TOKEN: False
- HF_HUB_ENABLE_HF_TRANSFER: False
- HF_HUB_ETAG_TIMEOUT: 10
- HF_HUB_DOWNLOAD_TIMEOUT: 10
```


Discussion:

Patch:
diff --git a/src/huggingface_hub/hf_file_system.py b/src/huggingface_hub/hf_file_system.py
index 050a1d9bf9..9aa5bffa1e 100644
--- a/src/huggingface_hub/hf_file_system.py
+++ b/src/huggingface_hub/hf_file_system.py
@@ -1069,6 +1069,7 @@ def read(self, length: int = -1):
             )
             hf_raise_for_status(self.response)
         try:
+            self.response.raw.decode_content = True
             out = self.response.raw.read(*read_args)
         except Exception:
             self.response.close()
@@ -1091,6 +1092,7 @@ def read(self, length: int = -1):
             )
             hf_raise_for_status(self.response)
             try:
+                self.response.raw.decode_content = True
                 out = self.response.raw.read(*read_args)
             except Exception:
                 self.response.close()


Test patch:
diff --git a/tests/test_hf_file_system.py b/tests/test_hf_file_system.py
index 16dd3b5ee1..d30151d5fd 100644
--- a/tests/test_hf_file_system.py
+++ b/tests/test_hf_file_system.py
@@ -11,12 +11,16 @@
 import fsspec
 import pytest
 
-from huggingface_hub import hf_file_system
+from huggingface_hub import constants, hf_file_system
 from huggingface_hub.errors import RepositoryNotFoundError, RevisionNotFoundError
-from huggingface_hub.hf_file_system import HfFileSystem, HfFileSystemFile, HfFileSystemStreamFile
+from huggingface_hub.hf_file_system import (
+    HfFileSystem,
+    HfFileSystemFile,
+    HfFileSystemStreamFile,
+)
 
 from .testing_constants import ENDPOINT_STAGING, TOKEN
-from .testing_utils import repo_name
+from .testing_utils import repo_name, with_production_testing
 
 
 class HfFileSystemTests(unittest.TestCase):
@@ -612,3 +616,13 @@ def test_exists_after_repo_deletion():
     api.delete_repo(repo_id=repo_id, repo_type="model")
     # Verify that the repo no longer exists.
     assert not hffs.exists(repo_id, refresh=True)
+
+
+@with_production_testing
+def test_hf_file_system_file_can_handle_gzipped_file():
+    """Test that HfFileSystemStreamFile.read() can handle gzipped files."""
+    fs = HfFileSystem(endpoint=constants.ENDPOINT)
+    # As of July 2025, the math_qa.py file is gzipped when queried from production:
+    with fs.open("datasets/allenai/math_qa/math_qa.py", "r", encoding="utf-8") as f:
+        out = f.read()
+    assert "class MathQa" in out