Skip to content

Conversation

jpvajda
Copy link
Contributor

@jpvajda jpvajda commented Jun 6, 2025

Proposed changes

  • Adds support for AWS Polly TTS provider for agent
  • Adds example for AWS Polly
  • Adds Readme how to use AWS Polly Example

Types of changes

  • 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)

Summary by CodeRabbit

  • New Features
    • Added support for AWS Polly as a text-to-speech provider, including configuration of credentials, engine type, and voice options.
    • Introduced an example script and documentation demonstrating how to integrate and use AWS Polly with the Deepgram Voice Agent.
  • Documentation
    • Added a comprehensive README with setup instructions, configuration examples, and troubleshooting tips for AWS Polly integration.

Copy link
Contributor

coderabbitai bot commented Jun 6, 2025

Walkthrough

The changes introduce AWS Polly as a supported text-to-speech provider for Deepgram Voice Agent. This includes new data structures for AWS credentials, updates to the provider configuration to support Polly-specific fields, an example Python script demonstrating integration, and comprehensive documentation for setup and troubleshooting.

Changes

File(s) Change Summary
deepgram/clients/agent/v1/websocket/options.py Added AWSPollyCredentials dataclass; updated SpeakProvider to support AWS Polly fields and voices; minor formatting cleanup.
examples/agent/aws_polly/README.md Added documentation detailing setup, configuration, and troubleshooting for AWS Polly integration.
examples/agent/aws_polly/main.py Added example script demonstrating Deepgram Voice Agent integration with AWS Polly as TTS provider.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant MainScript
    participant DeepgramClient
    participant DeepgramAgentAPI
    participant AWSPolly

    User->>MainScript: Start script
    MainScript->>DeepgramClient: Initialize with AWS Polly credentials
    MainScript->>DeepgramAgentAPI: Connect via WebSocket
    DeepgramAgentAPI->>MainScript: Connection established
    MainScript->>DeepgramAgentAPI: Send agent configuration (incl. Polly as speak provider)
    DeepgramAgentAPI->>AWSPolly: Request speech synthesis (with credentials)
    AWSPolly-->>DeepgramAgentAPI: Return synthesized audio
    DeepgramAgentAPI-->>MainScript: Deliver audio and events
    MainScript-->>User: Playback audio, handle events, keepalive loop
    User->>MainScript: Interact (e.g., keyboard interrupt)
    MainScript->>DeepgramAgentAPI: Close connection
Loading

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 Pylint (3.3.7)
deepgram/clients/agent/v1/websocket/options.py
examples/agent/aws_polly/main.py
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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: 2

🔭 Outside diff range comments (1)
deepgram/clients/agent/v1/websocket/options.py (1)

187-247: ⚠️ Potential issue

Address pylint violation for too many instance attributes.

The SpeakProvider enhancements properly support AWS Polly integration, but the class now exceeds pylint's instance attribute limit (8/7).

Consider these solutions:

  1. Add a pylint disable comment for this specific case
  2. Group related AWS Polly fields into a nested dataclass
  3. Increase the pylint limit if justified

Apply this diff to suppress the pylint warning:

 @dataclass
+# pylint: disable=too-many-instance-attributes
 class SpeakProvider(BaseResponse):
🧰 Tools
🪛 GitHub Actions: Check - lint

[error] 189-189: pylint: Too many instance attributes (8/7) (too-many-instance-attributes)

🧹 Nitpick comments (1)
examples/agent/aws_polly/main.py (1)

148-153: Consider more efficient keepalive implementation.

The current keepalive loop sends a message every second, which might be excessive. Consider using a longer interval or implementing a more sophisticated heartbeat mechanism.

Apply this diff for a more reasonable keepalive interval:

         # Keep the connection alive
         while True:
-            time.sleep(1)
+            time.sleep(30)  # Send keepalive every 30 seconds instead of every second
             keep_alive = AgentKeepAlive()
             connection.send(keep_alive.to_json())
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6380b56 and 1866b58.

📒 Files selected for processing (3)
  • deepgram/clients/agent/v1/websocket/options.py (4 hunks)
  • examples/agent/aws_polly/README.md (1 hunks)
  • examples/agent/aws_polly/main.py (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
deepgram/clients/agent/v1/websocket/options.py (1)
deepgram/clients/common/v1/shared_response.py (1)
  • BaseResponse (16-44)
🪛 LanguageTool
examples/agent/aws_polly/README.md

[uncategorized] ~63-~63: The preposition ‘to’ seems more likely in this position.
Context: ...e 1. Set up your environment variables in .env 2. Run the example: ```bash ...

(AI_HYDRA_LEO_REPLACE_IN_TO)

🪛 GitHub Actions: Check - lint
deepgram/clients/agent/v1/websocket/options.py

[error] 189-189: pylint: Too many instance attributes (8/7) (too-many-instance-attributes)

🔇 Additional comments (4)
examples/agent/aws_polly/README.md (1)

1-81: Well-structured documentation with comprehensive setup instructions.

The README provides excellent coverage of AWS Polly integration including prerequisites, configuration examples, and troubleshooting guidance. The examples correctly demonstrate the new AWSPollyCredentials usage for both IAM and STS authentication methods.

🧰 Tools
🪛 LanguageTool

[uncategorized] ~63-~63: The preposition ‘to’ seems more likely in this position.
Context: ...e 1. Set up your environment variables in .env 2. Run the example: ```bash ...

(AI_HYDRA_LEO_REPLACE_IN_TO)

deepgram/clients/agent/v1/websocket/options.py (2)

169-185: Well-implemented AWS Polly credentials dataclass.

The AWSPollyCredentials dataclass follows the established patterns in the codebase with proper field definitions, exclusion metadata, and dictionary access implementation. The support for both IAM and STS credential types provides flexibility for different AWS authentication scenarios.


210-215:

✅ Verification successful

Verify Union type handling in voice field.

The voice field now accepts Union[CartesiaVoice, str] to support both Cartesia objects and AWS Polly voice names. Ensure that downstream code properly handles both types during serialization/deserialization.


🏁 Script executed:

#!/bin/bash
# Description: Search for voice field usage to ensure proper type handling
# Expected: Find usages that can handle both CartesiaVoice objects and strings

echo "Searching for voice field usage in agent-related files..."
rg -A 3 -B 3 "\.voice" --type py | grep -E "(agent|speak|voice)"

echo -e "\nSearching for CartesiaVoice usage..."
rg -A 3 "CartesiaVoice" --type py

Length of output: 1785


🏁 Script executed:

#!/bin/bash
# Inspect serialization logic for the `voice` field in options.py

echo "Locating `to_dict` implementation in options.py..."
grep -R -n "def to_dict" deepgram/clients/agent/v1/websocket/options.py

echo -e "\nShowing the `to_dict` block around the `voice` field (lines ~190–260)..."
sed -n '190,260p' deepgram/clients/agent/v1/websocket/options.py

Length of output: 2977


🏁 Script executed:

#!/bin/bash
# Locate BaseResponse definition to inspect its to_dict implementation
echo "Searching for BaseResponse class definition..."
rg -n "class BaseResponse" -C 3 --type py

# Once found, display the to_dict method block (adjust path if needed)
# Example: if BaseResponse is in deepgram/clients/common/base.py
# sed -n '1,200p' deepgram/clients/common/base.py | grep -n -C 5 "def to_dict"

Length of output: 656


🏁 Script executed:

#!/bin/bash
# Display the BaseResponse implementation to inspect to_dict and JSON methods
echo "Showing deepgram/clients/common/v1/shared_response.py (first 200 lines):"
sed -n '1,200p' deepgram/clients/common/v1/shared_response.py

Length of output: 2260


Resolved: voice Union[CartesiaVoice, str] is correctly handled

DataClassJsonMixin’s to_dict serializes both CartesiaVoice instances (to dict) and strings natively, and the overridden __getitem__ in Options converts any dict back to CartesiaVoice via from_dict. No further changes are needed.

examples/agent/aws_polly/main.py (1)

87-95: Clarify audio configuration and sample rate consistency.

The audio input and output configurations use the same sample rate (24000), which is good for consistency. The comment about AWS Polly outputting MP3 is helpful for understanding the format choice.

Comment on lines +24 to +27
api_key = os.getenv("DEEPGRAM_API_KEY")
if not api_key:
raise ValueError("DEEPGRAM_API_KEY environment variable is not set")
print("API Key found")
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add validation for AWS credentials.

While the Deepgram API key is validated, the AWS credentials are not checked before usage, which could lead to runtime errors.

Apply this diff to add AWS credential validation:

         if not api_key:
             raise ValueError("DEEPGRAM_API_KEY environment variable is not set")
         print("API Key found")
+        
+        # Validate AWS credentials
+        aws_access_key = os.getenv("AWS_ACCESS_KEY_ID")
+        aws_secret_key = os.getenv("AWS_SECRET_ACCESS_KEY")
+        if not aws_access_key or not aws_secret_key:
+            raise ValueError("AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables are required")
+        print("AWS credentials found")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
api_key = os.getenv("DEEPGRAM_API_KEY")
if not api_key:
raise ValueError("DEEPGRAM_API_KEY environment variable is not set")
print("API Key found")
api_key = os.getenv("DEEPGRAM_API_KEY")
if not api_key:
raise ValueError("DEEPGRAM_API_KEY environment variable is not set")
print("API Key found")
# Validate AWS credentials
aws_access_key = os.getenv("AWS_ACCESS_KEY_ID")
aws_secret_key = os.getenv("AWS_SECRET_ACCESS_KEY")
if not aws_access_key or not aws_secret_key:
raise ValueError("AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables are required")
print("AWS credentials found")
🤖 Prompt for AI Agents
In examples/agent/aws_polly/main.py around lines 24 to 27, AWS credentials are
not validated before use, which can cause runtime errors. Add checks to verify
that the necessary AWS environment variables (such as AWS_ACCESS_KEY_ID and
AWS_SECRET_ACCESS_KEY) are set, and raise a clear error if any are missing,
similar to the existing validation for DEEPGRAM_API_KEY.

Comment on lines +116 to +134
credentials=AWSPollyCredentials(
type="IAM",
region="us-east-1",
access_key_id=os.getenv("AWS_ACCESS_KEY_ID", ""),
secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY", ""),
)
)

# Configure AWS Polly endpoint
options.agent.speak.endpoint = Endpoint(
method="POST", # Explicitly set the method for AWS Polly
url="https://polly.us-east-1.amazonaws.com/v1/speech", #use the correct region
headers=[
Header(
key="Content-Type",
value="application/json"
)
]
)
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix region inconsistency between credentials and endpoint.

The AWS credentials specify us-east-1 region, but this should be consistent throughout the configuration. Also, the hardcoded Polly endpoint URL may not be necessary as AWS SDKs typically handle endpoint resolution.

Apply this diff to ensure consistency:

             credentials=AWSPollyCredentials(
                 type="IAM",
-                region="us-east-1",
+                region="us-west-2",  # Match the region used in README examples
                 access_key_id=os.getenv("AWS_ACCESS_KEY_ID", ""),
                 secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY", ""),
             )
         )
 
-        # Configure AWS Polly endpoint
-        options.agent.speak.endpoint = Endpoint(
-            method="POST",  # Explicitly set the method for AWS Polly
-            url="https://polly.us-east-1.amazonaws.com/v1/speech", #use the correct region
-            headers=[
-                Header(
-                    key="Content-Type",
-                    value="application/json"
-                )
-            ]
-        )
+        # Note: AWS Polly endpoint is typically handled automatically by the AWS SDK
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
credentials=AWSPollyCredentials(
type="IAM",
region="us-east-1",
access_key_id=os.getenv("AWS_ACCESS_KEY_ID", ""),
secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY", ""),
)
)
# Configure AWS Polly endpoint
options.agent.speak.endpoint = Endpoint(
method="POST", # Explicitly set the method for AWS Polly
url="https://polly.us-east-1.amazonaws.com/v1/speech", #use the correct region
headers=[
Header(
key="Content-Type",
value="application/json"
)
]
)
credentials=AWSPollyCredentials(
type="IAM",
region="us-west-2", # Match the region used in README examples
access_key_id=os.getenv("AWS_ACCESS_KEY_ID", ""),
secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY", ""),
)
)
# Note: AWS Polly endpoint is typically handled automatically by the AWS SDK
🤖 Prompt for AI Agents
In examples/agent/aws_polly/main.py around lines 116 to 134, ensure the AWS
region specified in the credentials matches the region used in the Polly
endpoint configuration. Remove the hardcoded Polly endpoint URL and rely on the
AWS SDK's default endpoint resolution to maintain consistency and avoid region
mismatches.

@jpvajda
Copy link
Contributor Author

jpvajda commented Jun 6, 2025

More to do here as the example isn't running correctly yet, getting this error:

API Key found
Created WebSocket connection...
Starting connection...

Connection opened: {
    "type": "Open"
}


Welcome message: {
    "type": "Welcome",
    "request_id": "7d6bf5df-849e-471a-8198-f47be1aa2859"
}

Connection started successfully!
Press Ctrl+C to exit...

Error occurred: {
    "description": "Error parsing client message. Check the agent.speak.endpoint.headers field against the API spec.",
    "message": "",
    "type": "Error"
}

@jpvajda
Copy link
Contributor Author

jpvajda commented Jun 9, 2025

closing for #532 which will be our approach going forward for the Agent Provider objects.

@jpvajda jpvajda closed this Jun 9, 2025
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