# INTERNAL: 压缩字典不匹配：期望gzip但收到deflate

- **ID:** `grpc/compression-dictionary-mismatch`
- **领域:** grpc
- **类别:** encoding_error
- **验证级别:** ai_generated
- **修复率:** 82%

## 根因

客户端和服务器之间的gRPC消息压缩算法不匹配：客户端发送使用'deflate'压缩的消息，但服务器期望'gzip'，反之亦然，由于压缩配置不兼容。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| gRPC v1.63.0 | active | — | — |
| gRPC-Python v1.62.0 | active | — | — |
| gRPC-Go v1.64.0 | active | — | — |

## 解决方案

1. ```
   对齐压缩算法：在客户端和服务器上设置相同的压缩；例如在Python中：'grpc.insecure_channel(target, options=[("grpc.default_compression_algorithm", 2)])'（2=gzip），服务器上：'grpc.server(futures.ThreadPoolExecutor(), options=[("grpc.default_compression_algorithm", 2)])'。
   ```
2. ```
   使用环境变量覆盖压缩：在客户端和服务器上设置'GRPC_DEFAULT_COMPRESSION_ALGORITHM=gzip'以强制一致性，并设置'GRPC_DEFAULT_COMPRESSION_LEVEL=1'以实现低延迟。
   ```
3. ```
   实现客户端拦截器检测和记录压缩头部：检查'grpc-encoding'元数据，并在检测到不匹配时动态调整算法；如果协商失败，回退到无压缩。
   ```

## 无效尝试

- **** — Disabling compression entirely on the client side (e.g., 'grpc.default_compression_algorithm=none') may break if the server requires compression; the error changes to 'UNIMPLEMENTED: Decompressor is not installed'. (75% 失败率)
- **** — Switching to a third compression library (e.g., zstd) without updating both sides causes a new mismatch error; both client and server must agree on the algorithm. (85% 失败率)
- **** — Restarting the server with the same configuration doesn't fix the root cause; the mismatch persists until the client's compression algorithm is changed. (95% 失败率)
