Repository navigation
Stream reasoning deltas and stop timing out between events - #65
Merged
Merged
Conversation
`HttpSseStream::get_next_event` busy-polled the event queue every 1 ms and failed with `Timeout waiting for next event` after 30 seconds without a queued event. Reasoning models routinely stay silent longer than that: Claude Opus 5.5 and Fable 5.2 always think and omit the thinking text by default, and OpenAI-compatible servers stream reasoning in fields the parser ignored, so a long reasoning phase failed the stream although the connection was alive. - The consumer now blocks on a condition variable until an event arrives or the stream completes. A stalled connection is still detected: the stream thread's HTTP read timeout fails the request, which pushes an error event and completes the stream. - Add `kStreamEventTypeReasoningDelta` with the text in `StreamEvent::reasoning_delta`, emitted for Anthropic `thinking_delta` and for the `reasoning_content` (DeepSeek, vLLM, SGLang) and `reasoning` (OpenRouter, Ollama) delta fields of OpenAI-compatible servers. Based on #60 by @tv1wnd. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…-timeout # Conflicts: # include/ai/types/enums.h # src/providers/openai/openai_stream.cpp
…-timeout # Conflicts: # src/providers/openai/openai_stream.cpp # tests/unit/anthropic_stream_test.cpp
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit f141720. Configure here.
`mark_complete` pushed the finish event (waking the consumer) and set `is_complete_` in a later critical section. A consumer that had dequeued the finish event could see an empty, incomplete queue, wait, and then yield the empty sentinel event after the finish. Both now happen under `queue_mutex_` in one step, so `has_more_events` is false once the finish event has been consumed. Reported by Cursor Bugbot in #65 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
alexey-milovidov
added a commit
that referenced
this pull request
Nov 27, 2026
Conflicts (both sides kept): the component list in `cmake/ai-sdk-cpp-config.cmake.in` (`google` plus the optional `bedrock`), the component example subdirectories and messages in `examples/CMakeLists.txt`, and the provider lists in `README.md`. With #65 on main, `StreamEvent::reasoning` carries text, so the Gemini stream now emits thought parts (`thought: true`) as reasoning events, and streamed requests that think ask for them with `includeThoughts`. `GoogleStreamImpl` only uses `push_event` and `mark_complete`, which work unchanged with the new `HttpSseStream` queue. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

HttpSseStream::get_next_eventbusy-polled the event queue every 1 ms and failed withTimeout waiting for next eventafter 30 seconds without a queued event. Reasoning models routinely stay silent longer than that: Claude Opus 5.5 and Fable 5.2 always think, and omit the thinking text by default, and the Anthropicping/thinking_deltaevents were never queued. As a result, a long reasoning phase failed the stream even though the connection was alive. #60 fixed this forreasoning_contentby pushing an empty event; this PR finishes that work:kStreamEventTypeReasoningDeltaevents now carry the text inStreamEvent::reasoning_delta(is_reasoning_delta,StreamEvent::reasoning). They are emitted for Anthropicthinking_delta, and for thereasoning_content(DeepSeek, vLLM, SGLang) andreasoning(OpenRouter, Ollama) delta fields of OpenAI-compatible servers.concurrentqueueis no longer used by the library code; I left the dependency in CMake to avoid conflicting with #38, so it can be removed separately.Related: #60
🤖 Generated with Claude Code
Note
Medium Risk
Changes core SSE consumer concurrency and event semantics; incorrect finish/complete ordering could hang or duplicate events, but behavior is covered by new unit tests.
Overview
Reasoning chunks from providers are now surfaced as first-class stream events instead of empty placeholders or ignored SSE deltas.
StreamEventgainsreasoning_delta,StreamEvent::reasoning(), andis_reasoning_delta(). Anthropic mapsthinking_deltato those events (text may be empty when the API omits thinking). OpenAI-compatible parsers emit reasoning for bothreasoning_contentandreasoningdelta keys.HttpSseStreamreplaces the lock-free queue and 30s busy-poll timeout with a mutex-protecteddequeand condition-variable wait, so consumers can block through long silent “thinking” gaps without false timeouts; stalls are still bounded by the stream thread’s HTTP read timeout (~120s).mark_complete()queues the finish event and sets completion under the same lock so callers do not wait for spurious events after the terminal finish.Reviewed by Cursor Bugbot for commit 137dd07. Bugbot is set up for automated code reviews on this repo. Configure here.