# Add PDF Upload to a Streamlit File Q&A App

Yes. An agent reads your Streamlit uploader, adds PDF text extraction with pypdf, pins the dependency, and writes an AppTest proving a PDF question is answered.

[Run this on Rush](https://cloud.getrush.ai/run/add-pdf-support/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 page that takes the upload before touching it. In an app like streamlit/llm-examples that is `pages/1_File_Q&A.py`: a sidebar `st.text_input` for an API key, `st.file_uploader("Upload an article", type=("txt", "md"))`, a question input disabled until a file is present, and then `uploaded_file.read().decode()` to turn the upload into the article text that goes into the prompt. The `.decode()` call is the reason a PDF cannot simply be allowed in `type`: PDF bytes are not UTF-8 text, so the agent has to add extraction, not just a file extension.

It reads the rest of the repository to match how it is run and tested. `requirements.txt` pins `streamlit>=1.28`, `anthropic>=0.3.0` and `openai>=1.2` among others and has no PDF library; `requirements-dev.txt` holds `pytest`, `black` and `mypy`; `.ruff.toml` sets `line-length = 105`; `.pre-commit-config.yaml` runs `ruff` and `ruff-format`. `app_test.py` already uses `streamlit.testing.v1.AppTest` to drive `Chatbot.py` with a patched OpenAI client, and `.github/workflows` runs `streamlit/streamlit-app-action` with `ruff: true` and `pytest`, so any new test runs in CI without a new workflow.

The agent adds `pypdf` to `requirements.txt`, adds `"pdf"` to the uploader's `type` tuple, and replaces the bare `.decode()` with a small function that checks `uploaded_file.type` or the file name: a PDF is opened with `pypdf.PdfReader` and its pages' `extract_text()` results are joined, while `txt` and `md` keep the existing decode path. When a PDF yields no text at all, as a scanned document does, it shows `st.warning` saying the file has no extractable text instead of sending an empty article to the model. The prompt string, the `anthropic.Client` call and its parameters are left exactly as they are, so the change is confined to how the article text is produced.

Then it writes a test in `app_test.py` in the same style as `test_Chatbot`: it builds a one-page PDF with `pypdf.PdfWriter` in a temporary file, patches the Anthropic client's completion call to return a fixed answer, runs `AppTest.from_file("pages/1_File_Q&A.py")`, uploads the file, sets the question and asserts the answer appears and `at.exception` is empty. It runs `ruff check`, `ruff format --check` and `pytest` in its sandbox, pushes the branch, opens the pull request and reads the App testing workflow on it, fixing anything that fails.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add PDF support to the File Q&A page of this Streamlit app. Read pages/1_File_Q&A.py, requirements.txt, app_test.py and the GitHub Actions workflow first. Allow pdf in the file uploader, extract text from PDFs with pypdf page by page, keep the existing decode path for txt and md, and show a warning when a PDF has no extractable text instead of sending an empty article. Do not change the model call or the prompt. Pin pypdf in requirements.txt. Add an AppTest in app_test.py that uploads a generated one-page PDF with the model call patched and asserts the answer is shown. Run ruff and pytest before you push. Open a pull request and make sure the workflow passes on it.",
  "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 a new branch that changes `pages/1_File_Q&A.py`, `requirements.txt` and `app_test.py`. The page accepts `txt`, `md` and `pdf`, extracts PDF text page by page and warns on an empty result; `requirements.txt` gains a pinned `pypdf`; and `app_test.py` gains a test that drives the page with a generated PDF and a patched model call. In a repository shaped like llm-examples no workflow file changes, because the existing App testing workflow already runs `pytest` and `ruff`.

The proof is that workflow running on the pull request: the new test passes alongside `test_Chatbot` and `test_Langchain_Quickstart`, and `ruff` passes on the changed file at the repository's line length. The session's trajectory keeps the `pytest` and `ruff` output from the agent's sandbox, and the pull request description says which extraction library was chosen and why the empty-text case shows a warning.

Before merging, a reviewer checks that the pinned `pypdf` version is one they are happy to carry, that the warning text matches the app's tone, and whether very large PDFs should be truncated before they reach the prompt. The model call is unchanged in this pull request, so nothing about which model answers or how it is billed moves.

## Run it on every feature request labelled for an agent

Add an automation in the Project with a `linear` trigger on issues labelled `agent` and a prompt like this one written generally, asking the agent to implement the issue in the Streamlit app, keep the model call unchanged unless the issue says otherwise, and add an AppTest for the new behaviour. The trigger adds the issue after the prompt, so the agent reads the request from the issue itself; it fills in no placeholder in `input`.

Each labelled issue starts one run with its own sandbox and its own pull request, and the pull request link lands back on the issue. An automation has its own `max_runs_per_hour` cap, so a batch of labelled issues is spread out rather than refused. To start one run by hand instead, send this request from a script with a fresh UUID as `request_id`.

## Why run it on Rush?

Adding a file type sounds like one line until the decode call fails on the first real PDF and the test has to build a fixture document. On Rush the agent does the whole loop in its own sandbox: it reads the uploader, writes the extraction path, generates a PDF with `pypdf.PdfWriter`, drives the page with `AppTest` and runs `ruff` and `pytest` before the first push, then reads the workflow on its own pull request. Your laptop never installs anything.

The result comes with evidence. The new test runs in the repository's existing workflow on the agent's pull request, and the trajectory keeps every command with its output, so a reviewer can see the test passing before they open the diff. Because each run is independent, the same request can add PDF support to several internal Streamlit tools at once, each behind a pull request a person reviews.

## Frequently asked questions

### Will it change the model call or the prompt?

No. The prompt tells it not to, so the `anthropic.Client` call and the prompt string in `pages/1_File_Q&A.py` stay exactly as they are. The change is confined to how the article text is produced from the upload. Migrating the call to a newer API is a separate job with its own page and pull request.

### What happens with a scanned PDF that has no text layer?

The page shows an `st.warning` saying the file has no extractable text and does not send a question to the model. Optical character recognition is not added by default because it needs a system dependency; ask for it in the prompt and the agent adds it as an explained, separate change with the extra dependency named in the pull request.

### Why pypdf rather than another library?

It is pure Python, so it installs on Streamlit Community Cloud and in the GitHub Actions runner without system packages, and it can both read text and write the one-page fixture the test needs. If your repository already depends on another PDF library, the agent uses that one instead and says so in the pull request.

### Does the test actually call the model?

No. It patches the completion call the same way `test_Chatbot` patches the OpenAI client, so the test asserts the page's behaviour, from upload through extraction to showing the answer, without a network call or an API key. It runs in the existing App testing workflow with `pytest`.

## More Streamlit jobs

- [Move API Keys to st.secrets in a Streamlit App](https://getrush.ai/run/add-secrets-management/streamlit/)
- [Stream Chat Responses in a Streamlit App](https://getrush.ai/run/add-streaming-responses/streamlit/)
- [Show Token Usage per Reply in a Streamlit Chat App](https://getrush.ai/run/add-token-usage-display/streamlit/)

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

- [Persist User Settings in a Tauri App](https://getrush.ai/run/add-persistent-settings/tauri/)
- [Add PHPStan to a CodeIgniter 4 Application](https://getrush.ai/run/add-phpstan/codeigniter/)
- [Post Terraform Plan Comments on Pull Requests](https://getrush.ai/run/add-plan-comments-to-pr/terraform/)

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