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(evaluation): reject truncated atomic and classifier verdicts (#181)

Rudy Mizrahi Celekli3e96aa5d6d9a
diff --git a/ifixai/evaluation/atomic_claims.py b/ifixai/evaluation/atomic_claims.py
index 5ccd539..b6fe4d3 100644
--- a/ifixai/evaluation/atomic_claims.py
+++ b/ifixai/evaluation/atomic_claims.py
@@ -258,7 +258,7 @@ async def _send_atomic(
``asyncio.TimeoutError`` on deadline expiry.
"""
capped_config = evaluator._provider_config.model_copy(
- update={"max_tokens": _ATOMIC_MAX_TOKENS}
+ update={"max_tokens": _ATOMIC_MAX_TOKENS, "reject_truncated": True}
)
return await asyncio.wait_for(
evaluator._provider.send_message(messages, capped_config),
diff --git a/ifixai/evaluation/response_classifier.py b/ifixai/evaluation/response_classifier.py
index e454891..08549b8 100644
--- a/ifixai/evaluation/response_classifier.py
+++ b/ifixai/evaluation/response_classifier.py
@@ -150,6 +150,6 @@ async def classify_response(
]
# json_output: judge-only, forces a parseable {"class": ...} verdict (see ProviderConfig).
raw = await judge_provider.send_message(
- messages, judge_config.model_copy(update={"json_output": True})
+ messages, judge_config.model_copy(update={"json_output": True, "reject_truncated": True})
)
return _parse_json(raw)
diff --git a/ifixai/tests/evaluation/test_judge_cutoff_consumers.py b/ifixai/tests/evaluation/test_judge_cutoff_consumers.py
new file mode 100644
index 0000000..918e9c0
--- /dev/null
+++ b/ifixai/tests/evaluation/test_judge_cutoff_consumers.py
@@ -0,0 +1,92 @@
+from contextlib import asynccontextmanager
+
+import pytest
+from aiohttp import web
+
+from ifixai.core.types import ExpectedClaim, ProviderConfig
+from ifixai.evaluation.atomic_claims import (
+ score_atomic_claims,
+ score_atomic_claims_with_ground_truth,
+)
+from ifixai.evaluation.response_classifier import ResponseClass, classify_response
+from ifixai.judge.config import JudgeConfig
+from ifixai.judge.evaluator import JudgeEvaluator
+from ifixai.providers.base import ProviderTruncatedError
+from ifixai.providers.http import HttpProvider
+
+
+@asynccontextmanager
+async def local_judge(content, finish_reason):
+ async def reply(request):
+ await request.json()
+ return web.json_response({'choices': [{'message': {'content': content}, 'finish_reason': finish_reason}]})
+ app = web.Application()
+ app.router.add_post('/v1/chat/completions', reply)
+ runner = web.AppRunner(app)
+ await runner.setup()
+ site = web.TCPSite(runner, '127.0.0.1', 0)
+ await site.start()
+ endpoint = f'http://127.0.0.1:{site._server.sockets[0].getsockname()[1]}/v1'
+ try:
+ yield endpoint
+ finally:
+ await runner.cleanup()
+
+
[email protected]('finish_reason', ['length', 'stop'])
+async def test_atomic_consumer_rejects_native_http_cutoff(finish_reason):
+ # A missing closing array/object is recoverable by json-repair, but a
+ # transport-reported token cutoff is not a complete judge verdict.
+ content = '[{"claim":"Refunds require a receipt","supported":true,"reason":"receipt matches"'
+ if finish_reason == 'stop':
+ content += '}]'
+ async with local_judge(content, finish_reason) as endpoint:
+ judge = JudgeEvaluator(JudgeConfig(provider='http', endpoint=endpoint))
+ try:
+ result = await score_atomic_claims('A receipt is required.', 'Receipt required.', 'grounding', judge)
+ if finish_reason == 'length':
+ assert result.error and result.total == 0
+ else:
+ assert not result.error and result.total == 1 and result.score == 1.0
+ finally:
+ await judge.aclose()
+
+
[email protected]('finish_reason', ['length', 'stop'])
+async def test_ground_truth_consumer_rejects_native_http_cutoff(finish_reason):
+ claim = ExpectedClaim(claim='Refunds require a receipt', supported=True, source_id='policy', evidence='Receipt required.')
+ content = '[{"claim":"Refunds require a receipt","response_correct":true,"reason":"receipt matches"'
+ if finish_reason == 'stop':
+ content += '}]'
+ async with local_judge(content, finish_reason) as endpoint:
+ judge = JudgeEvaluator(JudgeConfig(provider='http', endpoint=endpoint))
+ try:
+ result = await score_atomic_claims_with_ground_truth('A receipt is required.', [claim], judge)
+ if finish_reason == 'length':
+ assert result.error and result.total == 0
+ else:
+ assert not result.error and result.total == 1 and result.score == 1.0
+ finally:
+ await judge.aclose()
+
+
[email protected]('finish_reason', ['length', 'stop'])
+async def test_classifier_consumer_rejects_native_http_cutoff_but_sut_keeps_text(finish_reason):
+ content = '{"class":"answer"}'
+ async with local_judge(content, finish_reason) as endpoint:
+ provider = HttpProvider()
+ config = ProviderConfig(provider='http', endpoint=endpoint, max_retries=0)
+ try:
+ if finish_reason == 'length':
+ with pytest.raises(ProviderTruncatedError):
+ await classify_response('A receipt is required.', 'Receipt policy?', provider, config)
+ else:
+ assert await classify_response('A receipt is required.', 'Receipt policy?', provider, config) is ResponseClass.ANSWER
+ # Judge-only overrides must not mutate the SUT configuration.
+ assert await provider.send_message([], config) == content
+ assert config.reject_truncated is False
+ finally:
+ await provider.aclose()