Skip to content

ChainlitContextException with cl.instrument_openai() #1946

Closed
@tygourm

Description

@tygourm

Describe the bug

I my chatbot, i expose my models by API using AsyncOpenAI. The calls are made in a service layer.
This service is used by both my chat and my API.
I have cl.instrument_openai() enabled, it works fine in the chat, but when I use my API, I have an exception:

2025-03-02 16:09:58   File "/usr/local/lib/python3.13/site-packages/literalai/wrappers.py", line 101, in wrapped
2025-03-02 16:09:58     result = await after_func(result, context, *args, **kwargs)
2025-03-02 16:09:58              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2025-03-02 16:09:58   File "/usr/local/lib/python3.13/site-packages/literalai/instrumentation/openai.py", line 476, in after
2025-03-02 16:09:58     on_new_generation(
2025-03-02 16:09:58     ~~~~~~~~~~~~~~~~~^
2025-03-02 16:09:58         generation,
2025-03-02 16:09:58         ^^^^^^^^^^^
2025-03-02 16:09:58     ...<3 lines>...
2025-03-02 16:09:58         },
2025-03-02 16:09:58         ^^
2025-03-02 16:09:58     )
2025-03-02 16:09:58     ^
2025-03-02 16:09:58   File "/usr/local/lib/python3.13/site-packages/chainlit/openai/__init__.py", line 27, in on_new_generation
2025-03-02 16:09:58     step = Step(
2025-03-02 16:09:58         name=generation.model if generation.model else generation.provider,
2025-03-02 16:09:58         type="llm",
2025-03-02 16:09:58         parent_id=parent_id,
2025-03-02 16:09:58     )
2025-03-02 16:09:58   File "/usr/local/lib/python3.13/site-packages/chainlit/step.py", line 205, in __init__
2025-03-02 16:09:58     self.thread_id = thread_id or context.session.thread_id
2025-03-02 16:09:58                                   ^^^^^^^^^^^^^^^
2025-03-02 16:09:58   File "/usr/local/lib/python3.13/site-packages/lazify.py", line 133, in __getattr__
2025-03-02 16:09:58     return getattr(self.value, name)
2025-03-02 16:09:58                    ^^^^^^^^^^
2025-03-02 16:09:58   File "/usr/local/lib/python3.13/site-packages/lazify.py", line 56, in value
2025-03-02 16:09:58     value = self._func(*self._args, **self._kwargs)
2025-03-02 16:09:58   File "/usr/local/lib/python3.13/site-packages/chainlit/context.py", line 108, in get_context
2025-03-02 16:09:58     raise ChainlitContextException from e
2025-03-02 16:09:58 chainlit.context.ChainlitContextException: Chainlit context not found

To Reproduce

Have cl.instrument_openai() enabled, and use AsyncOpenAI in a non-chat context.

Expected behavior

No error stack, At most a warning that AsyncOpenAI calls are being made in a non chat context.
I think that it can be avoided by stopping the process if thread_id is None.

Desktop (please complete the following information):

  • OS: Windows
  • Browser: Chrome
  • Version: Chainlit 2.2.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    backendPertains to the Python backend.bugSomething isn't workingneeds-triage

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions