← Back to timeline

Team project · Imperial College London

Clinical Multi-agent Text-to-SQL

Oct 2025 – Dec 2025

Answered 17 of 20 questions correctly in the team’s project evaluation.

Role: Planner-agent design, LangGraph integration and failure-case evaluation.

  • Multi-agent
  • LangGraph
  • Text-to-SQL
  • MIMIC-IV

Problem

Translate clinical questions into schema-grounded SQL over MIMIC-IV, with validation, repair and safe abstention.

System representation

Agent State Graph / Runtime Loop

Ask a question without having to write a database query

Hospital records are spread across tables. The team's assistant turns a question into SQL—the instructions used to retrieve records. My work focused on stepwise planning, LangGraph integration and failure-case testing.

From a question to a checked database queryIntent analysis routes answerable requests to the generator. Other requests stop. Inside the generator, SQL is checked and may be retried once. Unresolved failures return no SQL. A separate evaluator compares query results in SQLite.A question in everyday languageUnderstand the requestintent_analyzerAnswerableOtherwiseStopNo SQL returnedgeneratorWrite the database queryGemini 2.5 Pro · SQLCheck basic syntaxRepair · at most one retryStill invalidReturn SQLSEPARATE EVALUATION · SQLiteCompare with reference-query results
Example and implementation details
QUESTION FROM THE PROJECT PRESENTATION
How many patients were admitted in 2018?
Find: admissionsTime: 2018Return: a count

1 · Interpret the request

The intent step identifies the kind of question. It can stop with no query when the request is not answerable from the database.

ANSWERABLE

2 · Generate SQL using the table definitions

Gemini 2.5 Pro receives the question, its intent and the available schema: the names and structure of the tables.

3 · Check, fix, and try again when needed

Basic syntax checks catch malformed queries. The generator can retry after an error; unresolved failures return no SQL.

↺ Repair happens inside the generator

NOT ANSWERABLE

Stop without inventing an answer

A request for treatment advice is different from a request to retrieve a recorded fact. The system can return null instead of SQL.

TEAM EVALUATION

Does the query retrieve the right result?

The evaluator executes generated and reference SQL in SQLite and compares the returned results. Passing syntax checks alone does not establish that the question was answered correctly.

Implementation boundary: agents, retries and validation

The supplied notebook has two LangGraph nodes: intent_analyzer and generator. The generator makes at most two attempts in total. Checks cover basic SELECT structure, parentheses and incomplete clauses; table-name checks provide warnings rather than blocking output. SQL execution is part of the separate diagnostic evaluator. These checks do not certify clinical correctness.

Technical walkthrough

Nodes

A clinical question enters intent analysis, then SQL generation when answerable. The generator performs basic syntax checks and repair; execution and result comparison happen in the evaluator.

Shared state

LangGraph coordinates shared state, nodes and transitions.

Branches and repair loop

Answerable intents route to the generator; unanswerable intents end the graph. Syntax repair and retries take place inside the generator before output is returned.

Safe abstention

The system abstains when it cannot produce a safe answer.

Core Ideas / Concept Primer

The ideas behind the implementation

SQL and Text-to-SQL

SQL expresses a query over relational tables. Text-to-SQL translates a natural-language question into that query; a syntactically valid query can still answer the wrong question.

Clinical questions need a plan

The planner decomposes a question into cohort, time window, admissions and aggregation. Making these choices explicit helps separate interpretation of the question from query construction over MIMIC-IV.

State, nodes and edges

State carries information through a run. The supplied implementation has intent-analysis and generation nodes, with a conditional route to generate or end. The repair loop is internal to generation.

Validation is a decision point

Basic syntax checks can trigger another generation attempt. Table-name checks are advisory, while execution is tested separately. Neither a parseable query nor a matching response format guarantees the correct answer.

Implementation details

Clinical question

Natural-language request

Planner agent

Intent analysis and stepwise decomposition

SQL generation

Schema-grounded query construction

Validate and repair

Syntax, schema and execution checks with repair

Execute or abstain

Execute valid queries; abstain when unresolved

Results and Demonstrations

Questions answered correctly in team evaluation

17 / 20 (85%)
Evaluation details
Sample size
20 questions

Ownership

Designed and implemented

Designed stepwise planner logic; integrated LangGraph state, nodes and transitions.

Evaluated

Performed failure-case testing within the team evaluation, which answered 17 of 20 questions correctly.

Team context

Imperial College London team project using MIMIC-IV.