Repository navigation
feat: add httpx2 support - #1439
Iwaniukooo11 wants to merge 7 commits into
Conversation
🧪 Code Coverage (vs
|
| Base | PR | Delta | |
|---|---|---|---|
| src/a2a/utils/_httpx_compat.py (new) | — | 64.29% | — |
| Total | 93.56% | 93.49% | 🔴 -0.08% |
Generated by coverage-comment.yml
…onfig (#1445) # Description `RestTransport.delete_task_push_notification_config` goes through `_execute_request`, which always calls `response.json()`. The spec leaves the result to the server ([3.1.10](https://a2a-protocol.org/latest/specification/#3110-delete-push-notification-config): "Confirmation of deletion (implementation-specific)"), and the SDKs answer differently: - this SDK's server answers 200 with `{}`; - a2a-js answers 204 No Content ([`rest_handler.ts`](https://github.com/a2aproject/a2a-js/blob/3d3ca8291e1f6b1f53293f61c12ba46c36670000/src/server/express/rest_handler.ts#L548-L559)); - a2a-go answers 200 with no body ([`handleDeleteTaskPushConfig`](https://github.com/a2aproject/a2a-go/blob/ea739de3859f0574175bc9876eda39b7120da8e3/a2asrv/rest.go#L536-L557)). Against a2a-js or a2a-go servers, the config is deleted, but the Python client then raises `A2AClientError: JSON Decode Error`. A caller that retries or reports the failure is acting on a delete that succeeded. The delete now sends its request, checks the status, and does not read the body. Error responses still go through `_handle_http_error`, so a 404 with `TASK_NOT_FOUND` still raises `TaskNotFoundError`. `send_http_request` and the JSON-RPC transport are unchanged. The repro at the end of this description answers the delete the way each server does. On main (2cc1dd5): ``` a2a-python 200 b'{}' -> ok a2a-js 204 b'' -> A2AClientError: JSON Decode Error: Expecting value: line 1 column 1 (char 0) a2a-go 200 b'' -> A2AClientError: JSON Decode Error: Expecting value: line 1 column 1 (char 0) ``` On this branch, all three print `ok`. ``` $ pytest tests/client/transports/test_rest_client.py # src change reverted FAILED ...::TestTaskCallback::test_delete_task_push_notification_config_ignores_body[no_content] FAILED ...::TestTaskCallback::test_delete_task_push_notification_config_ignores_body[empty_200] 2 failed, 44 passed $ pytest --ignore=tests/integration/cross_version # this branch, Python 3.10, locked deps 2560 passed, 207 skipped, 4 xfailed, 1 xpassed $ pytest --cov=src src/a2a/client/transports/rest.py 87% (the new lines are covered) TOTAL 94% ``` The new tests also cover a mapped error and a closed client on this path. I left the cross-version tests out because their servers don't start within the 3 s timeout on my machine; the same happens on main. `ruff check`, `ruff format` and `ty check` pass. `ty` prints only the three existing unused `type: ignore` warnings. #1439 touches `send_http_request` and the transport type hints. This change stays inside `delete_task_push_notification_config` to keep out of its way, apart from one added import. #1436 percent-encodes the ids in this method's path, so whichever of the two lands second has a one-line conflict there; I'll rebase this one if #1436 goes first. - [x] Follow the [`CONTRIBUTING` Guide](https://github.com/a2aproject/a2a-python/blob/main/CONTRIBUTING.md). - [x] Make your Pull Request title in the <https://www.conventionalcommits.org/> specification. - [x] Ensure the tests and linter pass (Run `bash scripts/format.sh` from the repository root to format) - [x] Appropriate docs were updated (if necessary) No issue exists, since the new tests reproduce it. <details><summary>Repro script</summary> ```python import asyncio import httpx from a2a.client.transports.rest import RestTransport from a2a.types.a2a_pb2 import AgentCard, DeleteTaskPushNotificationConfigRequest # How each SDK's server confirms DELETE /tasks/{id}/pushNotificationConfigs/{configId}. SERVERS = {'a2a-python': (200, b'{}'), 'a2a-js': (204, b''), 'a2a-go': (200, b'')} async def main() -> None: for name, (status, body) in SERVERS.items(): transport = httpx.MockTransport(lambda r, s=status, b=body: httpx.Response(s, content=b)) async with httpx.AsyncClient(transport=transport) as http: client = RestTransport(http, AgentCard(), 'http://agent.example') try: await client.delete_task_push_notification_config( DeleteTaskPushNotificationConfigRequest(task_id='t1', id='cfg1') ) print(f'{name:10} {status} {body!r:5} -> ok') except Exception as e: print(f'{name:10} {status} {body!r:5} -> {type(e).__name__}: {e}') asyncio.run(main()) ``` </details> 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: mykytanetipa <mykytanetipa@google.com>
mykytanetipa
left a comment
There was a problem hiding this comment.
overall approach is lgtm, left minor comments + rebase is needed
There was a problem hiding this comment.
Have you run linter tests against both httpx and httpx2?
It seems that with httpx2 installed this module fails type checking. ty, mypy and pyright all reject the tuple assignments, since tuple[type[httpx.X], ...] doesn't accept httpx2.X, and in except HTTP_STATUS_ERROR as e they type e as the httpx error only. CI doesn't catch it because the linter runs without httpx2 and the ty ignore hides the import.
We can give type checkers their own branch under TYPE_CHECKING and keep the current try/except for the runtime values. In that branch, and type the catch-tuples as tuple[type[httpx.X], type[httpx2.X]], one entry per library, instead of the ... form. That's the only form where ty, mypy and pyright all type e as either library's error; with a union inside the ... form, mypy and pyright give up and type it as Any.
if TYPE_CHECKING:
from typing import TypeAlias
import httpx2
AsyncClient: TypeAlias = httpx.AsyncClient | httpx2.AsyncClient
Request: TypeAlias = httpx.Request | httpx2.Request
HTTP_STATUS_ERROR: tuple[
type[httpx.HTTPStatusError], type[httpx2.HTTPStatusError]
]
else:
try:
import httpx2
except ImportError:
AsyncClient = httpx.AsyncClient
Request = httpx.Request
HTTP_STATUS_ERROR = (httpx.HTTPStatusError,)
else:
AsyncClient = httpx.AsyncClient | httpx2.AsyncClient
Request = httpx.Request | httpx2.Request
HTTP_STATUS_ERROR = (httpx.HTTPStatusError, httpx2.HTTPStatusError)There was a problem hiding this comment.
seems like lots of unrelated reformatting, where it comes from? can we maybe avoid it for this PR and possibly land it separately?
| runs-on: ubuntu-latest | ||
| if: github.repository == 'a2aproject/a2a-python' | ||
| strategy: | ||
| matrix: |
There was a problem hiding this comment.
why 3.13 only? httpx2 seem to be supported for 3.11+
Description
This PR introduces support to client-passed
httpx2connection viahttpx_compatIt is backward compatible, which means that passing
httpxis also valid and exceptions/typing are properly handledThe compat layer exports catch-tuples for except/isinstance, type aliases for annotations, and
httpx_libThe only use of
httpxis left in 2 places:Tests
httpx_libfromhttpx_compat@pytest.mark.httpx2.This includes files that never mention httpx themselves:starlette.testclientpickshttpx2when it is installed, so anything using TestClient is affected too.Release-As: 1.4.0
refers to #1288