fix: propagate contextvars in sync_to_async for FunctionTool#21558
Open
NIK-TIGER-BILL wants to merge 3 commits intorun-llama:mainfrom
Open
fix: propagate contextvars in sync_to_async for FunctionTool#21558NIK-TIGER-BILL wants to merge 3 commits intorun-llama:mainfrom
NIK-TIGER-BILL wants to merge 3 commits intorun-llama:mainfrom
Conversation
When a sync function is wrapped for async execution via sync_to_async, the default executor thread does not inherit the caller's contextvars context. This breaks ContextVar propagation (e.g. OpenTelemetry spans) for sync tools invoked through acall(). Fix by snapshotting contextvars.copy_context() and running fn inside it via ctx.run() within the executor thread, mirroring the existing fix in async_utils.py's asyncio_run(). Closes run-llama#21555 Signed-off-by: NIK-TIGER-BILL <nik.tiger.bill@github.com>
Collaborator
|
This breaks at least one test in core. Linting is also failing |
…or compat Signed-off-by: NIK-TIGER-BILL <nik.tiger.bill@github.com>
Contributor
Author
|
@logan-markewich Thanks for the review — you were absolutely right. The previous |
Signed-off-by: NIK-TIGER-BILL <nik.tiger.bill@github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
When a sync function is wrapped for async execution via
sync_to_async, the default executor thread does not inherit the caller'scontextvarscontext. This breaksContextVarpropagation (e.g. OpenTelemetry spans) for sync tools invoked throughacall().The fix snapshots
contextvars.copy_context()and runsfninsidectx.run(...)within the executor thread, mirroring the existing fix already present inasync_utils.py'sasyncio_run().Fixes #21555
New Package?
Version Bump?
Type of Change
How Has This Been Tested?
test_function_tool_contextvar_propagationtotests/tools/test_base.pywhich sets aContextVarand asserts it is propagated into a syncFunctionToolcalled viaacall().tests/tools/test_base.py) passes locally (24/24).Suggested Checklist: