Discord error 4002: Decode error
Last verified against Discord Developer Docs - Opcodes and status codes
You sent an invalid payload to Discord. Don't do that!What error 4002 means
Discord could not decode a frame you sent. Where 4001 means the payload parsed but the opcode was wrong, 4002 means it did not parse at all: invalid JSON, an ETF payload when you connected with encoding=json (or vice versa), a compressed frame sent when you did not negotiate compression, or a binary frame where text was expected. Payload size is also a factor; the gateway enforces a maximum size for inbound frames and an oversized payload is rejected.
Again this is a custom-client problem in almost every case. With a library, the only realistic trigger is mismatched connection options (for example enabling zlib-stream transport compression without the library actually compressing) or an outdated library sending a payload format the gateway version no longer accepts.
What it looks like
// WebSocket close frame
code: 4002
reason: "Decode error"Why it happens
- Malformed JSON in an outbound frame
- Encoding mismatch: ETF vs JSON relative to the ?encoding= query parameter
- Compression mismatch: sending compressed frames without negotiating, or the reverse
- Binary frame sent where a text frame was required
- Oversized payload, for example a Request Guild Members with an enormous user_ids list
How to fix Discord error 4002
- 1Confirm the gateway URL query parameters (v, encoding, compress) match what your client actually sends
- 2Validate outbound JSON before sending; log the raw frame on close
- 3Keep Request Guild Members user_ids to the documented maximum (100) and split larger requests
- 4Upgrade or reconfigure the library so transport options are consistent
How to stop it recurring
Negotiate encoding and compression in exactly one place and derive both the URL and the serializer from the same setting. Add a size guard on any payload built from variable-length data before it is queued for the socket.
Encoding bugs are deterministic, which makes them the easiest close code to reproduce and unit test: capture the exact frame, replay it through your serializer, and assert round-tripping. The gateway connection parameters (version, encoding, compression) sit with the connection limits on the Discord limits page; the REST twin of a body Discord cannot parse is 50109.
Official reference: Discord Developer Docs - Opcodes and status codes. See all Discord error codes or the Discord limits and quotas.
Related codes
Error 4002 - quick answers
What does Discord error 4002 mean?
Discord could not decode a frame you sent. Where 4001 means the payload parsed but the opcode was wrong, 4002 means it did not parse at all: invalid JSON, an ETF payload when you connected with encoding=json (or vice versa), a compressed frame sent when you did not negotiate compression, or a binary frame where text was expected.
How do I fix Discord error 4002?
1. Confirm the gateway URL query parameters (v, encoding, compress) match what your client actually sends 2. Validate outbound JSON before sending; log the raw frame on close 3. Keep Request Guild Members user_ids to the documented maximum (100) and split larger requests 4. Upgrade or reconfigure the library so transport options are consistent
Should I retry after error 4002?
No. Retrying the same request produces the same error; the condition has to be fixed first. Treat it as a permanent failure for that message and surface it, rather than looping.
Stop debugging Discord by hand
Connect the channel through Conferbot: tokens, webhooks and retries are handled, failures show as readable status.