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

Messages API returned HTTP {error.code}

Error message

Messages API returned HTTP {error.code}

What it means

Error "Messages API returned HTTP {error.code}" thrown in rohitg00/ai-engineering-from-scratch.

Source

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

        self._opener = opener

    def create_message(self, payload: dict[str, Any]) -> dict[str, Any]:
        body = json.dumps(payload).encode("utf-8")
        request = urllib.request.Request(
            self.endpoint,
            data=body,
            method="POST",
            headers={
                "content-type": "application/json",
                "anthropic-version": ANTHROPIC_VERSION,
                "x-api-key": self._api_key,
            },
        )
        try:
            with self._opener(request, timeout=self.timeout_seconds) as response:
                decoded = json.loads(response.read().decode("utf-8"))
        except urllib.error.HTTPError as error:
            raise RuntimeError(f"Messages API returned HTTP {error.code}") from None
        if not isinstance(decoded, dict):
            raise ValueError("Messages API response must be an object")
        return decoded


def build_live_order_request(request: str, model: str) -> dict[str, Any]:
    """Build an inspectable wire payload with the same bounded lookup contract."""
    return {
        "model": model,
        "max_tokens": 128,
        "system": (
            "You are a bounded order-status router. Use lookup_order only when "
            "the user supplies an exact public order ID. Never claim execution."
        ),
        "messages": [{"role": "user", "content": request}],
        "tools": [
            {
                "name": "lookup_order",

View on GitHub (pinned to 39ea8a1c6d)

When it happens

Trigger: Thrown at certifications/claude/lessons/30-developer-application-capstone/code/main.py:66 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/a05301ec74addc9d. Report an issue: GitHub.