Emit <thinking> tag boundaries during streaming#294
Merged
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.
c748d39 to
d0badcb
Compare
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.
6 tasks
This was referenced May 7, 2026
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
ContentThinkingchunks are emitted during streaming, the streaming loop now emits<thinking>\nbefore the first thinking chunk and\n</thinking>\n\nwhen transitioning to non-thinking content (or at end of stream)content="text"consumers, tags are yielded as string chunks — concatenated output is well-formedcontent="all"consumers, behavior is unchanged — typedContentThinkingobjects are yielded, no tag stringsContentThinkingnow has a_completePrivateAttr (defaultTrue). Streaming chunks are constructed via_as_chunk()with_complete=False, so__str__()returns bare text for fragments instead of wrapping each one in<thinking>...</thinking>tags"\n\n"separator from the OpenAI provider'sreasoning_summary_text.doneevent (now redundant)Companion to tidyverse/ellmer#975.
Motivation
Currently, streaming thinking content has two issues:
content="all"mode yieldsContentThinkingobjects whose__str__()independently wraps each chunk in<thinking>...</thinking>— printing produces repeated tags around each fragmentcontent="text"mode yields thinking as bare strings indistinguishable from response textAfter this change, concatenating a
content="text"stream produces:And calling
str()on individualContentThinkingchunks incontent="all"mode returns the raw thinking text without tag wrapping.Test plan