microsoft/autogen · error · RuntimeError

Working directory not properly initialized

Error message

Working directory not properly initialized

What it means

Raised by the DockerCommandLineCodeExecutor.work_dir property when neither an explicit work_dir was given at construction nor the temp directory exists. work_dir resolves to: user-specified dir if provided, else the TemporaryDirectory created during start(); if neither is available the executor is in an uninitialized state and this RuntimeError fires.

Source

Thrown at python/packages/autogen-ext/src/autogen_ext/code_executors/docker/_docker_code_executor.py:394

        return CommandLineCodeResult(exit_code=last_exit_code, output="".join(outputs), code_file=code_file)

    @property
    def work_dir(self) -> Path:
        # If a user specifies a working directory, use that
        if self._work_dir is not None:
            # If a user specifies the current directory, warn them that this is deprecated
            if self._work_dir == Path("."):
                warnings.warn(
                    "Using the current directory as work_dir is deprecated.",
                    DeprecationWarning,
                    stacklevel=2,
                )
            return self._work_dir
        # If a user does not specify a working directory, use the default directory (tempfile.TemporaryDirectory)
        elif self._temp_dir is not None:
            return Path(self._temp_dir.name)
        else:
            raise RuntimeError("Working directory not properly initialized")

    @property
    def bind_dir(self) -> Path:
        # If the user specified a bind directory, return it
        if self._bind_dir is not None:
            return self._bind_dir
        # Otherwise bind_dir is set to the current work_dir as default
        else:
            return self.work_dir

    async def execute_code_blocks(
        self, code_blocks: List[CodeBlock], cancellation_token: CancellationToken
    ) -> CommandLineCodeResult:
        """(Experimental) Execute the code blocks and return the result.

        Args:
            code_blocks (List[CodeBlock]): The code blocks to execute.

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Access work_dir only after start(): use `async with DockerCommandLineCodeExecutor() as e:` and read e.work_dir inside the block
  2. Pass an explicit work_dir at construction if you need the path before starting: work_dir="/tmp/mywork"
  3. For pre-start planning, compute your own path and pass it as work_dir rather than reading the property

Example fix

# before
executor = DockerCommandLineCodeExecutor()
print(executor.work_dir)  # RuntimeError: not initialized

# after
executor = DockerCommandLineCodeExecutor(work_dir="/tmp/mywork")
print(executor.work_dir)  # /tmp/mywork, valid before start
Defensive patterns

Strategy: validation

Validate before calling

null

Type guard

def has_work_dir(executor) -> bool:
    return executor._work_dir is not None or executor._temp_dir is not None

Try / catch

try:
    wd = executor.work_dir
except RuntimeError:
    await executor.start()
    wd = executor.work_dir

Prevention

When it happens

Trigger: Accessing executor.work_dir (directly, or via bind_dir / file-listing helpers) before start() was ever called on an executor constructed with work_dir=None. Also reachable via code paths that touch work_dir after a failed start where temp-dir creation was skipped.

Common situations: Inspecting executor.work_dir immediately after construction to plan file placement; components that snapshot bind_dir early during wiring before the container starts; tests constructing executors without entering the context manager.

Related errors


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