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 HTTPX Streaming #448

Merged
merged 1 commit into from
Aug 14, 2024

Conversation

davidvonthenen
Copy link
Contributor

@davidvonthenen davidvonthenen commented Aug 6, 2024

Proposed changes

After a recent rebase, I broke the httpx stream functionality when doing a compare with meld. This feature hasn't gone through a release yet, so good that I caught it.

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

    • Introduced a text-to-speech functionality that enables real-time playback of synthesized speech using Deepgram's SDK.
  • Bug Fixes

    • Improved request handling by ensuring the "transport" key is only removed when present, enhancing function clarity.
  • Improvements

    • Modified the management of the HTTP client to allow better control of its lifecycle, which may improve performance but requires careful handling to avoid potential resource leaks.
  • Dependencies

    • Added new package dependencies: sounddevice==0.4.7 and numpy==2.0.1 for enhanced audio processing capabilities.

Copy link
Contributor

coderabbitai bot commented Aug 6, 2024

Walkthrough

The recent changes to the Deepgram client files include updates to the _handle_request_raw function in both asynchronous and synchronous clients. The async client now eliminates a redundant transport key handling, improving code clarity. In the sync client, context management for the httpx.Client has been modified, which could lead to resource management issues due to the absence of automatic closure. Additionally, new dependencies were added to example requirements, and a new text-to-speech implementation was introduced, enhancing functionality.

Changes

File Change Summary
deepgram/clients/abstract_async_client.py, deepgram/clients/abstract_sync_client.py Async client simplifies transport key handling; sync client modifies httpx.Client management, risking resource leaks.
examples/requirements-examples.txt Added sounddevice==0.4.7 and numpy==2.0.1 for enhanced audio processing and numerical computations.
examples/text-to-speech/rest/raw/hello_world_play/main.py New file implements text-to-speech functionality with real-time audio playback using Deepgram SDK.

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 78ec6d9 and d30f1e7.

Files selected for processing (2)
  • deepgram/clients/abstract_async_client.py (1 hunks)
  • deepgram/clients/abstract_sync_client.py (1 hunks)
Files skipped from review due to trivial changes (1)
  • deepgram/clients/abstract_async_client.py
Additional comments not posted (2)
deepgram/clients/abstract_sync_client.py (2)

340-340: Potential resource management issue.

The removal of the with statement means the httpx.Client instance will not be automatically closed, potentially leading to resource leaks if not properly managed elsewhere.

Ensure that the httpx.Client instance is explicitly closed after use to prevent resource leaks.


341-342: Verify the correctness of transport key removal.

The transport key is removed from kwargs after the httpx.Client instance is created. Ensure that this change does not affect the intended behavior of the function.

Verification successful

The removal of the transport key from kwargs is correct.

The transport key is used to initialize the httpx.Client or httpx.AsyncClient instance and is not required for the request itself. Removing it from kwargs ensures that only relevant parameters are passed to the request.

  • abstract_sync_client.py:

    transport = kwargs.get("transport")
    client = httpx.Client(timeout=timeout, transport=transport)
    if transport:
        kwargs.pop("transport")
    req = client.build_request(method, _url, headers=_headers, **kwargs)
  • abstract_async_client.py:

    transport = kwargs.get("transport")
    client = httpx.AsyncClient(timeout=timeout, transport=transport)
    if transport:
        kwargs.pop("transport")
    req = client.build_request(method, _url, headers=_headers, **kwargs)
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the correctness of `transport` key removal from `kwargs` in `_handle_request_raw`.

# Test: Search for the function usage. Expect: Only occurrences of the new behavior.
rg --type python -A 5 $'_handle_request_raw'

Length of output: 74


Script:

#!/bin/bash
# Description: Verify the correctness of `transport` key removal from `kwargs` in `_handle_request_raw`.

# Test: Search for the function usage. Expect: Only occurrences of the new behavior.
rg --glob '*.py' -A 5 '_handle_request_raw'

Length of output: 1704


Script:

#!/bin/bash
# Description: Extract the full implementation of the `_handle_request_raw` function from both `abstract_sync_client.py` and `abstract_async_client.py`.

# Extract the implementation from `abstract_sync_client.py`
ast-grep --lang python --pattern $'def _handle_request_raw($_, $_, $_, $_, $_, $$$) {\n $$$\n}' deepgram/clients/abstract_sync_client.py

# Extract the implementation from `abstract_async_client.py`
ast-grep --lang python --pattern $'async def _handle_request_raw($_, $_, $_, $_, $_, $$$) {\n $$$\n}' deepgram/clients/abstract_async_client.py

Length of output: 1244


Script:

#!/bin/bash
# Description: Extract the full implementation of the `_handle_request_raw` function from both `abstract_sync_client.py` and `abstract_async_client.py`.

# Extract the function definition from `abstract_sync_client.py`
ast-grep --lang python --pattern 'def _handle_request_raw($_, $_, $_, $_, $_, $$$)' deepgram/clients/abstract_sync_client.py

# Extract the function definition from `abstract_async_client.py`
ast-grep --lang python --pattern 'async def _handle_request_raw($_, $_, $_, $_, $_, $$$)' deepgram/clients/abstract_async_client.py

Length of output: 950


Script:

#!/bin/bash
# Description: Extract the full implementation of the `_handle_request_raw` function from both `abstract_sync_client.py` and `abstract_async_client.py`.

# Extract the implementation from `abstract_sync_client.py`
rg 'def _handle_request_raw' -A 50 deepgram/clients/abstract_sync_client.py

# Extract the implementation from `abstract_async_client.py`
rg 'async def _handle_request_raw' -A 50 deepgram/clients/abstract_async_client.py

Length of output: 4080

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d30f1e7 and f903063.

Files selected for processing (4)
  • deepgram/clients/abstract_async_client.py (1 hunks)
  • deepgram/clients/abstract_sync_client.py (1 hunks)
  • examples/requirements-examples.txt (1 hunks)
  • examples/text-to-speech/rest/raw/hello_world_play/main.py (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • deepgram/clients/abstract_async_client.py
  • deepgram/clients/abstract_sync_client.py
Additional context used
Learnings (1)
examples/text-to-speech/rest/raw/hello_world_play/main.py (1)
Learnt from: dvonthenen
PR: deepgram/deepgram-python-sdk#426
File: examples/text-to-speech/rest/file/hello_world_new/main.py:6-6
Timestamp: 2024-07-01T18:17:36.248Z
Learning: When reviewing example files in the Deepgram Python SDK, unused imports should not be flagged for removal as they may be needed for quick logging enablement.
Additional comments not posted (3)
examples/requirements-examples.txt (2)

8-8: LGTM!

The addition of sounddevice package is appropriate for enhanced audio processing capabilities.


9-9: LGTM!

The addition of numpy package is appropriate for numerical computations.

examples/text-to-speech/rest/raw/hello_world_play/main.py (1)

102-103: LGTM!

The entry point is correctly defined and calls the main function.

@davidvonthenen davidvonthenen merged commit fff2d0e into deepgram:main Aug 14, 2024
4 checks passed
@davidvonthenen davidvonthenen deleted the fix-httpx-streaming branch August 14, 2024 19:58
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