Repository navigation
fix(tokenusage): handle split Responses API completion events - #56
Closed
buvidk1234 wants to merge 1 commit into
Closed
buvidk1234 wants to merge 1 commit into
buvidk1234 wants to merge 1 commit into
Conversation
Signed-off-by: buvidk <1532493290@qq.com>
|
In this issue, we should consider the event fragment is really big, refer to this issue . |
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.
Summary
Fix token usage extraction for large OpenAI Responses API streaming responses
whose
response.completedevent is split across multiple callbacks.Closes #55
Changes
event: response.completedis received.Root cause
The previous implementation waited for the JSON value
"response.completed".That value is located near the end of a Responses API completion event.
When the event was split, the earlier
{"response": ...}fragments were notbuffered. The final fragment contained usage but was not valid standalone JSON,
so
response.usage.*resolved to zero.Relation to #54
This PR is related to #54 but intentionally keeps the production change scoped
to the callback sequence reproduced from the reported request:
introduced in fix(tokenusage): Fixed the issue where the openai/v1/responses API could not count tokens after SSE messages were truncated due to a large amount of data #17 while adding the standard SSE event marker.
GetTokenUsagedoes notreceive
isLastChunk, so it cannot distinguish an empty non-EOS callbackfrom an empty EOS callback that may need to flush a pending event.
leave
response.usage.*in an invalid fragment and make usage extractionreturn zero.
EOS-aware flushing and bounded buffering are useful hardening work, but require
separate behavioral contracts so they do not trade one token-accounting failure
for another.
Verification
The regression test returns:
All tests pass:
go test ./...