fix: No context vars for async agents replies
Why are these changes needed?
The Autogen currently utilizes the asyncio.get_event_loop().run_in_executor() method for generating async agent replies. However, this approach presents a challenge if the application decorates OpenAI calls (such as openai.resources.chat.completions.AsyncCompletions.create) for purposes like call tracing or statistics measurement. The problem is that context variables cannot be accessed in this setup.
To address this issue, suggested to use contextvars.copy_context().run() method. This change will allow context variables to be properly propagated and accessed.
Related issue number
Checks
- [ ] I've included any doc changes needed for https://microsoft.github.io/autogen/. See https://microsoft.github.io/autogen/docs/Contribute#documentation to build and test documentation locally.
- [ ] I've added tests (if relevant) corresponding to the changes introduced in this PR.
- [ ] I've made sure all auto checks have passed.
Codecov Report
All modified and coverable lines are covered by tests :white_check_mark:
Project coverage is 29.30%. Comparing base (
8a8fcd8) to head (61b70b9). Report is 4 commits behind head on 0.2.
Additional details and impacted files
@@ Coverage Diff @@
## 0.2 #4640 +/- ##
=======================================
Coverage 29.30% 29.30%
=======================================
Files 117 117
Lines 13013 13021 +8
Branches 2469 2470 +1
=======================================
+ Hits 3813 3816 +3
- Misses 8854 8859 +5
Partials 346 346
| Flag | Coverage Δ | |
|---|---|---|
| unittests | 29.30% <100.00%> (+<0.01%) |
:arrow_up: |
Flags with carried forward coverage won't be shown. Click here to find out more.
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.