Codeinterpreter-api session __exit__ hangs inside a Dash callback

This question could potentially be similar to Callback not exiting inside of Docker Container - Dash Python - Plotly Community Forum.

Hello, I am trying to write a Dash app with a callback, which contains a call to an OpenAI LLM agent via the open source codeinterpreter-api (GitHub - shroominic/codeinterpreter-api: 👾 Open source implementation of the ChatGPT Code Interpreter), which under the hood executes the Python code generated by the agent using a Jupyter kernel in shroominic/codebox-api: :package: CodeBox is the simplest cloud infrastructure for your LLM Apps and Services. (github.com). The codeinterpreter session starts and executes without any issue but then it hangs during the exit call in the process of terminating the Jupyter kernel. I had no issues running this in a Streamlit app.

Note that, sometimes, it works. For example in a PyCharm-debugger step-by-step execution, I once managed to make it work. This makes it even more puzzling.

Below is the MVP of the callback (the print(response.content) line is typically not executed).

Thanks for any help or suggestions!

@callback(
    Output('answer-placeholder', 'children'),
    Input('submit-button', 'n_clicks'),
    State('subject', 'value')
)
def call_code_interpreter(n_clicks, subject):
    if n_clicks > 0:
        prompt = f"Calculate the sum of 17 and {subject}."
        print(prompt)
        with CodeInterpreterSession(model="gpt-3.5-turbo") as session:
            response = session.generate_response(prompt, files=[])

        print(response.content)
        return response.content

Hello @A_Bohemian,

Welcome to the community!

I think for sessions dont you need to await for a response? Could this not be the issue you are running into?

Hello, thanks for the reply! The code line response = session.generate_response(prompt, files=[]) gets properly executed , i.e. the response is returned. I could even execute other code within the with block. The execution actually hangs when Python tries to call session.__exit__.

What happens if instead of with you just used a manual version?

You mean just creating the object and calling the generate_response method? Well, I can try, but the codeinterpreter is built in a way that for its proper functionality the session needs to be constructed in a with block, i.e. one has to call __enter__ and __exit__ methods.

Yes, thats what I mean.

I understand thats the way it is structured, but for troubleshooting we need to make sure that it doesnt work manually. :slight_smile: Plus, you might actually need more granular control.