Code structure
Low-level design
Module map
| Path | Responsibility |
|---|---|
api/routes | Maps HTTP requests to use cases. |
api/dependencies.py | Creates request dependencies. |
services/job_service.py | Runs job use cases. |
services/contracts.py | Defines the extraction provider port. |
services/llama_cloud.py | Implements the LlamaCloud adapter. |
repositories/jobs.py | Reads and writes job rows. |
models/extraction_job.py | Defines job state and table columns. |
static | Contains the browser form and polling code. |
Job state
PENDING → PROCESSING → COMPLETED
↘ FAILED
↘ CANCELLEDA final state is terminal. The service does not check LlamaCloud again after a final state.
Design patterns
Layered architecture
Each layer has one responsibility. Routes handle HTTP. Services handle use cases. Repositories handle persistence.
Ports and adapters
ExtractionProvider is a port. LlamaCloudExtractionProvider is an adapter. The job service depends on the port. The job service does not depend on the LlamaCloud SDK.
Repository pattern
JobRepository contains database operations. The service does not contain SQL queries.
Dependency injection
FastAPI creates the database session and service for each request. Tests can replace these dependencies.
Anti-corruption layer
The provider adapter changes external result fields and states into local values. Provider details do not enter the public API.
State machine
The status enum limits valid job states. Terminal jobs do not run more provider status checks.
Data model
| Column | Use |
|---|---|
id | Public local job ID. |
filename | Original file name. |
llama_file_id | Internal external file ID. |
llama_job_id | Internal external job ID. |
result_json | Extracted data. |
metadata_json | Confidence and source data. |
error_message | Safe error message. |