Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: failure when usage is none #1253

Merged
merged 2 commits into from
May 14, 2024
Merged

Conversation

aa875982361
Copy link

@aa875982361 aa875982361 commented May 8, 2024

User description

Features

Feature Docs

Influence

Result

Other


PR Type

Bug fix


Description

  • Fixed an issue where the application could encounter errors if chunk.usage was present but had a None value. Now, the code checks that chunk.usage is truthy before proceeding to use it.

Changes walkthrough 📝

Relevant files
Bug fix
openai_api.py
Fix handling of `None` value in `chunk.usage`                       

metagpt/provider/openai_api.py

  • Added a condition to check if chunk.usage is not only present but also
    truthy before using it.
  • +1/-1     

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Copy link

    PR Description updated to latest commit (8695629)

    Copy link

    PR Review 🔍

    ⏱️ Estimated effort to review [1-5]

    2, because the PR involves a simple conditional check to ensure that 'chunk.usage' is not only present but also truthy before proceeding. The change is minimal and straightforward, making the review process relatively quick and easy.

    🧪 Relevant tests

    No

    ⚡ Possible issues

    Possible Bug: The code checks if 'chunk.usage' is truthy, which is generally fine. However, if 'chunk.usage' can be a valid falsy value (like 0 or an empty list, depending on the context), this might unintentionally skip valid usage data. It would be beneficial to clarify what types of values 'chunk.usage' can hold and if any of these might be falsy yet valid.

    🔒 Security concerns

    No

    Copy link

    PR Code Suggestions ✨

    CategorySuggestions                                                                                                                                                       
    Enhancement
    Simplify attribute checking and value retrieval using getattr.

    Replace the check hasattr(chunk, "usage") and chunk.usage with a more Pythonic approach
    using getattr. This avoids accessing the attribute twice and handles potential None values
    more cleanly.

    metagpt/provider/openai_api.py [103]

    -if hasattr(chunk, "usage") and chunk.usage:
    +if getattr(chunk, "usage", None):
     

    @codecov-commenter
    Copy link

    codecov-commenter commented May 8, 2024

    Codecov Report

    All modified and coverable lines are covered by tests ✅

    Project coverage is 70.19%. Comparing base (f201b2f) to head (a473c74).

    ❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

    Additional details and impacted files
    @@            Coverage Diff             @@
    ##             main    #1253      +/-   ##
    ==========================================
    - Coverage   70.22%   70.19%   -0.04%     
    ==========================================
      Files         316      316              
      Lines       18860    18860              
    ==========================================
    - Hits        13245    13239       -6     
    - Misses       5615     5621       +6     

    ☔ View full report in Codecov by Sentry.
    📢 Have feedback on the report? Share it here.

    @garylin2099
    Copy link
    Collaborator

    LGTM

    @chg0901
    Copy link

    chg0901 commented May 19, 2024

    nice

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    4 participants