Skip to content

fix: Context.last_turn - #1423

Merged
akihikokuroda merged 4 commits into
generative-computing:mainfrom
akihikokuroda:issue1002
Jul 23, 2026
Merged

akihikokuroda merged 4 commits into
generative-computing:mainfrom
akihikokuroda:issue1002

Conversation

@akihikokuroda

Copy link
Copy Markdown
Contributor

Pull Request

Issue

Fixes #1002

Description

  1. Core Fix (mellea/core/base.py):
  • Updated ContextTurn.output type: ModelOutputThunk | Component | None (was just ModelOutputThunk | None)
  • Modified last_turn() method to check if last element is Message(role="assistant")
  • If found, place it in output slot (not model_input)
  • Uses local import to avoid circular dependency
  1. Simplified Workaround (mellea/stdlib/components/intrinsic/_util.py):
  • Removed the manual Message(role="assistant") workaround in _extract_last_response()
  • Now correctly expects Message in turn.output instead of turn.model_input
  • Simplified logic flow
  1. Fixed Dependent Code (mellea/stdlib/requirements/safety/guardian.py):
  • Found code accessing last_turn.output.value that would fail with Message (has .content, not .value)
  • Added type check to handle both Message and ModelOutputThunk
  1. Added Tests (test/core/test_base.py):
  • 7 new comprehensive tests for last_turn() with various scenarios
  • Tests for Message(role="assistant"), Message(role="user"), Message(role="system")
  • Tests for 1-element and 2-element history cases
  1. Updated Tests (test/stdlib/components/intrinsic/test_resolve_util.py):
  • Updated 1 test assertion to reflect corrected behavior
  • Now expects Message in turn.output (not turn.model_input)

Testing

  • Tests added to the respective file if code was changed
  • New code has 100% coverage if code was added
  • Ensure existing tests and github automation passes (a maintainer will kick off the github automation when the rest of the PR is populated)

Attribution

  • AI coding assistants used

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.

  • Component
  • Requirement
  • Sampling Strategy
  • Tool

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.

@github-actions github-actions Bot added the bug Something isn't working label Jul 21, 2026
@akihikokuroda
akihikokuroda marked this pull request as ready for review July 21, 2026 16:30
@akihikokuroda
akihikokuroda requested a review from a team as a code owner July 21, 2026 16:30

@ajbozarth ajbozarth left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Comment thread mellea/core/base.py Resolved
Signed-off-by: Akihiko Kuroda <[email protected]>
Signed-off-by: Akihiko Kuroda <[email protected]>
Signed-off-by: Akihiko Kuroda <[email protected]>

@planetf1 planetf1 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.

Nice fix — the last_turn() change is correct and the reasoning about role=="assistant" scoping matches the existing #377 discussion. Two small test-coverage gaps below, but nothing blocking.

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):

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.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

a test added.

Comment thread test/core/test_base.py
# 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

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.

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 None

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

a test added.

Signed-off-by: Akihiko Kuroda <[email protected]>
@akihikokuroda
akihikokuroda added this pull request to the merge queue Jul 23, 2026
Merged via the queue into generative-computing:main with commit 8f51417 Jul 23, 2026
10 checks passed
@akihikokuroda
akihikokuroda deleted the issue1002 branch July 23, 2026 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Context.last_turn doesn't correctly grab the response

3 participants