Skip to main content

Overview

The AgentLoader class provides a unified interface for instantiating Agent objects from on-disk definitions. It supports four file formats and dispatches automatically based on the file extension. Extensions are matched in any case, so TEAM.YML and team.yml both load. auto() reads back every format create_agent_file() writes. Every format is validated against one pydantic schema, AgentConfig, described below. Use this when you want agent definitions to live as data — checked into git, edited by humans, generated by tools — rather than hard-coded in Python.

Installation

Constructor

bool
default:"True"
Default for load_agents_from_markdown() when its own concurrent argument is not given. AgentLoader(concurrent=False) loads Markdown files one at a time.
Optional[int]
default:"None"
Worker threads for concurrent Markdown loading and for building CSV, JSON, and YAML rows in load_agents_from_csv(). None uses twice the CPU count.

AgentConfig

AgentConfig is the pydantic model every loader validates against. Its field names are Agent parameters, and every field defaults to None: a field left unset falls back to the Agent default, not to a loader-specific one. Any model name is accepted.
max_loops: auto is accepted in every format. Keys the schema does not define are kept on the model (model_extra). AgentConfig.agent_kwargs(extra=False) returns the fields that are set as Agent keyword arguments; with extra=True it also returns the undefined keys. YAML loading uses extra=True, so unknown YAML keys pass through to Agent as custom parameters. Markdown, CSV, and JSON loading use extra=False and drop them.

Methods

auto()

Dispatch to the right loader based on file extension. The simplest entry point.
Parameters:
  • file_path (str): Path to a .md, .yaml, .yml, .csv, or .json file. The extension is matched in any case.
  • *args, **kwargs: Forwarded to the underlying loader.
Returns: Whatever the matching loader returns: a List[Agent] for Markdown, CSV, and JSON, and the shape described under load_agents_from_yaml() for YAML. Raises: ValueError for any other extension.

load_single_agent()

Alias for auto() — loads a single file by dispatching on extension, and returns the same shape.

load_multiple_agents()

Apply auto() to a list of files. Each file may be a different format.
Parameters:
  • file_paths (List[str]): Paths to agent definition files.
Returns: One auto() result per input file, in order.

load_agent_from_markdown()

Load a single agent from a Markdown file.
**kwargs are Agent arguments that override the parsed configuration. Raises ValueError when the file fails to parse or the agent fails to build.

load_agents_from_markdown()

Load multiple agents from one or more Markdown files.
Union[str, List[str]]
A single file, a directory (every *.md file in it, read in name order), or a list of file paths. Raises FileNotFoundError when a string path does not exist.
Optional[bool]
default:"None"
Load files in parallel on max_workers threads when there is more than one. None uses the loader’s concurrent setting. Agents are returned in file order either way.
float
default:"10.0"
Files above this size are skipped.
Any
Override the parsed configuration for every agent, for example model_name="gpt-5.4".
A file that fails to parse or build is logged and skipped. Each file needs YAML frontmatter and a non-empty body, which becomes the system prompt. The frontmatter is validated as an AgentConfig, so any of its fields or aliases can be set there. name defaults to the file name without its extension and description to "Agent loaded from markdown"; every other unset field keeps the Agent default.
The module also exports two shortcuts that build a default AgentLoader:

load_agents_from_yaml()

Load agents, and a SwarmRouter if one is configured, from a YAML file or string.
Optional[str]
default:"None"
Path to the YAML definitions file. It holds either an agents list with an optional swarm_architecture section, or a bare list of agents, as create_agent_file() writes it. Raises FileNotFoundError when the file does not exist and yaml_string is not given.
ReturnTypes
default:"\"auto\""
Controls the return shape. One of "auto", "agents", "both", "swarm", "run_swarm". Any other value raises ValueError.
Optional[str]
default:"None"
YAML content to use instead of reading yaml_file.
Any
Agent arguments applied to every loaded agent, overriding the file.
Each agent entry is validated as an AgentConfig, and keys the schema does not define pass through to Agent. A nested model: block (model_name, temperature, max_tokens) is merged into the entry. An entry without a name is called Agent_1, Agent_2, and so on by position. swarm_architecture is passed to SwarmRouter as keyword arguments. Raises ValueError when no agents are defined.
"swarm" and "run_swarm" execute the swarm, which calls the model. They raise ValueError when the file has no swarm_architecture or no task.

load_many_agents_from_yaml()

Load agents from a list of YAML files with per-file return type control.
Returns one load_agents_from_yaml() result per file. return_types needs one entry per file; the default ["auto"] covers a single file and raises IndexError for more.

load_agents_from_csv()

Load agents from a CSV, JSON, or YAML file of agent rows.
Union[str, Path]
Path to a .csv, .json, .yaml, or .yml file (any case). A JSON or YAML file holds a list of agent objects. Raises FileNotFoundError when the file is missing and ValueError for any other extension.
Any
Agent arguments applied to every loaded agent, overriding the file, for example model_name="gpt-5.4".
Each row is validated as an AgentConfig, so columns use its field names or aliases (name, description, model, dynamic_temperature, streaming). A blank CSV cell means unset, so the Agent default applies. Rows are built on max_workers threads and returned in file order. Rows that fail validation are printed and skipped.

create_agent_file()

Validate agent configurations and write them to a CSV, JSON, or YAML file.
Union[str, Path]
Destination file. Its extension picks the format: .csv, .json, .yaml, or .yml, in any case. Any other extension raises ValueError.
List[Union[BaseModel, Dict[str, Any]]]
Agent configurations, as dicts or pydantic models. Each is validated as an AgentConfig and written with its canonical field names; unset fields and keys the schema does not define are left out. Raises pydantic.ValidationError for an invalid configuration.

parse_markdown_file()

Lower-level: parse a Markdown file without building an agent.
Returns the parsed AgentConfig. Raises FileNotFoundError when the file is missing and ValueError when it has no frontmatter or fails validation.

parse_yaml_frontmatter()

Lowest-level: split Markdown text into its YAML frontmatter and the body below it.
Returns: {"frontmatter": dict, "content": str}. When the text does not start with a --- line, has no closing ---, or its YAML fails to parse (logged as a warning), frontmatter is {} and content is the whole text unchanged.

Usage Examples

Auto-Dispatch on Extension

The shortest path — let auto() figure out the format.

Write and Read Back an Agent File

Load Many Files in One Call

Markdown with Concurrency Control

Compose with a Swarm

Source Code

View the source on GitHub.