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

# Python

Send a **single AI prompt** over HTTP and await the response from `send_ai`.

## Complete example

```python
import asyncio
from dnotifier import DNotifier

USER_ID = "user-123"

def extract_content(response) -> str:
    if isinstance(response, dict):
        data = response.get("data")
        if isinstance(data, dict) and data.get("content") is not None:
            return str(data["content"])
        if response.get("content") is not None:
            return str(response["content"])
    return str(response)

async def main():
    notifier = DNotifier(
        app_id="your-app-id",
        secret="your-app-secret",
        transport="http",
        user_id=USER_ID,
        on_connected=lambda: print("Authenticated"),
        on_message=lambda _: None,
    )

    await notifier.connect()

    if not notifier.ai_enabled:
        raise RuntimeError("AI is not enabled for this plan")

    response = await notifier.send_ai(
        sender_id=USER_ID,
        message={"text": "Summarize our refund policy in two sentences."},
    )

    answer = extract_content(response)
    print(f"AI: {answer}")

asyncio.run(main())
```

## Service class

```python
from dnotifier import DNotifier

class AiService:
    def __init__(self, notifier: DNotifier):
        self._notifier = notifier

    async def ask(self, user_id: str, question: str) -> str:
        response = await self._notifier.send_ai(
            sender_id=user_id,
            message={"text": question},
            save_history=True,
        )
        return extract_content(response)
```

## Without saving history

```python
await notifier.send_ai(
    sender_id=USER_ID,
    message={"text": "Classify this ticket as billing or technical."},
    save_history=False,
)
```

## Environment variables (CLI)

```python
import os
import asyncio
from dnotifier import DNotifier

async def main():
    notifier = DNotifier(
        app_id=os.environ["DNOTIFIER_APP_ID"],
        secret=os.environ["DNOTIFIER_SECRET"],
        transport="http",
        user_id="user-123",
    )
    await notifier.connect()
    # ...

asyncio.run(main())
```

## Troubleshooting

| Issue                            | Fix                                         |
| -------------------------------- | ------------------------------------------- |
| `RuntimeError: Not connected`    | `await notifier.connect()` first            |
| `ValueError: sender_id required` | Pass non-empty `sender_id`                  |
| Empty answer                     | Print full `response` dict to inspect shape |

## Learn more

* [**Sessions**](https://dnotifier.gitbook.io/product-docs/ai/sessions/python.md) — `session_id` continuation
* [**Chat-style messages**](https://dnotifier.gitbook.io/product-docs/ai/chat-style-messages/python.md) — `messages` array
