# atul2501/lt_foods_pr architecture

> Architecture diagram of the GitHub repository atul2501/lt_foods_pr, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/atul2501/lt_foods_pr
- Repository: https://github.com/atul2501/lt_foods_pr

## Overview

LT Foods Invoice Extraction API accepts PDFs from SAP uploads and email, extracts and verifies invoice data, then returns SAP ZFTVIA contract JSON. Its key reliability boundary is deterministic grounding and business-rule validation after LLM structuring. The README documents both the upload/poll API and email polling, plus optional SAP RFC push. Relationships within the pipeline are supported by the run-module reference index and README workflow; unsampled implementation details are not inferred beyond those references.

Main entry points: [`app/pipeline/run.py`](https://github.com/atul2501/lt_foods_pr/blob/main/app/pipeline/run.py) (Pipeline runner), [`app/email_ingest/ingest.py`](https://github.com/atul2501/lt_foods_pr/blob/main/app/email_ingest/ingest.py) (Email ingestion), [`app/api/routers/invoices.py`](https://github.com/atul2501/lt_foods_pr/blob/main/app/api/routers/invoices.py) (Invoice API), [`app/pipeline/llm_structurer.py`](https://github.com/atul2501/lt_foods_pr/blob/main/app/pipeline/llm_structurer.py) (LLM structuring)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_intake["PDF intake"]
  node_api_routes["Invoice API<br/>[invoices.py]"]
  node_email_runner["Email poller<br/>[runner.py]"]
  node_email_ingest["Email ingestion<br/>[ingest.py]"]
end

subgraph group_extract["Extraction and validation"]
  node_pipeline["Pipeline runner<br/>[run.py]"]
  node_triage["PDF triage<br/>[triage.py]"]
  node_text_extract["Text extraction<br/>[text_extract.py]"]
  node_ocr["Scanned-page OCR<br/>[ocr.py]"]
  node_normalize["Text normalization<br/>[normalize.py]"]
  node_llm_structurer["LLM structuring<br/>[llm_structurer.py]"]
  node_invoice_schema["Extraction schema<br/>[invoice_schema.py]"]
  node_grounding["Field grounding<br/>[grounding.py]"]
  node_business_rules["Business rules<br/>[business_rules.py]"]
  node_status["Result status<br/>[status.py]"]
end

subgraph group_delivery["API and results"]
  node_response_mapper["Contract mapping<br/>[to_response.py]"]
  node_result_schema["API result schema<br/>[envelope.py]"]
  node_result_store[("File result store<br/>[results_store.py]")]
end

subgraph group_integrations["External integrations"]
  node_imap["IMAP mailbox"]
  node_ollama_client["Ollama client<br/>[ollama_client.py]"]
  node_ollama["Ollama model"]
  node_sap_push["SAP RFC push<br/>[push.py]"]
  node_sap_rfc["SAP RFC endpoint"]
end

node_sap_user(("SAP/API caller"))
node_email_sender(("Invoice sender"))

node_sap_user -->|"uploads PDF"| node_api_routes
node_email_sender -->|"sends invoice"| node_imap
node_email_runner -->|"runs polling"| node_email_ingest
node_email_ingest -->|"fetches messages"| node_imap
node_api_routes -->|"starts extraction"| node_pipeline
node_email_ingest -->|"extracts PDFs"| node_pipeline
node_pipeline -->|"classifies pages"| node_triage
node_pipeline -->|"extracts digital text"| node_text_extract
node_pipeline -->|"OCRs scanned pages"| node_ocr
node_pipeline -->|"normalizes text"| node_normalize
node_pipeline -->|"structures text"| node_llm_structurer
node_llm_structurer -->|"requests generation"| node_ollama_client
node_ollama_client -->|"sends prompt"| node_ollama
node_llm_structurer -->|"validates output"| node_invoice_schema
node_pipeline -->|"verifies fields"| node_grounding
node_pipeline -->|"checks invoice"| node_business_rules
node_pipeline -->|"assigns status"| node_status
node_api_routes -->|"maps results"| node_response_mapper
node_email_ingest -->|"maps results"| node_response_mapper
node_response_mapper -->|"builds contract"| node_result_schema
node_api_routes -->|"reads and claims"| node_result_store
node_email_ingest -->|"writes pending results"| node_result_store
node_result_store -->|"returns contract JSON"| node_sap_user
node_email_ingest -.->|"optionally pushes PDF"| node_sap_push
node_sap_push -.->|"calls RFC"| node_sap_rfc

click node_api_routes "https://github.com/atul2501/lt_foods_pr/blob/main/app/api/routers/invoices.py"
click node_email_runner "https://github.com/atul2501/lt_foods_pr/blob/main/app/email_ingest/runner.py"
click node_email_ingest "https://github.com/atul2501/lt_foods_pr/blob/main/app/email_ingest/ingest.py"
click node_pipeline "https://github.com/atul2501/lt_foods_pr/blob/main/app/pipeline/run.py"
click node_triage "https://github.com/atul2501/lt_foods_pr/blob/main/app/pipeline/triage.py"
click node_text_extract "https://github.com/atul2501/lt_foods_pr/blob/main/app/pipeline/text_extract.py"
click node_ocr "https://github.com/atul2501/lt_foods_pr/blob/main/app/pipeline/ocr.py"
click node_normalize "https://github.com/atul2501/lt_foods_pr/blob/main/app/pipeline/normalize.py"
click node_llm_structurer "https://github.com/atul2501/lt_foods_pr/blob/main/app/pipeline/llm_structurer.py"
click node_invoice_schema "https://github.com/atul2501/lt_foods_pr/blob/main/app/schemas/invoice_schema.py"
click node_ollama_client "https://github.com/atul2501/lt_foods_pr/blob/main/app/services/ollama_client.py"
click node_grounding "https://github.com/atul2501/lt_foods_pr/blob/main/app/pipeline/grounding.py"
click node_business_rules "https://github.com/atul2501/lt_foods_pr/blob/main/app/pipeline/business_rules.py"
click node_status "https://github.com/atul2501/lt_foods_pr/blob/main/app/pipeline/status.py"
click node_response_mapper "https://github.com/atul2501/lt_foods_pr/blob/main/app/pipeline/to_response.py"
click node_result_schema "https://github.com/atul2501/lt_foods_pr/blob/main/app/schemas/envelope.py"
click node_result_store "https://github.com/atul2501/lt_foods_pr/blob/main/app/storage/results_store.py"
click node_sap_push "https://github.com/atul2501/lt_foods_pr/blob/main/app/sap/push.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api_routes,node_email_runner,node_email_ingest toneBlue
class node_pipeline,node_triage,node_text_extract,node_ocr,node_normalize,node_llm_structurer,node_invoice_schema,node_grounding,node_business_rules,node_status toneAmber
class node_response_mapper,node_result_schema,node_result_store,node_sap_user toneMint
class node_imap,node_ollama_client,node_ollama,node_sap_push,node_sap_rfc toneRose
class node_email_sender toneIndigo
```

## Components

### PDF intake

- [Invoice API](https://github.com/atul2501/lt_foods_pr/blob/main/app/api/routers/invoices.py): `app/api/routers/invoices.py`
- [Email poller](https://github.com/atul2501/lt_foods_pr/blob/main/app/email_ingest/runner.py): `app/email_ingest/runner.py`
- [Email ingestion](https://github.com/atul2501/lt_foods_pr/blob/main/app/email_ingest/ingest.py): `app/email_ingest/ingest.py`

### Extraction and validation

- [Pipeline runner](https://github.com/atul2501/lt_foods_pr/blob/main/app/pipeline/run.py): `app/pipeline/run.py`
- [PDF triage](https://github.com/atul2501/lt_foods_pr/blob/main/app/pipeline/triage.py): `app/pipeline/triage.py`
- [Text extraction](https://github.com/atul2501/lt_foods_pr/blob/main/app/pipeline/text_extract.py): `app/pipeline/text_extract.py`
- [Scanned-page OCR](https://github.com/atul2501/lt_foods_pr/blob/main/app/pipeline/ocr.py): `app/pipeline/ocr.py`
- [Text normalization](https://github.com/atul2501/lt_foods_pr/blob/main/app/pipeline/normalize.py): `app/pipeline/normalize.py`
- [LLM structuring](https://github.com/atul2501/lt_foods_pr/blob/main/app/pipeline/llm_structurer.py): `app/pipeline/llm_structurer.py`
- [Extraction schema](https://github.com/atul2501/lt_foods_pr/blob/main/app/schemas/invoice_schema.py): `app/schemas/invoice_schema.py`
- [Field grounding](https://github.com/atul2501/lt_foods_pr/blob/main/app/pipeline/grounding.py): `app/pipeline/grounding.py`
- [Business rules](https://github.com/atul2501/lt_foods_pr/blob/main/app/pipeline/business_rules.py): `app/pipeline/business_rules.py`
- [Result status](https://github.com/atul2501/lt_foods_pr/blob/main/app/pipeline/status.py): `app/pipeline/status.py`

### API and results

- [Contract mapping](https://github.com/atul2501/lt_foods_pr/blob/main/app/pipeline/to_response.py): `app/pipeline/to_response.py`
- [API result schema](https://github.com/atul2501/lt_foods_pr/blob/main/app/schemas/envelope.py): `app/schemas/envelope.py`
- [File result store](https://github.com/atul2501/lt_foods_pr/blob/main/app/storage/results_store.py): `app/storage/results_store.py`

### External integrations

- **IMAP mailbox**
- [Ollama client](https://github.com/atul2501/lt_foods_pr/blob/main/app/services/ollama_client.py): `app/services/ollama_client.py`
- **Ollama model**
- [SAP RFC push](https://github.com/atul2501/lt_foods_pr/blob/main/app/sap/push.py): `app/sap/push.py`
- **SAP RFC endpoint**

### Other components

- **SAP/API caller**
- **Invoice sender**

## Connections

- SAP/API caller → Invoice API: uploads PDF
- Invoice sender → IMAP mailbox: sends invoice
- Email poller → Email ingestion: runs polling (evidence: `app/email_ingest/runner.py`)
- Email ingestion → IMAP mailbox: fetches messages
- Invoice API → Pipeline runner: starts extraction (evidence: `app/api/routers/invoices.py`)
- Email ingestion → Pipeline runner: extracts PDFs (evidence: `app/email_ingest/ingest.py`)
- Pipeline runner → PDF triage: classifies pages (evidence: `app/pipeline/run.py`)
- Pipeline runner → Text extraction: extracts digital text (evidence: `app/pipeline/run.py`)
- Pipeline runner → Scanned-page OCR: OCRs scanned pages (evidence: `app/pipeline/run.py`)
- Pipeline runner → Text normalization: normalizes text (evidence: `app/pipeline/run.py`)
- Pipeline runner → LLM structuring: structures text (evidence: `app/pipeline/run.py`)
- LLM structuring → Ollama client: requests generation (evidence: `app/pipeline/llm_structurer.py`)
- Ollama client → Ollama model: sends prompt
- LLM structuring → Extraction schema: validates output (evidence: `app/pipeline/llm_structurer.py`)
- Pipeline runner → Field grounding: verifies fields (evidence: `app/pipeline/run.py`)
- Pipeline runner → Business rules: checks invoice (evidence: `app/pipeline/run.py`)
- Pipeline runner → Result status: assigns status (evidence: `app/pipeline/run.py`)
- Invoice API → Contract mapping: maps results (evidence: `app/api/routers/invoices.py`)
- Email ingestion → Contract mapping: maps results (evidence: `app/email_ingest/ingest.py`)
- Contract mapping → API result schema: builds contract (evidence: `app/pipeline/to_response.py`)
- Invoice API → File result store: reads and claims (evidence: `app/api/routers/invoices.py`)
- Email ingestion → File result store: writes pending results (evidence: `app/email_ingest/ingest.py`)
- File result store → SAP/API caller: returns contract JSON
- Email ingestion → SAP RFC push: optionally pushes PDF (evidence: `app/email_ingest/ingest.py`)
- SAP RFC push → SAP RFC endpoint: calls RFC
