> 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/ai-methods/python.md). Agents can also search and read this documentation through its [MCP server](https://dnotifier.gitbook.io/product-docs/~gitbook/mcp).

# Python

Requires `notifier.ai_enabled == True`. Prefer `transport="http"`.

## `send_ai()`

```python
async def send_ai(
    sender_id: str,
    message: Any,
    save_history: bool = True,
    session_id: str | None = None,
    provider: str | None = None,
    model: str | None = None,
) -> Any
```

| Parameter      | Type          | Required | Default         | Description                                                                                               |
| -------------- | ------------- | -------- | --------------- | --------------------------------------------------------------------------------------------------------- |
| `sender_id`    | `str`         | ✅        | —               | User id                                                                                                   |
| `message`      | `Any`         | ✅        | —               | Prompt or chat payload                                                                                    |
| `save_history` | `bool`        | —        | `True`          | Persist to AI history                                                                                     |
| `session_id`   | `str \| None` | —        | new UUID        | Continue existing session                                                                                 |
| `provider`     | `str \| None` | —        | project default | AI provider (e.g. `"open_ai"`). Merged into request `data`.                                               |
| `model`        | `str \| None` | —        | project default | Model id (e.g. `"gpt-4o"`). Merged into request `data`. Used for session logs and workflow observability. |

**Server metadata:** `type: 'ai'`, `code: 300`

```python
response = await notifier.send_ai(
    sender_id="user-123",
    provider="open_ai",
    model="gpt-4o",
    message={"text": "Summarize our refund policy."},
)

# Chat-style
await notifier.send_ai(
    sender_id="user-123",
    message={
        "useKnowledgeBase": True,
        "messages": [
            {"role": "system", "content": "You are a support agent."},
            {"role": "user", "content": "How do I reset my password?"},
        ],
    },
)
```

## `fetch_ai_history()`

```python
async def fetch_ai_history(sender_id: str) -> Any
```

**Server code:** `301`

## `delete_ai_history_message()`

```python
async def delete_ai_history_message(sender_id: str, message_id: str) -> Any
```

**Server code:** `302`

## `search()`

```python
async def search(
    sender_id: str,
    query: str,
    limit: int | None = None,
    min_similarity: float | None = None,
    filterby_source: str | None = None,
    vector_store: dict | None = None,
    embedding: dict | None = None,
    server_config: dict | None = None,
    **kwargs,
) -> Any
```

| Parameter                                    | Type            | Required | Description                                                                                                                                  |
| -------------------------------------------- | --------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| `sender_id`                                  | `str`           | ✅        | non-empty                                                                                                                                    |
| `query`                                      | `str`           | ✅        | non-empty after trim                                                                                                                         |
| `limit`                                      | `int \| None`   | —        | optional result cap                                                                                                                          |
| `min_similarity`                             | `float \| None` | —        | 0–1 similarity threshold                                                                                                                     |
| `filterby_source`                            | `str \| None`   | —        | metadata filter                                                                                                                              |
| `vector_store`, `embedding`, `server_config` | `dict \| None`  | —        | See [Vector stores](https://dnotifier.gitbook.io/product-docs/ai/vector-stores.md). CamelCase names (`vectorStore`, `serverConfig`) work too |

**Server code:** `303`

```python
hits = await notifier.search(
    sender_id="user-123",
    query="refund policy",
    limit=5,
    min_similarity=0.7,
    filterby_source="help-center",
)
```

## `add_document()`

```python
async def add_document(
    sender_id: str,
    record_id: str | None = None,
    content: str | None = None,
    text: str | None = None,
    doc_type: str | None = None,
    metadata: dict | None = None,
) -> Any
```

Also accepts `vector_store`, `embedding`, and `server_config` (`vectorStore` and `serverConfig` work too), including on update, delete, list, and get. See [Vector stores and embeddings](https://dnotifier.gitbook.io/product-docs/ai/vector-stores.md).

**Server code:** `306`

## `update_document()`

Same signature as `add_document`. **Server code:** `307`

## `delete_document()`

```python
async def delete_document(sender_id: str, record_id: str | None = None) -> Any
```

**Server code:** `308`

## `list_documents()`

```python
async def list_documents(
    sender_id: str,
    limit: int | None = None,
    offset: int | None = None,
    doc_type: str | None = None,
) -> Any
```

**Server code:** `309`

## `get_document()`

```python
async def get_document(sender_id: str, record_id: str | None = None) -> Any
```

**Server code:** `310`

## `send_with_openai()`

Alias that checks `ai_enabled` then delegates to `send_ai()`.

```python
await notifier.send_with_openai(
    sender_id="user-123",
    message={"text": "Hello"},
)
```

Raises `RuntimeError('AI is not enabled for this subscription.')` when AI is disabled.

## Document CRUD example

```python
await notifier.add_document(
    sender_id="user-123",
    record_id="doc-001",
    content="Refunds are processed within 5 business days.",
    doc_type="text",
    metadata={"source": "help-center"},
)

doc = await notifier.get_document(
    sender_id="user-123",
    record_id="doc-001",
)

await notifier.delete_document(
    sender_id="user-123",
    record_id="doc-001",
)
```
