fix: yield thinking tag boundaries in content='all' mode#296
Closed
fix: yield thinking tag boundaries in content='all' mode#296
Conversation
The streaming loop now emits `<thinking>\n` before the first thinking chunk and `\n</thinking>\n\n` on transition to non-thinking content (or at end of stream), giving consumers well-formed output. For `content="text"` mode, tags are yielded as string chunks so concatenated output is properly delimited. For `content="all"` mode, behavior is unchanged — typed ContentThinking objects are yielded. Also removes the synthetic "\n\n" separator from the OpenAI provider's reasoning_summary_text.done event since the thinking→text transition now provides the visual break. Companion to tidyverse/ellmer#975.
Streaming chunks are fragments, not complete thoughts. Adding a _complete PrivateAttr (default True) lets __str__() skip tag wrapping for chunks emitted during streaming, preventing repeated <thinking>...</thinking> around each fragment in content="all" mode. Providers now use ContentThinking._as_chunk() for streaming fragments.
Previously `<thinking>\n` and `\n</thinking>\n\n` boundary strings were only yielded to consumers when `content="text"`. Remove that guard so they are emitted in all modes, including `content="all"`. This is required by shinychat PR posit-dev/shinychat#210, which removes server-side thinking detection and relies on the client tag parser seeing `<thinking>` markers in the stream. Without this fix, tool-use apps (which require `content="all"`) with thinking-capable models render thinking content as regular assistant text. Both the sync and async `_submit_turns` paths are updated. Tests in `TestStreamThinkingAll` and the async `test_content_all_async` are updated to assert that tag boundary strings ARE present in the output.
Collaborator
Author
|
Superseded by a rebased PR on a clean branch. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
if content_mode == "text":guards around tag boundaryyieldstatements in both the sync and async_submit_turnspaths<thinking>\nand\n</thinking>\n\nare now yielded to consumers in all modes, not justcontent="text"TestStreamThinkingAllandtest_content_all_asyncto assert that tag boundaries ARE present alongsideContentThinkingobjectstest_order_of_chunkstest that verifies the exact sequence: open-tag string → ContentThinking → close-tag string → response textMotivation
shinychat PR posit-dev/shinychat#210 removes server-side thinking detection and relies entirely on the client's tag parser seeing
<thinking>markers in the stream. Tool-use apps must usecontent="all"mode. Without this fix those apps see thinking content rendered as regular assistant text because the<thinking>/</thinking>boundary strings are never passed downstream.Test plan
tests/test_stream_thinking.pypassuv run pyright chatlas/_chat.py→ 0 errors, 0 warnings