Skip to content

Conversation

jpvajda
Copy link
Contributor

@jpvajda jpvajda commented Jul 15, 2025

fixes #553

Summary of Changes Made:

1. Fixed Missing Method Implementation

  • ✅ Added inject_agent_message() method to sync client (client.py)
  • ✅ Added inject_agent_message() method to async client (async_client.py)
  • ✅ Both methods follow the exact same pattern as inject_user_message()
  • ✅ Proper error handling, logging, and type validation

2. Created Comprehensive Unit Tests

  • 14 unit tests covering both sync and async functionality
  • Options serialization tests - ensures JSON format is correct
  • Success path tests - verifies normal operation
  • Error handling tests - invalid types, None parameters, send failures
  • Integration tests - compares with inject_user_message
  • Cross-compatibility tests - ensures both methods work together

3. Verified Complete Functionality

  • 48 total agent tests pass - including our new 14 tests
  • No existing functionality broken - all previous tests still pass
  • Demonstration script confirms the fix works perfectly

Test Results:

============================================== test session starts ===============================================
collected 48 items

✅ All 48 agent tests passed in 0.16s
✅ 14 new inject_agent_message tests passed
✅ 3 existing inject_user_message tests still pass
✅ No regressions in existing functionality

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)

Summary by CodeRabbit

  • New Features

    • Added the ability to inject agent messages over WebSocket connections, allowing immediate triggering of agent statements in both synchronous and asynchronous clients.
  • Tests

    • Introduced comprehensive unit and integration tests to ensure correct behavior, error handling, and compatibility of the new agent message injection feature.

@jpvajda jpvajda requested a review from lukeocodes July 15, 2025 18:49
Copy link
Contributor

coderabbitai bot commented Jul 15, 2025

Walkthrough

A new inject_agent_message method has been implemented in both synchronous and asynchronous agent WebSocket clients, enabling immediate agent message injection using InjectAgentMessageOptions. Comprehensive unit and integration tests were also added to validate the new methods, including error handling and message serialization.

Changes

File(s) Change Summary
deepgram/clients/agent/v1/websocket/client.py
deepgram/clients/agent/v1/websocket/async_client.py
Added inject_agent_message method to both sync and async AgentWebSocketClient classes, including type checks, logging, and message sending logic.
tests/unit_test/test_unit_agent_inject_agent_message.py Added extensive unit and integration tests for the new inject_agent_message methods, including error cases and serialization checks.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant AgentWebSocketClient
    participant WebSocket

    User->>AgentWebSocketClient: inject_agent_message(options)
    AgentWebSocketClient->>AgentWebSocketClient: Validate options type
    AgentWebSocketClient->>WebSocket: send(str(options))
    WebSocket-->>AgentWebSocketClient: Send result (success/failure)
    AgentWebSocketClient-->>User: Return True/False
Loading
sequenceDiagram
    participant User
    participant AsyncAgentWebSocketClient
    participant WebSocket

    User->>AsyncAgentWebSocketClient: await inject_agent_message(options)
    AsyncAgentWebSocketClient->>AsyncAgentWebSocketClient: Validate options type
    AsyncAgentWebSocketClient->>WebSocket: await send(str(options))
    WebSocket-->>AsyncAgentWebSocketClient: Send result (success/failure)
    AsyncAgentWebSocketClient-->>User: Return True/False
Loading

Assessment against linked issues

Objective Addressed Explanation
Add inject_agent_message to sync and async clients (#553)
Method accepts InjectAgentMessageOptions parameter (#553)
Method returns boolean success/failure (#553)
Method sends proper WebSocket message format and handles errors (#553)
Tests: method exists, is callable, error handling, works in sync/async, serialization checks (#553)

Possibly related PRs

  • deepgram/deepgram-python-sdk#546: Adds the inject_agent_message method to both synchronous and asynchronous agent websocket clients, directly extending the pattern of inject_user_message.

Suggested reviewers

  • naomi-lgbt
  • lukeocodes

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/async_client.py
deepgram/clients/agent/v1/websocket/client.py
tests/unit_test/test_unit_agent_inject_agent_message.py

📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between eca4b1e and 25eb7c7.

📒 Files selected for processing (3)
  • deepgram/clients/agent/v1/websocket/async_client.py (1 hunks)
  • deepgram/clients/agent/v1/websocket/client.py (1 hunks)
  • tests/unit_test/test_unit_agent_inject_agent_message.py (1 hunks)
🧰 Additional context used
🧠 Learnings (4)
📓 Common learnings
Learnt from: dvonthenen
PR: deepgram/deepgram-python-sdk#424
File: deepgram/clients/speak/v1/response.py:48-209
Timestamp: 2024-10-09T02:19:48.728Z
Learning: User dvonthenen prefers to defer certain suggestions, specifically regarding error handling and documentation enhancements in new data classes of `deepgram/clients/speak/v1/response.py`, and may revisit them later.
Learnt from: dvonthenen
PR: deepgram/deepgram-python-sdk#424
File: deepgram/clients/speak/v1/response.py:48-209
Timestamp: 2024-06-27T00:06:23.128Z
Learning: User dvonthenen prefers to defer certain suggestions, specifically regarding error handling and documentation enhancements in new data classes of `deepgram/clients/speak/v1/response.py`, and may revisit them later.
deepgram/clients/agent/v1/websocket/async_client.py (4)
Learnt from: dvonthenen
PR: deepgram/deepgram-python-sdk#431
File: deepgram/clients/listen/v1/websocket/async_client.py:30-30
Timestamp: 2024-10-09T02:19:46.087Z
Learning: The unused import `LiveOptions` in `deepgram/clients/listen/v1/websocket/async_client.py` is intentional and will be used in PR #432.
Learnt from: dvonthenen
PR: deepgram/deepgram-python-sdk#431
File: deepgram/clients/listen/v1/websocket/async_client.py:30-30
Timestamp: 2024-07-11T14:10:24.647Z
Learning: The unused import `LiveOptions` in `deepgram/clients/listen/v1/websocket/async_client.py` is intentional and will be used in PR #432.
Learnt from: dvonthenen
PR: deepgram/deepgram-python-sdk#431
File: deepgram/clients/listen/v1/websocket/client.py:30-30
Timestamp: 2024-07-11T14:10:17.231Z
Learning: The `LiveOptions` import in `deepgram/clients/listen/v1/websocket/client.py` is intentionally present for future use and should not be flagged for removal in reviews.
Learnt from: dvonthenen
PR: deepgram/deepgram-python-sdk#431
File: deepgram/clients/listen/v1/websocket/client.py:30-30
Timestamp: 2024-10-09T02:19:46.087Z
Learning: The `LiveOptions` import in `deepgram/clients/listen/v1/websocket/client.py` is intentionally present for future use and should not be flagged for removal in reviews.
deepgram/clients/agent/v1/websocket/client.py (6)
Learnt from: dvonthenen
PR: deepgram/deepgram-python-sdk#431
File: deepgram/clients/listen/v1/websocket/async_client.py:30-30
Timestamp: 2024-10-09T02:19:46.087Z
Learning: The unused import `LiveOptions` in `deepgram/clients/listen/v1/websocket/async_client.py` is intentional and will be used in PR #432.
Learnt from: dvonthenen
PR: deepgram/deepgram-python-sdk#431
File: deepgram/clients/listen/v1/websocket/async_client.py:30-30
Timestamp: 2024-07-11T14:10:24.647Z
Learning: The unused import `LiveOptions` in `deepgram/clients/listen/v1/websocket/async_client.py` is intentional and will be used in PR #432.
Learnt from: dvonthenen
PR: deepgram/deepgram-python-sdk#431
File: deepgram/clients/listen/v1/websocket/client.py:30-30
Timestamp: 2024-07-11T14:10:17.231Z
Learning: The `LiveOptions` import in `deepgram/clients/listen/v1/websocket/client.py` is intentionally present for future use and should not be flagged for removal in reviews.
Learnt from: dvonthenen
PR: deepgram/deepgram-python-sdk#431
File: deepgram/clients/listen/v1/websocket/client.py:30-30
Timestamp: 2024-10-09T02:19:46.087Z
Learning: The `LiveOptions` import in `deepgram/clients/listen/v1/websocket/client.py` is intentionally present for future use and should not be flagged for removal in reviews.
Learnt from: dvonthenen
PR: deepgram/deepgram-python-sdk#426
File: deepgram/clients/listen/v1/websocket/__init__.py:8-8
Timestamp: 2024-07-01T19:21:39.778Z
Learning: Unused imports in `deepgram/clients/listen/v1/websocket/__init__.py` are retained to maintain backward compatibility and should not be flagged for removal in reviews.
Learnt from: dvonthenen
PR: deepgram/deepgram-python-sdk#426
File: deepgram/clients/listen/v1/websocket/__init__.py:8-8
Timestamp: 2024-10-09T02:19:46.086Z
Learning: Unused imports in `deepgram/clients/listen/v1/websocket/__init__.py` are retained to maintain backward compatibility and should not be flagged for removal in reviews.
tests/unit_test/test_unit_agent_inject_agent_message.py (2)
Learnt from: dvonthenen
PR: deepgram/deepgram-python-sdk#417
File: deepgram/clients/live/v1/client.py:14-14
Timestamp: 2024-10-09T02:19:48.728Z
Learning: Ignore suggestions to change import paths to local versions in test cases and examples as per user preference to use the actual `deepgram-sdk` package for testing.
Learnt from: dvonthenen
PR: deepgram/deepgram-python-sdk#417
File: deepgram/clients/live/v1/client.py:14-14
Timestamp: 2024-06-12T18:02:10.651Z
Learning: Ignore suggestions to change import paths to local versions in test cases and examples as per user preference to use the actual `deepgram-sdk` package for testing.
🧬 Code Graph Analysis (2)
deepgram/clients/agent/v1/websocket/async_client.py (4)
deepgram/clients/agent/v1/websocket/client.py (1)
  • inject_agent_message (643-665)
deepgram/clients/agent/v1/websocket/options.py (1)
  • InjectAgentMessageOptions (418-424)
deepgram/utils/verboselogs/__init__.py (2)
  • spam (155-158)
  • notice (150-153)
deepgram/clients/common/v1/abstract_async_websocket.py (1)
  • send (389-446)
deepgram/clients/agent/v1/websocket/client.py (3)
deepgram/clients/agent/v1/websocket/async_client.py (1)
  • inject_agent_message (647-669)
deepgram/clients/agent/v1/websocket/options.py (1)
  • InjectAgentMessageOptions (418-424)
deepgram/utils/verboselogs/__init__.py (2)
  • spam (155-158)
  • notice (150-153)
🔇 Additional comments (3)
deepgram/clients/agent/v1/websocket/client.py (1)

643-665: LGTM! Well-implemented method following established patterns.

The inject_agent_message method correctly mirrors the existing inject_user_message method's implementation pattern, maintaining consistency in:

  • Type validation with isinstance() check
  • Error handling and logging at appropriate levels
  • Return semantics (boolean success/failure)
  • Logging entry/exit points with spam level
  • Notice-level logging for operations

The implementation is clean, consistent, and follows the established codebase patterns.

deepgram/clients/agent/v1/websocket/async_client.py (1)

647-669: LGTM! Async method properly implemented.

The async inject_agent_message method correctly follows the established async patterns:

  • Proper use of await for the send operation
  • Consistent error handling and logging with the sync version
  • Maintains the same method signature and return semantics
  • Follows the existing async method patterns in the codebase

The implementation is clean and consistent with both the sync version and other async methods in the class.

tests/unit_test/test_unit_agent_inject_agent_message.py (1)

1-200: Excellent comprehensive test coverage!

This test file provides thorough coverage of the new inject_agent_message functionality:

Strengths:

  • Tests both sync and async implementations
  • Covers JSON serialization and default values
  • Tests success/failure scenarios with proper mocking
  • Validates error handling for invalid inputs
  • Integration tests compare agent vs user message options
  • Tests verify method existence and callable nature
  • Proper use of pytest fixtures and async markers

Coverage includes:

  • Options serialization and default values
  • Send method success/failure scenarios
  • Type validation and error handling
  • Cross-compatibility with existing inject_user_message
  • Async/await functionality

The test structure is well-organized with clear test names and comprehensive scenarios.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/inject-agent-message

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 auto-generate unit tests to generate unit tests for 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.

@jpvajda jpvajda merged commit c0e864b into main Jul 21, 2025
5 checks passed
@jpvajda jpvajda deleted the fix/inject-agent-message branch July 21, 2025 15:22
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.

Inject agent message missing method

2 participants