inspect_flow.api
Python API
init
Initialize the inspect_flow API.
def init(
log_level: str = DEFAULT_LOG_LEVEL,
display: DisplayType = "full",
dotenv_base_dir: str | None = ".",
) -> Nonelog_levelstr-
The Inspect Flow log level to use.
displayDisplayType-
The display mode.
dotenv_base_dirstr | None-
Directory (or file path) to search for
.envfiles. If a file path is given, its parent directory is used.Noneto skip.envloading. Defaults to"."(current working directory).
run
Run an inspect_flow evaluation.
def run(
spec: FlowSpec,
base_dir: str | None = None,
*,
dry_run: bool = False,
resume: bool = False,
) -> NonespecFlowSpec-
The flow spec configuration.
base_dirstr | None-
The base directory for resolving relative paths. Defaults to the current working directory.
dry_runbool-
If
True, do not run eval, but show a count of tasks that would be run. resumebool-
If
True, reuse the log directory from the previous run.
check
Check completeness of an inspect_flow evaluation against existing logs.
def check(
spec: FlowSpec,
base_dir: str | None = None,
*,
log_dir: str | None = None,
) -> CheckResult | NonespecFlowSpec-
The flow spec configuration.
base_dirstr | None-
The base directory for resolving relative paths. Defaults to the current working directory.
log_dirstr | None-
Log directory to check against. Overrides the
log_dirin the spec.
run_step
Run a step function on the given logs.
def run_step(
step: WrappedStepFunction[P],
logs: list[str] | list[EvalLog] | EvalLog | str,
dry_run: bool = False,
filter: LogFilter | str | Sequence[LogFilter | str] | None = None,
exclude: LogFilter | str | Sequence[LogFilter | str] | None = None,
recursive: bool = True,
expand_paths: bool = True,
store: FlowStore | None = None,
*args: P.args,
**kwargs: P.kwargs,
) -> NonestepWrappedStepFunction[P]-
The step function to run.
logslist[str] | list[EvalLog] | EvalLog | str-
EvalLog objects or paths to eval logs to process.
dry_runbool-
If True, run steps but skip writing logs to disk.
filterLogFilter | str | Sequence[LogFilter | str] | None-
A log filter or sequence of filters. Only logs that pass all filters are processed. Accepts callables, registered names, or “file.py@name” strings.
excludeLogFilter | str | Sequence[LogFilter | str] | None-
A log filter or sequence of filters. Logs that pass any exclude filter are skipped. Accepts the same formats as filter.
recursivebool-
Recurse into directories (default: True).
expand_pathsbool-
Expand directory paths to individual log paths (default: True). Set to False when paths are already resolved.
storeFlowStore | None-
Optional flow store. Written logs are added to the store.
*argsP.args-
Positional arguments to pass to the step function.
**kwargsP.kwargs-
Keyword arguments to pass to the step function.
load_spec
Load a spec from file.
def load_spec(
file: str,
*,
args: dict[str, Any] | None = None,
) -> FlowSpecfilestr-
The path to the spec file.
argsdict[str, Any] | None-
A dictionary of arguments to pass as kwargs to the function in the flow config.
config
Return the flow spec configuration.
def config(
spec: FlowSpec,
base_dir: str | None = None,
) -> strspecFlowSpec-
The flow spec configuration.
base_dirstr | None-
The base directory for resolving relative paths. Defaults to the current working directory.
list_logs
List log paths grouped by directory, directories ordered by most recent log file.
Within each directory, logs are sorted by filename timestamp descending. Logs without a timestamp prefix sort at the end.
def list_logs(
log_dir: str | None = None,
store: str | FlowStore = "auto",
since: str | datetime | None = None,
until: str | datetime | None = None,
) -> list[str]log_dirstr | None-
Directory to list logs from recursively. If provided, the store is not used.
storestr | FlowStore-
The store to read logs from. Can be a FlowStore instance, a path, or
"auto"for the default. Only used whenlog_dirisNone. sincestr | datetime | None-
Only include logs whose filename timestamp is at or after this date. Accepts a
datetimeor a date string. Date strings like"2024-01-15"resolve to midnight; relative expressions like"today"resolve to the current time. untilstr | datetime | None-
Only include logs whose filename timestamp is at or before this date. Accepts a
datetimeor a date string. Date strings like"2024-06-01"resolve to midnight; relative expressions like"yesterday"resolve to the current time minus one day.
store_get
Get a FlowStore instance.
def store_get(store: str = "auto", create: bool = True) -> FlowStorestorestr-
The store location. Can be a path to the store directory or
"auto"for the default store location. createbool-
Whether to create the store if it does not exist.
delete_store
Delete a flow store.
def delete_store(store_path: str) -> Nonestore_pathstr-
Path to the store directory.
copy_all_logs
Copy all log files from src_dir to dest_dir, preserving directory structure.
def copy_all_logs(src_dir: str, dest_dir: str, dry_run: bool, recursive: bool) -> Nonesrc_dirstr-
Source directory containing log files.
dest_dirstr-
Destination directory to copy log files to.
dry_runbool-
If True, preview what would be copied without making changes.
recursivebool-
If True, search src_dir recursively for log files.
tag
Add or remove tags on eval logs.
@step
def tag(
logs: list[EvalLog],
*,
add: list[str] | None = None,
remove: list[str] | None = None,
author: str | None = None,
reason: str | None = None,
) -> list[EvalLog]logslist[EvalLog]-
EvalLog objects to modify.
addlist[str] | None-
Tags to add.
removelist[str] | None-
Tags to remove.
authorstr | None-
Provenance author. Defaults to git user.
reasonstr | None-
Reason for the edit.
metadata
Set or delete metadata fields on eval logs.
@step
def metadata(
logs: list[EvalLog],
*,
set: dict[str, Any] | None = None,
remove: list[str] | None = None,
author: str | None = None,
reason: str | None = None,
) -> list[EvalLog]logslist[EvalLog]-
EvalLog objects to modify.
setdict[str, Any] | None-
Key-value pairs to set.
removelist[str] | None-
Keys to delete.
authorstr | None-
Provenance author. Defaults to git user.
reasonstr | None-
Reason for the edit.
copy
Copy eval logs to a destination directory.
@step
def copy(
logs: list[EvalLog],
*,
dest: str,
source_prefix: str | None = None,
overwrite: bool = False,
store: FlowStore | str | None = None, # noqa: ARG001 handled by @step wrapper
) -> list[EvalLog]logslist[EvalLog]-
list of EvalLog to copy.
deststr-
Destination directory (local or S3).
source_prefixstr | None-
Directory prefix to strip from source paths. Without this option, files are copied flat into the destination. When provided, preserves directory structure relative to the prefix.
overwritebool-
Overwrite existing files at the destination.
storeFlowStore | str | None-
Optional flow store. The copied log is added to the store.
FlowStore
Interface for flow store implementations.
class FlowStore(ABC)Attributes
store_pathstr-
The path to the store directory.
versionstr-
The store version.
Methods
- import_log_path
-
Import a log file(s) or directory(ies) into the store.
@abstractmethod def import_log_path( self, log_path: str | Sequence[str], recursive: bool = False, dry_run: bool = False, verbose: bool = False, ) -> Nonelog_pathstr | Sequence[str]-
Path or paths to log files or directories containing log files.
recursivebool-
Whether to search directories recursively.
dry_runbool-
Preview what would be imported without making changes
verbosebool-
Print paths of files being added
- get_logs
-
Get all log file paths in the store.
@abstractmethod def get_logs(self, filter: LogFilter | None = None) -> set[str]filterLogFilter | None-
Optional filter to apply to log headers. Only logs passing the filter are included. It is an error to specify both a per-call filter and a store-level filter.
- remove_log_prefix
-
Remove logs matching the given prefixes.
@abstractmethod def remove_log_prefix( self, prefix: str | Sequence[str], missing: bool = False, recursive: bool = False, dry_run: bool = False, verbose: bool = True, filter: LogFilter | None = None, ) -> Noneprefixstr | Sequence[str]-
One or more prefixes to match against log paths.
missingbool-
Whether to remove log paths that are missing from the file system.
recursivebool-
Whether to remove log files recursively.
dry_runbool-
Preview what would be removed without making changes
verbosebool-
Print paths of files being removed
filterLogFilter | None-
Optional filter to narrow which matched logs are removed. Each candidate log’s header is read and only those passing the filter are removed.
StepResult
Fine-grained return type for @step functions.
Steps can also return a sequence of EvalLog directly (equivalent to StepResult(logs=logs, modified=True)) or [] (equivalent to StepResult(modified=False, skip_log_steps=True)).
class StepResult(NamedTuple)Attributes
logslist[EvalLog]-
The logs returned to the caller.
modifiedbool-
Whether the logs were modified. When True, the logs are written back to disk and becomes the current log for subsequent nested steps. When False, the log is returned but the current context log is not advanced.
flushbool-
Write all dirty logs immediately, even if nested inside an outer step.
skip_log_stepsbool-
Skip remaining steps for this log. run_step will move to the next log.
DisplayType
Display type for flow output.
DisplayType = Literal["full", "rich", "plain"]