Skip to content

Ground ReAct answers in executed rows after submit - #794

Open
DinaLaptii wants to merge 1 commit into
mainfrom
fix/252-post-execution-answer-grounding
Open

DinaLaptii wants to merge 1 commit into
mainfrom
fix/252-post-execution-answer-grounding

Conversation

@DinaLaptii

@DinaLaptii DinaLaptii commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Rewrite the business answer from the executed result with a non-reasoning model, check it with a narrow deterministic number check (one retry), and fall back to a plain rendering of the rows. Gated by TEXT_TO_SQL_REACT_ANSWER_SYNTHESIS (on by default).

fixed of #252

The "SQL executed" branch

How it worked before

  • The answer text was taken as-is: submission.response or "Here is the result of the query.".
  • The model wrote that prose when it called submit, before the final SQL ran. So it could contradict the rows, for example "200 orders" in the text when the query returned 3.
  • The custom_analyses_used provenance was computed by a separate call, later, when writing to path_state.
  • Everything then went to format_and_respond with that text.

How it works now

  • The provenance analyses_used is computed once, earlier (line 184).
  • synthesize_answer is called on the non-reasoning model state["llm"]. It receives the question, the SQL, the actual rows, the assumptions, a flag saying whether the result was cut off, and metadata: tables with column descriptions and the saved analyses that were used. The model does not see the draft.
  • The model writes the answer from the rows only. A deterministic number check then compares the numbers in the text against the rows and the assumptions:
    • If none of the numbers is supported by the rows, the model is given a note explaining the problem and asked to rewrite. There is one retry.
    • If the numbers still disagree after that, the answer is built directly from the rows, one column = value line per row, at most 10 rows.
  • The result goes into SQLGenerationModel.response. analyses_used is stored in path_state unchanged.

Special cases in synthesis

  • Query did not run (result is empty): the draft is kept, or the text says the query was not run. Before, this case could read as "no rows".
  • Result is cut off or too large: the answer says so explicitly and does not present the figures as complete.
  • The model has no with_structured_output: the answer is built straight from the rows.
  • Flag TEXT_TO_SQL_REACT_ANSWER_SYNTHESIS=false: the old draft is returned, which fully restores the previous behavior.

What did not change

  • The predict, "no answer" and "answer without SQL" paths.
  • The SQL, sql_response_from_db, numeric_kinds, values_digest, candidates and the valid_sql decision.
  • The structure of custom_analyses_used.

Cost and limits

  • One extra LLM call for every answer that has SQL. With a retry, up to two.
  • The number check is deliberately narrow, as in Fix/answer correctness submit checks #688. It fires only when none of the numbers is supported by the rows. It misses subtle mismatches, but it almost never raises false alarms.
  • Certified descriptions at the ColumnAttribute level do not reach synthesis. Column descriptions and saved analyses do.

Summary by CodeRabbit

  • New Features
    • Answers to completed SQL queries are now synthesized from the returned database results and reviewed context.
    • Answers are checked for figures unsupported by the results. If a figure can’t be verified, the system retries once and then falls back to a result-based response.
    • Answer synthesis can be disabled with a configuration setting.
  • Documentation
    • Updated architecture documentation to describe answer synthesis, verification, fallback behavior, and configuration.

Rewrite the business answer from the executed result with a non-reasoning
model, check it with a narrow deterministic number check (one retry), and
fall back to a plain rendering of the rows. Gated by
TEXT_TO_SQL_REACT_ANSWER_SYNTHESIS (on by default).

Part of #252

Signed-off-by: dlaptii <dlaptii@nvidia.com>
@coderabbitai

coderabbitai Bot commented Dec 1, 2026

Copy link
Copy Markdown

Explain Change →

Note

CodeRabbit ASCII loading animation

Currently processing new changes in this PR. This may take a few minutes, please wait...

Review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 4741224 and 05afac4.

Files selected for processing: 8

  • auto_ontology/retrieval/text_to_sql/react/agent.py
  • auto_ontology/retrieval/text_to_sql/react/answer_numbers.py
  • auto_ontology/retrieval/text_to_sql/react/answer_synthesis.py
  • auto_ontology/retrieval/text_to_sql/react/config.py
  • auto_ontology/retrieval/text_to_sql/tests/test_react_agent.py
  • auto_ontology/retrieval/text_to_sql/tests/test_react_answer_numbers.py
  • auto_ontology/retrieval/text_to_sql/tests/test_react_answer_synthesis.py
  • docs/architecture.md

@DinaLaptii
DinaLaptii marked this pull request as draft December 1, 2026 10:27
@DinaLaptii
DinaLaptii marked this pull request as ready for review December 1, 2026 10:27

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.

1 participant