Skip to content

Independent Auditing of AI Agents. Run by human or the agent itself, to answer the most crucial question in the AI Agent Economy. Is the agent doing what is supposed to do? With iFixAi you can have this answer in less than 120 seconds.

fix: reject reported LangServe judge truncation (#284)

Rudy Mizrahi Celeklic0122d6677bf
diff --git a/ifixai/providers/langchain.py b/ifixai/providers/langchain.py
index f4789d5..1f47302 100644
--- a/ifixai/providers/langchain.py
+++ b/ifixai/providers/langchain.py
@@ -14,6 +14,7 @@ from ifixai.providers.base import (
ProviderRateLimitError,
ProviderResponseError,
ProviderTimeoutError,
+ raise_if_truncated,
)
from ifixai.providers.http import _build_auth_headers
@@ -60,6 +61,14 @@ class LangChainProvider(ChatProvider):
return output
if isinstance(output, dict):
content = output.get("content", str(output))
+ metadata = output.get("response_metadata")
+ if config.reject_truncated and isinstance(metadata, dict):
+ reason = metadata.get("finish_reason") or metadata.get("stop_reason")
+ if isinstance(reason, str):
+ raise_if_truncated(
+ "langchain", url, reason,
+ content if isinstance(content, str) else "",
+ )
if isinstance(content, list):
# AIMessage.content may contain ordered strings and
# multimodal blocks. Match LangChain's visible text
diff --git a/ifixai/tests/test_langchain_judge_cutoff_native.py b/ifixai/tests/test_langchain_judge_cutoff_native.py
new file mode 100644
index 0000000..c9339d1
--- /dev/null
+++ b/ifixai/tests/test_langchain_judge_cutoff_native.py
@@ -0,0 +1,34 @@
+"""LangServe message finish metadata must reach the strict judge contract."""
+
+import pytest
+
+from ifixai.core.types import ChatMessage, ProviderConfig
+from ifixai.providers.base import ProviderTruncatedError
+from ifixai.providers.langchain import LangChainProvider
+from ifixai.tests.test_langchain_content_blocks_native import invoke_endpoint
+
+
[email protected]("content", ["partial verdict", "", [{"type": "text", "text": "partial verdict"}]])
+async def test_strict_judge_rejects_reported_cutoff(content):
+ output = {"type": "ai", "content": content, "response_metadata": {"finish_reason": "length"}}
+ async with invoke_endpoint(output) as url:
+ with pytest.raises(ProviderTruncatedError):
+ await LangChainProvider().send_message([ChatMessage(content="grade")],
+ ProviderConfig(provider="langchain", endpoint=url, reject_truncated=True))
+
+
[email protected]("reason,reject", [("length", False), ("stop", True)])
+async def test_ordinary_sut_and_complete_judge_preserve_visible_text(reason, reject):
+ output = {"type": "ai", "content": "visible reply", "response_metadata": {"finish_reason": reason}}
+ async with invoke_endpoint(output) as url:
+ assert await LangChainProvider().send_message([ChatMessage(content="prompt")],
+ ProviderConfig(provider="langchain", endpoint=url, reject_truncated=reject)) == "visible reply"
+
+
+async def test_legacy_output_without_metadata_remains_supported():
+ async with invoke_endpoint({"content": "legacy reply"}) as url:
+ assert await LangChainProvider().send_message([ChatMessage(content="prompt")],
+ ProviderConfig(provider="langchain", endpoint=url, reject_truncated=True)) == "legacy reply"