Tool Configuration Best Practicesο
Use BaseSettings Instead of BaseModelο
Problem Backgroundο
AIECS tools use Pydantic for configuration management. In configuration classes, you must use BaseSettings instead of BaseModel:
β Wrong (will not automatically read environment variables):
from pydantic import BaseModel, Field, ConfigDict
class Config(BaseModel):
model_config = ConfigDict(env_prefix="DOC_PARSER_")
gcs_project_id: Optional[str] = Field(default=None)
β Correct (automatically reads environment variables):
from pydantic import Field, ConfigDict
from pydantic_settings import BaseSettings
class Config(BaseSettings):
model_config = ConfigDict(env_prefix="DOC_PARSER_")
gcs_project_id: Optional[str] = Field(default=None)
Key Differencesο
Feature |
BaseModel |
BaseSettings |
|---|---|---|
Source Package |
|
|
Environment Variable Reading |
β Not Supported |
β Automatically Supported |
Purpose |
Data Validation |
Configuration Management |
|
β None |
β Yes |
Why It Mattersο
When using get_tool(), if the configuration class uses BaseModel:
Environment variable
DOC_PARSER_GCS_PROJECT_IDwill not be readWill use default value
NoneCauses βGCS project ID not providedβ error
When using BaseSettings:
Automatically reads from environment variables
Supports
.envfilesCorrect configuration priority: code config > environment variables > default values
Configuration Priorityο
When using BaseSettings, configuration values are resolved in the following priority order (from highest to lowest):
Explicitly passed parameters (highest priority)
tool = get_tool("image", config={"timeout": 60})
Environment Variables
export DOC_PARSER_GCS_PROJECT_ID=my-project
.envFile# .env DOC_PARSER_GCS_PROJECT_ID=my-project
Default Values (lowest priority)
gcs_project_id: Optional[str] = Field(default=None)
Usage Examplesο
Method 1: Using Environment Variables (Recommended)ο
from dotenv import load_dotenv
load_dotenv() # Must be called before importing tools
from aiecs.tools import get_tool
# Automatically reads tool-specific env vars when configured on the tool class
tool = get_tool("image")
Method 2: Explicitly Passing Configurationο
from aiecs.tools import get_tool
tool = get_tool("image", config={
'timeout': 60,
})
Method 3: Mixed Usageο
# .env file
IMAGE_TOOL_TIMEOUT=60
# Code
from dotenv import load_dotenv
load_dotenv()
from aiecs.tools import get_tool
# timeout read from environment variables when supported by tool config
# explicit config passed from code (higher priority)
tool = get_tool("image", config={
'timeout': 90,
})
Dependenciesο
Ensure pydantic-settings is installed:
pip install pydantic pydantic-settings python-dotenv
Verify Configurationο
from dotenv import load_dotenv
load_dotenv()
from aiecs.tools import get_tool
tool = get_tool("document_parser")
# Check if configuration is loaded correctly
print(f"GCS Project ID: {tool.config.gcs_project_id}")
print(f"GCS Bucket: {tool.config.gcs_bucket_name}")
print(f"Enable Cloud Storage: {tool.config.enable_cloud_storage}")
Common Questionsο
Q: Why donβt environment variables work after using get_tool()?ο
A: Confirm that the configuration class inherits from BaseSettings instead of BaseModel.
Q: Variables in .env file are not being read?ο
A: Ensure load_dotenv() is called before importing tools:
# β
Correct order
from dotenv import load_dotenv
load_dotenv()
from aiecs.tools import get_tool
# β Wrong order
from aiecs.tools import get_tool
from dotenv import load_dotenv
load_dotenv() # Too late!
Q: How to check if a tool uses BaseSettings?ο
A: Check the Config class definition in the tool source code:
# Look in tool file
class Config(BaseSettings): # β
Correct
...
class Config(BaseModel): # β Wrong
...
Fixed Toolsο
The following tools have been updated to use BaseSettings:
β DocumentParserTool
β DocumentWriterTool
β AIDocumentWriterOrchestrator