Skip to content

fix: handle empty objects for providers#540

Merged
naomi-lgbt merged 3 commits into
mainfrom
fix/empty
Jun 13, 2025
Merged

fix: handle empty objects for providers#540
naomi-lgbt merged 3 commits into
mainfrom
fix/empty

Conversation

@naomi-lgbt

@naomi-lgbt naomi-lgbt commented Jun 13, 2025

Copy link
Copy Markdown
Contributor

Proposed changes

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

Summary by CodeRabbit

  • Refactor
    • Enhanced handling of optional and empty configuration fields for cleaner data serialization and optimized processing.
  • Chores
    • Increased logging verbosity in the example agent for more detailed output.
    • Corrected speech model configuration assignment for consistent provider settings in the example agent.

@coderabbitai

coderabbitai Bot commented Jun 13, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

The changes add serialization exclusion metadata to specific dataclass fields in the agent websocket options, ensuring fields are omitted from serialization when they are None, empty dictionaries, or empty instances. The __post_init__ methods are updated to avoid unnecessary conversions for empty dictionaries or empty instances. The example scripts update the logging verbosity level and correct the speech model assignment to align with the nested provider configuration pattern.

Changes

File(s) Change Summary
deepgram/clients/agent/v1/websocket/options.py Added metadata=dataclass_config(exclude=...) to provider fields in Think, Listen, Speak and to listen, think, speak in Agent. Updated __post_init__ to skip conversion for None, empty dicts, or empty instances.
examples/agent/simple/main.py Changed logging verbosity level from verboselogs.DEBUG (commented out) to active verboselogs.SPAM.
examples/agent/no_mic/main.py Corrected speech model assignment to options.agent.speak.provider.model from options.agent.speak.model to align with nested provider pattern.

Possibly related PRs

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)
examples/agent/no_mic/main.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 d062caf and 9880f83.

📒 Files selected for processing (1)
  • examples/agent/no_mic/main.py (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
examples/agent/no_mic/main.py (1)
deepgram/client.py (2)
  • agent (560-564)
  • speak (472-476)
🔇 Additional comments (1)
examples/agent/no_mic/main.py (1)

58-60:

Details

✅ Verification successful

Correct shift to the nested provider field – verify repo-wide consistency

Good catch moving the model assignment under options.agent.speak.provider; this matches the new dataclass structure introduced in the SDK.

To avoid latent bugs, please confirm no other files (examples, tests, docs) still reference the old path options.agent.speak.model.

Suggested quick check:


🏁 Script executed:

#!/bin/bash
# Find stale references to the old property path
rg -n $'\\.speak\\.model' --type py

Length of output: 34


No stale references to .speak.model remain — LGTM

  • Ran rg -n '\.speak\.model' --type py across the repo; no matches found.
  • All examples, tests, and docs now correctly use options.agent.speak.provider.model.
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Commit Unit Tests in branch fix/empty
  • Post Copyable Unit Tests in Comment

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (2)
deepgram/clients/agent/v1/websocket/options.py (2)

178-178: Same serialization gap for Listen.provider

See earlier comment – the predicate should also recognise an empty Provider instance.


195-195: Same serialization gap for Speak.provider

Empty Provider() objects will still leak into the wire format.

🧹 Nitpick comments (4)
deepgram/clients/agent/v1/websocket/options.py (3)

158-159: __post_init__ guard is getting unwieldy

The triple condition is beginning to hurt readability and is now duplicated in every model.
Consider extracting a small helper (e.g. _is_empty_provider(obj)) or re-using the lambda above to keep the rule in a single place.


181-182: Duplication of post-init logic

Listen.__post_init__ repeats the same conversion logic found in Think and Speak.
Factor this into a mix-in or utility to avoid three copies.


201-202: Repeated conversion guard

As above, refactor to a shared helper to ease future maintenance.

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

45-45: Verbosity level cranked to SPAM – deliberate?

verboselogs.SPAM is extremely chatty and will flood stdout in examples.
If this is meant only for local debugging, consider downgrading to DEBUG or gating it behind an environment variable so sample users aren’t overwhelmed.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 03bc7e3 and eddcf18.

📒 Files selected for processing (2)
  • deepgram/clients/agent/v1/websocket/options.py (5 hunks)
  • examples/agent/simple/main.py (1 hunks)
🔇 Additional comments (1)
deepgram/clients/agent/v1/websocket/options.py (1)

218-220: listen / think / speak default instances always serialise

Because default_factory eagerly builds non-empty dataclass objects, the exclude predicate is never true, so empty agent sections still appear in outbound JSON.

If the intent is to omit untouched sections, switch to default=None and create them lazily, or enhance the predicate to treat default instances as empty.

Comment thread deepgram/clients/agent/v1/websocket/options.py Outdated
@jpvajda

jpvajda commented Jun 13, 2025

Copy link
Copy Markdown
Contributor

simple - passes

async_simple - passes

When Testing no_mic I'm getting this error

Error: {
    "description": "Error parsing client message. Check the agent.speak field against the API spec.",
    "message": "",
    "type": "Error"
}
Connection closed: {
    "type": "Close"
}

I fixed this in this PR and closed out PR #539 .

Now it passes.

"speaker_playback": "true",
},
# verbose=verboselogs.DEBUG,
verbose=verboselogs.SPAM,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@naomi-lgbt did you want to change this to SPAM or leave it as DEBUG?

@naomi-lgbt
naomi-lgbt merged commit a2d66fe into main Jun 13, 2025
5 checks passed
@naomi-lgbt
naomi-lgbt deleted the fix/empty branch June 13, 2025 17:18
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