Skip to content

Conversation

@bdraco
Copy link
Member

@bdraco bdraco commented Nov 17, 2025

What do these changes do?

Adds a decode_text parameter to ws_connect() and WebSocketResponse() that allows receiving WebSocket TEXT messages as raw bytes instead of decoded strings.

When decode_text=False:

  • TEXT messages return bytes instead of str
  • Skips UTF-8 decoding overhead
  • Enables direct use with high-performance JSON parsers like orjson that accept bytes

This addresses the use case where applications want to avoid the wasteful decode step when using JSON parsers that can process bytes directly.

Are there changes in behavior for the user?

No breaking changes. The default decode_text=True preserves existing behavior.

New behavior when decode_text=False:

  • receive() returns WSMessageTextBytes (with bytes data) for TEXT messages
  • receive_str() returns bytes instead of str
  • receive_json() passes bytes to the loads function

Is it a substantial burden for the maintainers to support this?

Low burden. The implementation:

  • Adds a single boolean flag passed through to WebSocketReader
  • Uses existing message infrastructure with a new WSMessageTextBytes type
  • Requires typing_extensions dependency for Python < 3.13

Why typing_extensions?

We use TypeVar with default= parameter (PEP 696, Python 3.13+) to make WebSocketResponse and ClientWebSocketResponse generic without breaking existing code. Without the default, all existing code would need to change from:

ws = WebSocketResponse()  # Would error without type parameter

to:

ws: WebSocketResponse[Literal[True]] = WebSocketResponse()

With default=Literal[True], the existing code continues to work unchanged while allowing explicit typing for decode_text=False:

ws: WebSocketResponse[Literal[False]] = WebSocketResponse(decode_text=False)

Related issue number

Fixes #11763

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
  • Add a new news fragment into the CHANGES/ folder

@bdraco bdraco added backport-3.13 Trigger automatic backporting to the 3.13 release branch by Patchback robot backport-3.14 Trigger automatic backporting to the 3.14 release branch by Patchback robot labels Nov 17, 2025
@codecov
Copy link

codecov bot commented Nov 17, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.75%. Comparing base (d658596) to head (c85cce7).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff            @@
##           master   #11764    +/-   ##
========================================
  Coverage   98.74%   98.75%            
========================================
  Files         127      127            
  Lines       43879    44171   +292     
  Branches     2337     2342     +5     
========================================
+ Hits        43327    43619   +292     
  Misses        392      392            
  Partials      160      160            
Flag Coverage Δ
CI-GHA 98.60% <99.41%> (+<0.01%) ⬆️
OS-Linux 98.34% <99.41%> (+<0.01%) ⬆️
OS-Windows 96.68% <97.05%> (+0.01%) ⬆️
OS-macOS 97.56% <97.05%> (+<0.01%) ⬆️
Py-3.10.11 97.11% <95.87%> (-0.01%) ⬇️
Py-3.10.19 97.60% <98.23%> (-0.01%) ⬇️
Py-3.11.14 97.80% <98.23%> (-0.01%) ⬇️
Py-3.11.9 97.31% <95.87%> (-0.01%) ⬇️
Py-3.12.10 97.41% <95.87%> (-0.01%) ⬇️
Py-3.12.12 97.91% <98.23%> (+<0.01%) ⬆️
Py-3.13.11 98.16% <98.23%> (-0.01%) ⬇️
Py-3.14.2 98.17% <98.11%> (-0.02%) ⬇️
Py-3.14.2t 97.25% <95.61%> (-0.01%) ⬇️
Py-pypy3.11.13-7.3.20 97.41% <98.82%> (+<0.01%) ⬆️
VM-macos 97.56% <97.05%> (+<0.01%) ⬆️
VM-ubuntu 98.34% <99.41%> (+<0.01%) ⬆️
VM-windows 96.68% <97.05%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bdraco
Copy link
Member Author

bdraco commented Nov 17, 2025

now we have a problem with json.loads not decoding bytes

@bdraco
Copy link
Member Author

bdraco commented Nov 17, 2025

Ok hundreds of lines of typing is not worth it for this change

@codspeed-hq
Copy link

codspeed-hq bot commented Nov 17, 2025

CodSpeed Performance Report

Merging #11764 will not alter performance

Comparing no_decode_websocket_option (c85cce7) with master (d658596)

Summary

✅ 59 untouched

@bdraco bdraco reopened this Dec 12, 2025
@bdraco bdraco changed the title Add option to avoid decoding WebSocket TEXT Add decode_text parameter to WebSocket for receiving TEXT as bytes Dec 12, 2025
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Dec 12, 2025
Co-authored-by: Sam Bull <git@sambull.org>
Co-authored-by: Sam Bull <git@sambull.org>
@Dreamsorcerer
Copy link
Member

Should probably skip the 3.13 backport, as this is a significant new feature and will also likely have plenty of conflicts with new syntax etc.

@bdraco bdraco removed the backport-3.13 Trigger automatic backporting to the 3.13 release branch by Patchback robot label Dec 12, 2025
@bdraco
Copy link
Member Author

bdraco commented Dec 13, 2025

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-3.14 Trigger automatic backporting to the 3.14 release branch by Patchback robot bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Give raw string bytes in socket message instead of string

3 participants