Repository navigation
fix(python-sdk): preserve using_attributes context in async decorators - #2010
Conversation
Reviewer's GuideImplementationDetect coroutine functions and run each async invocation inside a shallow-copied attribute context, while preserving the synchronous decorator path and subclass state.
TestsAdd regression coverage for async attribute propagation, metadata, cleanup, overlapping calls, subclass state, and synchronous behavior.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
PR Summary by QodoPreserve custom attributes in async using_attributes decorators
AI Description
Merge Risk
|
Code Review by Qodo
1.
|
Linked issuesChecked at commit 7278e5d #2009 Bug: using_attributes loses span attributes in async functions · 2 of 2 met
|
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="sdk/python/src/openlit/__init__.py" line_range="903" />
<code_context>
self._token = None
+ def __call__(self, func):
+ if iscoroutinefunction(func):
+
+ @wraps(func)
</code_context>
<issue_to_address>
**Async work loses attributes**
When the decorated target is an async generator, an async callable, or a synchronous callable returning an awaitable, `iscoroutinefunction` sends only recognized coroutine functions through `async_wrapper`; otherwise, `super().__call__` exits the attribute context before the returned generator or awaitable executes, so instrumentation misses the configured attributes.
Keep the attribute context active while returned generators and awaitables execute, including those from callable instances and synchronous wrappers.
Also at `sdk/python/src/openlit/__init__.py:911`.
</issue_to_address>
### Comment 2
<location path="sdk/python/src/openlit/__init__.py" line_range="915" />
<code_context>
+
+ def _recreate_cm(self):
+ # Each decorated call needs its own context token, including overlapping tasks.
+ return type(self)(self._attributes)
+
def __enter__(self):
</code_context>
<issue_to_address>
**Subclass decorators fail before calls**
When a `using_attributes` subclass requires constructor arguments or state beyond `_attributes` and inherits `_recreate_cm`, `_recreate_cm` calls `type(self)(self._attributes)`, so construction raises `TypeError` or recreated instances lose subclass state, and decorated calls fail or run without that state.
Provide an overridable recreation path that preserves each subclass’s required constructor arguments and state.
</issue_to_address>Sourcery assessment
Approval pending. 2 findings to address first.
Blocking findings: sdk/python/src/openlit/__init__.py:903, sdk/python/src/openlit/__init__.py:915
| self._token = None | ||
|
|
||
| def __call__(self, func): | ||
| if iscoroutinefunction(func): |
There was a problem hiding this comment.
🟡 Medium · Async work loses attributes
When the decorated target is an async generator, an async callable, or a synchronous callable returning an awaitable, iscoroutinefunction sends only recognized coroutine functions through async_wrapper; otherwise, super().__call__ exits the attribute context before the returned generator or awaitable executes, so instrumentation misses the configured attributes.
Keep the attribute context active while returned generators and awaitables execute, including those from callable instances and synchronous wrappers.
Also at sdk/python/src/openlit/__init__.py:911.
Prompt for AI agents
In `sdk/python/src/openlit/__init__.py` at line 903:
**Async work loses attributes**
When the decorated target is an async generator, an async callable, or a synchronous callable returning an awaitable, `iscoroutinefunction` sends only recognized coroutine functions through `async_wrapper`; otherwise, `super().__call__` exits the attribute context before the returned generator or awaitable executes, so instrumentation misses the configured attributes.
Keep the attribute context active while returned generators and awaitables execute, including those from callable instances and synchronous wrappers.
Also at `sdk/python/src/openlit/__init__.py:911`.|
The subclass regression is valid. Added sync and async tests with a required extra constructor argument; both failed on the first commit. Context recreation now shallow-copies the instance and clears its reset token instead of calling the subclass constructor. All seven decorator tests plus 13 existing score tests pass (20 total). The other finding concerns async generators, callable instances, and synchronous functions returning awaitables. Those were not supported by the original ContextDecorator path either. This PR is scoped to the reported async-def regression in #2009 and preserves the synchronous path. Supporting all lazy return types would need separate lifecycle and generator-close tests, so I have left that broader change out of this fix. |
Merged PR summary
Generated from the merged PR description and bounded, component-scoped file evidence. Release preparation reuses this validated summary. |
Fixes #2009
Problem
ContextDecoratorenters and exits the attribute context while creating a coroutine, before the coroutine body runs. As a result,@using_attributes(...)on an async function loses the configured attributes, including those read by evaluation score events.Change
Tests
Added five regression tests covering attributes across an await, return values and coroutine metadata, exception cleanup, overlapping calls with different caller contexts, synchronous behavior, and cancellation cleanup.
Before the fix: three async regressions failed and the synchronous test passed. After the fix: all five new tests and the 13 existing score tests pass (18 total), using Python 3.10 and the real local SDK imports.
git diff --checkpasses. The full Python SDK suite was not run.Summary by Sourcery
Preserve using_attributes context across asynchronous decorator execution and isolate concurrent calls.
Bug Fixes:
Enhancements:
Tests: