Skip to content

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

Open
veeceey wants to merge 3 commits intoencode: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 intoencode:masterfrom
veeceey:fix/issue-3471-invalid-data-error-message

Conversation

@veeceey
Copy link

@veeceey veeceey commented Feb 8, 2026

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

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 encode#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
Copy link
Author

veeceey commented Feb 8, 2026

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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Raising incorrect error when passing array to data

1 participant