microsoft/autogen · error · ValueError

Failed to set up Azure container working directory

Error message

Failed to set up Azure container working directory

What it means

Thrown by AzureContainerCodeExecutor._setup_cwd when the bootstrap snippet `import os; os.chdir('/mnt/data')` returns a non-zero exit code inside the Azure container session. This step runs on first execution so that code can access uploaded files. A non-zero exit almost always means the session is unhealthy or /mnt/data does not exist in the container.

Source

Thrown at python/packages/autogen-ext/src/autogen_ext/code_executors/azure/_azure_container_code_executor.py:272

        # Attempt to load the function file to check for syntax errors, imports etc.
        exec_result = await self._execute_code_dont_check_setup(
            [CodeBlock(code=self._func_code, language="python")], cancellation_token
        )

        if exec_result.exit_code != 0:
            raise ValueError(f"Functions failed to load: {exec_result.output.strip()}")

        self._setup_functions_complete = True

    async def _setup_cwd(self, cancellation_token: CancellationToken) -> None:
        # Change the cwd to /mnt/data to properly have access to uploaded files
        exec_result = await self._execute_code_dont_check_setup(
            [CodeBlock(code="import os; os.chdir('/mnt/data')", language="python")], cancellation_token
        )

        if exec_result.exit_code != 0:
            raise ValueError("Failed to set up Azure container working directory")
        self._setup_cwd_complete = True

    async def get_file_list(self, cancellation_token: CancellationToken) -> List[str]:
        self._ensure_access_token()
        timeout = aiohttp.ClientTimeout(total=float(self._timeout))
        headers = {
            "Authorization": f"Bearer {self._access_token}",
        }
        url = self._construct_url("files")
        async with aiohttp.ClientSession(timeout=timeout) as client:
            task = asyncio.create_task(
                client.get(
                    url,
                    headers=headers,
                )
            )
            cancellation_token.link_future(task)
            try:

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Retry the execution: transient session-start failures on first use often succeed on a second call, which re-runs setup
  2. Call restart() to force a brand-new session id, then retry
  3. If using a custom image/session pool, ensure /mnt/data exists (create it in the Dockerfile) or is mounted by the session pool config
  4. Check that DefaultAzureCredential used by the executor is valid and has the ACASessions roles assigned, since exec failures surface as non-zero exit
Defensive patterns

Strategy: retry

Validate before calling

null

Type guard

null

Try / catch

try:
    result = await executor.execute_code_blocks(blocks, ct)
except ValueError as e:
    if "working directory" in str(e):
        await executor.restart()  # new session re-runs _setup_cwd
        result = await executor.execute_code_blocks(blocks, ct)

Prevention

When it happens

Trigger: First execute_code_blocks call after constructing the executor (or after restart()), where the ACA code session cannot execute the chdir: the session endpoint returned an error, the access token expired and the exec call failed, or the container image lacks the /mnt/data mount provided by default Azure Container Apps sessions.

Common situations: Using a custom container image or non-standard Azure Container Apps session pool where /mnt/data is not provisioned; a transient/blanked session (idle timeout) causing exec failures on first use; misconfigured DefaultAzureCredential yielding an invalid token so the exec call fails.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/1159c40480b3c83e. Report an issue: GitHub.