How to accept file uploads for LLM in FastAPI
Quick answer
Use FastAPI's
UploadFile and File to accept file uploads in an endpoint, then read the file content asynchronously or synchronously. Pass the extracted text to an LLM like gpt-4o via the OpenAI SDK for processing.PREREQUISITES
Python 3.8+pip install fastapi uvicorn openai>=1.0OpenAI API key set in environment variable OPENAI_API_KEY
Setup
Install fastapi for the web framework, uvicorn as the ASGI server, and openai SDK for LLM calls. Set your OpenAI API key in the environment variable OPENAI_API_KEY.
pip install fastapi uvicorn openai>=1.0 Step by step
Create a FastAPI app with a POST endpoint that accepts file uploads using UploadFile. Read the file content as text, then send it as a prompt to the gpt-4o model using the OpenAI SDK. Return the LLM's response as JSON.
import os
from fastapi import FastAPI, UploadFile, File
from openai import OpenAI
app = FastAPI()
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
@app.post("/upload-file")
async def upload_file(file: UploadFile = File(...)):
# Read file content as text
content = await file.read()
text = content.decode("utf-8")
# Call OpenAI gpt-4o model with file content as prompt
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": text}]
)
answer = response.choices[0].message.content
return {"filename": file.filename, "llm_response": answer}
# To run: uvicorn filename:app --reload output
{
"filename": "example.txt",
"llm_response": "Here is the summary of your file content..."
} Common variations
- Use
asyncendpoints to handle file uploads efficiently. - Support multiple files by accepting
List[UploadFile]. - Use other LLMs like
claude-3-5-sonnet-20241022with Anthropic SDK similarly. - Stream responses for large outputs using FastAPI's
StreamingResponse.
from typing import List
from fastapi import UploadFile, File
@app.post("/upload-multiple-files")
async def upload_multiple_files(files: List[UploadFile] = File(...)):
results = []
for file in files:
content = await file.read()
text = content.decode("utf-8")
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": text}]
)
results.append({"filename": file.filename, "llm_response": response.choices[0].message.content})
return results output
[
{"filename": "file1.txt", "llm_response": "Response 1..."},
{"filename": "file2.txt", "llm_response": "Response 2..."}
] Troubleshooting
- If you get
UnicodeDecodeError, ensure the uploaded file is UTF-8 encoded or handle decoding errors. - For large files, consider chunking the content before sending to the LLM to avoid token limits.
- If the API key is missing or invalid, FastAPI will raise authentication errors; verify
OPENAI_API_KEYis set.
Key Takeaways
- Use FastAPI's UploadFile and File to accept and read uploaded files asynchronously.
- Send file text content as prompt to LLMs like gpt-4o using the OpenAI SDK for processing.
- Handle encoding and file size carefully to avoid errors and token limits.
- Support multiple files by accepting lists of UploadFile in your endpoint.
- Set your API key securely in environment variables and never hardcode it.