ASTPlannerNode
Overview
- Generates a structured SQL AST plan (
PlanModel) using an LLM with structured output. - Exists to translate natural language intent into a deterministic, machine‑verifiable plan.
- Sits after
SchemaRetrieverNodein the SQL agent subgraph. - Class:
ASTPlannerNode - Source:
packages/nl2sql/src/nl2sql/pipeline/nodes/ast_planner/node.py
Responsibilities
- Serialize
relevant_tablesinto the planning prompt. - Pass user intent, expected schema, and error feedback to the LLM.
- Return
ASTPlannerResponsewith thePlanModel.
Position in Execution Graph
Upstream:
- SchemaRetrieverNode
- RefinerNode (retry loop)
Downstream:
- LogicalValidatorNode on success
- retry_handler on retryable failure
Trigger conditions: - Executed after schema retrieval and during retry loops.
flowchart LR
SchemaRetriever[SchemaRetrieverNode] --> Planner[ASTPlannerNode] --> Validator[LogicalValidatorNode]
Refiner[RefinerNode] --> Planner
Planner --> RetryHandler[retry_handler]
Inputs
From SubgraphExecutionState:
sub_query.intent(required)sub_query.expected_schema(optional)relevant_tables(required for schema grounding)errors(optional feedback for retries)
Validation performed:
- No explicit validation; relies on LLM structured output schema.
Outputs
Mutations to SubgraphExecutionState:
ast_planner_response(ASTPlannerResponse)reasoningwith plan summaryerrorson planning failure
Side effects:
- LLM invocation via
llm_registry.
Internal Flow (Step-by-Step)
- Serialize
relevant_tablesto JSON text. - Build feedback string from existing errors.
- Build
expected_schemapayload from sub‑query. - Invoke the LLM chain with prompt + structured output (
PlanModel). - Return
ASTPlannerResponsewith plan and reasoning. - On exception, emit
PLANNING_FAILUREerror and returnplan=None.
Contracts & Interfaces
Implements a LangGraph node callable:
Key contracts:
PlanModelASTPlannerResponse
Determinism Guarantees
- Deterministic only if LLM is configured deterministically.
- Plan structure is validated downstream; ordering is not enforced here.
Error Handling
Emits PipelineError with:
PLANNING_FAILUREon LLM or parsing errors.
Logs failures via logger.exception.
Retry + Idempotency
- Retries are orchestrated by the subgraph, not this node.
- Idempotency depends on LLM determinism.
Performance Characteristics
- One LLM call per planning attempt.
- Cost is dominated by LLM latency and token usage.
Observability
- Logger:
planner - Adds reasoning entries to subgraph state.
Configuration
- LLM config under agent name
ast_plannerinllm.yaml.
Extension Points
- Modify
PLANNER_PROMPTand examples. - Replace node in
build_sql_agent_graph()to use alternate planning logic.
Known Limitations
- No deterministic safeguards beyond structured output schema.
- No explicit timeout handling in the node itself.
Related Code
packages/nl2sql/src/nl2sql/pipeline/nodes/ast_planner/node.pypackages/nl2sql/src/nl2sql/pipeline/nodes/ast_planner/schemas.py