Skip to content

fix(openai): parse usage details from response API response chunk#1525

Merged
hassiebp merged 1 commit intomainfrom
fix-openai-response-api-usage
Feb 16, 2026
Merged

fix(openai): parse usage details from response API response chunk#1525
hassiebp merged 1 commit intomainfrom
fix-openai-response-api-usage

Conversation

@hassiebp
Copy link
Contributor

@hassiebp hassiebp commented Feb 16, 2026

Important

Fixes parsing of usage details in _extract_streamed_response_api_response() in openai.py to ensure complete data extraction.

  • Behavior:
    • Fixes parsing of usage details in _extract_streamed_response_api_response() in openai.py.
    • Now retrieves usage from raw_response.usage if not present in chunk.
  • Misc:
    • Minor change to ensure complete usage data extraction from API response chunks.

This description was created by Ellipsis 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.usage when not present in the chunk dictionary.

  • Previously, usage data would be None if only available on the raw_response object rather than in the chunk's __dict__
  • The fix uses the or operator to check raw_response.usage as a fallback, ensuring usage metrics are captured correctly
  • This is a minimal, focused fix that follows the existing pattern of using getattr() for object attribute access elsewhere in the codebase

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk - it's a simple bug fix that adds a fallback for data extraction
  • The change is a straightforward one-line fix that adds a fallback mechanism to retrieve usage data. It follows existing patterns in the codebase (using getattr() and the or operator), 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.
  • No files require special attention

Important Files Changed

Filename Overview
langfuse/openai.py Added fallback to retrieve usage from raw_response.usage when not present in chunk dictionary - ensures complete usage data extraction from streamed Response API responses

Flowchart

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]
Loading

Last reviewed commit: fc947dc

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file reviewed, no comments

Edit Code Review Agent Settings | Greptile

@hassiebp hassiebp merged commit 81b58cd into main Feb 16, 2026
7 of 12 checks passed
@hassiebp hassiebp deleted the fix-openai-response-api-usage branch February 16, 2026 16:26
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.

1 participant