Skip to content

Stream reasoning deltas and stop timing out between events - #65

Merged
alexey-milovidov merged 5 commits into
mainfrom
stream-reasoning-idle-timeout
Nov 27, 2026
Merged

alexey-milovidov merged 5 commits into
mainfrom
stream-reasoning-idle-timeout

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

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 the Anthropic ping/thinking_delta events were never queued. As a result, a long reasoning phase failed the stream even though the connection was alive. #60 fixed this for reasoning_content by pushing an empty event; this PR finishes that work:

  • The consumer now blocks on a condition variable (no polling) until an event arrives or the stream completes. There is no consumer-side timeout: a stalled connection is still detected, because the HTTP read timeout of the stream thread (120 s without any bytes) fails the request, which pushes an error event and completes the stream.
  • kStreamEventTypeReasoningDelta events now carry the text in StreamEvent::reasoning_delta (is_reasoning_delta, StreamEvent::reasoning). They are emitted for Anthropic thinking_delta, and for the reasoning_content (DeepSeek, vLLM, SGLang) and reasoning (OpenRouter, Ollama) delta fields of OpenAI-compatible servers.

concurrentqueue is 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.

StreamEvent gains reasoning_delta, StreamEvent::reasoning(), and is_reasoning_delta(). Anthropic maps thinking_delta to those events (text may be empty when the API omits thinking). OpenAI-compatible parsers emit reasoning for both reasoning_content and reasoning delta keys.

HttpSseStream replaces the lock-free queue and 30s busy-poll timeout with a mutex-protected deque and 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.

alexey-milovidov and others added 2 commits November 27, 2026 00:31
`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
@alexey-milovidov alexey-milovidov self-assigned this Nov 27, 2026
alexey-milovidov and others added 2 commits November 27, 2026 19:45
…-timeout

# Conflicts:
#	src/providers/openai/openai_stream.cpp
#	tests/unit/anthropic_stream_test.cpp
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ 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.

Comment thread src/providers/http_sse_stream.cpp Resolved
`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
alexey-milovidov merged commit 38d6270 into main Nov 27, 2026
3 checks passed
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>
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