feat: parse SSE streaming responses for response plugins#4
Closed
Mohammad-nassar10 wants to merge 5 commits into
Closed
feat: parse SSE streaming responses for response plugins#4Mohammad-nassar10 wants to merge 5 commits into
Mohammad-nassar10 wants to merge 5 commits into
Conversation
When the response body is not valid JSON (e.g., SSE/Server-Sent Events from streaming providers like Anthropic), parse the SSE data lines to extract usage and model information. This enables response plugins (usage-tracking, metering) to process streaming responses that were previously skipped with "Failed to parse response body as JSON". Also fixes two issues with streaming response handling: 1. Always respond to response headers so Envoy proceeds with body chunks (previously returned nil, causing per-message timeout) 2. Send an immediate ack for each non-EoS response body chunk so Envoy continues forwarding subsequent chunks instead of blocking Signed-off-by: Noy Itzikowitz <nitzikow@redhat.com>
Co-authored-by: David Breitgand <davidbreitgand@users.noreply.github.com> Signed-off-by: David Breitgand <davidbreitgand@users.noreply.github.com>
Contributor
|
@Mohammad-nassar10 , @ronenkat , PR looks good, but there is a subtle bug that I spotted. Left a suggestion (validated locally on my side) how to fix. IN SHORT: You can verify that by adding the attached unit test to // This test locks in the multiline SSE fix by asserting that there is one logical SSE event
// split across multiple `data:` lines is reconstructed before JSON parsing.
func TestParseSSEResponseBody_MultilineEvent(t *testing.T) {
body := []byte("data: {\"response\":\n" +
"data: {\"model\":\"gpt-4o\",\"usage\":{\"input_tokens\":12,\"output_tokens\":34}}}\n\n" +
"data: [DONE]\n")
got, err := parseSSEResponseBody(body)
if err != nil {
t.Fatalf("parseSSEResponseBody returned unexpected error: %v", err)
}
want := map[string]any{
"model": "gpt-4o",
"usage": map[string]any{
"input_tokens": float64(12),
"output_tokens": float64(34),
},
}
if diff := cmp.Diff(want, got); diff != "" {
t.Fatalf("parseSSEResponseBody returned unexpected result, diff(-want, +got): %s", diff)
}
}This line breaking is permitted in response and actually is often happening for SSE streaming |
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.
What type of PR is this?
/kind bug
What this PR does / why we need it:
Clone the PR #138 from main.