openhands/core/config/README.md
OpenHands uses a flexible configuration system that allows settings to be defined through environment variables, TOML files, and command-line arguments. The configuration is managed through a package structure in openhands/core/config/.
The main configuration class is:
OpenHandsConfig: The root configuration classThis class is defined as a Pydantic model, with field defaults for all settings.
The load_from_env function in the config package is responsible for loading configuration values from environment variables. It recursively processes the configuration classes, mapping environment variable names to class attributes.
DEFAULT_AGENT, WORKSPACE_BASE)The load_from_env function attempts to cast environment variable values to the types specified in the models. It handles:
str | None)If type casting fails, an error is logged, and the default value is retained.
If an environment variable is not set, the default value specified in the model is used.
Be cautious when setting sensitive information like API keys in environment variables. Ensure your environment is secure.
The load_openhands_config() function is the recommended way to initialize your configuration. It performs the following steps:
OpenHandsConfigconfig.toml file (if present)There are also command line args, which may work to override other sources.
Here's an example of how to use load_openhands_config():
from openhands.core.config import load_openhands_config
# Load all configuration settings
config = load_openhands_config()
# Now you can access your configuration
print(f"Default agent: {config.default_agent}")
print(f"Max iterations: {config.max_iterations}")
By using load_openhands_config(), you ensure that all configuration sources are properly loaded and processed, providing a consistent and fully initialized configuration for your application.
While this document focuses on environment variable configuration, OpenHands also supports:
These methods are handled by separate functions in the config package.
The OpenHands configuration system provides a flexible and type-safe way to manage application settings. By following the naming conventions and utilizing the provided functions, developers can easily customize the behavior of OpenHands components through environment variables and other configuration sources.