Skip to content

Build AI methods with your coding agent, then run them anywhere: as an MCP for chatbots, as a webapp for people, or via API for your software.

fix/Native-client-gateway-message · L-261004-34f05e (#1336)

Louis Choquel964bb22b10ab
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 04a13e3d0..e16d3a483 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,6 +17,7 @@
### Fixed
+- **A Manifold failure on the extract and search routes reports the service's own message**: `extract_manifold_metadata` takes the message from the response body's `error.message`, or else its top-level `message`, so a refused key now reads `Portkey Error: Invalid API Key. Error Code: 03` rather than httpx's `Client error '401 Unauthorized' for url …`; a body carrying neither, a body that is not JSON and a network failure keep the exception's own text. Because the quota check reads that message, a 429 on those routes whose body names a quota, such as `Monthly quota exceeded`, now classifies as quota exhaustion, to be fixed in billing, rather than as a transient rate limit to retry.
- **A kernel-only boot over a stale configuration file stays kernel-only (Breaking)**: the boot-tolerance replay no longer loads interpreter modules into a `RuntimeBoot` process, because the fix-op applier, the file transaction and their errors moved from `pipelex.pipeline` to the new kernel-layer `pipelex.fix_ops` package; import `FixWriteConflictError` and `FixTransactionError` from `pipelex.fix_ops.exceptions`, and the applier and transaction from `pipelex.fix_ops.applier` and `pipelex.fix_ops.file_transaction`.
### Removed
diff --git a/pipelex/providers/manifold/manifold_error_metadata.py b/pipelex/providers/manifold/manifold_error_metadata.py
index 4aa44732d..faf6a1781 100644
--- a/pipelex/providers/manifold/manifold_error_metadata.py
+++ b/pipelex/providers/manifold/manifold_error_metadata.py
@@ -8,7 +8,7 @@ here, beside the client that raises them.
from __future__ import annotations
import json
-from typing import Any
+from typing import Any, cast
from pipelex.cogt.inference.error_classification import ProviderErrorMetadata, error_code_from_body_code_first, parse_retry_after_seconds
from pipelex.cogt.inference.provider_name import ProviderName
@@ -51,6 +51,12 @@ def extract_manifold_metadata(exc: BaseException) -> ProviderErrorMetadata:
answers that carried none — under ``MANIFOLD_TRACE_ID_HEADER`` before the inherited vendor
spelling, which holds the same value and is only still read because the gateway still emits it.
+ **The message is the service's own when its body carries one.** ``str(exc)`` on an
+ ``httpx.HTTPStatusError`` is httpx's generic sentence (``Client error '401 Unauthorized' for url
+ …``), which says nothing about why the service refused, so the body's ``error.message`` is read
+ first, then its top-level ``message``; ``str(exc)`` stays the fallback for a body without
+ either, for a body that is not JSON and for an ``httpx.RequestError``, which has no body at all.
+
**It reports ``ProviderName.GATEWAY``**, and that is a decision rather than an oversight: the
service is a gateway, it phrases quota exhaustion and rate limiting the way the gateway
substrate does, and every ``match`` on ``ProviderName`` would need a second arm with the same
@@ -82,10 +88,32 @@ def extract_manifold_metadata(exc: BaseException) -> ProviderErrorMetadata:
return ProviderErrorMetadata(
provider=ProviderName.GATEWAY,
sdk_exception_type=type(exc).__name__,
- message=str(exc),
+ message=_message_from_body(body=body) or str(exc),
status_code=status_code,
request_id=request_id,
retry_after_seconds=retry_after_seconds,
provider_error_code=provider_error_code,
body=body,
)
+
+
+def _message_from_body(*, body: Any) -> str | None:
+ """The service's own explanation of a failure, read from its JSON body, if it gave one.
+
+ The nested ``error.message`` comes first, since it is the shape both the gateway and the native
+ routes' own refusals render; the top-level ``message`` the gateway also stamps is the fallback.
+ Only a non-empty string counts, so a ``null`` or a blank message falls through to the next.
+ """
+ if not isinstance(body, dict):
+ return None
+ body_dict = cast("dict[str, Any]", body)
+ sections: list[dict[str, Any]] = []
+ error_section = body_dict.get("error")
+ if isinstance(error_section, dict):
+ sections.append(cast("dict[str, Any]", error_section))
+ sections.append(body_dict)
+ for section in sections:
+ candidate = section.get("message")
+ if isinstance(candidate, str) and candidate.strip():
+ return candidate
+ return None
diff --git a/tests/unit/pipelex/providers/manifold/test_manifold_error_message.py b/tests/unit/pipelex/providers/manifold/test_manifold_error_message.py
new file mode 100644
index 000000000..f62284bbd
--- /dev/null
+++ b/tests/unit/pipelex/providers/manifold/test_manifold_error_message.py
@@ -0,0 +1,109 @@
+"""Which message `extract_manifold_metadata` reports for a failure on the manifold native routes.
+
+`str()` of an `httpx.HTTPStatusError` is httpx's generic sentence, `Client error '401 Unauthorized'
+for url …`, which never says why the service refused. The gateway and the native routes explain
+themselves in the JSON body instead, so the message is read from there — the nested
+`error.message` first, the top-level `message` second — and `str(exc)` is only the fallback for a
+failure whose body carries neither.
+"""
+
+from __future__ import annotations
+
+from typing import Any
+
+import httpx
+import pytest
+
+from pipelex.providers.manifold.manifold_error_metadata import extract_manifold_metadata
+
+_URL = "https://manifold.example.com/v1/pipelex/extract"
+_HTTPX_SENTENCE = f"Client error '401 Unauthorized' for url '{_URL}'"
+_GATEWAY_MESSAGE = "Portkey Error: Invalid API Key. Error Code: 03"
+
+
+def _status_error(*, json_body: Any | None = None, text_body: str | None = None, status_code: int = 401) -> httpx.HTTPStatusError:
+ """A native-route refusal as the manifold client raises it: `raise_for_status()` on a plain httpx response."""
+ request = httpx.Request("POST", _URL)
+ if json_body is not None:
+ response = httpx.Response(status_code=status_code, request=request, json=json_body)
+ else:
+ response = httpx.Response(status_code=status_code, request=request, text=text_body or "")
+ return httpx.HTTPStatusError(_HTTPX_SENTENCE, request=request, response=response)
+
+
+class TestTheMessageTheManifoldPathReports:
+ def test_reports_the_gateways_own_message(self) -> None:
+ """The body the gateway sends for a refused key, verbatim — its explanation is what reaches the user."""
+ body = {"status": "failure", "message": _GATEWAY_MESSAGE, "error": {"message": _GATEWAY_MESSAGE, "code": "03"}}
+
+ metadata = extract_manifold_metadata(_status_error(json_body=body))
+
+ assert metadata.message == _GATEWAY_MESSAGE
+ assert metadata.provider_error_code == "03"
+ assert metadata.status_code == 401
+
+ def test_prefers_the_nested_error_message_over_the_top_level_one(self) -> None:
+ body = {"message": "top-level", "error": {"message": "nested", "code": "pig-01"}}
+
+ metadata = extract_manifold_metadata(_status_error(json_body=body))
+
+ assert metadata.message == "nested"
+
+ def test_reads_the_top_level_message_when_the_error_object_has_none(self) -> None:
+ body = {"message": "top-level", "error": {"code": "pig-01"}}
+
+ metadata = extract_manifold_metadata(_status_error(json_body=body))
+
+ assert metadata.message == "top-level"
+
+ @pytest.mark.parametrize(
+ ("topic", "json_body"),
+ [
+ ("no message anywhere", {"error": {"code": "pig-01"}}),
+ ("a blank nested message and no top-level one", {"error": {"message": " "}}),
+ ("a null message", {"message": None}),
+ ("a message that is not a string", {"error": {"message": 42}, "message": ["a"]}),
+ ("a body that is not an object", ["unexpected"]),
+ ],
+ )
+ def test_falls_back_to_the_exception_text_when_the_body_explains_nothing(self, topic: str, json_body: Any) -> None:
+ metadata = extract_manifold_metadata(_status_error(json_body=json_body))
+
+ assert metadata.message == _HTTPX_SENTENCE, topic
+
+ def test_falls_back_to_the_exception_text_when_the_body_is_not_json(self) -> None:
+ metadata = extract_manifold_metadata(_status_error(text_body="<html>Unauthorized</html>"))
+
+ assert metadata.message == _HTTPX_SENTENCE
+ assert metadata.body is None
+
+ def test_a_request_error_keeps_its_own_text(self) -> None:
+ """A connect or timeout failure has no response, hence no body to read a message from."""
+ exc = httpx.ConnectError("connection refused", request=httpx.Request("POST", _URL))
+
+ metadata = extract_manifold_metadata(exc)
+
+ assert metadata.message == "connection refused"
+ assert metadata.status_code is None
+
+
+class TestTheQuotaReadingOfANativeRoute429:
+ """The quota check reads `metadata.message`, so on the native routes it now sees the service's words.
+
+ Before, it saw httpx's sentence, which never names a quota, so every 429 there read as a
+ transient rate limit to retry, even one the service explained as an exhausted quota.
+ """
+
+ def test_a_429_whose_body_names_a_quota_is_quota_exhaustion(self) -> None:
+ body = {"error": {"message": "Monthly quota exceeded for this workspace"}}
+
+ metadata = extract_manifold_metadata(_status_error(json_body=body, status_code=429))
+
+ assert metadata.is_quota_exhaustion
+
+ def test_a_429_whose_body_names_no_quota_stays_a_rate_limit(self) -> None:
+ body = {"error": {"message": "Too many requests, slow down"}}
+
+ metadata = extract_manifold_metadata(_status_error(json_body=body, status_code=429))
+
+ assert not metadata.is_quota_exhaustion