isolating code interpreter documents for Azure AI Agents

Leon OuChen 0 Reputation points
2025-05-07T04:47:41.52+00:00

Hi, I want to try isolating csv's for code interpreter to each thread so that when Someone creates a thread, and uploads a document only they query an agent with code interpreter on it. I've currently attempted this approach but it doesn't work and all csv's uploaded are available to all if asked. Does anyone know how to do it or if it can be done?


    def create_thread_and_upload_csv(self, file_path: str):
        """Create a new thread and upload a CSV file."""
        # Create a new thread
        file = self.project_client.agents.upload_file_and_poll(
                    file_path=file_path, 
                    purpose=FilePurpose.AGENTS
                )
        code_interpreter = CodeInterpreterTool(file_ids=[file.id])
        thread = self.project_client.agents.create_thread(
            tool_resources=code_interpreter.resources
        )
        return thread, file
        
    def run_agent_on_thread(self, thread_id: str, content: str, agent_id: Optional[str] = None, ):
        """Run an agent on a thread."""
        # Get the agent and thread
        agent = self.get_agent(agent_id) or self.default_agent_id
        message = self.project_client.agents.create_message(
            thread_id=thread_id,
            role="user",
            content=content
        )
        # Run the agent on the thread
        run = self.project_client.agents.create_and_process_run(
            agent_id=agent.id,
            thread_id=thread_id,
        )
        print(f"Agent run completed with status: {run.status}")
        if run.status == "failed" and hasattr(run, "last_error"):
            print(f"Run error details: {run.last_error}")

    def get_thread_messages(self, thread_id: str) -> List[Dict[str, Any]]:
        """Get all messages from a thread."""
        messages = self.project_client.agents.list_messages(thread_id)
        print(f"Retrieved {len(messages)} messages from thread {thread_id}.")
        print(messages)
        return messages
Azure AI Bot Service
Azure AI Bot Service
An Azure service that provides an integrated environment for bot development.
933 questions
{count} votes

1 answer

Sort by: Most helpful
  1. SriLakshmi C 5,365 Reputation points Microsoft External Staff Moderator
    2025-05-07T15:35:43.1666667+00:00

    Hello Leon OuChen,

    To ensure CSV files remain isolated per thread in Azure AI Agents, it's essential to tightly couple each file with the specific thread it belongs to.

    The current issue where uploaded files are accessible across all threads likely stems from shared file contexts or improperly scoped tool resources.

    When uploading a CSV using upload_file_and_poll, assign a purpose like FilePurpose.AGENTS, but more importantly, encapsulate this file within a CodeInterpreterTool that is initialized with only that file's ID.

    This tool should then be passed explicitly to the create_thread call, ensuring that the thread's resources are confined to its specific context. Proper thread-specific resource association ensures that each agent session has access only to its designated data, preventing cross-thread file leakage.

    To implement this, consider refining your method as follows:

    def create_thread_and_upload_csv(self, file_path: str):
        file = self.project_client.agents.upload_file_and_poll(
            file_path=file_path, 
            purpose=FilePurpose.AGENTS
        )
        code_interpreter = CodeInterpreterTool(file_ids=[file.id])
        thread = self.project_client.agents.create_thread(
            tool_resources=code_interpreter.resources
        )
        return thread, file
    

    This setup ensures each thread uses an isolated code interpreter tied to its specific file, maintaining data separation. Also, verify that any vector stores or attachments created during the thread initialization are scoped locally to avoid inadvertent sharing across sessions.

    Please refer this Azure AI Agent Service Code Interpreter (code-example)

    Azure AI Agent Service file search tool (upload-files-code-examples)

    I Hope this helps. Do let me know if you have any further queries.

    Thank you!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.