Skip to content
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ Each release links to full notes on the

### Fixed

- **`LLMComparator` no longer raises `KeyError: 'text'` when the reply does not
start with a text block**, as with a reasoning model that sends
`reasoningContent` first. The verdict is read from the reply's text blocks; a
reply with none still raises, now as a `ValueError` that says so.
- **A comparator key on a nested model now warns instead of being dropped in
silence or refused.** A nested model is scored field by field, so its
comparator is never called, as on the Python path. `comparator` on a
Expand Down
9 changes: 7 additions & 2 deletions src/stickler/comparators/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,18 @@ def _invoke_agent(self, prompt: str) -> str:
prompt: The formatted prompt string to send to the LLM.

Returns:
str: The text response from the LLM.
str: The reply's text blocks, joined. Other blocks, such as the
``reasoningContent`` a reasoning model sends first, are skipped.

Raises:
Exception: If the agent call fails or response format is unexpected.
"""
result = self.agent(prompt)
return result.message["content"][0]["text"]
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.

return "".join(texts)

def _compare(self, value1: Any, value2: Any) -> float:
"""Compare two values using LLM-based semantic analysis.
Expand Down
13 changes: 13 additions & 0 deletions tests/common/comparators/test_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ def test_client_initialization(self):
)
assert new_comp.agent is self.mock_agent_class.return_value

def test_compare_skips_non_text_blocks(self):
"""A reasoning model puts a ``reasoningContent`` block before the text."""
mock_result = MagicMock()
mock_result.message = {
"content": [
{"reasoningContent": {"reasoningText": {"text": "false?"}}},
{"text": "true"},
]
}
self.mock_agent.return_value = mock_result

assert self.comparator.compare("St.", "Street") == 1.0

@pytest.mark.parametrize(
"response", ["true", "TRUE", "True", " true ", " TRUE "]
)
Expand Down