Conversations
A conversation (engine.conversations: start, ask, stream, retry, ask_only, tool, report, get, list and delete, with the chat tools) is a chat over the database. The model decides, step by step, whether to answer or to ask the
database a question through the same typed plan and validator as run_query. It never writes SQL and never
sees result rows.
conversation = engine.conversations.start(user_context=me)
turn = engine.conversations.ask(conversation.id, "How many customers are there?", user_context=me)
print(turn.reply) # the model's closing message
print(turn.steps[0].result.sub_queries[0].sql) # what ran; the rows are not stored
follow_up = engine.conversations.ask(conversation.id, "and by country?", user_context=me)
Every method takes the caller (user_context, keyword-only and required). The methods are listed in
Conversations API.
A turn
flowchart TD
IN["ask(text) or ask(choice=...)"] --> K{"choice?"}
K -->|yes| RE["ask step: the pending question, with the choice"]
K -->|no| M["model call: reply or tool call"]
M -->|reply| ST["store the turn"]
M -->|"ask"| RUN["run_query: typed plan, validator, cost rule"]
M -->|"chat tool"| TOOL["registry tool: no model call"]
RUN -->|"clarification, approval or stop"| ST
RUN -->|"ok or error"| M
TOOL --> M
M -->|"bound reached"| LAST["one last call, tools off"]
LAST --> ST
RE --> ST
- The model chooses. Each step is one structured call (
ModelStep): areply, or onecallof theasktool with a question that stands alone, or of an enabled chat tool (a chart, a report). A reply may carryoffers: tools it suggests, which run only when a client callstool. The model sees the lastcontext_turnsturns as one JSON object (the questions that ran, each answer's summary, the columns, the datasources and tables read), never rows. Everything earlier said, the new message included, reaches it as a JSON string value: text in the history cannot pass for an instruction or a section marker. - The bound. At most
max_tool_callstool calls per turn, then one more model call with the tools off. A turn makes at mostmax_tool_calls + 1model calls. They count againstsettings.pipeline.max_llm_callsandmax_cost_usd; a refusal ends the turnstopped. - A step the model got wrong (an unknown tool, a bad argument, an option id that is not pending) is sent
back with what was wrong. After the parse retries the turn ends
errorwithMODEL_STEP_INVALID(Turn.error) and the reply "I could not tell what to do with that. Please ask it as a full question." - Each
askstep is a run of its own, with its owntrace_id, events and result pages, and the cost rule, scope and hooks of any run. A hook atrun.beforeseesconversation_id.
Clarifications and approvals
- A run that ends
needs_clarificationends the turn and setsConversation.pending. The next turn can passchoice="b"(an option id; the turn is of kindchoice, its text the option's label, and no model is called) or text the model maps to an option by callingaskwithchoice. Either way the pending request's own question is re-asked with the choice. Any next turn clearspending. Achoicewith nothing pending is aValueError. - A run that ends
needs_approvalends the turn. An approver acts on eachApprovalRequestinturn.steps[i].result.approvals_neededoutside the conversation; the asker then callsretry(conversation_id, user_context=..., approvals=[...]), which re-runs the lastaskstep's question as a turn of kindretry, with no model call. ask_only(conversation_id, question, user_context=..., choice=None, cancellation_token=None)runs just theasktool (a turn of kindtool, no model call) and returns the fullQueryResultwith its rows. MCP'saskwill use it.- A run that times out (
pipeline.timeout_s) fails only its step, witherror.codePIPELINE_TIMEOUT; the turn goes on and the model replies, for example by suggesting a narrower question. Eachaskstep runs on its own cancellation token, a child of the turn's, so the timeout never cancels the turn, while cancelling the turn (cancellation_token, or closing a stream) still stops the step in flight. Only a cancel ends a turnCANCELLED. After a timeout the old run may still finish its in-flight model call or SQL while the loop goes on, so the nextaskstep can overlap it.
Streaming
stream(...) takes the same arguments as ask and yields the caller-safe run events of each ask step
(run.started to run.finished), conversation.step after each step and, last, conversation.turn whose
data["stored"] is the stored Turn as JSON. Ownership and the arguments are checked at the call; a
conversation that is already running a turn raises ConversationBusy from the first next. Closing the
iterator early cancels the turn and stores nothing.
Ownership and retention
| Rule | Detail |
|---|---|
| Owner | start records the caller's tenant_id (else the engine's) and user_id. Any other caller gets ConversationNotFound, as for an id that does not exist. |
| Roles | Never stored. Each turn runs with the user_context passed to that call, so a role taken away applies at the next turn. |
No user_id |
Allowed, and owned by "no user": every caller without one in that tenant sees those conversations. Fine for a script, not for a server. |
| One turn at a time | A turn holds the state store's lock conversation:<id>; a second ask, retry or stream raises ConversationBusy. |
| Limits | conversations.max_turns (50): the next turn, whichever of ask, stream, retry or ask_only it is, is a ValueError naming CONVERSATION_TOO_LONG and the fix, "Start a new conversation." |
| Results | A stored step keeps its question, plan, SQL, explanation and summary, never the rows. Read them with engine.results.page(step.trace_id, ..., user_context=caller), as the conversation's caller, until they are purged. |
| Retention | engine.storage.purge() deletes conversations last updated more than conversations.max_age_days (30; null keeps them) ago and reports conversations in retention.purged. |
Events
conversation.started (from start), conversation.step (after each step, in the turn's scope, with the
tool, the status and the argument names, never values) and conversation.turn (once the turn is stored; it
closes the turn's scope). The turn's trace_id carries the loop's own llm.calls, and Turn.usage holds
their usage.
Configuration
conversations: in nl2sql.yaml (context_turns, max_tool_calls, max_turns, max_age_days) and the
conversation agent in llm.yaml; see Configuration System and
LLM.