Skip to content

Bound get_schema results for very wide tables in the SQL toolsets - #74017

Merged
pankajkoti merged 3 commits into
apache:mainfrom
astronomer:bound-get-schema-results-for-wide-tables-sql-toolsets
Oct 1, 2026
Merged

pankajkoti merged 3 commits into
apache:mainfrom
astronomer:bound-get-schema-results-for-wide-tables-sql-toolsets

Conversation

@pankajkoti

Copy link
Copy Markdown
Member

What

get_schema in SQLToolset and DataFusionToolset returned every column of a
table with no bound. On a table with a few thousand columns that is a
six-figure-byte tool result, and because an agent calls get_schema before it
can write a query, the result enters message history early and its cost is
re-paid on every later model request. On a several-thousand-column table an
agent could exhaust its context before issuing a single query, even after
#71317 bounded the query tool.

#71317 deliberately left get_schema alone, because the same fix does not
transfer: column names are the information the agent needs to write SQL, so
truncating to the first N columns leaves it unable to reference or discover the
rest.

How

get_schema now mirrors the query tool's bounded-output contract:

  • It returns a JSON object {"columns": [{"name", "type"}, ...], "column_count": N} instead of a bare list.
  • It accepts an optional name_contains substring filter, so the agent can ask
    for the columns relevant to its question on a wide table.
  • Above max_columns (new constructor argument, default 100) or the existing
    max_result_bytes budget, the full list is replaced by a bounded summary:
    column_count, a type_histogram, a sample_columns preview, and
    truncated / truncated_by / hint that name the limit hit and point back
    at name_contains.

Both toolsets move together. The shared logic lives in build_schema_result
next to build_query_result in query_results.py. The provider changelog
records the tool-result shape change, and the toolset docs gain a "Bounded
schema results" section.


Was generative AI tooling used to co-author this PR?
  • Yes - Claude Code (Opus 4.8)

Generated-by: Claude Code (Opus 4.8) following the guidelines


🤖 Generated with Claude Code

SQLToolset and DataFusionToolset exposed get_schema with no bound on the
number of columns returned. On a table with thousands of columns the result is
a six-figure-byte payload, and because an agent calls get_schema before it can
write a query, that payload enters message history early and its cost is
re-paid on every later model request. A prior change bounded the query tool but
deliberately left get_schema alone, because column names are what the agent
needs to write SQL, so truncating to the first N columns would leave it unable
to reference or discover the rest.

The bound therefore filters and summarizes rather than truncating. An optional
name_contains substring lets the agent ask for the columns relevant to its
question, and above the column cap or the byte budget the full list is replaced
by a summary that reports the shape (column count, a type histogram, a sample
of columns) and points back at the filter, matching the query tool's
bounded-output contract.

Co-Authored-By: Claude <[email protected]>
@pankajkoti
pankajkoti force-pushed the bound-get-schema-results-for-wide-tables-sql-toolsets branch from 9c43264 to f710a8d Compare October 1, 2026 09:49
@pankajkoti
pankajkoti marked this pull request as ready for review October 1, 2026 14:29
Comment thread providers/common/ai/docs/changelog.rst Resolved
Review found that sampling the first max_columns columns made the summary about
as large as the list it replaced and dropped the tail, which is the
truncate-to-first-N behaviour the design set out to avoid. The preview is now a
small fixed sample, so the summary stays far smaller than the full list at any
table width while still pointing the agent at the name_contains filter.

Alongside that, from the same review: when the histogram alone does not fit the
budget it is dropped but the sample is still filled, the summary hint is worded
from the limit it actually hit and pluralized, the no-match hint no longer sends
the agent back to the same filter on a wide table and now carries total_columns,
the changelog tells callers to check truncated before reading columns, and
max_result_bytes is documented as also bounding get_schema. Adds tests for the
byte-budget contract and that each toolset forwards its own bounds.

Co-Authored-By: Claude <[email protected]>
@pankajkoti
pankajkoti force-pushed the bound-get-schema-results-for-wide-tables-sql-toolsets branch from ab78b84 to 9f45867 Compare October 1, 2026 16:39
@pankajkoti
pankajkoti requested a review from kaxil October 1, 2026 16:47
A column too wide for the remaining budget now skips rather than stopping the
sample loop, so a wide struct type at the front of the preview window no longer
leaves sample_columns empty. The preview is an unordered illustration of names,
so there is no prefix to preserve. Also pins the max_columns boundary: exactly
the cap returns the full list, one more summarizes by count, and exactly the cap
over the byte budget summarizes by bytes.

Co-Authored-By: Claude <[email protected]>

@kaxil kaxil left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, both follow-ups look good.

@pankajkoti
pankajkoti merged commit e4d397f into apache:main Oct 1, 2026
85 checks passed
@pankajkoti
pankajkoti deleted the bound-get-schema-results-for-wide-tables-sql-toolsets branch October 1, 2026 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants