Skip to content

latent.flows.agent_inference_flow

agent_inference_flow — Run any BaseAgent on a dataset and collect results.

run_agent_inference is the library entry point: rows run concurrently on asyncio and Prefect is never imported, so it is safe inside another orchestrator (a Temporal activity, a web handler). agent_inference_flow runs the same rows as process_single_row Prefect tasks, for callers who want Prefect's task-run records and task cache policy.

A caller that records each result as it completes, without a DataFrame, uses run_items (latent.agents.eval.items, re-exported by latent.flows).

Functions

agent_inference_flow

agent_inference_flow(eval_data: pd.DataFrame, agent_factory: Callable[[], Any] | None = None, agent_factory_for_row: Callable[[dict[str, Any]], Any] | None = None, question_column: str = 'question', context_columns: list[str] | None = None, context_formatter: Callable[[dict[str, Any]], str] | None = None, id_column: str | None = None, concurrency: int = 1, include_events: bool = False, per_row_timeout_s: float | None = None) -> dict[str, Any]

run_agent_inference, with each row run as a Prefect task.

Each row is a process_single_row task (@latent.prefect.task): a task-run record, the task cache policy (row inputs plus task identity, 7-day expiry, result persisted; off in the dev environment), and an MLflow span when a run is active. Rows still run concurrently, concurrency at a time.

Outside a Prefect flow run with no PREFECT_API_URL, the first task call starts a temporary Prefect server for the process. Use run_agent_inference inside another orchestrator. Arguments and return value are run_agent_inference's.

run_agent_inference

run_agent_inference(eval_data: pd.DataFrame, agent_factory: Callable[[], Any] | None = None, agent_factory_for_row: Callable[[dict[str, Any]], Any] | None = None, question_column: str = 'question', context_columns: list[str] | None = None, context_formatter: Callable[[dict[str, Any]], str] | None = None, id_column: str | None = None, concurrency: int = 1, include_events: bool = False, per_row_timeout_s: float | None = None) -> dict[str, Any]

Run an agent on each row of eval_data and collect inference results.

Rows run concurrently on the calling event loop (asyncio.gather bounded by an asyncio.Semaphore of size concurrency). Prefect is never imported; agent_inference_flow is the variant that runs each row as a Prefect task.

Args: eval_data: DataFrame with at least a question column. agent_factory: Callable returning a BaseAgent instance. Use this when every row gets the same kind of agent. agent_factory_for_row: Callable(row_dict) -> BaseAgent, for when the agent depends on the row — a per-channel context, a persona, a conversation history. Mutually exclusive with agent_factory. Two parameters rather than one that sniffs arity: a factory with an optional parameter is genuinely ambiguous, and guessing wrong silently feeds it a row dict. question_column: Column containing the question/prompt. context_columns: Optional columns to include as context. context_formatter: Callable(row_dict) -> str for context formatting. id_column: Column to use as row ID. If None, uses DataFrame index. concurrency: Number of rows in flight at once; must be >= 1. For no bound, pass the row count. include_events: If True, each result row carries an events list: every event the agent streamed for that row, in order, as the latent.events dataclasses. A row that times out or raises keeps the events streamed before it stopped. per_row_timeout_s: Deadline for one row, agent construction included. A row that misses it becomes an error row; the rest continue.

Returns: Dict with inference_results (one dict per row, in row order) and summary.