Skip to content

fix: use asyncio.to_thread for non-blocking input in async chat examples - #216

Open
bodapatisaikrishna wants to merge 1 commit into
xai-org:mainfrom
bodapatisaikrishna:fix/async-examples-blocking-input
Open

bodapatisaikrishna wants to merge 1 commit into
xai-org:mainfrom
bodapatisaikrishna:fix/async-examples-blocking-input

Conversation

@bodapatisaikrishna

Copy link
Copy Markdown

Checklist

  • I have read both the CONTRIBUTING.md and Contributor License Agreement documents.
  • I have created an issue or feature request and received approval from xAI maintainers. (minor changes like fixing typos can skip this step)
  • I have tested my changes locally and they pass all CI checks.
  • I have added necessary documentation or updated existing documentation.

Description

In README.md (under Multi-Turn Chat (Asynchronous)) and examples/aio/chat.py, synchronous standard input("You: ") was invoked directly inside coroutines running on the asyncio event loop.

Calling synchronous blocking I/O on the main thread event loop blocks all concurrent background tasks, timeouts, polling timers, and streaming handlers until the user presses Enter.

This change:

  1. Updates the async chat snippet in README.md to use prompt = await asyncio.to_thread(input, "You: ").
  2. Adds an async helper ainput(prompt) wrapping asyncio.to_thread(input, prompt) in examples/aio/chat.py and updates all chat loops (basic_chat, chat_with_streaming, batch_chat, batch_chat_with_streaming).

Related Issue

Resolves #213

Type of Change

  • Bug fix
  • Documentation update

Additional Notes

Validated locally:

  • uv run ruff format --check: All 124 files formatted.
  • uv run ruff check: All checks passed.
  • uv run pyright: 0 errors, 0 warnings.
  • uv run pytest -q: 831 passed.

@bodapatisaikrishna
bodapatisaikrishna requested a review from a team as a code owner September 18, 2026 06:26
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.

[BUG] Blocking Synchronous I/O in Async Example Code (README.md)

1 participant