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 (5d67dcf) to head (637d829).
⚠️ Report is 1 commits behind head on master.
✅ 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.35% <99.41%> (+0.01%) ⬆️
OS-Windows 96.69% <97.05%> (+0.01%) ⬆️
OS-macOS 97.56% <97.05%> (+<0.01%) ⬆️
Py-3.10.11 97.12% <95.87%> (+<0.01%) ⬆️
Py-3.10.19 97.61% <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.15% <98.23%> (-0.01%) ⬇️
Py-3.14.2 98.17% <98.11%> (-0.01%) ⬇️
Py-3.14.2t 97.25% <95.61%> (-0.02%) ⬇️
Py-pypy3.11.13-7.3.20 97.42% <98.82%> (+0.01%) ⬆️
VM-macos 97.56% <97.05%> (+<0.01%) ⬆️
VM-ubuntu 98.35% <99.41%> (+0.01%) ⬆️
VM-windows 96.69% <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 (637d829) with master (5d67dcf)

Summary

✅ 59 untouched

@bdraco bdraco reopened this Dec 12, 2025
Co-authored-by: Sam Bull <[email protected]>
@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

@bdraco
Copy link
Member Author

bdraco commented Dec 19, 2025

The backport is proving tricky. I'll merge this one I have more of it worked out locally

@bdraco bdraco marked this pull request as ready for review December 19, 2025 23:22
@bdraco bdraco enabled auto-merge (squash) December 19, 2025 23:26
@bdraco bdraco merged commit 8b919d3 into master Dec 19, 2025
41 checks passed
@bdraco bdraco deleted the no_decode_websocket_option branch December 19, 2025 23:30
@patchback
Copy link
Contributor

patchback bot commented Dec 19, 2025

Backport to 3.14: 💔 cherry-picking failed — conflicts found

❌ Failed to cleanly apply 8b919d3 on top of patchback/backports/3.14/8b919d30a2339d3b9dbc53e8958f745b0f9703ea/pr-11764

Backporting merged PR #11764 into master

  1. Ensure you have a local repo clone of your fork. Unless you cloned it
    from the upstream, this would be your origin remote.
  2. Make sure you have an upstream repo added as a remote too. In these
    instructions you'll refer to it by the name upstream. If you don't
    have it, here's how you can add it:
    $ git remote add upstream https://github.com/aio-libs/aiohttp.git
  3. Ensure you have the latest copy of upstream and prepare a branch
    that will hold the backported code:
    $ git fetch upstream
    $ git checkout -b patchback/backports/3.14/8b919d30a2339d3b9dbc53e8958f745b0f9703ea/pr-11764 upstream/3.14
  4. Now, cherry-pick PR Add decode_text parameter to WebSocket for receiving TEXT as bytes #11764 contents into that branch:
    $ git cherry-pick -x 8b919d30a2339d3b9dbc53e8958f745b0f9703ea
    If it'll yell at you with something like fatal: Commit 8b919d30a2339d3b9dbc53e8958f745b0f9703ea is a merge but no -m option was given., add -m 1 as follows instead:
    $ git cherry-pick -m1 -x 8b919d30a2339d3b9dbc53e8958f745b0f9703ea
  5. At this point, you'll probably encounter some merge conflicts. You must
    resolve them in to preserve the patch from PR Add decode_text parameter to WebSocket for receiving TEXT as bytes #11764 as close to the
    original as possible.
  6. Push this branch to your fork on GitHub:
    $ git push origin patchback/backports/3.14/8b919d30a2339d3b9dbc53e8958f745b0f9703ea/pr-11764
  7. Create a PR, ensure that the CI is green. If it's not — update it so that
    the tests and any other checks pass. This is it!
    Now relax and wait for the maintainers to process your pull request
    when they have some cycles to do reviews. Don't worry — they'll tell you if
    any improvements are necessary when the time comes!

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

bdraco added a commit that referenced this pull request Dec 19, 2025
…11764)

Co-authored-by: Sam Bull <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
(cherry picked from commit 8b919d3)
bdraco added a commit that referenced this pull request Dec 19, 2025
…11764)

Co-authored-by: Sam Bull <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
(cherry picked from commit 8b919d3)
bdraco added a commit that referenced this pull request Dec 19, 2025
…11764)

Co-authored-by: Sam Bull <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
(cherry picked from commit 8b919d3)
bdraco added a commit that referenced this pull request Dec 19, 2025
…11764)

Co-authored-by: Sam Bull <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
(cherry picked from commit 8b919d3)
bdraco added a commit that referenced this pull request Dec 19, 2025
…11764)

Co-authored-by: Sam Bull <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
(cherry picked from commit 8b919d3)
bdraco added a commit that referenced this pull request Dec 19, 2025
…11764)

Co-authored-by: Sam Bull <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
(cherry picked from commit 8b919d3)
bdraco added a commit that referenced this pull request Dec 20, 2025
…11764) (#11858)

Co-authored-by: Sam Bull <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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