Claude Code 学习站

CCA-F 练习题库· Prompt Engineering & Structured Output

Claude occasionally returns JSON with a trailing comma, causing parse failures. What is the production-correct fix?

  1. A在 system prompt 里加上 'always return valid JSON without trailing commas',并在输入之后再重复一遍这条规则
  2. BPost-process with regex to strip trailing commas
  3. CImplement a validate → retry loop: validate against schema, feed the error back to Claude on failure
  4. DSwitch to a model that never produces invalid JSON

正确答案:C

解析

  • Cis correct. The verified production pattern for structured-output reliability is programmatic: parse/validate the response against the target schema and, on failure, send the specific validation error back to Claude in a targeted retry so it self-corrects — this closes the quality loop and handles any malformation, not just trailing commas.
  • Aprompt-only "always return valid JSON" reduces but never guarantees structural correctness and offers no recovery when Claude still errs.
  • B(regex strip of trailing commas) is a symptom patch: it fixes one narrow failure mode and stays brittle to every other malformation (truncation, unescaped quotes, missing brackets), so it breaks on the next variant.
  • Dis false — no model is guaranteed to never emit invalid JSON. One caveat worth noting: the genuinely strongest production approach in current guidance is constrained generation via tool use / structured outputs (which forces schema-valid JSON at generation time), but that is not among the offered options, so C remains the correct choice.
  • Bis a defensible cheap patch for this one syntactic error, but its brittleness makes it clearly inferior to C as the general "production-correct" fix.

延伸阅读

本题为本站自有原创练习题,非任何官方考试内容;解析对照 Anthropic 公开文档撰写, 如与最新文档不符请以官方为准。想在限时环境下检验水平,可参加 模拟考(题目与本页题库不重叠)。