-
Notifications
You must be signed in to change notification settings - Fork 8
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
Session bug (fix with context variables) #161
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice - haven't used contextvars before. Only change I'd be in favor of is to remove the default value for the contextvar. I think that's an easy way to get bugs that are difficult to reproduce.
log10/load.py
Outdated
except Exception as e: | ||
logging.warn(f"LOG10: failed to log: {e}. Skipping") | ||
# Print the exception tracompletion_urlceback |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mangled comment
log10/load.py
Outdated
if last_completion_response_var.get() is None: | ||
return None | ||
response = last_completion_response_var.get() | ||
return url + "/app/" + response["organizationSlug"] + "/completions/" + response["completionID"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe write as fstring eg
f'{url}/app/{response["organizationSlug"]}/completions/{response["completionId"]}'
# | ||
# Context variables | ||
# | ||
session_id_var = contextvars.ContextVar("session_id", default=get_session_id()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Slight preference to not set a default here and force an explicit set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need this to keep the existing behavior - where llm calls in outer most "context" i.e. no context, still gets grouped together
import uuid | ||
|
||
|
||
session_id_var = contextvars.ContextVar("session_id", default=str(uuid.uuid4())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here - slight preference to not set a default. Makes it more explicit and simpler to test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was just for testing, if that's ok to keep
session_id_var.reset(self.token) | ||
|
||
|
||
def simulated_llm_call(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you removed the default value - I'd expect to see an explicit set here (or in an outer scope) before get is possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note sure I follow. Is it to set uuids explicitly by having it as an input to the session object?
async def test_nested_async_contexts(): | ||
print("") | ||
before_outer_session = await simulated_llm_acall() | ||
async with ExampleSession(): | ||
before_inner_session = await simulated_llm_acall() | ||
|
||
async with ExampleSession(): | ||
inner_session = await simulated_llm_acall() | ||
|
||
assert inner_session != before_inner_session | ||
assert inner_session != before_outer_session | ||
|
||
assert await simulated_llm_acall() == before_inner_session | ||
|
||
assert await simulated_llm_acall() == before_outer_session | ||
|
||
|
||
asyncio.run(test_nested_async_contexts()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adding the @pytest.mark.asyncio
before the def func, then we don't need to call asyncio.run
here. We have setup that way in other tests (e.g openai tests)
await asyncio.gather(run_session(delay=0, run_time=3), run_session(delay=1, run_time=1)) | ||
|
||
|
||
asyncio.run(test_overlapping_async_contexts()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here as well
No description provided.