Skip to content

Fix: Improve error message for invalid data parameter with AsyncClient - #3758

Open
veeceey wants to merge 3 commits into
encode:masterfrom
veeceey:fix/issue-3471-invalid-data-error-message
Open

Fix: Improve error message for invalid data parameter with AsyncClient#3758
veeceey wants to merge 3 commits into
encode:masterfrom
veeceey:fix/issue-3471-invalid-data-error-message

Conversation

@veeceey

@veeceey veeceey commented Feb 8, 2026

Copy link
Copy Markdown

Summary

Fixes #3471

When AsyncClient receives invalid data like data=[{"a": "b"}] (a list of dicts), it was raising a confusing error:

RuntimeError: Attempted to send a sync request with an AsyncClient instance.

This error is misleading because the user IS using an async client correctly - the actual problem is passing invalid data.

With this fix, both sync and async clients now raise a clear error immediately:

TypeError: Expected bytes-like object in 'data' sequence, got dict. Use 'json=' for JSON data or 'data={...}' for form data.

Changes

  • Added validation in encode_request() to check if data is a list/tuple containing non-bytes objects
  • Raises TypeError with helpful error message directing users to correct alternatives (json= or data={...})
  • Added regression tests

Test Plan

Before this fix:

async_client = httpx.AsyncClient()
await async_client.request("POST", "/post", data=[{"a": "b"}])
# RuntimeError: Attempted to send a sync request with an AsyncClient instance.

After this fix:

async_client = httpx.AsyncClient()
await async_client.request("POST", "/post", data=[{"a": "b"}])
# TypeError: Expected bytes-like object in 'data' sequence, got dict. Use 'json=' for JSON data or 'data={...}' for form data.

Valid use cases still work:

  • data={"key": "value"} (dict for form encoding)
  • data=[b"bytes", b"data"] (list of bytes)
  • json={"key": "value"} (JSON encoding)

Testing

  • Manual testing with both sync and async clients
  • Added regression tests in tests/test_content.py
  • Verified existing test cases still pass
@veeceey

veeceey commented Feb 8, 2026

Copy link
Copy Markdown
Author

All CI passing on Python 3.9-3.13, ready for merge

@veeceey

veeceey commented Feb 19, 2026

Copy link
Copy Markdown
Author

Friendly ping - any chance someone could take a look at this when they get a chance? Happy to make any changes if needed.

veeceey added 3 commits March 10, 2026 06:08
When AsyncClient receives invalid data like data=[{"a": "b"}] (list of dicts),
it was raising "Attempted to send a sync request with an AsyncClient instance"
which is misleading. The actual issue is invalid data format.

This fix adds early validation in encode_request() to check if data is a
list/tuple containing non-bytes objects, and raises a clear TypeError with
helpful guidance (use json= or data={...} instead).

Fixes #3471
Remove unnecessary try-except block that was catching TypeError during iteration.
The validation logic doesn't need exception handling since we're only checking
types of items in a list/tuple that we've already confirmed exists.
@veeceey
veeceey force-pushed the fix/issue-3471-invalid-data-error-message branch from 5a2b188 to 843346f Compare March 10, 2026 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant