Skip to content

fix: stop rejecting valid NL-search SQL that contains the word error - #855

Merged
elliotxx merged 1 commit into
KusionStack:mainfrom
pujitha24:auto/issue-735
Oct 10, 2026
Merged

elliotxx merged 1 commit into
KusionStack:mainfrom
pujitha24:auto/issue-735

Conversation

@pujitha24

Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind bug

What this PR does / why we need it:

Natural language search returns a false "query is invalid" error (rendered as an HTTP 500) for queries like "Which pods are in error state", while other queries succeed.

The root cause is in ConvertTextToSQL (pkg/core/manager/ai/search.go): it validated the LLM's raw response with IsInvalidQuery, which flagged the response as invalid whenever it contained the case-insensitive substring "error" anywhere in the text. A query about pod error state legitimately produces a generated SQL statement whose WHERE clause itself contains the word "error" (matching a Kubernetes pod status value), so this substring check rejected perfectly valid SQL as invalid.

ConvertTextToSQL now uses "was a SELECT statement extracted from the response" as the sole validity signal, since the prompt (prompt.go, Text2sqlType) already instructs the model to always emit SQL starting with select * from and ending with ;, or the literal word "Error" when it cannot produce a query. The now-unused IsInvalidQuery helper and its test were removed since nothing else in the repo referenced it.

Validation:

  • go build ./pkg/core/manager/ai/... passes.
  • go test ./pkg/core/manager/ai/... -run TestConvertTextToSQL -v passes. The new TestConvertTextToSQL/ValidQueryWithErrorAsValue case reproduces the reported bug: confirmed by stashing this change and re-running the same test, which fails with query is invalid on the old code and passes after this fix.
  • gofmt -l on all changed files reports no issues.
  • Could not run this repo's make test / make lint targets to completion: the local machine's disk is almost entirely full (roughly 300-900Mi free on a 228Gi volume), and pkg/core/manager/ai transitively depends on k8s.io/kubernetes/pkg/apiserver through pkg/kubernetes/registry, which this disk cannot fully compile with the repo's gcflags. This is a pre-existing local environment constraint unrelated to this change, not a failure of the change itself — the narrower go build/go test commands above did complete successfully against the real compiled package. Happy to address anything the full CI run surfaces.

Which issue(s) this PR fixes:

Fixes #735

Motivation:
Natural language search returns a false "query is invalid" error for
queries such as "Which pods are in error state", while unrelated
queries succeed. The handler then renders this as an HTTP 500.

Approach:
ConvertTextToSQL (pkg/core/manager/ai/search.go) validated the LLM's
raw response with IsInvalidQuery, which flagged the response as
invalid whenever it contained the case-insensitive substring "error"
anywhere in the text. A query about pod error state legitimately
produces a generated SQL statement whose WHERE clause itself contains
the word "error" (matching a Kubernetes pod status value), so this
substring check rejected valid SQL as invalid.

ConvertTextToSQL now uses "was a SELECT statement extracted from the
response" as the sole validity signal, since the prompt (prompt.go,
Text2sqlType) already instructs the model to always emit SQL starting
with "select * from" and ending with ";", or the literal word "Error"
when it cannot produce a query. The now-unused IsInvalidQuery helper
and its test were removed since nothing else in the repo referenced
it.

Validation:
- go build ./pkg/core/manager/ai/... passes.
- go test ./pkg/core/manager/ai/... -run TestConvertTextToSQL -v
  passes. The new TestConvertTextToSQL/ValidQueryWithErrorAsValue case
  reproduces the reported bug: verified by stashing this change and
  re-running the same test, which fails with "query is invalid" on
  the old code and passes after this fix.
- gofmt -l on all changed files reports no issues.
- Could not run this repo's `make test` / `make lint` targets to
  completion: the local machine's disk is almost entirely full
  (roughly 300-900Mi free on a 228Gi volume), and pkg/core/manager/ai
  transitively depends on k8s.io/kubernetes/pkg/apiserver through
  pkg/kubernetes/registry, which this disk cannot fully compile with
  the repo's gcflags. This is a pre-existing local environment
  constraint unrelated to this change, not a failure of the change
  itself; the narrower go build/go test commands above did complete
  successfully against the real compiled package.

Report: KusionStack#735
Signed-off-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com>
Assisted-by: claude-sonnet-5 (via Claude Code)
@github-actions

github-actions Bot commented Sep 14, 2026 •

Copy link
Copy Markdown

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@pujitha24

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA


@pujitha24

Copy link
Copy Markdown
Contributor Author

recheck

1 similar comment
@pujitha24

Copy link
Copy Markdown
Contributor Author

recheck

@pujitha24

Copy link
Copy Markdown
Contributor Author

This is rebased and the checks are green; if anything would make it easier to review, I am happy to adjust it.

@elliotxx elliotxx 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.

Nice catch, thanks for fixing this! 🙌 LGTM. CLA and license checks are green; the Check and Constraints workflows were waiting for approval, so I've kicked them off. Will merge once they pass.

@elliotxx
elliotxx merged commit cd5a111 into KusionStack:main Oct 10, 2026
5 of 8 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 10, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix: Natural Language Search Error on Complex Queries

2 participants