Repository navigation
fix: Context.last_turn - #1423
Conversation
ajbozarth
left a comment
There was a problem hiding this comment.
I'll leave final approval to others, but this LGTM, Claude review comments and nits below:
Correct, minimal, well-scoped fix for #1002. All three library callers of last_turn().output are updated, mypy is clean, and the affected tests pass locally. Nice side effect: _resolve_question() (untouched) is also corrected, since the assistant message now lands in output instead of being mis-read as the question. Two optional notes inline, neither blocking.
Signed-off-by: Akihiko Kuroda <[email protected]>
Signed-off-by: Akihiko Kuroda <[email protected]>
Signed-off-by: Akihiko Kuroda <[email protected]>
| if self._risk == "function_call" or effective_risk == "function_call": | ||
| content = last_turn.output.value or "" | ||
| # Extract content from either ModelOutputThunk or Message | ||
| if isinstance(last_turn.output, Message): |
There was a problem hiding this comment.
This branch handles Message in last_turn.output for the function_call risk path, but I don't see a test exercising it — the existing suite only covers the ModelOutputThunk path here. Worth adding a case where the last turn's output is a manually-added assistant Message with risk="function_call", so the content = last_turn.output.content line (and the tool_calls lookup right after) actually gets covered.
There was a problem hiding this comment.
a test added.
| # System message should be in model_input, not output | ||
| assert isinstance(turn.model_input, Message) | ||
| assert turn.model_input.role == "system" | ||
| assert turn.output is None |
There was a problem hiding this comment.
The new tests cover user/system messages falling through to model_input, but there's no case for a role="tool" message — which per the comment in last_turn() ("tool messages fall through to input") is exactly the scoping decision this PR is making. Might be worth adding:
def test_last_turn_tool_message_treated_as_input():
from mellea.stdlib.context import ChatContext
ctx = ChatContext().add(Message("tool", "tool result"))
turn = ctx.last_turn()
assert turn is not None
assert isinstance(turn.model_input, Message)
assert turn.model_input.role == "tool"
assert turn.output is NoneThere was a problem hiding this comment.
a test added.
Signed-off-by: Akihiko Kuroda <[email protected]>
8f51417
Pull Request
Issue
Fixes #1002
Description
Testing
Attribution
Adding a new component, requirement, sampling strategy, or tool?
If your PR adds or modifies one of the types below, check the matching box. A checklist of type-specific review items will be posted as a comment.
NOTE: Please ensure you have an issue that has been acknowledged by a core contributor and routed you to open a pull request against this repository. Otherwise, please open an issue before continuing with this pull request.