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

# Python

Continue a multi-turn AI conversation by passing **`session_id`** from the first response into follow-up `send_ai` calls.

## Complete multi-turn example

```python
import asyncio
from dnotifier import DNotifier

USER_ID = "user-123"

def session_id_from_response(response) -> str | None:
    if not isinstance(response, dict):
        return None
    metadata = response.get("metadata")
    if isinstance(metadata, dict):
        packet = metadata.get("packet")
        if isinstance(packet, dict) and packet.get("id") is not None:
            return str(packet["id"])
        if metadata.get("id") is not None:
            return str(metadata["id"])
    return None

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

    await notifier.connect()

    # Turn 1 — new session
    first = await notifier.send_ai(
        sender_id=USER_ID,
        message={"text": "I need help with billing for order #4821."},
    )

    session_id = session_id_from_response(first)
    print(f"Session: {session_id}")

    # Turn 2 — same session
    second = await notifier.send_ai(
        sender_id=USER_ID,
        session_id=session_id,
        message={"text": "What documents do I need to submit?"},
    )

    print(f"Follow-up: {second}")

asyncio.run(main())
```

## Persist session per ticket

```python
class TicketStore:
    def __init__(self):
        self._sessions: dict[str, str] = {}

    def session_id_for_ticket(self, ticket_id: str) -> str | None:
        return self._sessions.get(ticket_id)

    def save_session(self, ticket_id: str, session_id: str):
        self._sessions[ticket_id] = session_id

async def handle_user_message(notifier, store: TicketStore, ticket_id: str, text: str):
    session_id = store.session_id_for_ticket(ticket_id)

    response = await notifier.send_ai(
        sender_id=USER_ID,
        session_id=session_id,
        message={"text": text},
    )

    if session_id is None:
        new_session = session_id_from_response(response)
        if new_session:
            store.save_session(ticket_id, new_session)
```

## Chat-style in a session

```python
first = await notifier.send_ai(
    sender_id=USER_ID,
    message={
        "messages": [
            {"role": "system", "content": "You are a billing specialist."},
            {"role": "user", "content": "I was charged twice."},
        ],
    },
)

session_id = session_id_from_response(first)

await notifier.send_ai(
    sender_id=USER_ID,
    session_id=session_id,
    message={
        "messages": [
            {"role": "user", "content": "Here is my receipt number: R-9912."},
        ],
    },
)
```

## Learn more

* [**AI history**](https://dnotifier.gitbook.io/product-docs/ai/ai-history/python.md)
* [**Session logging**](https://dnotifier.gitbook.io/product-docs/ai/session-logging/python.md)
