Skip to content

CCDV-F · Domain 6: Prompt and Context Engineering · 15% of the exam

Task 6.3: Structured Output and Defensive Parsing

Strict schemas for structured outputs, validating responses, and parsing free-text fallbacks defensively so downstream code never receives malformed or unexpected output.

Study it

  • Structured outputs, response validation and defensive parsing

    Lesson coming

Sample questions

Try each one before opening the answer. Every option is explained, with the Anthropic documentation page that proves it.

Question 1 · choose 1

Your structured-outputs request uses this Pydantic model through the Python SDK:class Scorecard(BaseModel): vendor: str scores: dict[str, float] # criterion name -> scoreEvery response parses, but scores is always {}. The criteria differ from vendor to vendor, so their names are not known in advance. What should you change?

  1. AAdd additionalProperties: true to the scores object so that Claude may add keys
  2. BMark scores as required so that Claude has to fill it with values
  3. CDescribe the expected criteria in the field's description so that Claude knows which keys to return
  4. DMake scores a list of items, each with a key and a value field
Show the answer and why
  • AAdd additionalProperties: true to the scores object so that Claude may add keys

    Incorrect

    Structured outputs require additionalProperties to be false on every object.

  • BMark scores as required so that Claude has to fill it with values

    Incorrect

    A required object with no listed keys can still only be returned as {}.

  • CDescribe the expected criteria in the field's description so that Claude knows which keys to return

    Incorrect

    Claude can only return the keys the schema lists, whatever the description says.

  • DMake scores a list of items, each with a key and a value field

    Correct

    When keys are not known in advance, an array of key and value items replaces the map.

Because additionalProperties must be false, Claude can only return the keys a schema lists. A dict lists no keys, so the SDK sends it as an object with empty properties and Claude can only return {}. For unknown keys, use an array whose items carry a key and a value.

Question 2 · choose 1

Your extraction service sends requests that use structured outputs with a JSON schema and parses the reply like this:resp = client.messages.create(**request) data = json.loads(resp.content[0].text)It worked for months. After you changed only the model field to a current Claude model, the parsing line fails on every response, while the responses' stop_reason is "end_turn". What fix keeps the parsing robust?

  1. AParse the first content block whose type is "text" instead of the block at index 0
  2. BAdd the fields of the block at index 0 to the schema so it matches too
  3. CRetry the request until the block at index 0 holds valid JSON, since constrained output still fails at times
  4. DDrop the schema and ask for JSON in the prompt, so that the reply comes back as one block
Show the answer and why
  • AParse the first content block whose type is "text" instead of the block at index 0

    Correct

    The schema-valid JSON is in the text block; the block now at index 0 is not part of the constrained output.

  • BAdd the fields of the block at index 0 to the schema so it matches too

    Incorrect

    The schema constrains only Claude's direct output, so no schema change reaches the block at index 0.

  • CRetry the request until the block at index 0 holds valid JSON, since constrained output still fails at times

    Incorrect

    The first block is a thinking block by design, so retrying returns the same order.

  • DDrop the schema and ask for JSON in the prompt, so that the reply comes back as one block

    Incorrect

    That gives up the schema guarantee and does not change which block comes first.

Thinking is on by default on current models, and thinking blocks arrive before the text block. The grammar of structured outputs applies only to Claude's direct output, not to thinking, so the schema-valid JSON is in the text block. Select blocks by type rather than by position.

Question 3 · choose 1

Your structured-outputs schema includes a step_by_step_reasoning property so that reviewers can see how Claude reached each verdict. Some responses come back as refusals instead of matching the schema. What does the structured outputs documentation suggest?

  1. AReplace the property with one that asks for a short explanation
  2. BMake the property optional so that Claude can skip it
  3. CMove the property to the end of the schema so that it is generated last
  4. DRetry the refused requests until one matches the schema
Show the answer and why
  • AReplace the property with one that asks for a short explanation

    Correct

    A property asking for step-by-step reasoning might lead to a reasoning_extraction refusal.

  • BMake the property optional so that Claude can skip it

    Incorrect

    The issue is what the property asks for, not whether it is required.

  • CMove the property to the end of the schema so that it is generated last

    Incorrect

    The property's position does not change what it asks for.

  • DRetry the refused requests until one matches the schema

    Incorrect

    The guidance changes the property instead of retrying.

A schema property that asks for the model's thinking or step-by-step reasoning might lead to a reasoning_extraction refusal. Ask for a short explanation instead and keep reasoning in thinking blocks.

Question 4 · choose 1

Your TypeScript Claude Agent SDK app asks for structured output with these options:options: { outputFormat: { type: "json_schema", schema: { type: "object", properties: { name: { type: "string" }, email: { type: "string", format: "email" } }, required: ["name", "email"] } } }Runs end with subtype success and a structured_output, but some email values are not email addresses. What should you do?

  1. AFix the schema, because it is not valid JSON Schema and the SDK ignored it and returned free text
  2. BMake email optional, because the SDK checks the format keyword only on properties that aren't required
  3. CValidate the field in your own code, because the SDK accepts format as an annotation without enforcing it
  4. DRemove the format keyword, because any schema that contains it is treated as invalid by the SDK
Show the answer and why
  • AFix the schema, because it is not valid JSON Schema and the SDK ignored it and returned free text

    Incorrect

    A schema that isn't valid JSON Schema fails the run at startup, and these runs returned structured_output.

  • BMake email optional, because the SDK checks the format keyword only on properties that aren't required

    Incorrect

    The SDK's validator doesn't enforce format at all, whether the property is required or not.

  • CValidate the field in your own code, because the SDK accepts format as an annotation without enforcing it

    Correct

    The format keyword is accepted as an annotation and isn't enforced by the SDK's validator.

  • DRemove the format keyword, because any schema that contains it is treated as invalid by the SDK

    Incorrect

    Current versions accept format as an annotation; the rest of the schema is still validated.

Pass a JSON Schema in outputFormat and the result message carries validated data in structured_output. The format keyword, such as "format": "email", is accepted as an annotation and isn't enforced by the SDK's validator, so check such values in your own code. A schema that isn't valid JSON Schema fails the run at startup with an error naming the problem.

Practice domain 6 →Practice all domains →