Repository navigation
Fail the attempt when SQLToolset cannot reach its database - #76783
Conversation
|
RunsOn re-runs them automatically when the run finishes; no action is needed. |
amoghrajesh
left a comment
There was a problem hiding this comment.
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?
|
@amoghrajesh afaik the changelog note is only for breaking changes users have to act on (it says so at the top of |
253454e to
e2ffb39
Compare
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.
e2ffb39 to
ba8c361
Compare
SQLToolsetturned every exception into aModelRetry, 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-minireplied "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 calledlist_tables,queryandget_schemaone after another until it ran out of retries. With durable execution there's a second problem: theModelRetryis 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
OperationalErrorfor both "no such column" and "unable to open database file"), so the toolset looks at where the error came from instead:DbApiHook, raises.DbApiHook.run, and the Snowflake, Databricks and Exasol overrides, connect throughself.get_conn(), so the toolset wrapsget_connon its own hook instance. The URI is built first because for Databricks that fetches a token. A failure in either raisesCould not reach the database of connection '<id>': <driver message>.list_tablesandget_schemause the SQLAlchemy inspector. Building it (loading the dialect, connecting) raises, as does aDBAPIErrorwith no statement or withconnection_invalidatedset.Everything after the connection is open counts as the statement's error.
A connection that drops while a
queryis running still goes to the model.DbApiHook.runpasses back the raw driver exception, and most dialects can't tell a disconnect from it alone: psycopg 3'sis_disconnectneeds the live connection, whichrunhas 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 whoseget_conndoesn'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_codeinto aModelRetry(theMontyRuntimeErrorbranch incode_mode/_toolset.py), and that was true before this PR too. The CodeMode and SQL toolset pages now say so.