Generate Stream with Thinking
#OVERVIEW
- Add Streaming Line Classifier
- Add Generate Stream with Thinking API
Related PR: #1819 , #1864
@jstar0 / @arnabnandy7 - please integrate with your PR and test e2e
In your PR, please use StreamingLlmOperationsImp from this PR
@arimu1 @jorander - FYI
Thank you
@jorander forwarded your request on Claude to Alex H. Please contact me on Discord. Thanks
Quality Gate passed
Issues
0 New issues
0 Accepted issues
Measures
0 Security Hotspots
100.0% Coverage on New Code
0.0% Duplication on New Code
@igordayen #1864 is a replacement for #1819, not sure why we are treating it separately.
@arnabnandy7 Got it, then please integrate #1864. I assume #1819 should be closed then.
Any inquiries - let me know. Thanks.
Thanks, @igordayen . I reviewed the StreamingLlmOperationsImpl changes in #1873 specifically, as requested.
The placement is compatible with #1864: generateStreamWithThinking() runs above doTransformStream(), so it receives the final text stream from streamWithToolLoop(). I also agree that moving reusable classification logic into embabel-agent-ai is a good direction.
However, I don’t think we can use the implementation unchanged because its current classification contract comes from object streaming:
- ordinary non-JSON text is emitted as
Thinking(CONTINUATION) - JSON lines are dropped;
- no
StreamingEvent.Objectis emitted for the final text answer.
For example:
<think>reasoning</think>
Final answer
the implementation in #1873 produces two thinking events. The contract in #1864 requires:
Thinking("reasoning")
Object("Final answer")
There are also a few streaming differences to reconcile:
rawChunksToLines()waits for a newline and therefore buffers long reasoning or answer paragraphs- it trims lines and removes blank lines, changing the generated text
- its mutable buffer is not scoped with
Flux.defer, so repeated or concurrent subscriptions can share parser state - multi-line closing tags may remain in the emitted content
- dynamic tags with attributes are not handled equivalently
I suggest we reuse the architectural direction from #1873, but first evolve StreamingLineClassifier into a subscription-scoped text-stream classifier that emits both Thinking and Object, preserves formatting, and handles arbitrary chunk boundaries. At that point, #1864 can delegate to it instead of maintaining separate parsing logic.
I’m happy to integrate that version and add end-to-end coverage across the tool loop. Also, #1819 can be closed because #1864 replaces it.
- no
StreamingEvent.Objectis emitted for the final text answer.
@arnabnandy7 - yes, that is the intent. Just buffering per line + emitting every line as a thinking event. That is done intentionally, not to mix concepts. Use createObject for stream if an object is required.
- no
StreamingEvent.Objectis emitted for the final text answer.@arnabnandy7 - yes, that is the intent. Just buffering per line + emitting every line as a thinking event. That is done intentionally, not to mix concepts. Use createObject for stream if an object is required.
Thanks, @igordayen. Understood that emitting every non-JSON line as Thinking is intentional in #1873.
The distinction I want to clarify is that StreamingEvent.Object<String> in #1864 does not imply JSON conversion or createObjectStream(). It represents the final text channel in the mixed stream.
For example:
<think>reasoning</think>
Final answer
needs to expose both channels to the caller:
Thinking("reasoning")
Object("Final answer")
If every line is emitted as Thinking, the caller cannot distinguish the final answer from reasoning. createObjectStream() does not cover this use case because the requested output is ordinary streamed text, not a structured object.
I also saw @jorander's concern about buffering pure text until a newline. That is another reason I don’t think #1864 can adopt StreamingLlmOperationsImpl from #1873 unchanged.
It looks like we currently have two different intended APIs:
- a thinking-only, newline-classified stream in #1873
- a mixed thinking-and-final-text stream in #1864
Before integrating them, I think we need agreement on which contract generateStreamWithThinking() should expose. I’ll keep #1864 development on hold as requested until that contract is clarified.
I suggest we reuse the architectural direction from #1873, but first evolve
StreamingLineClassifierinto a subscription-scoped text-stream classifier that emits bothThinkingandObject
==> intent was not to mix concepts. Object creation is the responsibility of the "objectCreate" API. Thanks
Thinking("reasoning") Object("Final answer")
==> Do you see a practical need there? Perhaps consider createObject
@igordayen it's done, open for review.
cc @jorander
@igordayen I add a link between the discussion in #1881 and this PR since I think they are very much connected.
Adding #1932 for references
@arnabnandy7 @jorander @arimu1 @azanux - please see updated Discussion:
https://github.com/embabel/embabel-agent/discussions/1881, if appealing (obviously, interface needs to be detailed) as a solution for any custom aggregation in streaming mode - vote for it, please. thank you
@arnabnandy7 @jorander @arimu1 @azanux - please see updated Discussion:
#1881, if appealing (obviously, interface needs to be detailed) as a solution for any custom aggregation in streaming mode - vote for it, please. thank you
Closing.