逐日AI

面试题库

共 328 题,当前筛选 1 题。

5 天提示词工程零基础

D3 结构化输出:JSON schema、模板与变量、多语言输出

  • 模型返回的 JSON 解析或校验失败时,你会怎么设计兜底?重试几次、怎么重试、失败之后怎么办?When the model's JSON fails to parse or validate, how do you design the fallback — how many retries, how do you retry, and what happens after the last failure?
    国内高频海外高频进阶#structured-output#error-handling

    分析过程 · 先想清楚再作答

    1. 这题是生产题,考的是「有没有见过模型抽风」。答「加个 try catch 重试三次」是新手答案,它没回答重试时发什么、也没回答最后怎么办。
    2. 拆法:分三层。校验层返回错误列表而不是布尔值;重试层把错误列表拼进用户消息,让模型知道上一次错在哪,原样重发大概率同样的错;降级层返回空值并记录,交调用方决定跳过还是人工处理。
    3. 重试次数:一次就够。两次以上还不对说明问题不在这条输入而在提示词或 schema,应该修模板而不是继续重试;每次重试都是一次完整调用的钱和延迟。
    4. 结论里最重要的一条:降级不要抛异常,也不要把「差一点」的结果凑合着用。抽取失败是正常业务分支;半对的结构化数据比没有数据更危险,因为下游会把它当真的。
    5. 追问方向:怎么区分「模型抽风」和「提示词有问题」?看失败率——偶发是抽风,某类输入稳定失败是提示词或 schema 缺覆盖,应该把那类输入加进测试集;另一个追问是重试会不会放大成本,答案是要有预算上限并监控重试率。

    How to reason about it · think before answering

    1. A production question that checks whether you have seen a model misbehave. 'Wrap it in try/catch and retry three times' is the novice answer — it says nothing about what you resend or what happens at the end.
    2. Three layers. Validation returns an error list, not a boolean. Retry appends that list to the user message so the model knows what to fix; resending verbatim mostly reproduces the error. Degradation returns null and logs, leaving skip-or-human to the caller.
    3. Retry count: one is enough. Persistent failure means the prompt or schema lacks coverage, so fix the template instead of retrying; each retry costs a full call.
    4. The key conclusion: do not throw on degradation, and do not use a near-miss result. Extraction failure is a normal branch; half-correct structured data is worse than none because downstream code trusts it.
    5. Follow-ups: how to tell flakiness from a prompt bug? Failure rate — sporadic is flakiness, a stable failing input class is missing coverage and belongs in the test set. And does retrying inflate cost? Cap it and monitor the retry rate.

    答题要点

    • 三层:校验返回错误列表、带着错误原因重试一次、失败后返回空值并记录
    • 重试时必须把错误列表拼回用户消息,原样重发大概率同样的错
    • 重试一次足够,稳定失败说明模板或 schema 缺覆盖,该修模板不该继续重试
    • 降级不抛异常、不用半对的结果;监控重试率,稳定失败的输入加进测试集

    Key points

    • Three layers: validation returns an error list, one retry carries those errors back, then degrade to null and log
    • Retries must include the error list in the user message; verbatim resends reproduce the error
    • One retry is enough; persistent failure means the template or schema lacks coverage
    • Never throw on degradation or use near-miss output; monitor retry rate and add failing inputs to the test set