Subject: Unable to Retrieve Code Interpreter Output Without Explicit print
Statement
Hi,
I’m trying to retrieve the Code Interpreter’s output using the OpenAI Assistants API. However, I can only do so if I explicitly instruct the assistant to use a print
statement in the executed code.
If I do not request print
, the output is not available in the API response, even though the assistant correctly responds with the expected result. This suggests that the output must be handed over internally to the assistant, but I’m unable to access it programmatically.
Steps to Reproduce
Here’s an example of my implementation:
from openai import OpenAI
import os
from dotenv import load_dotenv
import requests
load_dotenv()
client = OpenAI()
assistant = client.beta.assistants.retrieve("my_assistant")
thread = "my_thread"
message = client.beta.threads.messages.create(
thread_id=thread,
role="user",
content="Use Code Interpreter to find the square root of numbers 1 through 10. (Ensure you use `print` to display the result.)"
)
print("Starting run stream...")
stream = client.beta.threads.runs.create(
thread_id=thread,
assistant_id=assistant.id,
instructions="Please address the user as Jane Doe. The user has a premium account.",
stream=True,
)
stream_content = []
for event in stream:
stream_content.append(event)
if event.event == "thread.message.created":
print("Assistant:")
elif event.event == "thread.message.delta":
print(event.data.delta.content[0].text.value, end='')
elif event.event == "thread.run.step.created":
if event.data.step_details.type == "tool_calls":
print("Tool Call Detected:")
elif event.event == "thread.run.step.delta":
print(event.data.delta.step_details.tool_calls[0].code_interpreter.input, end='')
# Function concludes with 'thread.run.step.completed'
Observed Behavior
-
When I ask the assistant to use
print
:- The Code Interpreter output appears correctly in the response.
-
When I do NOT request
print
:- The last computed value (e.g.,
result = sqrt(7)
) does not appear in the response. - The
thread.run.step.completed
event does not contain any outputs from the Code Interpreter.
- The last computed value (e.g.,
Expected Behavior
- The API should return all Code Interpreter outputs, even if the assistant does not explicitly use
print()
. - Since ChatGPT’s UI correctly displays results without
print
, this functionality must exist somewhere in the API.
Question
Is there a way to access the Code Interpreter’s output without requiring an explicit print
statement?
Any insights would be much appreciated!
Thanks!