Repository navigation
RubyLLM2 W2-06: Report stream events the parsers skip - #48
Conversation
Fork PR #45 on the 1.16 line reports every stream event a parser cannot decode instead of dropping it silently, and Supernova relies on config.on_stream_parse_error and Message#skipped_stream_events. Bring the same interface to the 2.0 base so the wave 3 gem bump boots. Each dropped event is logged at warn, passed to the hook as a frozen SkippedStreamEvent, and counted on the streamed message. SSE data, Bedrock eventstream payloads and Converse string deltas report; a bad delta is decoded and reported once per event. Co-authored-by: Sam Boland <sam.boland@appfolio.com>
|
Review of 30d2178. Three changes requested; everything else checked out (list below).
Verified and fine:
|
I've queued all three changes, and the coding agent is working on them now.
|
The two header sites stay in SITES for 1.16 compatibility but nothing on 2.0 reports them, so the docs say so. The hook example now uses a generic logger call, and a spec covers summing skipped events across streamed pause_turn segments.
Pushed Changed 2 files (+18 −3): — via Coder session (view) |
|
🤖 Supernova Code Review — View trace — via automation "Code Review Opus 5.5 Medium" (run), installed by Sam Boland (sam.boland@appfolio.com) |
There was a problem hiding this comment.
I reviewed the skipped-stream-event port: the SSE, Bedrock eventstream, Converse delta and InvokeAnthropic paths, the accumulator counting, the pause_turn merge, the config option and the docs. I left one inline finding.
lib/ruby_llm/protocols/converse/thinking_stream.rb:21: string-encoded reasoning deltas are decoded for the chunk'sthinking, butThinkingStreamignores them. Their text and signature are silently left out ofraw_reasoning, which is used to replay the turn.
The rest looked correct to me: reporting, counting, the hook's rescue, the ensure cleanup of @stream_accumulator, and handling of exception frames.
— via automation "Code Review Opus 5.5 Medium" (run), installed by Sam Boland (sam.boland@appfolio.com)
| start = part.dig('start', 'reasoningContent') | ||
| @blocks[index] = Support::Utils.deep_dup(start) if start | ||
| delta = part.dig('delta', 'reasoningContent') | ||
| delta = part['delta']['reasoningContent'] if part['delta'].is_a?(Hash) |
There was a problem hiding this comment.
ThinkingStream now skips string-encoded deltas, while the chunk extractors decode them. That means reasoning text and signatures sent this way get dropped from raw_reasoning.
Converse::Streaming#normalized_delta JSON-decodes a string delta, so extract_thinking_delta and extract_signature_from_delta still put that reasoning into the chunk's thinking. This line only reads part['delta'] when it is already a Hash. For a valid string delta like '{"reasoningContent":{"reasoningText":{"text":"…","signature":"…"}}}', the text and signature never reach @blocks. The final raw_reasoning then loses that block or its signature. That is the data used to replay the turn verbatim, and Bedrock rejects a thinking block that is missing its signature on the next request.
Before this PR the same input raised TypeError. Now it fails silently, which is harder to notice. Decode the delta the same way the extractors do, for example by passing the already-normalized delta into ThinkingStream#add, or by having it parse a string delta itself, so both paths see the same reasoning.
What this does
Ports the 1.16 "report skipped stream events" feature (fork PR #45, which merges into
mainon the 1.16 line) onto the 2.0 base. This PR targetssupernova-2-0, notmainor upstream. It keeps the 1.16 interface exactly. Nothing in Supernova changes here.Why: Supernova's initializer sets
config.on_stream_parse_error = ->(event) { Llm::StreamEventSkipped.record(event) }and readsmessage.skipped_stream_eventswhen it saves a reply.supernova-2-0has neither, so the wave 3 gem bump would fail at boot with aNoMethodErroronon_stream_parse_error=. Before this change, a stream event a parser could not decode was dropped silently.New interface
config.on_stream_parse_error(default nil) inconfiguration.rb.Message#skipped_stream_events. It is 0 when nothing was dropped or the reply was not streamed.RubyLLM::SkippedStreamEvent(new filelib/ruby_llm/skipped_stream_event.rb) is a frozenStructwith keyword fieldsprovider,model,site,event_type,byte_sizeanderror_class.siteis one ofsse_data,event_payload,event_type_header,exception_type_header,content_block_delta.event_typeis the SSEevent:name or the Bedrock:event-typeheader, or nil.error_classis nil when the payload decoded but was not a JSON object.log_fieldsrenderskey=valuepairs joined by spaces, with nil shown as-.SkippedStreamEvent.report(event, config)logs a warning (RubyLLM skipped a stream event it could not parse: <fields>) and then calls the hook. An exception raised by the hook is rescued and logged at warn, so it never ends the stream.Reportingmixin provides a privateskip_stream_event. It builds and freezes the event, bumps the current stream accumulator's count and reports using@config || RubyLLM.config.Counting
StreamAccumulatorgainsskipped_eventsandskip_event, andto_messagepassesskipped_stream_events:toMessage.merge_turn_segmentsin the Anthropic protocol sums the count across segments, so merging pause-turn segments does not lose it.Report sites
Protocol::Streaming#handle_datareports:sse_datafor aJSON::ParserError(witherror_class) and for a payload that parses to a non-Hash (error_classnil). It now takes anevent_type:argument, whichhandle_ssepasses from the SSE event name.stream_responsesets@stream_accumulatorand clears it in anensure.EventStreamincludesReporting.decode_event_payload(payload, event_type:)reports:event_payloadfor a parse error (the outer payload or its base64bytes) and for a non-Hash result. The InvokeAnthropic stream path goes through this same method.normalized_deltais rewritten to cache the decoded delta per event, so a bad delta is reported once even though several extractors read it. A string delta that fails to parse, or is not an object, is reported as:content_block_deltaand becomes{}.Crash fixes in code the new path exposed
ThinkingStreamnow reads the delta only when it is a Hash. A string delta with a block index raisedTypeErrorand ended the stream.tool_call_delta_event?now goes throughnormalized_deltainstead of reading the rawevent.dig('delta', 'toolUse').Docs:
docs/_core_features/streaming.md,docs/_getting_started/configuration-connection.mdanddocs/_getting_started/configuration.mdare updated.models.jsonandaliases.jsonwere not touched.Review notes
event_type_headerandexception_type_headersites are inSITESfor interface parity with 1.16, but nothing reports them yet. The currentevent_headerhas no rescue that drops an event. I did not confirm whether the base ever had one.{}. TheThinkingStreamandtool_call_delta_event?edits are small robustness fixes beyond the literal scope.'returns nil for an undecodable payload'used to assert the old silent debug log. It now stubswarn.Testing
spec/ruby_llm/skipped_stream_event_spec.rb, askipped eventsblock in the Converse streaming spec, and SSE site specs inprotocol/streaming_spec.rb.invoke_anthropic_spec.rbgains end-to-end stream specs, including a bad base64bytesframe.json_schemer/regexp_parsergem incompatibility in the environment, so it was excluded. 47 failures remain (aninstance_double(Configuration)problem in connection setup, plus a pricing spec). The same 47 fail on the base commit, so there are no new failures.spec/ruby_llm/protocols/invoke_anthropic_spec.rbran withLANG=C.UTF-8, 41 examples, 0 failures, including the new pause_turn skipped-events spec, which fails with the summing line inmerge_turn_segmentsremoved.supernova-2-0, so these local runs are the only test runs.overcommithooks were run.Type of change
Scope check
Required for new features
Quality check
overcommit --installand all hooks passbundle exec rake vcr:record[provider_name]bundle exec rspecmodels.json,aliases.json)AI-generated code
API changes