{"record":{"id":"1da593bfc32d9d84","repo":"arduino/Arduino","slug":"stream-decompress-mode-must-be-gzip-or-deflate","errorCode":null,"errorMessage":"stream_decompress mode must be gzip or deflate","messagePattern":"stream_decompress mode must be gzip or deflate","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"arduino-core/src/processing/app/i18n/python/requests/utils.py","lineNumber":358,"sourceCode":"            tried_encodings.append(encoding)\n\n    # Fall back:\n    try:\n        return str(r.content, encoding, errors='replace')\n    except TypeError:\n        return r.content\n\n\ndef stream_decompress(iterator, mode='gzip'):\n    \"\"\"Stream decodes an iterator over compressed data\n\n    :param iterator: An iterator over compressed data\n    :param mode: 'gzip' or 'deflate'\n    :return: An iterator over decompressed data\n    \"\"\"\n\n    if mode not in ['gzip', 'deflate']:\n        raise ValueError('stream_decompress mode must be gzip or deflate')\n\n    zlib_mode = 16 + zlib.MAX_WBITS if mode == 'gzip' else -zlib.MAX_WBITS\n    dec = zlib.decompressobj(zlib_mode)\n    try:\n        for chunk in iterator:\n            rv = dec.decompress(chunk)\n            if rv:\n                yield rv\n    except zlib.error:\n        # If there was an error decompressing, just return the raw chunk\n        yield chunk\n        # Continue to return the rest of the raw data\n        for chunk in iterator:\n            yield chunk\n    else:\n        # Make sure everything has been returned from the decompression object\n        buf = dec.decompress(bytes())\n        rv = buf + dec.flush()","sourceCodeStart":340,"sourceCodeEnd":376,"githubUrl":"https://github.com/arduino/Arduino/blob/a0df6e0e83b652c72bc78b0a1376c54d6ebc3bee/arduino-core/src/processing/app/i18n/python/requests/utils.py#L340-L376","documentation":"stream_decompress decompresses a chunk iterator using zlib, but only supports 'gzip' and 'deflate' transfer encodings. Any other mode string is rejected with this ValueError before a zlib decompress object is created.","triggerScenarios":"Calling stream_decompress(iterator, mode=...) with a mode other than 'gzip' or 'deflate' (e.g. 'br', 'zstd', None, or a case variant like 'GZIP'), typically via stream_untransfer when decoding a streamed response body.","commonSituations":"Server sends a Content-Encoding the helper doesn't support (brotli, zstd) and callers pass it straight through; typo or wrong casing in the mode string; tests that call stream_decompress directly with an unsupported encoding.","solutions":["Pass exactly 'gzip' or 'deflate' (lowercase) as mode.","If the response is brotli/zstd encoded, use a library that supports it (brotli/zstandard packages) instead of stream_decompress.","Normalize the mode before calling: mode = mode.lower(), and only if it is in ('gzip', 'deflate').","If the body is not compressed at all, skip decompression rather than passing an invalid mode."],"exampleFix":"// before\nstream_decompress(chunks, mode='GZIP')\n# after\nstream_decompress(chunks, mode='gzip')","handlingStrategy":"validation","validationCode":"if mode not in ('gzip', 'deflate'):\n    raise ValueError('unsupported encoding: %r' % mode)","typeGuard":null,"tryCatchPattern":"try:\n    out = stream_decompress(chunks, mode=enc)\nexcept ValueError:\n    out = chunks  # fall back to undecoded stream","preventionTips":["Normalize Content-Encoding to lowercase before dispatch","Only claim support for gzip/deflate in Accept-Encoding headers","Route brotli/zstd to dedicated decoders"],"tags":["python","requests","zlib","streaming","compression"],"backgroundTag":"invalid-enum-value","analyzedSha":"a0df6e0e83b652c72bc78b0a1376c54d6ebc3bee","analyzedAt":"2026-09-06T10:13:38.901Z","contentChangedAt":"2026-09-06T10:13:38.901Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}