Commit c014b91
authored
Bound SQL toolset query results by size, not just row count (#71317)
* Bound SQL toolset query results by size, not just row count
A tool result stays in the model's message history for the rest of the run, so
its cost is re-paid on every subsequent request. max_rows bounded rows, which
says nothing about size: one row of a 3000-column table dwarfs a thousand rows
of a narrow one, and the truncation happened after fetching the whole result,
so the worker paid the full transfer cost for rows it then discarded.
Three changes to the query tool of SQLToolset and DataFusionToolset:
- The result is columnar. Column names are serialized once rather than once
per row, and positional rows keep same-named columns that a dict per row
silently collapsed.
- max_result_bytes bounds the serialized payload. Rows are dropped from the
end until it fits, and the result names the limit it hit so the agent can
narrow its projection instead of paging.
- SQLToolset fetches through DbApiHook.run's handler protocol with fetchmany
rather than get_records, so rows past max_rows never leave the cursor.
* Address review: trustworthy total_rows, byte-accurate budget, honest docs
- total_rows: rowcount is only a query total on drivers that buffer the whole
result. python-oracledb reports rows fetched so far, so after a capped fetch
it equals the cap -- a 10M-row query would report total_rows: 51. Discard any
count no larger than what was fetched; row_count is already the total when the
result was not truncated.
- Byte budget: ensure_ascii escaped each CJK character to six bytes instead of
three, truncating a non-ASCII result several times earlier than the equivalent
English one and charging the model for the escapes. Measure encoded bytes so
max_result_bytes means what it says.
- Truncation hint: "No row fits" was false whenever one wide row preceded narrow
ones, and the partial case -- the common one -- carried no guidance at all.
Name the row that stopped it, and hint on every byte-capped result.
- A cursor that can neither describe nor fetch now raises instead of rendering
as an empty table; the Exasol full-fetch path reports its exact total.
- max_rows docs no longer imply the fetch bound reaches the database. No hook
opens a server-side cursor here, so a client-buffering driver has already
transferred the rows; only the Python conversion is skipped.
- Changelog note for the output-shape change.
* Correct docs that described the pre-fix truncation behaviour
Rows are returned as a contiguous prefix, stopping at the first that does not fit
the remaining budget. Three places still said "dropped from the end until the
payload fits", which reads as though a wide row would be skipped and later ones
packed in.
Also: the max_rows parameter entry claimed rows beyond it are "never fetched from
the cursor", contradicting the qualification the same page carries further down,
and the query tool description told agents that `truncated` means more rows
matched, when it also fires when the result was simply too large.1 parent fd0f7e2 commit c014b91
8 files changed
Lines changed: 789 additions & 83 deletions
File tree
- providers/common/ai
- docs
- src/airflow/providers/common/ai
- toolsets
- utils
- tests/unit/common/ai
- toolsets
- utils
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
28 | 40 | | |
29 | 41 | | |
30 | 42 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
128 | 128 | | |
129 | 129 | | |
130 | 130 | | |
131 | | - | |
| 131 | + | |
| 132 | + | |
132 | 133 | | |
133 | 134 | | |
134 | 135 | | |
| |||
203 | 204 | | |
204 | 205 | | |
205 | 206 | | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
206 | 263 | | |
207 | 264 | | |
208 | 265 | | |
| |||
223 | 280 | | |
224 | 281 | | |
225 | 282 | | |
226 | | - | |
| 283 | + | |
| 284 | + | |
227 | 285 | | |
228 | 286 | | |
229 | 287 | | |
| |||
267 | 325 | | |
268 | 326 | | |
269 | 327 | | |
| 328 | + | |
| 329 | + | |
270 | 330 | | |
271 | 331 | | |
272 | 332 | | |
| |||
616 | 676 | | |
617 | 677 | | |
618 | 678 | | |
619 | | - | |
620 | | - | |
621 | | - | |
622 | | - | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
623 | 687 | | |
624 | 688 | | |
625 | 689 | | |
| |||
735 | 799 | | |
736 | 800 | | |
737 | 801 | | |
738 | | - | |
| 802 | + | |
| 803 | + | |
739 | 804 | | |
740 | 805 | | |
741 | 806 | | |
| |||
763 | 828 | | |
764 | 829 | | |
765 | 830 | | |
766 | | - | |
767 | | - | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
768 | 835 | | |
769 | 836 | | |
770 | 837 | | |
| |||
Lines changed: 28 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
39 | 44 | | |
40 | 45 | | |
41 | 46 | | |
| |||
97 | 102 | | |
98 | 103 | | |
99 | 104 | | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
100 | 114 | | |
101 | 115 | | |
102 | 116 | | |
| |||
105 | 119 | | |
106 | 120 | | |
107 | 121 | | |
| 122 | + | |
108 | 123 | | |
109 | 124 | | |
110 | 125 | | |
111 | 126 | | |
112 | 127 | | |
113 | 128 | | |
| 129 | + | |
114 | 130 | | |
115 | 131 | | |
116 | 132 | | |
| |||
133 | 149 | | |
134 | 150 | | |
135 | 151 | | |
136 | | - | |
| 152 | + | |
137 | 153 | | |
138 | 154 | | |
139 | 155 | | |
| |||
199 | 215 | | |
200 | 216 | | |
201 | 217 | | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
212 | 229 | | |
213 | 230 | | |
214 | 231 | | |
| |||
0 commit comments