fix(openai): parse usage details from response API response chunk#1525
Merged
fix(openai): parse usage details from response API response chunk#1525
Conversation
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.
Important
Fixes parsing of
usagedetails in_extract_streamed_response_api_response()inopenai.pyto ensure complete data extraction.usagedetails in_extract_streamed_response_api_response()inopenai.py.usagefromraw_response.usageif not present inchunk.This description was created by
for fc947dc. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Summary
Fixes missing usage data in streamed Response API responses by adding a fallback to retrieve usage from
raw_response.usagewhen not present in the chunk dictionary.Noneif only available on theraw_responseobject rather than in the chunk's__dict__oroperator to checkraw_response.usageas a fallback, ensuring usage metrics are captured correctlygetattr()for object attribute access elsewhere in the codebaseConfidence Score: 5/5
getattr()and theoroperator), doesn't introduce new dependencies, and only affects the specific function handling streamed Response API responses. The logic is sound and the change is minimal.Important Files Changed
raw_response.usagewhen not present in chunk dictionary - ensures complete usage data extraction from streamed Response API responsesFlowchart
flowchart TD A[Iterate through chunks] --> B{raw_response exists in chunk?} B -->|No| A B -->|Yes| C[Try to get usage from chunk dict] C --> D{usage found in chunk?} D -->|Yes| E[Use chunk usage] D -->|No| F[Fallback: get usage from raw_response.usage] E --> G[Continue processing] F --> G G --> H[Extract model, output, metadata] H --> I[Return model, completion, usage, metadata]Last reviewed commit: fc947dc