Skip to content

Fix PicklingError for discovered agent modules in dev mode#13

Merged
mahimairaja merged 1 commit intofix/pickling-issuefrom
cursor/small-point-1ad3
Mar 21, 2026
Merged

Fix PicklingError for discovered agent modules in dev mode#13
mahimairaja merged 1 commit intofix/pickling-issuefrom
cursor/small-point-1ad3

Conversation

@mahimairaja
Copy link
Copy Markdown
Collaborator

Problem

Running python main.py dev in the examples directory fails with:

_pickle.PicklingError: Can't pickle <class 'openrtc_discovered_dental.DentalAgent'>: import of module 'openrtc_discovered_dental' failed

This occurs because _load_agent_module dynamically loads agent modules using importlib.util but never registers them in sys.modules. When LiveKit's dev mode uses watchfiles.arun_process()multiprocessing, it needs to pickle DentalAgent. Pickling a class requires its module to be importable by name, which fails without an entry in sys.modules.

Fix

Register dynamically loaded agent modules in sys.modules before execution, following the standard importlib pattern:

  1. Add the module to sys.modules before calling exec_module() (supports self-references during execution)
  2. Clean up sys.modules on failure to avoid leaving partial state

Testing

All 36 existing tests pass.

Open in Web Open in Cursor 

Register dynamically loaded agent modules in sys.modules before execution
so that multiprocessing can pickle classes defined in those modules.

The LiveKit 'dev' mode uses watchfiles.arun_process() which spawns child
processes via multiprocessing. When pickling an Agent subclass, Python
needs to import the module by name. Without registering the module in
sys.modules, the import fails with:

  _pickle.PicklingError: Can't pickle <class 'openrtc_discovered_*'>:
  import of module 'openrtc_discovered_*' failed

This follows the standard importlib pattern: add the module to sys.modules
before executing it (for self-reference support) and clean up on failure.

Co-authored-by: Mahimai Raja J <mahimairaja3@gmail.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 21, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 558c912d-9482-48fa-a7f5-5d403e1f052f

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch cursor/small-point-1ad3

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov-commenter
Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.05%. Comparing base (f46e619) to head (4fa2f8f).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@                  Coverage Diff                   @@
##           fix/pickling-issue      #13      +/-   ##
======================================================
+ Coverage               87.92%   88.05%   +0.13%     
======================================================
  Files                       3        3              
  Lines                     265      268       +3     
======================================================
+ Hits                      233      236       +3     
  Misses                     32       32              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mahimairaja mahimairaja marked this pull request as ready for review March 21, 2026 14:51
@mahimairaja mahimairaja merged commit e856b47 into fix/pickling-issue Mar 21, 2026
5 of 6 checks passed
mahimairaja added a commit that referenced this pull request Mar 21, 2026
Fix PicklingError for discovered agent modules in dev mode (#13)
@mahimairaja mahimairaja deleted the cursor/small-point-1ad3 branch March 23, 2026 17: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.

3 participants