agent failed to parse wrangler d1 execute table output: column splitter broke on wide rows
Fixes agents that parse wrangler d1 execute results by splitting the pretty table on whitespace and break on wide rows. Use when D1 query results parse correctly for narrow rows but corrupt on wide ones. Not for D1 query failures, empty results, or JSON output parsing.
TL;DR
wrangler d1 execute prints results as a pretty ASCII table, and agents split columns on whitespace. Wide values containing spaces shift every column after them, corrupting the parse. Use the JSON output format for machine parsing and leave the pretty table for humans.
The query
agent failed to parse wrangler d1 execute table output: column splitter broke on wide rowsUse this when
- D1 results parse fine for narrow rows, garbage for wide ones.
- The agent's extracted values are shifted or merged.
- The same query works when a human reads the table.
Not for
- D1 query errors (fix the SQL).
- Empty result sets (nothing to parse).
- Agents already using JSON output (different bug).
Steps
Step 1: Confirm the parse corruption
Run the query and compare the agent's parsed values against the raw table.
Expected result: columns after any space-containing value are shifted. The whitespace split is the culprit.
Step 2: Switch the agent to JSON output
npx wrangler d1 execute [db] --remote --command "SELECT ..." --jsonExpected result: the output is structured JSON with named fields. No column splitting is needed or possible.
Step 3: Parse the JSON properly
Read the result array and access fields by name. Handle the envelope format wrangler wraps around the rows.
Expected result: values are correct regardless of width, spaces, or unicode. The parse is robust by construction.
Step 4: Ban pretty-table parsing in the runbook
Expected result: every agent D1 read uses --json. The pretty table is for humans at terminals only.
Step 5: Verify with the widest real row
Run the previously-failing query and check the agent's extracted values.
Expected result: correct values for the wide rows that broke the old parser.
Why it happens
Pretty tables are a human interface: alignment is visual, and spaces inside values are indistinguishable from column separators to a naive splitter. Agents reach for the default output (the table) and write the simplest parser (split on whitespace). It works until the first wide row, which is usually production data.
Edge cases
- JSON output can be large for big result sets. Page with LIMIT/OFFSET rather than parsing megabytes.
- The --json envelope differs between wrangler versions. Parse defensively: find the rows array, do not assume a fixed path.
- Nulls, numbers, and blobs have JSON representations that differ from the table's. Handle types, not just strings.
- If the agent must show humans a table, render it FROM the parsed JSON rather than parsing the rendered table.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_MePpJbiCk5pOejyVRY31aw