Azure / Azure/azure-sdk-for-python
[azure-core] _aiohttp_body_helper does not decompress Content-Encoding: br (Brotli)
- Lingua principale
- Python
- Stelle
- 5.6k
- Fork
- 3.4k
- Merge medio
- 2g 2h
- PR unite (30g)
- 213
Descrizione
### Bug Report
**Package:** `azure-core` 1.41.0 (also affects `azure-ai-agentserver-responses` 1.0.0b7)
#### Description
`_aiohttp_body_helper` in `azure.core.utils._pipeline_transport_rest_shared` handles `Content-Encoding: gzip` and `Content-Encoding: deflate`, but does **not** handle `Content-Encoding: br` (Brotli). When an upstream service returns a Brotli-compressed response, the raw compressed bytes pass through to `response.text()`, which then raises `UnicodeDecodeError`.
#### Reproduction
This surfaces when using `azure-ai-agentserver-responses` as a Foundry Hosted Agent. The Foundry storage API (`/storage/history/item_ids`) returns `Content-Encoding: br`. The `FoundryStorageProvider` calls `http_resp.text()` which triggers the decode error.
The `FoundryStorageProvider` intentionally excludes `ContentDecodePolicy` from its pipeline (with a comment noting gzip crashes), so there is no other layer handling decompression.
#### Root Cause
In `azure/core/utils/_pipeline_transport_rest_shared.py`:
```python
def _aiohttp_body_helper(response):
...
enc = response.headers.get("Content-Encoding")
if enc in ("gzip", "deflate"): # <- "br" not handled
...
return response._content # raw Brotli bytes pass through
```
#### Expected Behavior
Brotli responses should be decompressed transparently, matching how gzip/deflate are handled.
#### Suggested Fix
```python
if enc in ("gzip", "deflate"):
...
elif enc == "br":
import brotli
response._content = brotli.decompress(response._content)
response._decompressed_content = True
return response._content
```
(The `brotli` package is already a common transitive dependency via `aiohttp[speedups]`.)
#### Workaround
We monkey-patch `_aiohttp_body_helper` at import time to add Brotli support.
#### Environment
- Python 3.11
- `azure-core==1.41.0`
- `azure-ai-agentserver-responses==1.0.0b7`
- Platform: Azure AI Foundry Hosted Agent (Linux container)
Guida per i contributori
Apri la guida per i contributori
Direzione di ricerca
Inizia in azure/core/utils/_pipeline_transport_rest_shared.py, in _aiohttp_body_helper, e confronta la gestione esistente di gzip e deflate con il percorso br segnalato. Controlla come viene dichiarata la disponibilità di Brotli, quindi verifica che una risposta codificata con Brotli venga decompressa prima dell'esecuzione di response.text() e non generi più UnicodeDecodeError.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- backend
- Tipo di issue
- Bug
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Stato di attività
- Tranquilla
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 76/100