# Add File Uploads to a Reflex Chat App

Yes. An agent adds an attach button to a Reflex chat app, stores the file per chat, puts its text in front of the model, tests it in a browser and opens a PR.

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

## What the agent does

The agent reads how a question travels through the app before it adds a file to it. In an app like reflex-dev/reflex-chat, `chat/components/chat.py` builds `action_bar()` as an `rx.form` with one `rx.input` whose `id` is `question`, a `Send` button that is `loading` and `disabled` while `State.processing`, `reset_on_submit=True` and `on_submit=State.process_question`. `chat/state.py` keeps `_chats`, a dict from chat name to a list of `QA` entries (`question` and `answer`), with `current_chat`, `processing` and `is_modal_open` beside it; `process_question` reads `form_data["question"]` and hands it to `openai_process_question`, which appends a `QA`, sets `processing`, builds the `messages` list from the chat's history with a system prompt, drops the empty answer at the end, calls `OpenAI().chat.completions.create` with `stream=True` and the model from `OPENAI_MODEL`, and yields after each delta. `chat/components/navbar.py` renders the chat list in a drawer and `delete_chat` removes an entry from `_chats`. `rxconfig.py` names the app `chat`, and `requirements.txt` lists `reflex` and `openai` only.

The upload goes beside the question, not through the model. The agent adds an `rx.upload` to the action bar with `id="attachment"`, `max_files=1`, an `accept` map limited to `.txt`, `.md` and `.pdf`, and a size cap, rendered as a small button next to the input with the selected file name shown from `rx.selected_files("attachment")`. A new `handle_upload` event handler takes `files: list[rx.UploadFile]`, writes each to `rx.get_upload_dir()` under the chat's name, and extracts text: read directly for `.txt` and `.md`, and through `pypdf` for `.pdf`, which the agent adds to `requirements.txt` as the one new dependency. The extracted text and the file name go into a new `_attachments` dict keyed like `_chats`, so `delete_chat` clears them with the chat, and a `clear_attachment` handler calls `rx.clear_selected_files` and removes the file.

In `openai_process_question`, the attachment becomes part of the context the model already gets: when the current chat has one, a second system message is inserted after the existing prompt with the file name and its text, trimmed to a character budget the agent sets from the model's context and reports in the pull request. The `QA` entry for the question records the file name so `message()` in `chat.py` can show a small label under the question, and the type hints keep `pyright`, which `.github/workflows/ci.yml` runs on every push, clean. Nothing else in the streaming loop changes.

Then it runs it. The agent installs the requirements with `uv` as the workflow does, runs `pyright .` and `ruff check`, and starts `reflex run` with `OPENAI_API_KEY` set to a dummy value and `OPENAI_BASE_URL` pointed at a small local server it writes for the test, which answers a streamed completion echoing the first lines of the system messages it receives. In its own browser it creates a chat, attaches a short `.md` file, sends a question, and checks that the file name appears under the question and that the stub's answer contains the file's text, which proves the attachment reached the request. It deletes the chat and confirms the upload directory entry is gone, then runs `reflex export` as `check-export` does.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add file uploads to this Reflex chat app so a user can attach a text, Markdown or PDF file to a chat and have its contents given to the model. Read rxconfig.py, requirements.txt, chat/chat.py, chat/state.py, chat/components/chat.py, chat/components/navbar.py and .github/workflows/ci.yml first. Add an rx.upload with max_files=1, an accept map for .txt, .md and .pdf and a size cap to the action bar, an upload handler that saves the file under rx.get_upload_dir() per chat and extracts its text with pypdf for PDFs, an attachments dict in State keyed like the chats and cleared by delete_chat, and a second system message carrying the file name and text when the chat has an attachment, trimmed to a budget you state. Show the file name under the question. Install the requirements with uv, run pyright and ruff, start the app with a dummy OPENAI_API_KEY and OPENAI_BASE_URL pointed at a local stub that echoes the system messages, attach a file in your browser and confirm its text reaches the request, run reflex export, and open a pull request with what you checked.",
  "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

A pull request that changes `chat/state.py` (the attachments dict, two handlers and the extra system message), `chat/components/chat.py` (the upload control and the label under a question), and `requirements.txt` (one line for `pypdf`), with the upload directory added to `.gitignore`. The description states the size cap and the text budget, shows the `pyright`, `ruff` and `reflex export` results, and describes the browser check with the stub server and what the stub received.

A reviewer checks the accepted types and the cap against what their users send, decides whether the budget should be a setting, and merges. The app then takes a file per chat without any change to how questions and answers are stored or streamed.

## Run it on every terminal hand-off

A developer who has just been asked for attachments in the demo starts this from the terminal with this body and the repository in `resources`, and goes back to their own work. The run reads the real state class and the real action bar, so the control lands where the form already is and the text joins the messages the app already builds.

Send a `request_id`, a UUID, with the body so a retried start returns the run it already began. When the turn ends, `GET /o/{org}/sessions/{id}` names the pull request, and a follow-up to the same session can ask for images through the model's vision input instead of text extraction, or for several files per chat.

## Why run it on Rush?

The change touches state, a component and the request to the model, and the only honest check is a browser with a file in it and a look at what the model was sent. The agent does that in its own sandbox with a stub standing in for the API, so no real key is spent, and keeps every command, the stub's log and the pages it opened in the run's record, so the pull request shows the file's text in the request rather than asserting it got there.

The same run fits any Reflex app with a form: a support tool that wants a screenshot attached to a ticket, an internal search that takes a CSV, or an app whose state lives in a database through `rx.Model` where the attachment belongs in a table rather than a dict. Each gets an upload wired to its own handlers and checked in its own UI.

## Frequently asked questions

### Where are uploaded files stored?

In the directory `rx.get_upload_dir()` returns, under a folder per chat, on the server running the app. The run adds that directory to `.gitignore` and removes a chat's files when the chat is deleted; a deployment with several app instances needs shared storage, which the pull request notes.

### Will a large PDF blow the model's context?

The text is trimmed to a budget before it goes into the system message, and the budget is stated in the pull request and easy to change. A file past the upload size cap is refused by the control before it reaches the server.

### How is it tested without an OpenAI key?

The `openai` client reads `OPENAI_BASE_URL`, so the run points it at a local stub that streams back whatever system messages it received. That proves the attachment reaches the request without spending a token; the real model is used only once the change is deployed with the team's key.

### Does this change how answers stream?

No. The streaming loop in `openai_process_question` is untouched; only the `messages` list it sends gains one entry when the chat has an attachment, and the `QA` entry gains the file name for display.

## More Reflex jobs

- [Add Pre-commit Hooks to a Reflex App](https://getrush.ai/run/add-pre-commit-hooks/reflex/)
- [Add a SQLite Database to a Reflex App](https://getrush.ai/run/add-sqlite-database/reflex/)
- [Write Integration Tests for a Reflex App](https://getrush.ai/run/write-integration-tests/reflex/)

## The same job on other stacks

- [Add File Uploads to a Vapor API](https://getrush.ai/run/add-file-uploads/vapor/)
- [Add File Uploads to an AdonisJS App](https://getrush.ai/run/add-file-uploads/adonisjs/)
- [Add File Uploads to a CodeIgniter 4 App](https://getrush.ai/run/add-file-uploads/codeigniter/)

## 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/)
