Skip to content

BUG: InterpreterError: Forbidden function evaluation: 'open' is not among the explicitly allowed tools #1830

@arturoPMM

Description

@arturoPMM

Problem
Even though I pass additional_authorized_imports=["*"] to CodeAgent, the Python Interpreter returns the following error when trying to open a local file.
Code execution failed at line 'with open('myfile.xlsx', 'rb') as f: data = f.read()' due to: InterpreterError: Forbidden function evaluation: 'open' is not among the explicitly allowed tools or defined/imported in the preceding code

Steps to reproduce
Please provide a minimal, self-contained, and reproducible example of the bug.

  1. Pass to the user prompt a task that requires opening a local file (e.g. pptx, xlsx,...) and the relative path to the file.
  2. Create a CodeAgent with additional_authorized_imports=["*"]
  3. At the start of the execution, you should get this error.
def run_agent(
    message: str,
) -> str:
    model = AzureOpenAIServerModel(**model_kwargs)
    agent = CodeAgent(
        model=model,
        tools=[],
        additional_authorized_imports=["*"],
        code_block_tags=("<code>", "</code>"),
        use_structured_outputs_internally=True
    )

    attachments_section = "ATTACHED FILES:\n" + "\n".join(file_lines)
    user_prompt = (message + "ATTACHED FILES:\n" "/temp/myfile.xlsx"
    content = agent.run(user_prompt)

    return content

Actual behavior and error logs

import pandas as pd
 from io import BytesIO
 import base64

 with open('/temp/myfile.xlsx', 'rb') as f:
     data = f.read()
     buffer = BytesIO(data)
     df = pd.read_excel(buffer)
     dest = BytesIO()
     df.to_excel(dest, index=False)
     dest.seek(0)

import base64
b64 = base64.b64encode(dest.read()).decode('utf-8')
final_answer({ "content": "Here is the new Excel file.", "document": { "filename": "output.xlsx", "content_type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "data": b64 } }) 

Code execution failed at line 'with open('/temp/myfile.xlsx', 'rb') as f: data = f.read()' due to: InterpreterError: Forbidden function evaluation: 'open' is not among the explicitly allowed tools or defined/imported in the preceding code `

Expected behavior
Since I allow all authorized imports, I would expect it to open the file. Note it works well if it tries to open directly from pandas, but not with the with open.

Environment:

  • OS: macOS, Linux
  • Python version: 3.13
  • Package version: 1.22.0

Additional context (optional)
I found a workaround that is creating the tool below and passing it to the agent:

@tool
def read_binary_file(file_path: str) -> bytes:
    try:
        path = Path(file_path)
        if not path.exists():
            raise FileNotFoundError(f"File not found at path: {file_path}")
        
        if not path.is_file():
            raise ValueError(f"Path is not a file: {file_path}")
        
        with open(path, 'rb') as f:
            content = f.read()
        
        return content
    except Exception as e:
        raise RuntimeError(f"Error reading binary file {file_path}: {str(e)}")

Checklist

  • I have searched the existing issues and have not found a similar bug report.
  • I have provided a minimal, reproducible example.
  • I have provided the full traceback of the error.
  • I have provided my environment details.
  • I am willing to work on this issue and submit a pull request. (optional)

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