Skip to content

Fail the attempt when SQLToolset cannot reach its database - #76783

Merged
kaxil merged 2 commits into
mainfrom
sql-toolset-infra-errors
Nov 12, 2026
Merged

kaxil merged 2 commits into
mainfrom
sql-toolset-infra-errors

Conversation

@kaxil

@kaxil kaxil commented Nov 9, 2026

Copy link
Copy Markdown
Member

SQLToolset turned every exception into a ModelRetry, including an unreachable host, rejected credentials and a missing connection. The model can't fix any of those. Running against a SQLite file that couldn't be opened, gpt-5.4-mini replied "I couldn't access the database... try again once the connection issue is fixed" and the run succeeded, so the task finished green with that sentence as its output and Airflow never retried it. Claude Opus 5.5 called list_tables, query and get_schema one after another until it ran out of retries. With durable execution there's a second problem: the ModelRetry is recorded, so the retry after the outage replays the failure to the model instead of querying again.

With this change, failing to reach the database raises and fails the attempt. Errors from the statement itself still go back to the model as ModelRetry, so it can fix its SQL, and that correction still replays under durable execution. A raised call isn't recorded, so the retry queries the database again.

Design rationale

DBAPI exception classes can't separate the two cases (SQLite raises OperationalError for both "no such column" and "unable to open database file"), so the toolset looks at where the error came from instead:

  • Looking up the connection and its hook. A connection that isn't defined, or isn't a DbApiHook, raises.
  • Building the hook's URI and getting a connection. DbApiHook.run, and the Snowflake, Databricks and Exasol overrides, connect through self.get_conn(), so the toolset wraps get_conn on its own hook instance. The URI is built first because for Databricks that fetches a token. A failure in either raises Could not reach the database of connection '<id>': <driver message>.
  • list_tables and get_schema use the SQLAlchemy inspector. Building it (loading the dialect, connecting) raises, as does a DBAPIError with no statement or with connection_invalidated set.

Everything after the connection is open counts as the statement's error.

A connection that drops while a query is running still goes to the model. DbApiHook.run passes back the raw driver exception, and most dialects can't tell a disconnect from it alone: psycopg 3's is_disconnect needs the live connection, which run has already closed, and the Snowflake and BigQuery dialects don't implement it. The model usually just runs the query again, on a new connection. Hooks whose get_conn doesn't touch the network behave the same way. The SQL toolset docs cover both cases.

Behaviour change: an outage now fails the attempt on the first tool call, where before it either used up the model's retries or let the run finish without data. There are no new config options or APIs.

Known issues

The split doesn't apply inside CodeMode. pydantic-ai-harness turns any exception a tool raises from run_code into a ModelRetry (the MontyRuntimeError branch in code_mode/_toolset.py), and that was true before this PR too. The CodeMode and SQL toolset pages now say so.

@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

⚠️ 1 job(s) of attempt 1 were stopped because AWS reclaimed their EC2 Spot instances. These are not test failures.

RunsOn re-runs them automatically when the run finishes; no action is needed.

@amoghrajesh amoghrajesh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since this is a behaviour change, ie: a database outage used to reach the model as ModelRetry and now fails the attempt. Also, a non DbApiHook connection now raises _ConnectionFailed instead of ValueError - so maybe we should have a changelog for this?

@kaxil

kaxil commented Nov 11, 2026

Copy link
Copy Markdown
Member Author

@amoghrajesh afaik the changelog note is only for breaking changes users have to act on (it says so at the top of changelog.rst), and this one's a fix: an outage could end the run green with no data. The ValueError never reached a task either, since execute_tool turned every exception into a ModelRetry.

@kaxil
kaxil enabled auto-merge (squash) November 11, 2026 12:55
@kaxil
kaxil force-pushed the sql-toolset-infra-errors branch 2 times, most recently from 253454e to e2ffb39 Compare November 11, 2026 21:39
SQLToolset handed every exception back to the model as a ModelRetry,
including an unreachable host, rejected credentials and a missing
connection. No SQL change fixes those: the model either answered without
data, so the task succeeded with an apology as its output, or spent its
retries. Under durable execution the recorded ModelRetry also replayed
the outage on retry instead of querying again.

Failing to reach the database now raises: resolving the connection's
hook, building the hook's URI (which can fetch credentials), the
hook's get_conn (which DbApiHook.run uses to connect), building the
SQLAlchemy inspector for list_tables and get_schema, and
a SQLAlchemy DBAPIError without a statement or with
connection_invalidated. A raised call is not recorded, so a durable
retry runs it again. An error from the statement itself stays a
ModelRetry, so the model can still correct its SQL and the correction
replays. Inside CodeMode the harness still hands the error back to the
model; the docs say so.

The error is SQLToolsetConnectionError in
airflow.providers.common.ai.exceptions, so a retry policy can match it.
get_schema built the inspector only to warm the hook's cache before
get_table_schema read through it, which a hook overriding either one
would defeat. It now reads the columns from the inspector, as
list_tables does. Also rename the connection-error helpers and say in
the docs that a RuntimeError, not SQLToolsetConnectionError, is raised
when the driver's message holds a secret the masker cannot remove.
@kaxil
kaxil force-pushed the sql-toolset-infra-errors branch from e2ffb39 to ba8c361 Compare November 12, 2026 13:22
@kaxil
kaxil disabled auto-merge November 12, 2026 14:55
@kaxil
kaxil merged commit 6c2e7b3 into main Nov 12, 2026
116 of 118 checks passed
@kaxil
kaxil deleted the sql-toolset-infra-errors branch November 12, 2026 14:55
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.

4 participants