Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python

fix: handle unauthorized word being considered as an error (#2533)

Lucas Oliveira3df6f9397eb2
diff --git a/frontend/lib/chat-stream-errors.test.ts b/frontend/lib/chat-stream-errors.test.ts
index 7b8f410f6..2fd9daf45 100644
--- a/frontend/lib/chat-stream-errors.test.ts
+++ b/frontend/lib/chat-stream-errors.test.ts
@@ -93,6 +93,28 @@ describe("looksLikeProviderErrorContent", () => {
),
false,
);
+ assert.equal(
+ looksLikeProviderErrorContent(
+ "Unauthorized absence may be treated as disciplinary.",
+ ),
+ false,
+ );
+ assert.equal(
+ looksLikeProviderErrorContent(
+ "Unauthorized access to payroll records is prohibited.",
+ ),
+ false,
+ );
+ });
+
+ it("recognizes error-shaped unauthorized messages", () => {
+ assert.equal(looksLikeProviderErrorContent("Unauthorized"), true);
+ assert.equal(looksLikeProviderErrorContent("HTTP 401: Unauthorized"), true);
+ assert.equal(looksLikeProviderErrorContent("Error: unauthorized"), true);
+ assert.equal(
+ looksLikeProviderErrorContent("Unauthorized: Access token is expired"),
+ true,
+ );
});
it("flags IBM disabled API key messages", () => {
diff --git a/frontend/lib/chat-stream-errors.ts b/frontend/lib/chat-stream-errors.ts
index 768c6f06d..44b77abab 100644
--- a/frontend/lib/chat-stream-errors.ts
+++ b/frontend/lib/chat-stream-errors.ts
@@ -164,7 +164,9 @@ export function looksLikeProviderErrorContent(text: string): boolean {
}
// Tighter matches for phrases that also appear in ordinary explanations.
if (
- /(?:^|\berror\b[:\s]*|\b(?:401|403)\b[:\s]*)unauthorized\b/.test(lowered) ||
+ /(?:\berror\b[:\s]*|\b(?:401|403)\b[:\s]*)unauthorized\b|^\s*unauthorized\s*(?:[.!]?\s*$|:)/.test(
+ lowered,
+ ) ||
/(?:^|\berror\b[:\s]*)permission denied\b/.test(lowered) ||
/\bpermission denied\s*:/.test(lowered) ||
/(?:^|\berror\b[:\s]*)quota exceeded\b/.test(lowered) ||
diff --git a/src/api/provider_validation.py b/src/api/provider_validation.py
index b05bd664e..8d36e6fe1 100644
--- a/src/api/provider_validation.py
+++ b/src/api/provider_validation.py
@@ -54,8 +54,19 @@ _PROVIDER_CREDENTIAL_ERROR_MARKERS = (
"could not authenticate",
)
+# ``unauthorized`` is a reliable signal in provider exceptions, but ordinary
+# assistant replies can use it in policy language ("unauthorized absence" or
+# "unauthorized access"). Content needs an error-shaped use of the word.
+_UNAUTHORIZED_PROVIDER_ERROR_CONTENT_RE = re.compile(
+ r"(?:\berror\b[:\s]*|\b(?:401|403)\b[:\s]*)unauthorized\b"
+ r"|^\s*unauthorized\s*(?:[.!]?\s*$|:)",
+ re.IGNORECASE,
+)
+
-_PROVIDER_ERROR_CONTENT_MARKERS = _PROVIDER_CREDENTIAL_ERROR_MARKERS + (
+_PROVIDER_ERROR_CONTENT_MARKERS = tuple(
+ marker for marker in _PROVIDER_CREDENTIAL_ERROR_MARKERS if marker != "unauthorized"
+) + (
"rate limit",
"rate_limit",
"permission denied",
@@ -322,7 +333,9 @@ def looks_like_provider_error_content(text: str | None) -> bool:
if is_generic_upstream_error(content):
return True
lowered = content.lower()
- if any(marker in lowered for marker in _PROVIDER_ERROR_CONTENT_MARKERS):
+ if any(marker in lowered for marker in _PROVIDER_ERROR_CONTENT_MARKERS) or (
+ _UNAUTHORIZED_PROVIDER_ERROR_CONTENT_RE.search(lowered)
+ ):
return True
# Raw provider payloads often embed an error object: as JSON, or as the
# Python dict the OpenAI SDK prints (`Error code: 400 - {'error': {...}}`).
diff --git a/tests/unit/test_langflow_stream_provider_errors.py b/tests/unit/test_langflow_stream_provider_errors.py
index faa4ea1ba..15e9f3556 100644
--- a/tests/unit/test_langflow_stream_provider_errors.py
+++ b/tests/unit/test_langflow_stream_provider_errors.py
@@ -180,6 +180,99 @@ async def test_langflow_stream_yields_provider_error_chunk(
assert stored_id in agent_module.active_conversations.get(user_id, {})
+ "response_text",
+ [
+ "Unauthorized absence may be treated as disciplinary.",
+ "Unauthorized access to payroll records is prohibited.",
+ ],
+)
+async def test_langflow_stream_keeps_policy_text_with_unauthorized(
+ monkeypatch, store_in_memory, response_text
+):
+ """Ordinary policy language must not turn a successful turn into an error."""
+
+ async def stream_policy(*_args, **_kwargs) -> AsyncIterator[bytes]:
+ yield (
+ json.dumps(
+ {
+ "id": "resp-policy",
+ "delta": {"content": response_text},
+ }
+ )
+ + "\n"
+ ).encode("utf-8")
+
+ monkeypatch.setattr(agent_module, "async_stream", stream_policy)
+
+ user_id = "test-policy-text-user"
+ agent_module.active_conversations.pop(user_id, None)
+ chunks = await _collect_error_chunks(
+ agent_module.async_langflow_chat_stream(
+ langflow_client=object(),
+ flow_id="flow-id",
+ prompt="What does the leave policy say?",
+ user_id=user_id,
+ )
+ )
+
+ assert chunks == [
+ {
+ "id": "resp-policy",
+ "delta": {"content": response_text},
+ }
+ ]
+ assert not any(chunk.get("status") == "failed" for chunk in chunks)
+ assert (
+ agent_module.active_conversations[user_id]["resp-policy"]["messages"][-1]["error"]
+ is not True
+ )
+
+
+async def test_langflow_stream_marks_explanatory_unauthorized_as_error(
+ monkeypatch, store_in_memory
+):
+ response_text = "Unauthorized: Access token is expired"
+
+ async def stream_error_text(*_args, **_kwargs) -> AsyncIterator[bytes]:
+ yield (
+ json.dumps(
+ {
+ "id": "resp-unauthorized",
+ "delta": {"content": response_text},
+ }
+ )
+ + "\n"
+ ).encode("utf-8")
+
+ monkeypatch.setattr(agent_module, "async_stream", stream_error_text)
+
+ user_id = "test-unauthorized-error-user"
+ agent_module.active_conversations.pop(user_id, None)
+ chunks = await _collect_error_chunks(
+ agent_module.async_langflow_chat_stream(
+ langflow_client=object(),
+ flow_id="flow-id",
+ prompt="Why did this request fail?",
+ user_id=user_id,
+ )
+ )
+
+ assert chunks[-1] == {
+ "status": "failed",
+ "finish_reason": "error",
+ "error": {"message": response_text},
+ "id": "resp-unauthorized",
+ "response_id": "resp-unauthorized",
+ }
+ assert (
+ agent_module.active_conversations[user_id]["resp-unauthorized"]["messages"][-1]["error"]
+ is True
+ )
+
+
@pytest.mark.asyncio
async def test_langflow_stream_persists_error_onto_existing_thread(monkeypatch, store_in_memory):
provider_message = "Invalid API key for Anthropic. Check your credentials."
diff --git a/tests/unit/test_provider_error_formatting.py b/tests/unit/test_provider_error_formatting.py
index 77f9c7799..17dfd9c3a 100644
--- a/tests/unit/test_provider_error_formatting.py
+++ b/tests/unit/test_provider_error_formatting.py
@@ -60,6 +60,10 @@ def test_is_provider_credential_error():
assert is_provider_credential_error("Provided API key could not be found.")
assert is_provider_credential_error("Provided API key is disabled.")
assert is_provider_credential_error(json.dumps({"errorMessage": "api key revoked"}))
+ assert is_provider_credential_error("401, message='Unauthorized', url='http://x/v1/chat'")
+ assert is_provider_credential_error("Error code: 401 - Unauthorized")
+ assert is_provider_credential_error('{"detail":"Unauthorized"}')
+ assert is_provider_credential_error("litellm.AuthenticationError: Unauthorized")
assert not is_provider_credential_error("Rate limit exceeded")
@@ -95,6 +99,14 @@ def test_looks_like_provider_error_content():
'Failed to authenticate: {"errorMessage":"Provided API key could not be found."}'
)
assert not looks_like_provider_error_content("OpenRAG is an open-source package.")
+ assert not looks_like_provider_error_content(
+ "Unauthorized absence may be treated as disciplinary."
+ )
+ assert not looks_like_provider_error_content(
+ "Unauthorized access to payroll records is prohibited."
+ )
+ assert looks_like_provider_error_content("Error: unauthorized")
+ assert looks_like_provider_error_content("Unauthorized: Access token is expired")
assert is_generic_upstream_error("An unknown error occurred.")