Server
WorkflowServer
#
WorkflowServer(*, middleware: list[Middleware] | None = None, exception_handlers: Mapping[Any, Any] | None = None, workflow_store: AbstractWorkflowStore | None = None, persistence_backoff: list[float] = [0.5, 3], runtime: Runtime | None = None, idle_timeout: float = 60.0, sse_heartbeat_interval: float | None = 25.0, accept_context_api: bool = False)
HTTP server that exposes workflows as REST APIs.
Wraps one or more Workflow instances behind an HTTP API with endpoints
for running workflows, streaming events, and sending human-in-the-loop
input. Includes a built-in debugging UI served at the root path.
Example:
from workflows import Workflow, step
from workflows.events import StartEvent, StopEvent
from llama_agents.server import WorkflowServer
class GreetingWorkflow(Workflow):
@step
async def greet(self, ev: StartEvent) -> StopEvent:
name = ev.get("name", "World")
return StopEvent(result=f"Hello, {name}!")
server = WorkflowServer()
server.add_workflow("greet", GreetingWorkflow())
# Run with: python -m workflows.server my_server.py
# Or programmatically:
# await server.serve(host="0.0.0.0", port=8080)
The ASGI application is available as server.app for embedding in a
larger application or mounting behind a reverse proxy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
middleware
|
list[Middleware] | None
|
Starlette middleware to apply to the ASGI app. Defaults to a permissive CORS configuration. Passing a custom list replaces the default entirely. |
None
|
exception_handlers
|
Mapping[Any, Any] | None
|
Starlette exception handlers mapping exception types to handler callables. Defaults to JSON error responses with logging. Passing a custom mapping replaces the default entirely. |
None
|
workflow_store
|
AbstractWorkflowStore | None
|
Persistence backend for handler state, events, and
ticks. Defaults to |
None
|
persistence_backoff
|
list[float]
|
Retry delays (in seconds) when writing handler
state to the store fails. Each entry is a sleep duration before
the next attempt. Defaults to |
[0.5, 3]
|
runtime
|
Runtime | None
|
Custom workflow runtime. When |
None
|
idle_timeout
|
float
|
Seconds to wait after a workflow becomes idle before
releasing it from memory. The workflow is automatically
reloaded when new events arrive. Defaults to |
60.0
|
sse_heartbeat_interval
|
float | None
|
Seconds between SSE keep-alive comments
( |
25.0
|
accept_context_api
|
bool
|
Allow the |
False
|
Source code in llama_agents/server/server.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
add_workflow
#
add_workflow(name: str, workflow: Workflow, additional_events: list[type[Event]] | None = None) -> None
Register a workflow under the given name.
The workflow becomes available at /workflows/{name}/run and
/workflows/{name}/run-nowait.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
URL-safe name for the workflow. |
required |
workflow
|
Workflow
|
The workflow instance to serve. |
required |
additional_events
|
list[type[Event]] | None
|
Extra event types to expose in the debugger UI
and |
None
|
Source code in llama_agents/server/server.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
get_workflows
#
get_workflows() -> dict[str, Workflow]
Return registered workflows as a dict by name. Only available after start().
Source code in llama_agents/server/server.py
163 164 165 166 167 168 169 | |
start
async
#
start() -> WorkflowServer
Resumes previously running workflows, if they were not complete at last shutdown.
Idle workflows are not resumed - they remain released and will be loaded on-demand when events arrive for them.
Source code in llama_agents/server/server.py
175 176 177 178 179 180 181 182 | |
contextmanager
async
#
contextmanager() -> AsyncGenerator[WorkflowServer, None]
Use this server as a context manager to start and stop it
Source code in llama_agents/server/server.py
184 185 186 187 188 189 190 191 | |
stop
async
#
stop() -> None
Gracefully shut down all running workflow handlers.
Source code in llama_agents/server/server.py
193 194 195 | |
serve
async
#
serve(host: str = 'localhost', port: int = 80, uvicorn_config: dict[str, Any] | None = None) -> None
Start the HTTP server and block until shutdown.
Calls start() internally before serving.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
Bind address. Defaults to |
'localhost'
|
port
|
int
|
Bind port. Defaults to |
80
|
uvicorn_config
|
dict[str, Any] | None
|
Additional keyword arguments forwarded to
|
None
|
Source code in llama_agents/server/server.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |