Skip to content

fix(eval): keep the trailing SQL candidate and stop cutting queries at a quoted semicolon - #135

Open
ddaihao2022 wants to merge 1 commit into
LLMSQL:mainfrom
ddaihao2022:fix-126-find-sql-quoted-semicolons
Open

ddaihao2022 wants to merge 1 commit into
LLMSQL:mainfrom
ddaihao2022:fix-126-find-sql-quoted-semicolons

Conversation

@ddaihao2022

Copy link
Copy Markdown
Contributor

Summary

find_sql() (llmsql/utils/regex_extractor.py) had two extraction bugs described in #126.

  1. The first 10 candidates won. Every occurrence of the word SELECT starts a candidate, so a model that explains itself before answering ("I need to select the right column...") filled all 10 slots with prose and its actual query was never evaluated.
  2. A ; ended the query even inside quotes. SELECT a FROM "Table" WHERE b = 'x;y'; was truncated to SELECT a FROM "Table" WHERE b = 'x.

This pull request prefers the contents of a ```sql block when the model writes one, otherwise keeps the last N candidates, and replaces the .*?(?=(?:;|\n{4,}|```|$)) lookahead with a small scanner that skips quoted runs. It also documents the any-of-N scoring rule in the evaluation docs.

Closes #126

Changes

  • llmsql/utils/regex_extractor.py
    • _fenced_sql_blocks(): collect the contents of ```sql blocks, dropping the terminating ; so fenced queries look the same as scanned ones.
    • _query_end(): walk forward from SELECT and stop at a ;, a run of four or more newlines, a fence, or the end of the text. Quoted runs are skipped through _skip_quoted(), which also understands the doubled-quote escape ('it''s'). The fence is tested before the quote check so ``` still terminates a query instead of being read as a backtick identifier.
    • find_sql(): prefer fenced blocks, otherwise scan, then return results[-limit:] instead of results[:limit]. limit <= 0 still returns [].
  • tests/utils/test_regex_extractor.py: three new classes covering a ; inside a literal / escaped literal / quoted identifier / backtick identifier, a query that follows 12 lines of reasoning prose, fence preference, and the fallback when a fence is untagged or empty.
  • docs/docs/evaluation.rst: new "How a Prediction Is Scored" section stating that a sample counts as correct when any of the extracted candidates matches gold, and which candidates are kept.

No change to the scoring itself: the evaluator still loops over every candidate and marks a match when any result equals gold.

Verification

  • PYTHONPATH=. pytest tests/utils/test_regex_extractor.py -q → 32 passed (20 pre-existing + 12 new).
  • Reverse check: with only llmsql/utils/regex_extractor.py stashed, 9 of the new tests fail, so they pin both bugs rather than describing the current behaviour.
  • PYTHONPATH=. pytest tests/utils/test_evaluation_utils.py tests/utils/test_regex_extractor.py tests/utils/test_rich_utils.py tests/utils/test_utils.py tests/test_init.py -q → 91 passed, 1 skipped, 3 failed. The 3 failures are tests/test_init.py lazy-import tests that need torch, which is not installed on this machine; they are unrelated to this change and fail identically on an unmodified checkout.
  • ruff check llmsql/utils/regex_extractor.py tests/utils/test_regex_extractor.py → All checks passed.
  • mypy llmsql/utils/regex_extractor.py → Success: no issues found in 1 source file.

The rest of the suite (inference tests, tests/cli, integration) needs torch / vllm and a GPU, so it was not run here.

Checklist

  • Unit tests for both reported cases
  • Ruff and mypy clean on the touched files
  • Existing extractor tests unchanged and still passing
  • Scoring behaviour (any-of-N) preserved and documented
  • Full suite with torch / vllm — not run, no GPU on this machine

…t a quoted semicolon

find_sql() returned the first 10 candidates. A model that reasons before
answering starts its prose with words like "select", so those prose candidates
filled every slot and the real query at the end was never evaluated. It also
ended a query at the first ";", even inside a string literal or a quoted
identifier, turning WHERE b = 'x;y'; into WHERE b = 'x'.

- prefer the contents of a ```sql block when the model writes one
- otherwise keep the last N candidates instead of the first N
- find the end of a query with a scanner that skips quoted runs, so a ";"
  inside a literal (including a doubled-quote escape) is not a terminator;
  the fence is checked before the quotes so ``` still ends a query
- document the any-of-N scoring rule in the evaluation docs

Closes LLMSQL#126
@codecov

codecov Bot commented Oct 9, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.24561% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
llmsql/utils/regex_extractor.py 98.24% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@DzmitryPihulski DzmitryPihulski left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ddaihao2022, thank you for your contribution!
One small regression before we merge: the old pattern required a word boundary after the keyword (SELECT\b), but the new _SELECT = re.compile(r"(?i)(SELECT)") does not. So ordinary words now start candidates:

find_sql('I selected the Price column.\nSELECT "Price" FROM "Table"')
# main: ['SELECT "Price" FROM "Table"']
# PR:   ['selected the Price column.\nSELECT "Price" FROM "Table"', 'SELECT "Price" FROM "Table"']

These junk candidates fail to execute (inflating sql_errors) and, more importantly, use up the 10 candidate slots in long reasoning outputs. Please change it to:

_SELECT = re.compile(r"(?i)(SELECT)\b")

and add a test for "selected" / "Selection" not producing candidates.

For context, we re-scored all 13 leaderboard models' full outputs (80,330 each) with your branch: accuracy goes up by +0.02 to +0.54 pp (most for models that reason in prose, like Nemotron-3-Nano and Qwen3-0.6B). With the \b fix, items flipping right→wrong drop from 223 to 179 and wrong→right rise from 1,294 to 1,297, so the fix is a clear improvement.

If you find LLMSQL useful, we'd be grateful if you could give the project a ⭐ on GitHub, it really helps us!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Evaluation] find_sql drops the final query after reasoning text and truncates at ; inside literals

2 participants