Repository navigation
fix: read LLMComparator verdict from text blocks only - #645
pcbeingused33 wants to merge 8 commits into
Conversation
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
| 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") |
There was a problem hiding this comment.
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.
…reasoning-block # Conflicts: # CHANGELOG.md
|
Thanks! This now has a |
…reasoning-block # Conflicts: # CHANGELOG.md
sromoam
left a comment
There was a problem hiding this comment.
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
- 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,:155and:191). How It Works doesn't mention reasoning blocks, and:191says "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 areasoningContentblock raisesValueErrorout ofcompare_with()(dev raisedKeyErrorthere too), while a reply ofmaybescores 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
:191say "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.
- start step 4 with something like "Only the reply's text blocks are read, so a reasoning model's reasoning is skipped.", and make
Smaller
- Agree with vawsgit's still-open inline note on testing the
ValueErrorbranch. The cheapest way in:test_malformed_response_empty_content_arrayandtest_malformed_response_missing_text_key(tests/common/comparators/test_llm.py:471and:480) already send replies with no text block, but assertpytest.raises(Exception), which dev'sIndexErrorandKeyErrorpass too. Tightening them topytest.raises(ValueError, match="no text content block")and adding areasoningContent-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.
|
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. |
…reasoning-block # Conflicts: # CHANGELOG.md
Issue Reference
No tracking issue.
Description of Changes
LLMComparatorraisedKeyError: 'text'when a reply did not start with a text block, which is what happens with a reasoning model that sendsreasoningContentfirst.Changes Made
_invoke_agentjoins the reply's text blocks instead of readingcontent[0]["text"]ValueErrorthat says soValueErrorfromcompare()and fromcompare_with()ValueError, including a reasoning-only reply, andCHANGELOG.mdrecords the fixWhy These Changes Are Needed
The verdict is in a later block, so the comparison failed although the model answered.
comparere-raises the error, so the caller gets no score.get_comparison_detailsgoes through the same method and returned{"error": "'text'", ...}.Reproduction
A real
strands.Agentwith a stubModelthat streams areasoningContentblock and then the texttrue:Before:
KeyError: 'text'. After:1.0. No call to a hosted model was made.Testing
The new test fails on
devwithKeyError: 'text'and passes with the fix. The three no-text cases fail while the reply is still read as the first block'stextkey and pass once text blocks are joined.tests/common/comparators/test_llm.pypasses (67 tests) andruff checkis clean on the comparator and that file. The full test suite was not run.Checklist
CHANGELOG.mdentry added under[Unreleased]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.