{"record":{"id":"b9f8ecd78b98d71d","repo":"rohitg00/ai-engineering-from-scratch","slug":"messages-api-response-must-be-an-object","errorCode":null,"errorMessage":"Messages API response must be an object","messagePattern":"Messages API response must be an object","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"certifications/claude/lessons/30-developer-application-capstone/code/main.py","lineNumber":68,"sourceCode":"    def create_message(self, payload: dict[str, Any]) -> dict[str, Any]:\n        body = json.dumps(payload).encode(\"utf-8\")\n        request = urllib.request.Request(\n            self.endpoint,\n            data=body,\n            method=\"POST\",\n            headers={\n                \"content-type\": \"application/json\",\n                \"anthropic-version\": ANTHROPIC_VERSION,\n                \"x-api-key\": self._api_key,\n            },\n        )\n        try:\n            with self._opener(request, timeout=self.timeout_seconds) as response:\n                decoded = json.loads(response.read().decode(\"utf-8\"))\n        except urllib.error.HTTPError as error:\n            raise RuntimeError(f\"Messages API returned HTTP {error.code}\") from None\n        if not isinstance(decoded, dict):\n            raise ValueError(\"Messages API response must be an object\")\n        return decoded\n\n\ndef build_live_order_request(request: str, model: str) -> dict[str, Any]:\n    \"\"\"Build an inspectable wire payload with the same bounded lookup contract.\"\"\"\n    return {\n        \"model\": model,\n        \"max_tokens\": 128,\n        \"system\": (\n            \"You are a bounded order-status router. Use lookup_order only when \"\n            \"the user supplies an exact public order ID. Never claim execution.\"\n        ),\n        \"messages\": [{\"role\": \"user\", \"content\": request}],\n        \"tools\": [\n            {\n                \"name\": \"lookup_order\",\n                \"description\": \"Read one visible order status. Never changes order state.\",\n                \"input_schema\": {","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/certifications/claude/lessons/30-developer-application-capstone/code/main.py#L50-L86","documentation":"Raised in AnthropicMessagesHTTPTransport.create_message when the HTTP 200 response body decodes to something other than a JSON object. The Messages API contract requires a top-level object (content, stop_reason, usage), so a list, string, or scalar body means the response is not a valid Messages reply.","triggerScenarios":"Thrown at certifications/claude/lessons/30-developer-application-capstone/code/main.py:68 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Confirm the endpoint actually serves the Messages API and is not returning a JSON array or plain text","Check for intercepting proxies or gateways that rewrap responses","Assert the response shape in tests with a fake transport returning a well-formed message object"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}