Skip to content

fix: read LLMComparator verdict from text blocks only - #645

Open
pcbeingused33 wants to merge 8 commits into
awslabs:devfrom
pcbeingused33:bugfix/llm-comparator-reasoning-block
Open

pcbeingused33 wants to merge 8 commits into
awslabs:devfrom
pcbeingused33:bugfix/llm-comparator-reasoning-block

Conversation

@pcbeingused33

@pcbeingused33 pcbeingused33 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Issue Reference

No tracking issue.

Description of Changes

LLMComparator raised KeyError: 'text' when a reply did not start with a text block, which is what happens with a reasoning model that sends reasoningContent first.

Changes Made

  • _invoke_agent joins the reply's text blocks instead of reading content[0]["text"]
  • A reply with no text block still raises, now a ValueError that says so
  • The LLM-as-a-Judge guide says only text blocks are read, and that a reply with none raises ValueError from compare() and from compare_with()
  • Regression tests pin that ValueError, including a reasoning-only reply, and CHANGELOG.md records the fix

Why These Changes Are Needed

The verdict is in a later block, so the comparison failed although the model answered. compare re-raises the error, so the caller gets no score. get_comparison_details goes through the same method and returned {"error": "'text'", ...}.

Reproduction

A real strands.Agent with a stub Model that streams a reasoningContent block and then the text true:

LLMComparator(model=ReasoningModel()).compare("St.", "Street")

Before: KeyError: 'text'. After: 1.0. No call to a hosted model was made.

Testing

  • Unit tests added/updated
  • Integration tests added/updated
  • Manual testing completed
  • All existing tests pass

The new test fails on dev with KeyError: 'text' and passes with the fix. The three no-text cases fail while the reply is still read as the first block's text key and pass once text blocks are joined. tests/common/comparators/test_llm.py passes (67 tests) and ruff check is clean on the comparator and that file. The full test suite was not run.

Checklist

  • Code follows the project's coding standards
  • CHANGELOG.md entry added under [Unreleased]
  • Tests added/updated for new functionality

Additional Notes

This change was prepared with AI assistance.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@vawsgit vawsgit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ LGTM!

content = result.message["content"]
texts = [block["text"] for block in content if "text" in block]
if not texts:
raise ValueError("LLM reply has no text content block")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Optional: the new ValueError branch has no test. One case with content=[{"reasoningContent": {...}}] (or []) that asserts pytest.raises(ValueError, match="no text content block") on compare(), and {"error": "LLM reply has no text content block", "comparison_result": False} from get_comparison_details(), would pin it. Measured on the PR head with a stub strands Model: both behave that way today.

@digalaviz

Copy link
Copy Markdown
Contributor

Thanks! This now has a CHANGELOG.md conflict with dev after today's merges. Could you bring dev in? CI hasn't run on this PR yet, so I'll approve the workflow run once it's updated.

@sromoam sromoam left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @pcbeingused33, this is a small, well-scoped fix and the repro in the description made it easy to check. I reproduced the KeyError: 'text' on current dev with a real strands.Agent and a stub Model that streams a reasoningContent block and then true. On this head (9ff38d9), compare() returns 1.0 and get_comparison_details() shows llm_response='true'. The new test fails on dev and passes here, and with dev merged in locally tests/common/comparators passes (1459 passed). The pushes since vawsgit's review are dev merges only, so the code is what was approved.

Must fix

  1. Have the LLM-as-a-Judge guide say which part of a reply is read, and what a reply with no text does (docs/docs/Guides/Comparators/llm-as-a-judge.md:154, :155 and :191). How It Works doesn't mention reasoning blocks, and :191 says "a reply that adds reasoning scores 0.0 with a warning", which someone using a reasoning model will read as covering their replies. Step 5 also says a reply that isn't a verdict "does not raise, so one verbose reply does not stop a bulk run". With the stub on a two-field model, a reply holding only a reasoningContent block raises ValueError out of compare_with() (dev raised KeyError there too), while a reply of maybe scores 0.0 with a warning and the run goes on. The docs are where we settle what should happen, so please:
    • start step 4 with something like "Only the reply's text blocks are read, so a reasoning model's reasoning is skipped.", and make :191 say "a reply whose text adds reasoning";
    • say in step 5 what a reply with no text block does. If raising is what you intend, state it there next to "It does not raise". If it should follow step 5 instead, score it 0.0 with the warning and update the CHANGELOG line to match.

Smaller

  1. Agree with vawsgit's still-open inline note on testing the ValueError branch. The cheapest way in: test_malformed_response_empty_content_array and test_malformed_response_missing_text_key (tests/common/comparators/test_llm.py:471 and :480) already send replies with no text block, but assert pytest.raises(Exception), which dev's IndexError and KeyError pass too. Tightening them to pytest.raises(ValueError, match="no text content block") and adding a reasoningContent-only case pins it: all three pass here and fail on dev.

#676 also rewrites _invoke_agent and keeps content[0]["text"] on its plain-text fallback, so whichever of the two lands second should carry this text-block read over.

One heads-up: we're not merging anything outside the 1.1.0 milestone until 1.1.0 lands, so this will wait until after that even once it's ready. It merges cleanly with dev today, so there's no need to keep bringing dev in before then.

Only text blocks are scored. A reply with no text block, including one
that holds only reasoning, raises ValueError from compare() and from
compare_with(). The guide now says so, and the malformed-reply tests
require that ValueError.
@pcbeingused33

Copy link
Copy Markdown
Contributor Author

The guide now says only the reply's text blocks are read, so a reasoning model's reasoning is skipped, and the guidelines line says a reply whose text adds reasoning scores 0.0 with a warning. Step 5 still says a reply that is not a verdict does not raise. Next to that, it says a reply with no text block, including one that holds only reasoning, raises ValueError from compare() and from compare_with(). The changelog line is unchanged because that raise was already the behavior.

test_malformed_response_empty_content_array and test_malformed_response_missing_text_key now expect ValueError with "no text content block". test_compare_reasoning_only_raises covers a reasoningContent-only reply: compare() raises that ValueError, and get_comparison_details() returns the error dict. Those three fail while the reply is still read as content[0]["text"] (IndexError or KeyError) and pass with the text-block read.

Nothing outside this pull request was changed. Whichever of this and #676 lands second will still need to keep the text-block read on the plain-text path.

This reply was drafted with AI assistance.

@sromoam sromoam added the anytime Non-breaking improvement off the release critical path: merge whenever it's ready label Dec 4, 2026

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

anytime Non-breaking improvement off the release critical path: merge whenever it's ready

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants