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

# Python

Fetch and delete stored **AI conversation** turns over HTTP.

## Fetch history

```python
import asyncio
from dnotifier import DNotifier

USER_ID = "user-123"

async def main():
    notifier = DNotifier(
        app_id="your-app-id",
        secret="your-app-secret",
        transport="http",
        user_id=USER_ID,
    )

    await notifier.connect()

    await notifier.send_ai(
        sender_id=USER_ID,
        message={"text": "What is your return policy?"},
        save_history=True,
    )

    history = await notifier.fetch_ai_history(sender_id=USER_ID)
    print(history)

asyncio.run(main())
```

## Delete a message

```python
history = await notifier.fetch_ai_history(sender_id=USER_ID)

message_id = None
if isinstance(history, dict) and isinstance(history.get("data"), dict):
    messages = history["data"].get("messages")
    if isinstance(messages, list) and messages:
        first = messages[0]
        if isinstance(first, dict):
            message_id = first.get("id")

if message_id:
    await notifier.delete_ai_history_message(
        sender_id=USER_ID,
        message_id=str(message_id),
    )
```

## Load history for UI

```python
async def load_ai_history(notifier: DNotifier) -> list[dict]:
    raw = await notifier.fetch_ai_history(sender_id=USER_ID)
    if isinstance(raw, dict) and isinstance(raw.get("data"), dict):
        messages = raw["data"].get("messages")
        if isinstance(messages, list):
            return messages
    return []
```

## Skip history for internal calls

```python
await notifier.send_ai(
    sender_id=USER_ID,
    message={"text": "Classify: urgent or normal"},
    save_history=False,
)
```

## Learn more

* [**Sessions**](https://dnotifier.gitbook.io/product-docs/ai/sessions/python.md)
* [**Chat history**](https://dnotifier.gitbook.io/product-docs/chat/chat-history/python.md)
