Skip to content

sp_BlitzCache: Fix ai_prompt returning nulls when @AI > 0#3856

Closed
Copilot wants to merge 2 commits intodevfrom
copilot/fix-null-values-in-ai-columns
Closed

sp_BlitzCache: Fix ai_prompt returning nulls when @AI > 0#3856
Copilot wants to merge 2 commits intodevfrom
copilot/fix-null-values-in-ai-columns

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 16, 2026

ai_prompt could be NULL when @AI = 1 or @AI = 2 because the prompt-building UPDATE filtered out rows where QueryPlan IS NULL, and the output SELECT would concatenate @AISystemPrompt + NULL → NULL.

Changes

  • Remove QueryPlan IS NOT NULL gate from prompt builder — prompts are now built for all rows when @AI > 0
  • Explicit missing-data messages in prompt textISNULL(..., N'N/A') replaced with CASE WHEN ... IS NULL THEN N'(Query text/plan could not be retrieved.)' for both QueryText and QueryPlan
  • Both-null override — when both QueryText and QueryPlan are NULL, ai_prompt is set to a simple message with no metrics or system prompt:
    SET ai_prompt = N'Prompt not generated because we can''t find the query text or query plan.'
  • AI cursor and @ai=2 advice — filter changed from AND QueryPlan IS NOT NULL to AND (QueryPlan IS NOT NULL OR QueryText IS NOT NULL) so API calls still fire when at least one artifact exists
  • Output SELECT — both standard and expert mode [AI Prompt] columns now skip prepending @AISystemPrompt for the both-null fallback message
  • Constitution cursor — expanded to include databases where only QueryText is available
Original prompt

This section details on the original issue you should resolve

<issue_title>sp_BlitzCache: ai_advice or ai_prompt may be returning nulls</issue_title>
<issue_description>What is the current behavior?
During a very active and enjoyable live stream, we seemed to notice that sometimes we got nulls in the ai_advice, ai_prompt, ai_payload, etc columns of sp_BlitzCache when the @ai parameter was set to 1 or 2.

What is the expected behavior?

If @ai > 0, the ai_prompt column should never be null. It should always have a value based on the prompt and query metrics. If our prompt-building query can't get ONE of the two (query plan or query text), the prompt should say that those objects couldn't be retrieved. If our prompt-building query can't get EITHER of those (both query plan and query text are null), then the ai_prompt column should only say, "Prompt not generated because we can't find the query text or query plan." and not include any metrics or job description stuff.)</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

- Remove QueryPlan IS NOT NULL filter from prompt-building UPDATE
- Add explicit messages when query text/plan cannot be retrieved
- Override ai_prompt with simple message when both are NULL
- Update AI cursor to process rows with at least one of plan/text
- Update output SELECT to skip system prompt for both-null case
- Update constitution cursor to include text-only rows

Co-authored-by: BrentOzar <245462+BrentOzar@users.noreply.github.com>
Copilot AI changed the title [WIP] [ISSUE-123] Fix nulls in sp_BlitzCache ai_advice and ai_prompt sp_BlitzCache: Fix ai_prompt returning nulls when @AI > 0 Mar 16, 2026
Copilot AI requested a review from BrentOzar March 16, 2026 17:09
@BrentOzar BrentOzar closed this Mar 28, 2026
@BrentOzar BrentOzar deleted the copilot/fix-null-values-in-ai-columns branch March 28, 2026 15:22
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.

sp_BlitzCache: ai_advice or ai_prompt may be returning nulls

2 participants