Skip to content

fix(harness): read OpenAI refusal field and content-part arrays - #402

Merged
msoedov merged 1 commit into
msoedov:mainfrom
sarankumar1325:fix/harness-openai-refusal-and-content-parts
Nov 26, 2026
Merged

msoedov merged 1 commit into
msoedov:mainfrom
sarankumar1325:fix/harness-openai-refusal-and-content-parts

Conversation

@sarankumar1325

Copy link
Copy Markdown
Contributor

Problem

extract_response in the harness only accepts a string choices[0].message.content. Two common OpenAI-compatible response shapes are rejected with ValueError("Expected text content in response"):

  • OpenAI refusals: when a model refuses, OpenAI can return "content": null with the refusal text in message.refusal.
  • Content-part arrays: several OpenAI-compatible providers/proxies return content as a list such as [{"type": "text", "text": "..."}].

attempt() catches the exception and records the attempt as error, so result_code returns 2. In a security harness that is the worst case: an explicit refusal gets reported as a broken target instead of being classified.

Fix

A small _message_text helper:

  • returns content when it is a string (unchanged);
  • joins the text of text / output_text parts when content is a list;
  • returns message.refusal when content is null and refusal is a non-empty string.

Anything else still raises, so the malformed-choices tests from #400 keep passing. That includes {"content": None} with no refusal, empty or non-text part lists, and non-string content.

Tests

New cases in tests/harness/test_response_parsing.py cover the refusal field, the content-part join, and the shapes that must still be rejected. pytest tests/harness: 75 passed. Without the loop.py change, the two new positive tests fail. black is clean, and ruff reports the same findings before and after the change (all pre-existing).

AI-assisted change. I reviewed it and ran the tests locally.

extract_response only accepted a string message.content. OpenAI returns
content: null with the model's refusal text in message.refusal, and
several OpenAI-compatible providers return content as a list of parts.
Both shapes raised ValueError, so the attempt was recorded as an error
(exit code 2) instead of being classified - a refusal was reported as a
broken target.

Accept the refusal text when content is null, and join the text of
text/output_text parts. Messages with no usable text still raise.

Signed-off-by: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com>

@msoedov msoedov left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, appreciate the patch

@msoedov
msoedov merged commit 9abd77d into msoedov:main Nov 26, 2026
2 checks passed
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.

2 participants