Skip to content

API reference

Everything below is importable from fastaxi, unless its heading says otherwise.

The app

fastaxi.AXI

AXI(
    name: str,
    *,
    description: str = "",
    version: str | None = None,
    dist: str | None = None,
    response_format: str | None = None,
    format_flag: bool = True,
    strict_docs: bool = False,
    builtins: bool = True,
    skill: str | Path | None = None,
    skill_description: str | None = None,
)

Bases: Group

An Agent eXperience Interface CLI.

response_format sets an app-wide output format ("toon" or "csv") that commands without their own response_format use; by default each command's format is inferred from its return type.

skill replaces the generated Agent Skill text (a string or a Path); {bin}, {usage} and {commands} in it are filled in. See fastaxi.setup.skill. skill_description overrides just its description.

home

home(fn: CommandFn) -> CommandFn
home(
    *,
    next_steps: Sequence[str] | None = None,
    empty: str | None = None,
    default_fields: Sequence[str] | None = None,
    max_default_fields: int = MAX_DEFAULT_FIELDS,
    items_key: str | None = None,
) -> Callable[[CommandFn], CommandFn]
home(fn: Any = None, **options: Any) -> Any

Register the content-first view shown when the tool runs with no arguments.

Usable as @app.home or @app.home(next_steps=[...]).

all_children

all_children() -> dict[str, Command | Group]

Children as users see them: the app's commands plus the built-ins (setup) it does not override.

commands

commands(*, builtins: bool = True) -> list[Command]

Every command in --help order, home view first. builtins=False leaves out setup.

fill_bin

fill_bin(text: str) -> str

Replace {bin} with the tool name.

render_skill

render_skill() -> str

This tool's Agent Skill (SKILL.md) text, generated from its help.

tools

tools(
    *,
    prefix: str | None = None,
    include: Sequence[str] | None = None,
    exclude: Sequence[str] | None = None,
    home: bool = True,
    errors: Literal["return", "raise"] = "return",
) -> list[Callable[..., Any]]

Each command as a plain function, ready to hand to an agent framework.

Tools are named <prefix>_<command> (prefix defaults to the app name). include/exclude take command paths such as "list" or "repo ls"; home=False leaves out the home view. Tools return the CLI's text output; errors come back as text unless errors="raise". See fastaxi.tools.

check

check() -> tuple[list[str], list[str]]

Check docstrings against signatures. Returns (errors, warnings).

validate

validate() -> None

Raise DefinitionError for docstring drift; warn about undocumented parameters.

get_version

get_version() -> str

version= if given, else the installed version of dist, else "unknown".

exec_path

exec_path() -> str

Path of the executable this run was started from (for hooks).

run

run(argv: Sequence[str] | None = None) -> NoReturn

Entry point: run with sys.argv and exit.

invoke

invoke(
    args: str | Sequence[str] = (),
    *,
    prog: str | None = None,
) -> Result

Run in-process like a shell would and capture the output (for tests).

The first call also runs validate(), so docstring drift fails tests.

main

main(
    argv: Sequence[str] | None = None,
    *,
    out: IO[str],
    err: IO[str],
    prog: str | None = None,
) -> int

Run once and return the exit code. Never raises for expected failures.

fastaxi.Group

Group(
    name: str, parent: Group | None, help: str | None = None
)

A named set of subcommands, such as repo in tool repo ls. Create one with group().

root property

root: AXI

The AXI this group belongs to.

full_name property

full_name: str

How it is typed, with the app name: "todo-axi repo".

path property

path: str

How it is typed after the app name; "" for the app itself.

summary property

summary: str

One-line description for --help listings.

all_children

all_children() -> dict[str, Command | Group]

Children as users see them (the root adds built-in commands).

group

group(name: str, *, help: str | None = None) -> Group

Add a subgroup and return it; register commands on it with .command.

command

command(fn: CommandFn) -> CommandFn
command(
    name: str | None = None,
    *,
    help: str | None = None,
    examples: Sequence[str] | None = None,
    response_format: str | None = None,
    default_fields: Sequence[str] | None = None,
    max_default_fields: int = MAX_DEFAULT_FIELDS,
    items_key: str | None = None,
    next_steps: Sequence[str] | None = None,
    empty: str | None = None,
) -> Callable[[CommandFn], CommandFn]
command(name: Any = None, **options: Any) -> Any

Register a command. Usable as @app.command or @app.command(...).

next_steps are suggested follow-up commands shown with the output ({bin} becomes the tool name); empty is the message shown when the command returns an empty list. A command can replace both, and set a total and summary, by returning data, Response(...).

fastaxi.Command

Command(
    fn: Callable[..., Any],
    name: str,
    parent: Group,
    *,
    help: str | None = None,
    examples: Sequence[str] | None = None,
    response_format: str | None = None,
    default_fields: Sequence[str] | None = None,
    max_default_fields: int = MAX_DEFAULT_FIELDS,
    items_key: str | None = None,
    next_steps: Sequence[str] | None = None,
    empty: str | None = None,
    is_home: bool = False,
)

A registered command: its function, parsed parameters and output plan.

Built by Group.command and AXI.home, which pass their options through (see Group.command); is_home marks the home view. Definition problems raise DefinitionError here, at registration time.

full_name property

full_name: str

How it is typed, with the app name: "todo-axi list".

path property

path: str

How it is typed after the app name: "list", "repo ls"; "" for the home view.

where property

where: str

How definition errors name this command.

summary property

summary: str

One-line description: help= if given, else the docstring summary.

usage

usage() -> str

The usage: line, e.g. todo-axi view <id> [flags].

invoke

invoke(values: dict[str, Any]) -> tuple[Any, Extras]

Call the command. Returns its data and the extras to show with it.

ainvoke async

ainvoke(values: dict[str, Any]) -> tuple[Any, Extras]

invoke for callers already inside an event loop.

render

render(
    result: object,
    extras: Extras,
    *,
    output_format: str | None = None,
    fields: list[str] | None,
    full: bool,
    full_command: str,
) -> Rendered

Render a result from invoke; output_format defaults to the command's planned format.

fastaxi.Result dataclass

Result(stdout: str, stderr: str, exit_code: int)

What AXI.invoke captured from one run.

fastaxi.axi.Invoke module-attribute

Invoke: TypeAlias = Callable[..., Result]

The type of AXI.invoke; use it to annotate pytest fixtures.

Command output

fastaxi.Response dataclass

Response(
    *,
    total: int | None = None,
    summary: Mapping[str, object] = dict[str, object](),
    next_steps: Sequence[str] | None = None,
    empty: str | None = None,
)

Extras for one command result, returned as data, Response(...).

Every field is optional; a field left unset keeps fastaxi's default.

  • total: how many items exist in all, when the command returns only some of them. Defaults to the number of items returned.
  • summary: precomputed aggregates, shown as extra keys.
  • next_steps: suggested next commands, replacing the decorator's. Unlike the decorator's, they are also shown when the list is empty.
  • empty: the message for an empty list, replacing the decorator's.
Example
@app.command(name="list", next_steps=["{bin} view <id>"])
def list_todos(status: str = "open") -> tuple[list[Todo], Response]:
    todos = db.query(status)
    return todos[:50], Response(
        total=len(todos),
        summary={"overdue": 3},
        empty=f"no {status} todos",
    )

# $ todo list
# id,title,status
# ...
# count: 50 of 213 total
# overdue: 3
# help[1]: todo view <id>

Markers

fastaxi.Default module-attribute

Default = _DefaultMarker()

Marks a record field as shown by default in list output.

Fields not marked are still available with --fields. Without any Default marks, fastaxi picks up to four fields itself (id, title, status...).

Example
class Todo(BaseModel):
    id: Annotated[int, Default]
    title: Annotated[str, Default]
    body: str = ""

# $ todo list
# id,title
# 1,Ship v1

fastaxi.Truncate dataclass

Truncate(chars: int = 1000)

Truncate a string field to chars characters unless --full is passed.

The cut value ends with how much of it is shown, and the output suggests the same command with --full.

Example
class Todo(BaseModel):
    id: int
    body: Annotated[str, Truncate(500)]

# $ todo view 42
# id: 42
# body: "Cover the quickstart… (500 of 2048 chars)"
# help[1]: todo view 42 --full

fastaxi.Arg dataclass

Arg(help: str | None = None)

Bases: ParamInfo

A positional argument.

Parameters without a default are positional already; Arg adds help text, or makes a parameter that has a default an optional positional.

Example
@app.command()
def view(id: Annotated[int, Arg(help="Todo id")]) -> Todo: ...

@app.command()
def search(query: Annotated[str, Arg(help="Text to look for")] = "") -> list[Todo]: ...

# $ todo view --help        ->  usage: todo view <id> [flags]
# $ todo search --help      ->  usage: todo search [<query>] [flags]

fastaxi.Flag dataclass

Flag(
    help: str | None = None,
    aliases: Sequence[str] = (),
    hidden: bool = False,
)

Bases: ParamInfo

A --flag.

Parameters with a default are flags already; Flag adds help text and spellings, or makes a required parameter a required flag. aliases are extra spellings that are accepted (e.g. ["-a"]). hidden flags work but are left out of help and tools.

Example
@app.command(name="list")
def list_todos(
    assignee: Annotated[str | None, Flag(aliases=["-a"])] = None,
    debug: Annotated[bool, Flag(hidden=True)] = False,
) -> list[Todo]: ...

# $ todo list --help
#   --assignee, -a <assignee>
# $ todo list -a bob --debug    # both work; --debug is not in help

Errors

fastaxi.AxiError

AxiError(
    message: str,
    *,
    hint: str | None = None,
    help: str | Sequence[str] | None = None,
    code: int | None = None,
    **extra: Any,
)

Bases: Exception

An expected failure. Rendered on stdout; exits with code (default 1).

help should hold complete commands that fix or work around the problem.

to_dict

to_dict() -> dict[str, Any]

The error as data for output: error, hint, any extra keys, then help.

fastaxi.UsageError

UsageError(
    message: str,
    *,
    hint: str | None = None,
    help: str | Sequence[str] | None = None,
    code: int | None = None,
    **extra: Any,
)

Bases: AxiError

Bad invocation: unknown flag, missing argument, invalid value. Exits 2.

fastaxi.DefinitionError

Bases: TypeError

The AXI itself is defined incorrectly (raised at registration time).

Tools

fastaxi.tools.tool_spec

tool_spec(fn: Callable[..., Any]) -> ToolSpec

The ToolSpec of a tool built by AXI.tools(). Raises TypeError for any other function.

fastaxi.tools.ToolSpec dataclass

ToolSpec(
    name: str,
    description: str,
    parameters: dict[str, Any],
    command: str,
)

What a raw tool-calling API needs: name, description and a JSON schema.

fastaxi.tools.AxiToolError

Bases: Exception

Raised by tools built with errors="raise". str(exc) is the AXI error text.