Skip to content

CodeInterpreter tool is appending data to the end of the filename in containers #911

Open
@TheRobotCarlson

Description

@TheRobotCarlson

Please read this first

  • Have you read the docs?Agents SDK docs - ✅
  • Have you searched for related issues? Others may have faced similar issues. - ✅

Describe the bug

The code interpreter tool seems to have a formatting bug when showing files in an already-running container. This results in the agent writing and running code referencing incorrect files.

Debug information

  • Agents SDK version: "openai-agents>=0.0.19",
  • Python 3.13.2
  • Windows 11

Repro steps

Ideally provide a minimal python script that can be run to reproduce the bug.

Code

Run this twice:

"""Test script for containers in openai calls."""

import asyncio
import uuid

from agents import (
    Agent,
    CodeInterpreterTool,
    ItemHelpers,
    MessageOutputItem,
    Runner,
    ToolCallItem,
    ToolCallOutputItem,
    trace,
)
from openai import OpenAI
from pydantic import BaseModel


def get_or_create_container(name: str) -> str:
    """Get or create a container with the given name.
    """
    client = OpenAI()
    containers = client.containers.list()
    for container in containers.data:
        if container.name == name and container.status != "expired":
            print(f"Container {name} already exists, using it")
            return container.id

    print(f"Container {name} does not exist, creating it")
    return client.containers.create(name=name).id


def upload_if_not_exists(container_id: str, data: str) -> str:
    """Upload the data to the container if it doesn't already exist."""
    client = OpenAI()
    files = client.containers.files.list(container_id=container_id)
    for file in files.data:
        if file.path == data:
            return file.id

    file_path = client.containers.files.create(
        container_id=container_id,
        file=data.encode("utf-8"),
    )
    return file_path.path


class RunContext(BaseModel):
    """Context for the run."""


async def main() -> None:
    container_id = get_or_create_container("insight-analysis-container-2")

    data = """
    This is some sample data. Say banana 10 times if you can see this data.
    """

    file_path = upload_if_not_exists(container_id, data)

    print(f"File path: {file_path}")

    context = RunContext()
    agent = Agent(
        name="Insight Agent",
        instructions="Analyze the data using the code interpreter",
        tools=[
            CodeInterpreterTool(
                tool_config={
                    "type": "code_interpreter",
                    "container": container_id,
                },
            ),
        ],
    )
    conversation_id = uuid.uuid4().hex[:16]

    with trace("Insight agent", group_id=conversation_id):
        result = Runner.run_streamed(
            agent,
            "Analyze the data available in the container",
            context=context,
        )
        async for event in result.stream_events():
            if (
                event.type == "run_item_stream_event"
                and event.item.type == "tool_call_item"
                and event.item.raw_item.type == "code_interpreter_call"
            ):
                print(f"Code interpreter code:\n```\n{event.item.raw_item.code}\n```\n")
            elif event.type == "run_item_stream_event":
                if isinstance(event.item, ToolCallItem):
                    print(
                        f"Calling a tool: {event.item.raw_item.name} with args: {event.item.raw_item.arguments}",
                    )
                elif isinstance(event.item, ToolCallOutputItem):
                    print(f"Tool call output: {event.item.raw_item['output']}")
                elif isinstance(event.item, MessageOutputItem):
                    print(f"{ItemHelpers.text_message_output(event.item)}")
                else:
                    print(f"Other event: {event.item.type}")

        print(f"Final output: {result.final_output}")


if __name__ == "__main__":
    asyncio.run(main())

Truncated Output

Notice how the file names are messed up on its first pass?

Code interpreter code:

import pandas as pd

# Load the first file to inspect its contents
file_path_1 = '/mnt/data/223815cf5d0afc930bf472290111b634-uploadThis file is NOT accessible with the myfiles_browser tool.'
file_path_2 = '/mnt/data/01592886941500a0220e799239274d57-uploadThis file is NOT accessible with the myfiles_browser tool.'

Expected behavior

The file names are unmarred by the string appended to the end of the filename.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions