Repository navigation
feat: report stream events the parsers skip (DUX-14977) #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module RubyLLM | ||
| SkippedStreamEvent = Struct.new(:provider, :model, :site, :event_type, :byte_size, :error_class, | ||
| keyword_init: true) | ||
|
|
||
| # A streamed event a parser could not decode and dropped. Passed to config.on_stream_parse_error; | ||
| # its fields are documented in docs/_core_features/streaming.md and must stay stable. | ||
| class SkippedStreamEvent | ||
| SITES = %i[sse_data event_payload event_type_header exception_type_header content_block_delta].freeze | ||
|
|
||
| def self.report(event, config) | ||
| RubyLLM.logger.warn("RubyLLM skipped a stream event it could not parse: #{event.log_fields}") | ||
| config.on_stream_parse_error&.call(event) | ||
| rescue StandardError => e | ||
| RubyLLM.logger.warn("RubyLLM on_stream_parse_error hook raised #{e.class}: #{e.message}") | ||
| end | ||
|
|
||
| def log_fields | ||
| to_h.map { |key, value| "#{key}=#{value.nil? ? '-' : value}" }.join(' ') | ||
| end | ||
|
|
||
| # Mixed into the streaming parsers. A skip never logs the payload itself, only its size. | ||
| module Reporting | ||
| private | ||
|
|
||
| def skip_stream_event(site, payload, event_type: nil, error: nil) | ||
| event = SkippedStreamEvent.new( | ||
| provider: @provider&.slug, | ||
| model: @model&.id, | ||
| site: site, | ||
| event_type: event_type, | ||
| byte_size: skipped_payload_bytesize(payload), | ||
| error_class: error&.class&.name | ||
| ).freeze | ||
| @stream_accumulator&.skip_event | ||
| SkippedStreamEvent.report(event, @config || RubyLLM.config) | ||
| nil | ||
| end | ||
|
|
||
| def skipped_payload_bytesize(payload) | ||
| payload.respond_to?(:bytesize) ? payload.bytesize : payload&.size | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A payload that is valid JSON but not an object still crashes the stream instead of being reported as a skip.
JSON.parsereturnsnil, an Integer, or an Array fornull,5, or[...]. Thenouter['bytes']raisesNoMethodErrororTypeError, and the rescue only catchesJSON::ParserError. The same thing happens whenbytesdecodes to a non-object: Converse'sdecode_eventspasses it on tonest_event_under_type/build_chunk, which callevent.key?/event.digand raise. The streaming guide says:event_payloadcovers payloads that were "not valid JSON or not a JSON object", so the Converse path doesn't do what the docs say. The non-Hash branch added inInvokeAnthropic#decode_invoke_eventonly catches part of these cases. The fix is to make the guard here cover both protocols:With that in place, the separate
unless data.nil?skip ininvoke_anthropic/streaming.rbcan go. Add a Converse spec for anullor array payload.