Skip to main content

Installation

Python 3.11 or later is required.

Public API

Everything you need is exported at the top level:
PIIDetectedError, InterpolationError, and FactoryNodeError are not re-exported at the top level — import them from sirenspec.exceptions when needed.

load_workflow

Load and validate a workflow file, returning a Workflow instance.
Signature:
Raises:

execute

Execute a workflow asynchronously and return a structured trace dict.
Signature:
Parameters: Returns: A dict with the same structure as the sirenspec run JSON output. See the CLI Reference for field descriptions.

execute_streaming

Execute a workflow and yield typed events as each node completes. Ideal for building interactive CLI tools, web dashboards, or any integration that needs real-time progress.
Signature:
Parameters: Yields: The generator yields two types of events:
  1. NodeCompleteEvent — Emitted once per node (active or skipped):
    • node_id: str — The identifier of the completed node.
    • node_type: str — One of "agent", "tool", "swrm", "factory", or "workflow".
    • status: Literal["success", "skipped", "failed"] — The node’s execution result.
    • output: Any — The node’s output value (string, dict, list, or None).
    • writes: str — The context path written by this node (only for successful agent nodes).
    • error: str | None — Error message when status="failed".
    • tokens: int — Total tokens consumed by this node (0 for tool and skipped nodes).
    • agents: list[dict[str, Any]] | None — Per-agent execution traces (swrm nodes only). Each dict contains id, prompt_sent, response_received, tokens, duration_ms, and error. None for all non-swrm node types.
    • duration_ms: float | None — Wall-clock execution time in milliseconds for swrm nodes. None for all other node types.
  2. SummaryEvent — Emitted once at the end of execution:
    • total_nodes: int — Number of nodes that ran (active nodes only, excluding skipped).
    • total_tokens: int — Aggregate token count across all active nodes.
    • status: Literal["success", "failed"] — Overall workflow status.
    • duration_ms: float — Wall-clock execution time in milliseconds.
Example: Building a Progress Bar

WorkflowRegistry

A registry that maps string names to Workflow instances. Pass a populated registry to execute or execute_streaming when your workflow uses workflow nodes with named ref values. File-path refs (starting with . or /) are resolved directly from disk — no registry needed for those.
Methods:

Error Handling

All SirenSpec exceptions inherit from SirenSpecError:

Extending SirenSpec

Custom LLM Provider

Implement the LLMProvider Protocol to add support for a new backend:

Custom Guardrail

Subclass Guardrail to add a new per-node policy:

Token Usage

TokenUsage is a frozen dataclass for tracking prompt and completion tokens:
Fields: Properties / Methods:

Streaming in Web Applications

The streaming API is ideal for interactive dashboards, chat interfaces, or real-time monitoring:
On the client side (JavaScript):