# yxh-y/tablerag architecture

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

- Interactive diagram: https://gitdiagram.com/yxh-y/tablerag
- Repository: https://github.com/yxh-y/tablerag

## Overview

1. **What this repo is**
   TableRAG is a two-stage system for heterogeneous document QA: an **offline database construction pipeline** and an **online iterative reasoning pipeline**. Its core idea is to combine **SQL execution over structured tables** with **text retrieval** for multi-hop answer generation.

2. **Main architectural boundary**
   The repository is split into two major subsystems:
   - `offline_data_ingestion_and_query_interface/`: builds and serves the structured data layer
   - `online_inference/`: runs the TableRAG reasoning loop against that data layer  
   This separation is important: the online agent does not ingest raw tables directly; it talks to a query service backed by MySQL.

3. **Offline ingestion pipeline**
   The offline side ingests the Excel-based table dataset from `dataset/hybridqa/dev_excel.zip`, maps values/types, and persists them into a **MySQL 8 database**.  
   Key components:
   - `data_persistent.py`: main ingestion entrypoint
   - `sql_alchemy_helper.py`: database access/ORM-style helper
   - `dtype_mapping.py`: type normalization for table fields
   - `common_utils.py`, `log_service.py`: shared helpers and logging  
   Configuration is driven by `config/database_config.json`.

4. **Persistent data store**
   MySQL is the durable storage layer for TableRAG’s structured knowledge. The dataset is transformed from spreadsheet files into relational tables so downstream reasoning can execute SQL-style operations efficiently. This is the main stateful boundary in the system.

5. **Query service layer**
   `offline_data_ingestion_and_query_interface/src/interface.py` starts the database query service, and `handle_requests.py`/`service.py` implement request handling around it.  
   This service is the bridge between the online reasoning agent and the database. The README indicates it is configured with an external model/API endpoint, so this layer likely handles request orchestration and query execution rather than direct database access from the online side.

6. **Online reasoning loop**
   `online_inference/main.py` is the top-level orchestrator for inference. It runs a **multi-iteration reasoning process** with a configurable `max_iter`, using the current question, intermediate results, and tool outputs to progressively refine the answer.

7. **Tooling used during inference**
   The online side uses a tool-based architecture:
   - `tools/retriever.py`: textual retrieval over supporting passages or evidence
   - `tools/sql_tool.py`: SQL query tool that talks to the offline query service
   - `utils/tool_utils.py`, `utils/utils.py`: tool output formatting and shared inference helpers  
   This is the core TableRAG hybrid design: retrieve text when unstructured evidence is needed, and query SQL when table operations are needed.

8. **Prompting and chat orchestration**
   `online_inference/prompt.py` and `chat_utils.py` manage prompt construction and LLM interaction. `config.py` stores the model endpoint, API key, and query-service URL.  
   This means the online subsystem depends on two external services:
   - an **LLM inference endpoint**
   - the **TableRAG SQL query service**

9. **Evaluation path**
   `online_inference/evaluation/hybrid_eval.py` evaluates outputs on the HybridQA/HeteQA-style benchmark data. The online pipeline consumes a JSON data file such as `my_dev.json`, generates answers, and then the evaluation module scores them.

10. **End-to-end data flow**
   The full architecture is:
   **Excel tables → offline ingestion → MySQL → query service → online reasoning loop → final answer**  
   In parallel, the online loop can also pull **textual evidence via retriever** and combine it with SQL results before producing the final response. This hybrid flow is the defining architectural feature of the repo.

Main entry points: [`online_inference/main.py`](https://github.com/yxh-y/tablerag/blob/main/online_inference/main.py) (Inference loop), [`offline_data_ingestion_and_query_interface/src/data_persistent.py`](https://github.com/yxh-y/tablerag/blob/main/offline_data_ingestion_and_query_interface/src/data_persistent.py) (Ingestor), [`offline_data_ingestion_and_query_interface/src/service.py`](https://github.com/yxh-y/tablerag/blob/main/offline_data_ingestion_and_query_interface/src/service.py) (Service core), [`offline_data_ingestion_and_query_interface/src/sql_alchemy_helper.py`](https://github.com/yxh-y/tablerag/blob/main/offline_data_ingestion_and_query_interface/src/sql_alchemy_helper.py) (DB helper)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_offline["Offline data layer"]
  node_excel_dataset["Excel tables<br/>dataset<br/>[dev_excel.zip]"]
  node_db_config["DB config"]
  node_data_persistent["Ingestor<br/>pipeline<br/>[data_persistent.py]"]
  node_dtype_mapping["Type mapping<br/>normalization<br/>[dtype_mapping.py]"]
  node_sql_helper[("DB helper<br/>persistence")]
  node_common_utils["Common utils<br/>helpers<br/>[common_utils.py]"]
  node_log_service["Logging<br/>observability<br/>[log_service.py]"]
  node_interface["Query service<br/>[interface.py]"]
  node_handle_requests["Request handler<br/>[handle_requests.py]"]
  node_service["Service core<br/>[service.py]"]
end

subgraph group_online["Online reasoning"]
  node_main["Inference loop<br/>orchestrator<br/>[main.py]"]
  node_prompt["Prompting<br/>[prompt.py]"]
  node_chat_utils["Chat utils<br/>llm-integration<br/>[chat_utils.py]"]
  node_retriever["Retriever<br/>retrieval<br/>[retriever.py]"]
  node_sql_tool["SQL tool<br/>[sql_tool.py]"]
  node_tool_utils["Tool utils<br/>helpers<br/>[tool_utils.py]"]
  node_utils["Inference utils<br/>helpers<br/>[utils.py]"]
  node_hybrid_eval["Hybrid eval<br/>evaluation<br/>[hybrid_eval.py]"]
end

subgraph group_external["External systems"]
  node_mysql[("MySQL 8<br/>database")]
  node_llm_api(("LLM endpoint<br/>external-api"))
end

node_excel_dataset -->|"ingest"| node_data_persistent
node_db_config -->|"configure"| node_data_persistent
node_data_persistent -->|"normalize"| node_dtype_mapping
node_data_persistent -->|"write"| node_sql_helper
node_sql_helper -->|"store"| node_mysql
node_common_utils -.->|"support"| node_data_persistent
node_log_service -.->|"log"| node_data_persistent
node_interface -->|"serve"| node_handle_requests
node_handle_requests -->|"dispatch"| node_service
node_service -->|"query db"| node_sql_helper
node_service -->|"access"| node_mysql
node_main -->|"build"| node_prompt
node_main -->|"llm call"| node_chat_utils
node_main -->|"retrieve"| node_retriever
node_main -->|"sql query"| node_sql_tool
node_sql_tool -->|"request"| node_interface
node_prompt -->|"invoke"| node_llm_api
node_chat_utils -->|"chat"| node_llm_api
node_tool_utils -.->|"format"| node_main
node_utils -.->|"support"| node_main
node_hybrid_eval -.->|"evaluate"| node_main

click node_excel_dataset "https://github.com/yxh-y/tablerag/blob/main/offline_data_ingestion_and_query_interface/dataset/hybridqa/dev_excel.zip"
click node_db_config "https://github.com/yxh-y/tablerag/blob/main/offline_data_ingestion_and_query_interface/config/database_config.json"
click node_data_persistent "https://github.com/yxh-y/tablerag/blob/main/offline_data_ingestion_and_query_interface/src/data_persistent.py"
click node_dtype_mapping "https://github.com/yxh-y/tablerag/blob/main/offline_data_ingestion_and_query_interface/src/dtype_mapping.py"
click node_sql_helper "https://github.com/yxh-y/tablerag/blob/main/offline_data_ingestion_and_query_interface/src/sql_alchemy_helper.py"
click node_common_utils "https://github.com/yxh-y/tablerag/blob/main/offline_data_ingestion_and_query_interface/src/common_utils.py"
click node_log_service "https://github.com/yxh-y/tablerag/blob/main/offline_data_ingestion_and_query_interface/src/log_service.py"
click node_interface "https://github.com/yxh-y/tablerag/blob/main/offline_data_ingestion_and_query_interface/src/interface.py"
click node_handle_requests "https://github.com/yxh-y/tablerag/blob/main/offline_data_ingestion_and_query_interface/src/handle_requests.py"
click node_service "https://github.com/yxh-y/tablerag/blob/main/offline_data_ingestion_and_query_interface/src/service.py"
click node_main "https://github.com/yxh-y/tablerag/blob/main/online_inference/main.py"
click node_prompt "https://github.com/yxh-y/tablerag/blob/main/online_inference/prompt.py"
click node_chat_utils "https://github.com/yxh-y/tablerag/blob/main/online_inference/chat_utils.py"
click node_retriever "https://github.com/yxh-y/tablerag/blob/main/online_inference/tools/retriever.py"
click node_sql_tool "https://github.com/yxh-y/tablerag/blob/main/online_inference/tools/sql_tool.py"
click node_tool_utils "https://github.com/yxh-y/tablerag/blob/main/online_inference/utils/tool_utils.py"
click node_utils "https://github.com/yxh-y/tablerag/blob/main/online_inference/utils/utils.py"
click node_hybrid_eval "https://github.com/yxh-y/tablerag/blob/main/online_inference/evaluation/hybrid_eval.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_excel_dataset,node_db_config,node_data_persistent,node_dtype_mapping,node_sql_helper,node_common_utils,node_log_service,node_interface,node_handle_requests,node_service toneBlue
class node_main,node_prompt,node_chat_utils,node_retriever,node_sql_tool,node_tool_utils,node_utils,node_hybrid_eval toneAmber
class node_mysql,node_llm_api toneMint
```

## Components

### Offline data layer

Builds and serves the structured table store.

- [Excel tables](https://github.com/yxh-y/tablerag/blob/main/offline_data_ingestion_and_query_interface/dataset/hybridqa/dev_excel.zip): dataset · HybridQA spreadsheet source data packaged as a zip archive. · `offline_data_ingestion_and_query_interface/dataset/hybridqa/dev_excel.zip`
- [DB config](https://github.com/yxh-y/tablerag/blob/main/offline_data_ingestion_and_query_interface/config/database_config.json): config · Database connection and persistence settings. · `offline_data_ingestion_and_query_interface/config/database_config.json`
- [Ingestor](https://github.com/yxh-y/tablerag/blob/main/offline_data_ingestion_and_query_interface/src/data_persistent.py): pipeline · Main offline ingestion entrypoint that loads tables into relational storage. · `offline_data_ingestion_and_query_interface/src/data_persistent.py`
- [Type mapping](https://github.com/yxh-y/tablerag/blob/main/offline_data_ingestion_and_query_interface/src/dtype_mapping.py): normalization · Normalizes table field values and data types during ingestion. · `offline_data_ingestion_and_query_interface/src/dtype_mapping.py`
- [DB helper](https://github.com/yxh-y/tablerag/blob/main/offline_data_ingestion_and_query_interface/src/sql_alchemy_helper.py): persistence · MySQL access and ORM-style helper layer. · `offline_data_ingestion_and_query_interface/src/sql_alchemy_helper.py`
- [Common utils](https://github.com/yxh-y/tablerag/blob/main/offline_data_ingestion_and_query_interface/src/common_utils.py): helpers · Shared offline-side utility functions. · `offline_data_ingestion_and_query_interface/src/common_utils.py`
- [Logging](https://github.com/yxh-y/tablerag/blob/main/offline_data_ingestion_and_query_interface/src/log_service.py): observability · Logging utilities used by the offline pipeline. · `offline_data_ingestion_and_query_interface/src/log_service.py`
- [Query service](https://github.com/yxh-y/tablerag/blob/main/offline_data_ingestion_and_query_interface/src/interface.py): service · Starts the database query service exposed to online inference. · `offline_data_ingestion_and_query_interface/src/interface.py`
- [Request handler](https://github.com/yxh-y/tablerag/blob/main/offline_data_ingestion_and_query_interface/src/handle_requests.py): service · Routes and handles incoming query-service requests. · `offline_data_ingestion_and_query_interface/src/handle_requests.py`
- [Service core](https://github.com/yxh-y/tablerag/blob/main/offline_data_ingestion_and_query_interface/src/service.py): service · Core service logic behind the query interface. · `offline_data_ingestion_and_query_interface/src/service.py`

### Online reasoning

Runs hybrid retrieval and SQL-based inference.

- [Inference loop](https://github.com/yxh-y/tablerag/blob/main/online_inference/main.py): orchestrator · Top-level multi-iteration TableRAG reasoning coordinator. · `online_inference/main.py`
- [Prompting](https://github.com/yxh-y/tablerag/blob/main/online_inference/prompt.py): prompting · Builds prompts and chat context for the reasoning loop. · `online_inference/prompt.py`
- [Chat utils](https://github.com/yxh-y/tablerag/blob/main/online_inference/chat_utils.py): llm-integration · LLM chat/request interaction helpers. · `online_inference/chat_utils.py`
- [Retriever](https://github.com/yxh-y/tablerag/blob/main/online_inference/tools/retriever.py): retrieval · Textual evidence retrieval tool. · `online_inference/tools/retriever.py`
- [SQL tool](https://github.com/yxh-y/tablerag/blob/main/online_inference/tools/sql_tool.py): tool · Issues SQL queries through the offline query service. · `online_inference/tools/sql_tool.py`
- [Tool utils](https://github.com/yxh-y/tablerag/blob/main/online_inference/utils/tool_utils.py): helpers · Formats tool outputs and supports inference-time tool usage. · `online_inference/utils/tool_utils.py`
- [Inference utils](https://github.com/yxh-y/tablerag/blob/main/online_inference/utils/utils.py): helpers · Shared inference helpers and support functions. · `online_inference/utils/utils.py`
- [Hybrid eval](https://github.com/yxh-y/tablerag/blob/main/online_inference/evaluation/hybrid_eval.py): evaluation · Benchmark evaluation for generated answers. · `online_inference/evaluation/hybrid_eval.py`

### External systems

Runtime dependencies outside the repository.

- **MySQL 8**: database · Durable relational store for structured table knowledge.
- **LLM endpoint**: external-api · External model or API endpoint used for prompting and generation.

## Connections

- Excel tables → Ingestor: ingest · Spreadsheet tables are loaded into the offline ingestion pipeline.
- DB config → Ingestor: configure · Ingestion uses the database configuration.
- Ingestor → Type mapping: normalize · Types and values are normalized before persistence.
- Ingestor → DB helper: write · The ingestion pipeline persists records through the DB helper.
- DB helper → MySQL 8: store · Structured table data is stored in MySQL.
- Common utils → Ingestor: support · Shared utilities support the offline pipeline.
- Logging → Ingestor: log · Logging services capture ingestion activity.
- Query service → Request handler: serve · The interface starts the request handling service.
- Request handler → Service core: dispatch · Requests are routed into the service core.
- Service core → DB helper: query db · The service core reaches MySQL through the DB helper.
- Service core → MySQL 8: access · Query execution ultimately reads and writes the relational store.
- Inference loop → Prompting: build · The inference loop builds prompts for each iteration.
- Inference loop → Chat utils: llm call · The loop delegates generation and chat interaction.
- Inference loop → Retriever: retrieve · Text evidence is fetched as one branch of the hybrid reasoning flow.
- Inference loop → SQL tool: sql query · Structured questions are answered through the SQL tool.
- SQL tool → Query service: request · SQL tool calls the offline query service.
- Prompting → LLM endpoint: invoke · Prompted generation is sent to the external LLM endpoint.
- Chat utils → LLM endpoint: chat · Chat utilities communicate with the external model API.
- Tool utils → Inference loop: format · Tool outputs are normalized for iterative reasoning.
- Inference utils → Inference loop: support · Shared utilities support orchestration and inference flow.
- Hybrid eval → Inference loop: evaluate · The evaluation path scores outputs from the inference loop.
