Skip to content

fix(tokenusage): merge fragmented responses events - #53

Open
ump45nose wants to merge 1 commit into
higress-group:mainfrom
ump45nose:fix_responses_sse_fragment_usage
Open

ump45nose wants to merge 1 commit into
higress-group:mainfrom
ump45nose:fix_responses_sse_fragment_usage

Conversation

@ump45nose

@ump45nose ump45nose commented Aug 11, 2026 •

Copy link
Copy Markdown

Summary

  • Buffer Responses API JSON from its opening fragment
  • Parse model and usage only after the JSON is complete
  • Add a regression test for a tail-positioned response.completed field

Problem

The existing merger starts buffering only after it sees response.completed without usage. Large Responses API events can place response.completed in the final transport fragment, after the model and usage data have already arrived in incomplete fragments. Those earlier fragments are discarded, so token extraction either returns zero or reads a nested tool schema as the model

Fix

The merger now retains an incomplete JSON object from its opening brace and appends later transport fragments until json.Valid confirms a complete event. Complete JSON events and non-JSON chunks continue through immediately

Addresses higress-group/higress#4424

Validation

Before the fix at d677f6c, the focused regression failed with an empty model and zero usage

go test ./pkg/tokenusage -run '^TestGetTokenUsageMergesFragmentedResponsesCompletedEvent$' -count=1 -v
expected: "gpt-5.6-sol"
actual:   ""

After 5320d3b:

go test ./pkg/tokenusage -count=1
ok github.com/higress-group/wasm-go/pkg/tokenusage

go vet ./pkg/tokenusage

Higress will still need to consume a wasm-go revision containing this change

@CLAassistant

CLAassistant commented Aug 11, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Signed-off-by: ump45nose <52391318+ump45nose@users.noreply.github.com>
@ump45nose
ump45nose force-pushed the fix_responses_sse_fragment_usage branch from a655487 to 5320d3b Compare August 11, 2026 18:11
@ump45nose
ump45nose marked this pull request as ready for review August 12, 2026 06:57
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.

2 participants