> For the complete documentation index, see [llms.txt](https://dnotifier.gitbook.io/product-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dnotifier.gitbook.io/product-docs/reference/workflow-exports/python.md). Agents can also search and read this documentation through its [MCP server](https://dnotifier.gitbook.io/product-docs/~gitbook/mcp).

# Python

## Imports

```python
from dnotifier import (
    Agent,
    define_agent,
    Workflow,
    WorkflowContext,
    WorkflowRunner,
    WorkflowError,
)
```

## `define_agent()`

```python
def define_agent(
    name: str,
    run: Callable[[WorkflowContext], Awaitable] | None = None,
    *,
    provider: str | None = None,
    model: str | None = None,
    description: str | None = None,
    instructions: str | None = None,
    tools: list[dict] | None = None,
    permissions: dict | None = None,
    observability: bool = False,
) -> Agent
```

Classic agents pass `run`. Tool agents pass `tools` (and may omit `run`).

```python
intent_agent = define_agent(
    name="intent-agent",
    run=lambda ctx: classify_intent(ctx),
)

async def classify_intent(ctx):
    await ctx.send_ai(
        message={"text": "Classify: search or general?"},
        save_history=False,
        label="Intent classification",
    )
    return {"intent": "search"}
```

Also on the client: `notifier.tools.register`, `notifier.mcp.connect`, `notifier.run_agent`.\
→ [Tools & MCP — Python](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/tools-and-mcp/python.md)

## `Workflow` class

```python
Workflow(
    name: str,
    entry: Callable[[WorkflowContext], Awaitable],
    description: str | None = None,
    observability: bool = False,
)
```

| Parameter       | Type           | Required | Default | Description            |
| --------------- | -------------- | -------- | ------- | ---------------------- |
| `name`          | `str`          | ✅        | —       | Workflow identifier    |
| `entry`         | async callable | ✅        | —       | Main workflow function |
| `description`   | `str \| None`  | —        | —       | ≤ 100 characters       |
| `observability` | `bool`         | —        | `False` | Dashboard telemetry    |

### Methods

| Method                                                | Returns         | Description                                                              |
| ----------------------------------------------------- | --------------- | ------------------------------------------------------------------------ |
| `register_agent(name, agent \| {"receiver_id": ...})` | `Workflow`      | Register one local or remote agent                                       |
| `register_agents(agents: dict)`                       | `Workflow`      | Register multiple (fluent)                                               |
| `get_agent(name)`                                     | `Agent \| None` | Lookup **local** agent                                                   |
| `listen(notifier)`                                    | `Workflow`      | Host local agents for remote `run_agent`, MCP, and A2A calls (WebSocket) |
| `stop_listening()`                                    | `Workflow`      | Stop answering remote calls                                              |

```python
workflow = Workflow(
    name="intent-router",
    description="Route to search or general Q&A",
    observability=True,
    entry=intent_router_entry,
).register_agents({
    "intent-agent": intent_agent,
})
```

## `WorkflowContext`

| Member / method                     | Description                                           |
| ----------------------------------- | ----------------------------------------------------- |
| `notifier`                          | `DNotifier` instance                                  |
| `sender_id`                         | `str`                                                 |
| `workflow`                          | `Workflow`                                            |
| `input`                             | `Any`                                                 |
| `state`                             | `dict[str, Any]`                                      |
| `agent_name`                        | `str \| None`                                         |
| `run_agent(name, input_data=None)`  | Invoke a registered agent                             |
| `run_agents(spec)`                  | Run agents in parallel → `{ results }`                |
| `wait_for_message(timeout_ms=None)` | Wait for next inbound realtime message (WebSocket)    |
| `send_ai(**opts)`                   | AI with observability (`provider` / `model` optional) |
| `fetch_ai_history(**opts)`          | AI history                                            |
| `search(**opts)`                    | Semantic search                                       |
| `add_document(**opts)`              | Add document                                          |
| `update_document(**opts)`           | Update document                                       |
| `delete_document(**opts)`           | Delete document                                       |
| `list_documents(**opts)`            | List documents                                        |
| `get_document(**opts)`              | Get document                                          |
| `record_step(step: dict)`           | Custom observability step                             |

## `run_workflow()`

On `DNotifier`:

```python
async def run_workflow(
    workflow: Workflow,
    input_data: Any,
    sender_id: str | None = None,
) -> dict[str, Any]
```

Returns `{"result": ..., "state": ..., "executionId": ...}`.

## `WorkflowRunner` (advanced)

```python
runner = WorkflowRunner(
    notifier=notifier,
    sender_id="user-123",
    workflow=workflow,
)
run = await runner.run(input_data="How does messaging work?")
```

## `WorkflowError`

Exception subclass for validation failures (empty names, missing agents, description too long).

```python
raise WorkflowError("intent-agent: empty input", agent_name="intent-agent")
```
