rohitg00/ai-engineering-from-scratch · error · ValueError

unexpected Messages API response contract

Error message

unexpected Messages API response contract

What it means

Raised in run_live_wire when the transport's response is missing required Messages API members (content blocks, model/role fields, stop_reason) so the reply cannot be mapped onto the wire contract. It protects the demo agent from proceeding on an unrecognized response shape.

Solutions

  1. Compare the actual response keys against the expected Messages schema and fix the producer or fake
  2. Update the contract check if the API legitimately added/renamed response fields
  3. Return a complete response object (role, content list, stop_reason, model) from test transports
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at certifications/claude/lessons/30-developer-application-capstone/code/main.py:102 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26). Data as JSON: /api/errors/252995ae0f904749. Report an issue: GitHub.

Appendix: source

Thrown at certifications/claude/lessons/30-developer-application-capstone/code/main.py:102

                "name": "lookup_order",
                "description": "Read one visible order status. Never changes order state.",
                "input_schema": {
                    "type": "object",
                    "properties": {"order_id": {"type": "string", "pattern": "^[A-Z]-[0-9]{2,6}$"}},
                    "required": ["order_id"],
                    "additionalProperties": False,
                },
            }
        ],
    }


def run_live_wire(transport: MessagesTransport, request: str, model: str) -> dict[str, Any]:
    """Exercise the real serialization boundary and return only safe metadata."""
    response = transport.create_message(build_live_order_request(request, model))
    content = response.get("content")
    if response.get("type") != "message" or response.get("role") != "assistant" or not isinstance(content, list):
        raise ValueError("unexpected Messages API response contract")
    content_types = [block.get("type") for block in content if isinstance(block, dict)]
    return {
        "type": response["type"],
        "role": response["role"],
        "stop_reason": response.get("stop_reason"),
        "content_types": content_types,
        "message_id_present": isinstance(response.get("id"), str),
    }


@dataclass(frozen=True)
class TraceEvent:
    type: str
    detail: dict[str, Any]


@dataclass(frozen=True)
class SupportResult:

View on GitHub (pinned to 39ea8a1c6d)