Skip to content

Fixing 'active_steps' getting mixed in async operations #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion literalai/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def get_current_step(self):
"""
Gets the current step from the context.
"""
active_steps = active_steps_var.get()
active_steps = active_steps_var.get([])
if active_steps and len(active_steps) > 0:
return active_steps[-1]
else:
Expand Down
2 changes: 1 addition & 1 deletion literalai/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from literalai.observability.step import Step
from literalai.observability.thread import Thread

active_steps_var = ContextVar[List["Step"]]("active_steps", default=[])
active_steps_var = ContextVar[List["Step"]]("active_steps", default=None)
active_thread_var = ContextVar[Optional["Thread"]]("active_thread", default=None)
active_root_run_var = ContextVar[Optional["Step"]]("active_root_run_var", default=None)

Expand Down
4 changes: 2 additions & 2 deletions literalai/instrumentation/mistralai.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def update_step_after(
def before_wrapper(metadata: Dict):
def before(context: BeforeContext, *args, **kwargs):
active_thread = active_thread_var.get()
active_steps = active_steps_var.get()
active_steps = active_steps_var.get([])
generation = init_generation(metadata["type"], kwargs)

if (active_thread or active_steps) and not callable(on_new_generation):
Expand All @@ -225,7 +225,7 @@ def before(context: BeforeContext, *args, **kwargs):
def async_before_wrapper(metadata: Dict):
async def before(context: BeforeContext, *args, **kwargs):
active_thread = active_thread_var.get()
active_steps = active_steps_var.get()
active_steps = active_steps_var.get([])
generation = init_generation(metadata["type"], kwargs)

if (active_thread or active_steps) and not callable(on_new_generation):
Expand Down
4 changes: 2 additions & 2 deletions literalai/instrumentation/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def update_step_after(
def before_wrapper(metadata: Dict):
def before(context: BeforeContext, *args, **kwargs):
active_thread = active_thread_var.get()
active_steps = active_steps_var.get()
active_steps = active_steps_var.get([])
generation = init_generation(metadata["type"], kwargs)

if (active_thread or active_steps) and not callable(on_new_generation):
Expand All @@ -209,7 +209,7 @@ def before(context: BeforeContext, *args, **kwargs):
def async_before_wrapper(metadata: Dict):
async def before(context: BeforeContext, *args, **kwargs):
active_thread = active_thread_var.get()
active_steps = active_steps_var.get()
active_steps = active_steps_var.get([])
generation = init_generation(metadata["type"], kwargs)

if (active_thread or active_steps) and not callable(on_new_generation):
Expand Down
2 changes: 1 addition & 1 deletion literalai/observability/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(
self.parent_id = parent_id

def end(self):
active_steps = active_steps_var.get()
active_steps = active_steps_var.get([])

if len(active_steps) > 0:
parent_step = active_steps[-1]
Expand Down
4 changes: 2 additions & 2 deletions literalai/observability/step.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def __init__(
setattr(self, key, value)

def start(self):
active_steps = active_steps_var.get()
active_steps = active_steps_var.get([])
if len(active_steps) > 0:
parent_step = active_steps[-1]
if not self.parent_id:
Expand All @@ -405,7 +405,7 @@ def end(self):
self.end_time = utc_now()

# Update active steps
active_steps = active_steps_var.get()
active_steps = active_steps_var.get([])

# Check if step is active
if self not in active_steps:
Expand Down
4 changes: 2 additions & 2 deletions literalai/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def wrapped(*args, **kwargs):
try:
result = original_func(*args, **kwargs)
except Exception as e:
active_steps = active_steps_var.get()
active_steps = active_steps_var.get([])
if active_steps and len(active_steps) > 0:
current_step = active_steps[-1]
current_step.error = str(e)
Expand Down Expand Up @@ -87,7 +87,7 @@ async def wrapped(*args, **kwargs):
try:
result = await original_func(*args, **kwargs)
except Exception as e:
active_steps = active_steps_var.get()
active_steps = active_steps_var.get([])
if active_steps and len(active_steps) > 0:
current_step = active_steps[-1]
current_step.error = str(e)
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,12 @@ async def test_ingestion(
with async_client.step(name="test_ingestion") as step:
step.metadata = {"foo": "bar"}
assert async_client.event_processor.event_queue._qsize() == 0
stack = active_steps_var.get()
stack = active_steps_var.get([])
assert len(stack) == 1

assert async_client.event_processor.event_queue._qsize() == 1

stack = active_steps_var.get()
stack = active_steps_var.get([])
assert len(stack) == 0

@pytest.mark.timeout(5)
Expand Down