From fead31af6a6071b96bcdac1740a33d13b2af93a1 Mon Sep 17 00:00:00 2001 From: Leandr0Duar7e Date: Wed, 25 Nov 2026 18:59:07 +0100 Subject: [PATCH] fix: report model refusals instead of KeyError in LLM classifiers --- js/llm.test.ts | 47 ++++++++++++++++++++++++++++++++++++++++ js/llm.ts | 5 +++++ py/autoevals/llm.py | 2 ++ py/autoevals/test_llm.py | 34 +++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+) diff --git a/js/llm.test.ts b/js/llm.test.ts index d5d4fc0..976e47a 100644 --- a/js/llm.test.ts +++ b/js/llm.test.ts @@ -6,6 +6,7 @@ import { afterAll, afterEach, beforeAll, describe, expect, test } from "vitest"; import { Battle, buildClassificationTools, + ClosedQA, DEFAULT_MODEL, LLMClassifierFromTemplate, OpenAIClassifier, @@ -666,4 +667,50 @@ Issue Description: {{page_content}} // Reset for other tests init({ client }); }); + + test("llm classifier raises on refusal", async () => { + server.use( + http.post("https://api.openai.com/v1/chat/completions", () => + HttpResponse.json({ + id: "chatcmpl-test", + object: "chat.completion", + created: 1234567890, + model: "claude-haiku-5-5", + choices: [ + { + index: 0, + message: { + role: "assistant", + content: null, + tool_calls: [ + { + id: "call_test", + type: "function", + function: { name: "select_choice", arguments: "{}" }, + }, + ], + }, + finish_reason: "content_filter", + }, + ], + }), + ), + ); + + init({ + client: new OpenAI({ + apiKey: "test-api-key", + baseURL: "https://api.openai.com/v1", + }), + }); + + await expect( + ClosedQA({ + input: "Is 2+2 four?", + output: "Yes, 4.", + criteria: "The answer says 4.", + model: "claude-haiku-5-5", + }), + ).rejects.toThrow("finish_reason=content_filter"); + }); }); diff --git a/js/llm.ts b/js/llm.ts index 4a17115..cf54791 100644 --- a/js/llm.ts +++ b/js/llm.ts @@ -247,6 +247,11 @@ export async function OpenAIClassifier( ); if (resp.choices.length > 0) { + if (resp.choices[0].finish_reason === "content_filter") { + throw new Error( + "Model refused to respond (finish_reason=content_filter)", + ); + } return { name, ...parseResponse(resp.choices[0].message!, choiceScores), diff --git a/py/autoevals/llm.py b/py/autoevals/llm.py index 6ab0745..0ed8d31 100644 --- a/py/autoevals/llm.py +++ b/py/autoevals/llm.py @@ -280,6 +280,8 @@ def _process_response(self, resp): def _postprocess_response(self, resp): if len(resp["choices"]) > 0: + if resp["choices"][0].get("finish_reason") == "content_filter": + raise ValueError("Model refused to respond (finish_reason=content_filter)") return self._process_response(resp["choices"][0]["message"]) else: raise ValueError("Empty response from OpenAI") diff --git a/py/autoevals/test_llm.py b/py/autoevals/test_llm.py index fe1b4ae..0567d75 100644 --- a/py/autoevals/test_llm.py +++ b/py/autoevals/test_llm.py @@ -12,6 +12,7 @@ from autoevals.llm import ( DEFAULT_MODEL, Battle, + ClosedQA, Factuality, LLMClassifier, OpenAILLMClassifier, @@ -792,3 +793,36 @@ async def get_thread(self): classifier.eval(output="x", expected="y", trace=trace) assert trace.calls == 0 + + +@respx.mock +def test_llm_classifier_raises_on_refusal(): + respx.route(method="POST", path__regex=r".*/chat/completions$").respond( + json={ + "id": "chatcmpl-test", + "object": "chat.completion", + "created": 1734029028, + "model": "claude-haiku-5-5", + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "content": None, + "tool_calls": [ + { + "id": "call_test", + "type": "function", + "function": {"name": "select_choice", "arguments": "{}"}, + } + ], + }, + "finish_reason": "content_filter", + } + ], + } + ) + + llm = ClosedQA(model="claude-haiku-5-5", client=OpenAI(api_key="test")) + with pytest.raises(ValueError, match="finish_reason=content_filter"): + llm.eval(input="Is 2+2 four?", output="Yes, 4.", criteria="The answer says 4.")