# Persist Chat History in a Streamlit App

Yes. An agent moves a Streamlit chatbot's messages from session_state into SQLite, adds a conversation picker, keeps AppTest green and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/persist-chat-history/streamlit/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the app as it is. In a repository shaped like streamlit/llm-examples, `Chatbot.py` is the entry page: it asks for an OpenAI key in the sidebar under the `chatbot_api_key` widget key, seeds `st.session_state["messages"]` with one assistant greeting, replays the list through `st.chat_message`, and on each `st.chat_input` appends the user turn, calls `client.chat.completions.create` with the hardcoded `gpt-3.5-turbo` and appends the reply. Every message lives only in `session_state`, so a browser refresh or a Streamlit rerun that loses the session starts the conversation over. `pages/5_Chat_with_user_feedback.py` keeps the same list shape and adds a thumbs widget keyed on `len(messages)`, and `pages/1_File_Q&A.py` has no history at all. `requirements.txt` pins `streamlit>=1.28`, `openai>=1.2` and an old LangChain, `requirements-dev.txt` adds pytest, and `app_test.py` drives `Chatbot.py` with `streamlit.testing.v1.AppTest`, patching `openai.resources.chat.Completions.create` and asserting on `at.chat_message[1]` and `at.chat_message[2]`. `.github/workflows/app-testing.yml` runs that suite with `streamlit/streamlit-app-action`, ruff on, under Python 3.11, and `.ruff.toml` sets a 105-character line length.

The change keeps the page small and puts storage in one module. The agent adds `history.py` with a `ChatStore` over `sqlite3` and the standard library only, so `requirements.txt` gains nothing: a `conversations` table with an id, a title taken from the first user message and a `created_at`, and a `messages` table with `conversation_id`, `role`, `content` and a position, created with `CREATE TABLE IF NOT EXISTS` on first use. The database path comes from `st.secrets["CHAT_DB_PATH"]` when set and defaults to a file beside the app, and the connection is opened through `st.cache_resource` with `check_same_thread=False` so one handle serves reruns. `Chatbot.py` loads the current conversation's rows into `session_state["messages"]` when the id changes, writes each user and assistant turn through `store.append` right where the list is appended today, and gains a sidebar `st.selectbox` of saved conversations plus a "New conversation" button that clears the list and the id. The greeting is still shown but not stored, so a saved conversation replays exactly what was said.

The tests are extended rather than rewritten. `test_Chatbot` keeps its patched completion and its assertions, pointing `CHAT_DB_PATH` at a temporary file through `AppTest.secrets`, and new cases check that a second `AppTest.from_file("Chatbot.py").run()` against the same file shows the earlier turns, that "New conversation" starts an empty list without deleting the old rows, and that the selectbox lists the first user message as the title. `history.py` gets its own small tests for ordering and for creating the tables twice. The agent runs `ruff check .`, `ruff format --check .` and `pytest -v` until all pass, matching what the workflow runs.

It applies the same change to `pages/5_Chat_with_user_feedback.py` only if the prompt asks, because that page's feedback key depends on the message count; by default it leaves the page alone and says so in the pull request. Then it pushes a branch and opens the pull request with the schema, the secret name and the test list in the description.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Persist chat history in this Streamlit app. Add a history.py module with a SQLite store using only the standard library, with conversations and messages tables created on first use and the path read from st.secrets CHAT_DB_PATH with a default beside the app. Make Chatbot.py save each user and assistant turn, reload a conversation on rerun, and add a sidebar conversation picker and a new-conversation button. Keep the existing AppTest in app_test.py passing, add cases that prove a rerun shows earlier turns, run ruff and pytest as the workflow does, leave the other pages unchanged, and open a pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request on its own branch. The diff adds `history.py` and its tests, changes `Chatbot.py` for the store calls and the sidebar controls, extends `app_test.py`, and adds the database file pattern to `.gitignore`. `requirements.txt` is unchanged because the store uses `sqlite3` from the standard library. The description shows the two-table schema, names the `CHAT_DB_PATH` secret and explains why the feedback page was left as it was.

The checks are the proof. The repository's `App testing` workflow runs ruff and the pytest suite on the pull request, and the session's trajectory at `GET /o/{org}/sessions/{id}/messages` keeps each `pytest -v` and `ruff` invocation with its output, so a reviewer sees the rerun case pass rather than reading that it does.

Before merging, a reviewer checks that the greeting is displayed but never stored, that the picker's titles come from the first user message and not from the model reply, and that the database path on Streamlit Community Cloud is set through secrets, since that filesystem does not survive a redeploy and the pull request says so. They also confirm the `cache_resource` connection is not shared across users in a way that leaks a conversation, which the per-conversation id in `session_state` prevents.

## Run it on every new chat page added

A team that adds LLM pages over time sends `POST /o/{org}/sessions` with this prompt each time a page needs memory, naming the page file in the prompt and the repository in `resources`. The first run adds `history.py`; later runs reuse it and wire one more page, so each pull request is one page's worth of change.

Give the API key a `webhook_url` and Rush POSTs `run.completed` when the turn ends, with `session_url` to read the pull request from. A `request_id` that is a UUID v5 of the page filename makes a retried request return the run already started rather than opening a second pull request.

## Why run it on Rush?

Adding persistence to a Streamlit app is easy to get subtly wrong: reruns happen on every widget change, a module-level connection breaks under threads, and the `AppTest` harness has its own rules about secrets and reruns. On Rush the run works in its own cloud sandbox, installs the pinned requirements, runs the real `AppTest` suite against a temporary database and iterates until it passes, while your local checkout and your own SQLite file stay as they were.

The result is checkable. The pull request shows the schema and every line that touches the store, the repository's workflow runs ruff and pytest on it, and the trajectory keeps the test output, so review is about whether the conversation picker belongs in the sidebar, not about whether the tests ran.

## Frequently asked questions

### Why SQLite and not a hosted database?

Because the app has no database today and `requirements.txt` should not grow for a feature this small. `sqlite3` ships with Python, the store is one file, and the path is read from `st.secrets`, so swapping in another backend later is a change to `history.py` alone. The pull request names that boundary.

### Does it work on Streamlit Community Cloud?

It runs there, but the filesystem does not survive a redeploy, so a SQLite file is reset when the app redeploys. The pull request description says this and suggests pointing `CHAT_DB_PATH` at a mounted volume on your own host, or replacing the store's backend, for history that must outlive deploys.

### Will my existing AppTest keep passing?

Yes. `test_Chatbot` keeps its patched `Completions.create` and its assertions on the chat messages; the only addition is a temporary `CHAT_DB_PATH` secret so the test never writes beside the app. The new cases sit beside it in `app_test.py` and run in the same workflow.

### Is the OpenAI key stored with the history?

No. Only `role` and `content` for user and assistant turns are written, and the key stays in the `chatbot_api_key` sidebar widget exactly as today. The greeting message is shown on an empty conversation but never saved.

### What about the feedback page?

It is left unchanged by default, because `streamlit_feedback` is keyed on the message count and a reloaded history would change which widget a thumbs click belongs to. Ask for it in the prompt, or in a follow-up on the same session, and the agent wires it with a key based on the stored message id instead.

## More Streamlit jobs

- [Pin the Python Dependencies of a Streamlit App](https://getrush.ai/run/pin-python-dependencies/streamlit/)
- [Review Every Pull Request to a Streamlit App](https://getrush.ai/run/review-pull-requests/streamlit/)
- [Upgrade LangChain in a Streamlit App](https://getrush.ai/run/upgrade-langchain/streamlit/)

## More jobs like this: Hand off a long task

- [Make a Chrome Extension Popup Remember Its State](https://getrush.ai/run/persist-popup-state/chrome-extension/)
- [Pin GitHub Actions to Commit SHAs in Your Workflows](https://getrush.ai/run/pin-action-versions/github-actions/)
- [Protect a DynamoDB Global Table and Its KMS Keys from Deletion in an AWS CDK App](https://getrush.ai/run/protect-stateful-resources/aws-cdk/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
