You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
UnicodeDecodeError in stream.posts() on multi-byte UTF-8 chunks at TCP boundaries #3
When using xdk.Client.stream.posts(...) for the Filtered Stream, the call crashes with UnicodeDecodeError whenever a TCP chunk boundary cuts a multi-byte UTF-8 character (emoji, accents, CJK) in the middle.
This is a classic streaming UTF-8 issue: chunk.decode('utf-8') is called per chunk without using an IncrementalDecoder, so any byte sequence split across two chunks raises the error.
Reproduction
Hard to reproduce on demand (depends on TCP chunking + a tweet containing a multi-byte char around the chunk boundary), but in production it happens regularly (~44 crashes/day for a stream watching ~1500 tweets/day in French — emojis are very common).
[DISCONNECT] UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe2 in position 1023: unexpected end of data
Non-retryable error: Unexpected error: 'utf-8' codec can't decode byte 0xe2 in position 1023: unexpected end of data
Position is frequently 1023 (suggesting a 1024-byte read buffer is being decoded as-is), but I also see positions 572, 754, 63-65.
Use a codecs.getincrementaldecoder('utf-8')() (or higher-level io.TextIOWrapper / httpx.Response.iter_lines() pattern) instead of decoding each chunk independently.
Bypass xdk for streaming, use httpx.Client.stream("GET", url).iter_lines() directly. This works because httpx uses an IncrementalDecoder internally for iter_lines/iter_text. xdk is still useful for the REST API.
Environment
Python 3.13
Debian (systemd service)
X API v2 Filtered Stream endpoint /2/tweets/search/stream
Activity
qed- commentedon Aug 13, 2026
I don't think this should be problem anymore. iter_content() is called with decode_unicode=True which does use an incremental decoder.
https://github.com/xdevplatform/xdk-python/blame/main/xdk/streaming.py#L415