Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions langfuse/_client/observe.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import asyncio
import contextvars
import inspect
Expand Down Expand Up @@ -695,13 +696,16 @@ async def aclose(self) -> None:
return

try:
try:
if sys.version_info >= (3, 11):
await asyncio.create_task(
self.generator.aclose(),
context=self.context,
) # type: ignore
except TypeError:
await self.context.run(asyncio.create_task, self.generator.aclose())
else:
await self.context.run(
asyncio.create_task,
self.generator.aclose(),
)
except (Exception, asyncio.CancelledError) as error:
self._finalize_with_error(error)
raise
Expand All @@ -717,14 +721,12 @@ def __del__(self) -> None:
async def __anext__(self) -> Any:
try:
# Run the generator's __anext__ in the preserved context
try:
# Python 3.11+ approach with explicit task context
if sys.version_info >= (3, 11):
item = await asyncio.create_task(
self.generator.__anext__(), # type: ignore
context=self.context,
) # type: ignore
except TypeError:
# Python 3.10 fallback - create the task inside the preserved context.
else:
item = await self.context.run(
asyncio.create_task,
self.generator.__anext__(), # type: ignore
Expand Down