輸出格式
基於 Cursor 官方 Output Format 文件解釋 text、json、stream-json、partial output、事件型別、失敗行為和指令碼消費邊界。
📖 本篇術語速查表
| 英文 / 縮寫 | 中文 | 一句話解釋 |
|---|---|---|
| 輸出格式 | output format | CLI 結果的格式(文本 / JSON 等)。 |
| 可解析 | parseable | 讓程式能讀取結果。 |
| 自動化 | automation | 輸出對接後續流程。 |
不想讀完?把下面這段提示詞丟給 AI 幫你跑完——幫你設計 Cursor CLI 的輸出格式,便於自動化解析。
你是 Cursor CLI 輸出格式顧問,幫我設計便於自動化解析、對接後續流程的輸出。
【角色】
你熟悉 CLI 的輸出格式選項、怎麼讓結果可解析、怎麼對接後續處理。
【輸入】
- 我的任務和後續怎麼用結果:___
- 是給人看還是程式讀:___
- 需要哪些欄位:___
- 對接的下游:___
【工作流程】
1. 按用途選輸出格式
2. 設計可解析的結構
3. 處理成敗判斷和錯誤輸出
4. 給對接下游的方式
【輸出規範】
▌一、輸出格式選擇
▌二、可解析結構
▌三、成敗 / 錯誤輸出
▌四、對接下游
【硬約束】
- 程式讀的用結構化格式
- 成敗要能程式判斷
- 錯誤輸出清晰
- 不確定的格式標註需查官方文件
- 給的方案能直接用
- 每條結論落到可照做步驟,不空泛
- 給的每條結論都要落到具體可照做的步驟或示例,不停留在「建議」「考慮一下」這類沒法直接執行的空泛表述
- 不確定的引數、設定或格式一律以官方文件為準,給的示例標註適用版本,避免照搬過時寫法誤導Cursor CLI 的 --output-format 只在 print mode 中使用,也就是結合 --print / -p,或 stdout 非 TTY、stdin 被 pipe 時推斷為 print mode。
閱讀目標:讀完本章,你應該能判斷什麼時候用 text、json、stream-json,並能避免把自然語言輸出當穩定 API 解析。
1. 三種輸出格式
| Format | 適合 | 不適合 |
|---|---|---|
text | 人讀結果、PR comment、簡單記錄 | 機器強解析 |
json | 指令碼讀取最終結果、需要 session/request 後設資料 | 需要即時進度 |
stream-json | 即時進度、工具呼叫事件、長任務觀察 | 只要最終答案的簡單指令碼 |
預設格式是 text。
agent -p "Summarize this repo"
agent -p --output-format json "Return a concise risk report"
agent -p --output-format stream-json "Analyze this repo and report progress"商業級預設:人看用 text,機器解析用 json,需要過程事件用 stream-json。
2. JSON format
json 格式在成功結束時輸出一個 JSON object,並以 newline 結尾。它不會輸出 delta 或 tool events,而是把文本聚合到最終 result。
成功結構包含這些欄位:
| Field | 含義 |
|---|---|
type | terminal result,通常是 result |
subtype | 成功時為 success |
is_error | 成功時為 false |
duration_ms | 總執行時間 |
duration_api_ms | API 請求時間 |
result | 完整 assistant response text |
session_id | 目前 execution 的 session ID |
request_id | optional request ID,可能不存在 |
失敗時,process 會用 non-zero exit code,並把錯誤寫到 stderr;失敗場景不會保證輸出 well-formed JSON object。
指令碼里要同時處理 stdout、stderr 和 exit code:
if output=$(agent -p --output-format json "Review this diff" 2>err.log); then
printf '%s\n' "$output" | jq -r '.result'
else
cat err.log
exit 1
fi3. Stream JSON format
stream-json 輸出 NDJSON:每一行是一個 JSON object,代表執行過程中的一個 event。
常見 event types:
| Type | 說明 |
|---|---|
system / init | session 開始,包含 cwd、model、permissionMode 等 |
user | 使用者輸入 prompt |
assistant | assistant message |
tool_call / started | tool call 開始 |
tool_call / completed | tool call 完成 |
result / success | 成功結束的 terminal event |
成功時 stream 以 terminal result event 結束。失敗時,process non-zero exit,stream 可能提前結束,並把錯誤寫到 stderr。
讀取 stream 時,不要假設每次都有最終 result;必須處理異常退出。
4. Partial output 去重規則
如果需要 character-level streaming,使用:
agent -p --output-format stream-json --stream-partial-output \
"Analyze this project and stream progress"開啟後,assistant events 會出現三類情況。官方規則可以壓成這樣:
timestamp_ms | model_call_id | 含義 | 動作 |
|---|---|---|---|
| present | absent | 新文本 delta | append |
| present | present | tool call 前的 buffered flush | skip |
| absent | absent | turn 結束時 final flush | skip |
如果不需要即時字元流,只想要最終答案,不要解析 assistant events,直接讀 terminal result event 的 result 欄位。
5. Tool events 怎麼用
tool_call events 分 started 和 completed。它們可以幫助你記錄 Agent 做過什麼。
| Tool | started 裡看什麼 | completed 裡看什麼 |
|---|---|---|
| Read file | path | content metadata、totalLines、totalChars |
| Write file | path、fileText、toolCallId | absolute path、linesCreated、fileSize |
| Other tools | function name、arguments | tool-specific result |
用途:
- 在 CI 中生成審計記錄。
- 統計 Agent 是否寫了檔案。
- 追蹤讀取了哪些敏感路徑。
- 將 tool call ID 與 completion event 對齊。
不要把 tool event 當業務成功的唯一依據。最終仍要看 exit code、result event、git diff 和測試結果。
6. Text format
text 只輸出最終 assistant message,不包含中間 progress、tool call summaries 或事件流。
適合:
- PR comment。
- 人讀摘要。
- 本地指令碼輸出到 markdown。
- 不需要機器解析欄位的報告。
不適合:
- 判斷是否寫檔案。
- 統計 tool calls。
- 需要 request/session metadata。
- 嚴格 JSON contract。
7. 指令碼消費建議
| 需求 | 推薦 |
|---|---|
| 人看一段總結 | --output-format text |
| 取最終結果 | --output-format json 並讀 .result |
| 需要即時進度 | --output-format stream-json |
| 需要逐字流 | 再加 --stream-partial-output |
| 需要審計工具呼叫 | 解析 tool_call events |
| 失敗處理 | 同時檢查 exit code 和 stderr |
實現建議:
- 每行 JSON 用正式 parser,不用字串切割。
- consumer 忽略未知欄位,因為官方說明未來可能加欄位。
- session ID 在單次 execution 內保持一致,可用於記錄關聯。
- print mode 中 thinking events 被 suppress,不要等待 thinking events。
深讀:為什麼 text 不是 API
text 是給人看的最終回答,天然可能因為模型、prompt、上下文和版本變化而改變措辭。
一旦指令碼依賴某個中文標題、冒號或 bullet,就會變脆。機器消費結果要麼用 json,要麼讓 prompt 明確生成 JSON,再結合 --output-format json 做雙層約束。
本章自檢
完成本章後,用這 3 個問題檢查自己是否真正理解:
json失敗時為什麼不能假設 stdout 一定是 well-formed JSON?stream-json與--stream-partial-output的差別是什麼?- partial output 中哪類 assistant event 應該 append,哪兩類應該 skip?
透過標準:你能寫一個指令碼,正確處理 exit code、stderr、JSON 解析失敗和 terminal result。
官方來源
- Cursor CLI Output Format —— 官方 text、json、stream-json、partial output、event types 和 implementation notes。
- Cursor Headless CLI —— 官方 headless / print mode 使用場景。
- Cursor CLI Parameters —— 官方
--output-format引數說明。