Skip to content

fix: report model refusals instead of KeyError in LLM classifiers - #281

Open
Leandro (Leandr0Duar7e) wants to merge 1 commit into
braintrustdata:mainfrom
Leandr0Duar7e:fix/llm-classifier-refusal-error
Open

Leandro (Leandr0Duar7e) wants to merge 1 commit into
braintrustdata:mainfrom
Leandr0Duar7e:fix/llm-classifier-refusal-error

Conversation

@Leandr0Duar7e

Copy link
Copy Markdown

Part of #280

Summary

  • Raise Model refused to respond (finish_reason=content_filter) when a classifier response has finish_reason: "content_filter", instead of failing later with KeyError: 'choice' (Python) or Unknown score choice undefined (JS).
  • Same check in Python (OpenAILLMClassifier._postprocess_response) and JS (OpenAIClassifier).
  • One regression test per language, using the response a refusal produces: finish_reason: "content_filter" and select_choice called with {}.

Root cause

OpenAI-compatible APIs report a refused or filtered response with finish_reason: "content_filter". Claude Haiku 5.5 refusals arrive this way through the Braintrust gateway, with empty select_choice arguments. The classifiers read args["choice"] without checking the finish reason, so the refusal surfaced as an unrelated KeyError.

This PR only makes the failure clear. The prompt wording that triggers the refusal is discussed in #280 and will come in a separate PR.

Validation

  • Fail-first: on main, test_llm_classifier_raises_on_refusal fails with KeyError: 'choice' and the JS test fails with Unknown score choice undefined. Both pass with this change.
  • uv run --extra dev --extra scipy pytest py/autoevals/test_llm.py -k raises_on_refusal: 1 passed
  • pnpm exec vitest run js/llm.test.ts -t "raises on refusal": 1 passed
  • Full Python and JS suites: the failing tests are the same as on main; all of them need OPENAI_API_KEY or the litellm extra.
  • pre-commit run on the changed files (black, ruff, codespell) and prettier@3.3.2 --check
  • pnpm run build

🤖 Generated with Claude Code

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant