chainlit icon indicating copy to clipboard operation
chainlit copied to clipboard

ChainlitContextException when accessing user session in RedisDataLayer initialization

Open rccl-aftellez98 opened this issue 7 months ago • 0 comments
trafficstars

Describe the bug: When initializing the RedisDataLayer (a custome data layer), an attempt to access the user session via cl.user_session.get("user").identifier results in a ChainlitContextException: Chainlit context not found. This occurs because the context is not fully built at the time of data layer initialization, but I need to access the user identifier to load the corresponding threads.

If I have the user_id as a fixed value it will load WITHOUT ANY ISSUES. In my case I am using the email as the identifier after authenticationg with Microfost. So if I have a fixed email. I am able to retrive everything, but I use user_id = cl.user_session.get("user").identifier # This line causes the error, for the email, I will get errors.

To Reproduce Steps to reproduce the behavior:

  1. Implement a custom data layer using Redis with the following code: `@cl.data_layer def get_data_layer(): """ Returns an instance of RedisDataLayer. """ return RedisDataLayer()

class RedisDataLayer(cl_data.BaseDataLayer): def init(self, threads=None): self.threads: List[ThreadDict] = threads() # Attempt to access user session during initialization user_id = cl.user_session.get("user").identifier # This line causes the error`

  1. Start the Chainlit app.
  2. Observe the error message when the data layer is being initialized. Expected behavior I expected the RedisDataLayer to initialize successfully and load the threads associated with the user without encountering a context error.

rccl-aftellez98 avatar Mar 28 '25 19:03 rccl-aftellez98