Skip to content

fix: refactor DoubaoBot to use AsyncArk with proper async streaming - #613

Open
octo-patch wants to merge 1 commit into
yihong0618:mainfrom
octo-patch:fix/doubao-async-streaming
Open

fix: refactor DoubaoBot to use AsyncArk with proper async streaming#613
octo-patch wants to merge 1 commit into
yihong0618:mainfrom
octo-patch:fix/doubao-async-streaming

Conversation

@octo-patch

Copy link
Copy Markdown

Closes #594

Problem

The DoubaoBot implementation has several issues introduced when it was
migrated to the Volcengine Ark V3 SDK:

  1. Blocks the event loop: Both ask and ask_stream call the
    synchronous Ark client inside async def methods without await,
    blocking asyncio while waiting for network I/O.

  2. Streaming loop uses a sync iterator: ask_stream used for chunk in … instead of async for chunk in …, which prevents asyncio from
    yielding control between chunks.

  3. Missing split_sentences: Other bots (chatgptapi, ppio, jiekou)
    pass the raw LLM token stream through split_sentences before
    yielding to the TTS layer, producing natural sentence-boundary
    chunks. DoubaoBot was yielding raw tokens, resulting in choppy or
    broken TTS output.

  4. No guard for empty choices: A chunk with an empty choices
    list would raise IndexError. chatgptapi_bot guards against this
    with if not event.choices: continue.

  5. Deprecated default model: skylark-chat is an old Ark V2 model
    name. The current Ark V3 equivalent is doubao-pro-32k (users can
    still override via gpt_options).

  6. ask returned None on error instead of "", violating the
    BaseBot abstract method signature.

Solution

  • Swap ArkAsyncArk and add await to chat.completions.create
  • Iterate the streaming response with async for
  • Wire split_sentences into ask_stream, matching ppio/chatgptapi pattern
  • Add if not chunk.choices: continue and if content is None: continue guards
  • Change default model to doubao-pro-32k
  • Return "" instead of None from ask on error

Testing

Changes follow the exact patterns already used by ppio_bot.py and
chatgptapi_bot.py, which are known to work correctly in production.

- Replace sync `Ark` client with `AsyncArk` to avoid blocking the event
  loop during API calls
- Use `async for` to iterate the streaming response instead of a sync
  `for` loop
- Add `split_sentences` for cleaner TTS chunking, matching the pattern
  used by other bots (ppio, chatgptapi)
- Add guard for empty `choices` to prevent IndexError on certain chunks
- Return `""` (not `None`) from `ask` on error to satisfy the `BaseBot`
  return type contract
- Update default model from the deprecated `skylark-chat` to
  `doubao-pro-32k` (the current Ark V3 model name)
- Fix module docstring (was copy-pasted from glm_bot)

Closes yihong0618#594

Co-Authored-By: Octopus <liyuan851277048@icloud.com>
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.

豆包

1 participant