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 get_usage_request when Response Not Available #473

Merged

Conversation

davidvonthenen
Copy link
Contributor

@davidvonthenen davidvonthenen commented Oct 21, 2024

Proposed changes

Address: #457

Implement example to expose error.

Types of changes

What types of changes does your code introduce to the community Python SDK?
Put an x in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update or tests (if none of the other choices apply)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • I have read the CONTRIBUTING doc
  • I have lint'ed all of my code using repo standards
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

NA

Summary by CodeRabbit

  • New Features

    • Enhanced error handling for usage requests in the Deepgram client, improving robustness and reporting.
    • Introduced a new script for managing projects and transcribing audio files via the Deepgram API.
  • Bug Fixes

    • Improved error checks to ensure expected response data is present when fetching usage requests.
  • Documentation

    • Added comments and formatting adjustments for clarity in the codebase.

Copy link
Contributor

coderabbitai bot commented Oct 21, 2024

Walkthrough

The pull request introduces modifications to the AsyncManageClient and ManageClient classes, enhancing error handling in the get_usage_request method by incorporating checks for the presence of a "response" field in the returned JSON. If the field is missing, a DeepgramError is raised. Additionally, a new script is added to tests/edge_cases/usage_to_fast/main.py, which interacts with the Deepgram API to manage projects and handle audio transcription, including error handling for various scenarios.

Changes

File Path Change Summary
deepgram/clients/manage/v1/async_client.py Added import for DeepgramError; enhanced error handling in get_usage_request method.
deepgram/clients/manage/v1/client.py Added imports for json and DeepgramError; improved error handling in get_usage_request method.
tests/edge_cases/usage_to_fast/main.py New file implementing a script to manage projects and transcribe audio files with error handling.

Possibly related PRs

  • Fix Reported Manage Issues #460: The changes in the get_usage_request method in both the main PR and this PR involve error handling and the introduction of DeepgramError, indicating a direct connection in enhancing error management related to usage requests.

Suggested reviewers

  • jpvajda
  • SandraRodgers
  • lukeocodes

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Outside diff range and nitpick comments (4)
tests/edge_cases/usage_to_fast/main.py (2)

1-22: Consider making the audio file path configurable.

The AUDIO_FILE constant is hardcoded, which might limit the script's flexibility. Consider making it configurable through environment variables or command-line arguments for easier testing with different audio files.


32-44: Improve project selection logic.

The script currently selects the first project from the list, which might not always be the desired behavior. Consider adding logic to select a specific project based on criteria or allow user input for project selection.

deepgram/clients/manage/v1/client.py (1)

1007-1014: LGTM: Improved error handling for unavailable responses.

The new error handling logic effectively addresses the issue mentioned in the PR objectives. It checks for the absence of a 'response' field and raises a DeepgramError with an informative message when the response is not available.

A minor suggestion for improvement:

Consider using a more specific exception name, such as DeepgramResponseNotAvailableError, to provide more context about the specific error condition. This would allow users to catch and handle this specific error case if needed.

-        if json_result.get("response") is None:
-            raise DeepgramError(
-                "Response is not available yet. Please try again later."
-            )
+        if json_result.get("response") is None:
+            raise DeepgramResponseNotAvailableError(
+                "Response is not available yet. Please try again later."
+            )

Don't forget to add the new exception class to the appropriate module if you decide to implement this suggestion.

deepgram/clients/manage/v1/async_client.py (1)

1004-1011: LGTM: Improved error handling for unavailable responses.

The added code effectively addresses the issue mentioned in GitHub issue #457. It properly checks for the presence of the "response" field and raises a DeepgramError with a clear message when the response is not available.

A minor suggestion for improvement:

Consider using a constant for the error message to improve maintainability. For example:

RESPONSE_NOT_AVAILABLE_ERROR = "Response is not available yet. Please try again later."

# In the method:
if json_result.get("response") is None:
    raise DeepgramError(RESPONSE_NOT_AVAILABLE_ERROR)

This change would make it easier to update the error message in the future if needed.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between eb4ed92 and 382069e.

📒 Files selected for processing (3)
  • deepgram/clients/manage/v1/async_client.py (2 hunks)
  • deepgram/clients/manage/v1/client.py (2 hunks)
  • tests/edge_cases/usage_to_fast/main.py (1 hunks)
🧰 Additional context used
🔇 Additional comments (6)
tests/edge_cases/usage_to_fast/main.py (1)

1-83: Overall assessment: Good test script with room for improvement.

This script serves its purpose as a test case for the Deepgram API, demonstrating the usage of various SDK features. The structure is clear, and it includes basic error handling. However, there are several areas where it can be improved:

  1. Make the audio file path configurable.
  2. Use environment variables for sensitive information like the API key.
  3. Improve project selection logic.
  4. Add error handling for file reading.
  5. Implement a polling mechanism for usage request retrieval.

Implementing these suggestions will make the script more robust, flexible, and suitable for various testing scenarios.

deepgram/clients/manage/v1/client.py (3)

7-7: LGTM: New imports for enhanced error handling.

The addition of json and DeepgramError imports is appropriate for the new error handling logic in the get_usage_request method.

Also applies to: 13-13


Line range hint 1-1214: Summary: Effective implementation of error handling for unavailable responses.

The changes in this PR successfully address the issue of handling unavailable responses in the get_usage_request method. The implementation is clean, focused, and consistent with the existing code style.

Key points:

  1. New imports (json and DeepgramError) support the enhanced error handling.
  2. The get_usage_request method now checks for the presence of the 'response' field and raises an informative error when it's missing.

These changes improve the robustness of the SDK and provide better feedback to users when responses are not immediately available.

Suggestions for consideration:

  1. Using a more specific exception name for clearer error handling.
  2. Reviewing other API-interacting methods for potential similar error handling needs.

Overall, this is a valuable improvement to the Deepgram Python SDK.


1007-1014: Verify if similar error handling is needed in other methods.

The new error handling for unavailable responses is a good addition to the get_usage_request method. It might be beneficial to review other methods in this class that interact with the API to see if they would benefit from similar error handling.

To help with this verification, you can run the following script:

This script will help identify other methods that use self.get and process JSON responses, which might benefit from similar error handling.

deepgram/clients/manage/v1/async_client.py (2)

7-7: LGTM: Import changes are appropriate.

The addition of the json import and the inclusion of DeepgramError in the import from ...common are well-justified changes. These modifications support the new error handling in the get_usage_request method.

Also applies to: 13-13


7-7: Summary: Effective implementation of error handling for unavailable responses.

The changes in this file successfully address the issue described in GitHub issue #457. The addition of JSON parsing and checking for the "response" field in the get_usage_request method improves the robustness of the SDK by explicitly handling cases where the response is not yet available.

Key improvements:

  1. Added json import for parsing the response.
  2. Included DeepgramError in the import from ...common.
  3. Implemented error checking and raising of DeepgramError when the response is unavailable.

These changes enhance the user experience by providing clear feedback when a response is not ready, aligning well with the PR objectives.

Also applies to: 13-13, 1004-1011

tests/edge_cases/usage_to_fast/main.py Show resolved Hide resolved
tests/edge_cases/usage_to_fast/main.py Show resolved Hide resolved
tests/edge_cases/usage_to_fast/main.py Show resolved Hide resolved
@davidvonthenen davidvonthenen merged commit 433e6f2 into deepgram:main Oct 21, 2024
5 checks passed
@davidvonthenen davidvonthenen deleted the fix-manaage-null-response branch October 21, 2024 16:07
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