Skip to content

Bug: Flaky test in langchain_messages.py - qualitative assertion too strict #384

Closed

Description

@planetf1

Bug: Flaky test in langchain_messages.py - qualitative assertion too strict

Description

The langchain_messages.py example test fails due to LLM output variability, even with a seed set. The assertion is too strict for a qualitative test.

Failing Test

  • docs/examples/library_interop/langchain_messages.py (line 44)

Error

assert 'Hi there!' in 'The last message you sent to me was "Hello!". How can I assist you today?'
AssertionError: assert 'Hi there!' in 'The last message you sent to me was "Hello!". How can I assist you today?'

Test Setup

# Context contains:
# - System: "You are a helpful assistant"
# - Human: "Hello!"
# - AI: "Hi there!"

# Question asked:
response = m.chat(
    "What was the last assistant message?",
    model_options={ModelOption.SEED: 2}
).content

assert "Hi there!" in response  # FAILS

Root Cause

Model misinterprets "last assistant message" as "last message to the assistant" and references the human's "Hello!" instead of its own "Hi there!".

Contributing factors:

  1. Ambiguous phrasing in the question
  2. Exact string matching inappropriate for qualitative output
  3. Model instruction-following variability (even with seed)

Proposed Solutions

Option 1: Clearer question + fuzzy matching (recommended)

response = m.chat(
    "What was the last message YOU sent?",
    model_options={ModelOption.SEED: 2}
).content
assert any(word in response.lower() for word in ["hi", "there"])

Option 2: Test functionality, not exact output

# Just verify context access works
assert len(response) > 0

Option 3: Improve system prompt

SystemMessage(content="You are a helpful assistant. Answer questions about the conversation literally and accurately.")

Recommendation

Option 1 - qualitative tests should never use exact string matching. The test's purpose is verifying context handling, not exact phrasing.

Additional Context

Context is correctly passed to the model (it mentions "Hello!"). Issue is instruction-following + strict assertion.

Activity

  1. psschwei commented on May 12, 2026

    @psschwei
    Contributor

    @planetf1 is this still an issue?

  2. planetf1 commented on May 13, 2026

    @planetf1
    ContributorAuthor

    Closing as not seen in practice recently. The assertion question was tightened in a prior fix, which appears to have resolved the flakiness. The broader problem of exact-string assertions on LLM output is being tracked more systematically in #692 and #735.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions