ColtenOuO opened a new pull request, #70137:
URL: https://github.com/apache/airflow/pull/70137

   ## Summary
   
   `LLMSQLQueryOperator._strip_llm_output` strips markdown code fences from LLM 
output before the generated SQL is validated/executed. The existing logic only 
handles fences where the LLM's response spans multiple lines
   (e.g. ` ```sql\nSELECT 1\n``` `) — it splits on `"\n"` and requires at least 
2 lines before attempting to drop the opening/closing fence.
   
   When the whole fenced response is on a single line (e.g. ` ```SELECT 1``` `, 
no internal newline), `text.split("\n")` yields a
   single element, the `len(lines) >= 2` guard is never satisfied, and the 
fence-stripping is skipped entirely — the backticks stay attached and the value 
is passed straight into `_validate_sql` (and potentially execution) as-is.
   
   The operator's system prompt already asks the LLM not to use markdown at all 
(`"Return ONLY the SQL query, no explanation or markdown."`), but the existing 
multi-line fence-handling logic (and its test coverage) already assumes LLMs 
commonly ignore that instruction and wrap output in a fence anyway. 
   
   This change extends the same defensive handling to the single-line variant 
of that same behavior, which the multi-line path doesn't cover.
   
   ## Changes
   
   - `_strip_llm_output` gains an `elif` branch for the case where the fenced
     text has no internal newline: strip the leading/trailing ` ``` ` directly
     instead of relying on line-splitting. The existing multi-line branch is
     untouched, so behavior for every previously-tested case is unchanged.
   
   ## Test plan
   
   - Added two regression cases to `TestStripLLMOutput`: a single-line fence
     with no language tag, and one wrapping a longer query.
   - Full `test_llm_sql.py` suite passes (44 passed), including all
     pre-existing multi-line fence cases unchanged.
   - `ruff format` / `ruff check`, `prek --stage pre-commit`, and
     `breeze run mypy` all clean on the changed file.
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to