Skip to content

feat: expose async stream cancel handle - #148

Open
MukundaKatta wants to merge 1 commit into
xai-org:mainfrom
MukundaKatta:codex/async-stream-cancel-handle
Open

feat: expose async stream cancel handle#148
MukundaKatta wants to merge 1 commit into
xai-org:mainfrom
MukundaKatta:codex/async-stream-cancel-handle

Conversation

@MukundaKatta

Copy link
Copy Markdown

Summary

  • return a cancelable async iterator handle from chat.stream() while preserving existing async for usage
  • expose cancel() and aclose() so callers can stop the underlying gRPC stream early
  • document the early-stop pattern and add a regression test for transport cancellation

Closes #142

Tests

  • uv run pytest tests/aio/chat_test.py -q
  • uv run pytest tests/aio/chat_test.py::test_stream_cancel_cancels_underlying_rpc tests/aio/chat_test.py::test_streaming tests/aio/chat_test.py::test_stream_creates_span_with_correct_attributes -q
  • uv run ruff check src/xai_sdk/aio/chat.py tests/aio/chat_test.py
  • uv run pyright src/xai_sdk/aio/chat.py tests/aio/chat_test.py
  • uv run ruff format --check src/xai_sdk/aio/chat.py tests/aio/chat_test.py
  • git diff --check

@kalyanamdewri

Copy link
Copy Markdown

Same-seed comparison of #143 and #148

I ran fixture seed 142 against main 4358bc235e8641ba5f0cb54599675d098385d4bf, #143 9b55aa6b9b8fa24756a6cacddf91dcbe4bfe5523, and #148 7ed05b5f9e0247af49f1aa24abfd77ec30b4b597.

Ref Cancel outcome Fragments delivered Final gRPC status Live tasks/channels after 500 ms
main aclose RuntimeError: asynchronous generator is already running; blocked read PENDING 3 NOT_TERMINAL 1/1
#143 aclose returned; blocked read CancelledError 3 CANCELLED 0/0
#148 aclose returned; blocked read CancelledError 3 CANCELLED 0/0

Under this fixture, #143 and #148 preserved the measured invariant that cancellation converges to a clean terminal state without dangling tracked replay tasks or calls. All 3/3 independent repetitions per ref produced the same normalized result within that ref.

Harness, trace, and environment lock (harness commit 9655539bce02fd5aaa1ad41bf0e50cecf22dd6fa): public evidence gist (report sha256:8f3f2a21eef1bc2e733fadf59dd0c703c5589a9822bfcb192e76deb3e199912d; trace sha256:7284d81230894c360c0b5b4252e3b6145b93736225c4fe3bf8a311a818383bf5). Untested: the live xAI service, real gRPC/TCP channel ownership, SDK retries or retry backoff, and batch streaming. #143 is currently conflicting with main, so this is exact-head behavioral evidence, not integration evidence.

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.

[FEATURE] Expose a transport-level cancel/close handle on async streaming responses

2 participants