Overview
TheAgentLoader 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
Default for
load_agents_from_markdown() when its own concurrent argument is not given. AgentLoader(concurrent=False) loads Markdown files one at a time.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.file_path(str): Path to a.md,.yaml,.yml,.csv, or.jsonfile. The extension is matched in any case.*args,**kwargs: Forwarded to the underlying loader.
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 forauto() — loads a single file by dispatching on extension, and returns the same shape.
load_multiple_agents()
Applyauto() to a list of files. Each file may be a different format.
file_paths(List[str]): Paths to agent definition files.
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.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.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.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.
AgentLoader:
load_agents_from_yaml()
Load agents, and aSwarmRouter if one is configured, from a YAML file or string.
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.Controls the return shape. One of
"auto", "agents", "both", "swarm", "run_swarm". Any other value raises ValueError.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.
load_many_agents_from_yaml()
Load agents from a list of YAML files with per-file return type control.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.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.Agent arguments applied to every loaded agent, overriding the file, for example model_name="gpt-5.4".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.Destination file. Its extension picks the format:
.csv, .json, .yaml, or .yml, in any case. Any other extension raises ValueError.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.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.{"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 — letauto() figure out the format.