Overview
Concrete utility functions for type handling, tool schema generation, and configuration processing used throughout the framework.
Description
The `evoagentx/utils/utils.py` module provides central utility functions used by virtually every subsystem. Key functions include: `normalize_text`, `compile_tool_schemas`, `pydantic_to_parameters`, `string_to_json_schema_type`, `string_to_python_type`, `normalize_param_type`, `validate_param`, `to_params`, `tool_names_to_tools`, `create_agent_from_dict`, `add_llm_config_to_agent_dict`, `generate_dynamic_class_name`, `get_unique_class_name`, `make_parent_folder`, `remove_none`, `recursive_remove`, `format_tool_calls`, and `format_validation_error`.
Usage
- Tool schema generation: Use `compile_tool_schemas` to generate OpenAI-compatible tool schemas.
- Type conversion: Use `string_to_python_type` and `string_to_json_schema_type` for type mapping.
- Agent creation: Use `create_agent_from_dict` to instantiate agents from config dicts.
- Validation: Use `validate_param` to check parameter types and required fields.
Code Reference
Source Location
Signature
def normalize_text(text: str) -> str
def compile_tool_schemas(tools: List[Union[Tool, Toolkit]]) -> List[Dict]
def pydantic_to_parameters(model: Type[BaseModel]) -> List[Parameter]
def string_to_json_schema_type(type_str: str) -> str
def string_to_python_type(type_str: str) -> type
def normalize_param_type(type_str: str) -> str
def validate_param(value: Any, param_type: str, param_name: str) -> Any
def to_params(params: List[Union[dict, Parameter]]) -> List[Parameter]
def tool_names_to_tools(tool_names: List[str], available_tools: List) -> List
def create_agent_from_dict(agent_dict: dict, llm_config=None, tools=None) -> Agent
def add_llm_config_to_agent_dict(agent_dict: dict, llm_config) -> dict
def generate_dynamic_class_name(prefix: str = "Dynamic") -> str
def get_unique_class_name(base: str, existing: set) -> str
def make_parent_folder(path: str) -> None
def remove_none(d: dict) -> dict
def recursive_remove(d: Any, keys: List[str]) -> Any
def format_tool_calls(tool_calls: List[dict]) -> str
def format_validation_error(error: Exception) -> str
Import
from evoagentx.utils.utils import (
normalize_text, compile_tool_schemas, validate_param,
create_agent_from_dict, to_params, make_parent_folder,
)
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| tools |
Toolkit] |
Yes (compile) |
Tools to generate schemas for
|
| type_str |
str |
Yes (convert) |
Type string to convert
|
| agent_dict |
dict |
Yes (create) |
Agent configuration dict
|
Outputs
| Name |
Type |
Description
|
| compile_tool_schemas returns |
List[Dict] |
OpenAI-compatible tool schemas
|
| create_agent_from_dict returns |
Agent |
Instantiated agent
|
| validate_param returns |
Any |
Validated parameter value
|
Usage Examples
Generate Tool Schemas
from evoagentx.utils.utils import compile_tool_schemas
from evoagentx.tools.file_tool import FileToolkit
schemas = compile_tool_schemas([FileToolkit()])
# Returns OpenAI function-calling compatible schemas
Create Agent from Dict
from evoagentx.utils.utils import create_agent_from_dict
from evoagentx.models.model_configs import OpenAILLMConfig
agent_dict = {
"name": "MyAgent",
"description": "An agent",
"prompt": "Do {task}",
"inputs": [{"name": "task", "type": "str", "description": "Task"}],
"outputs": [{"name": "result", "type": "str", "description": "Result"}],
}
llm_config = OpenAILLMConfig(model="gpt-4o-mini", openai_key="sk-...")
agent = create_agent_from_dict(agent_dict, llm_config=llm_config)
Related Pages
Implements Principle
Requires Environment