langchain - 💡(How to fix) Fix test_stream_error_callback expects [] instead of [[]] for empty stream error result [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
langchain-ai/langchain#36865Fetched 2026-04-19 15:04:04
View on GitHub
Comments
1
Participants
2
Timeline
4
Reactions
0
Author
Timeline (top)
labeled ×2closed ×1commented ×1

test_stream_error_callback still has an incorrect expectation in the empty streaming-error case.

After #30228 made the assertions execute, the test now exercises the callback path correctly. In the i == 0 case, the callback receives an LLMResult whose generations preserves the batch dimension, so the observed value is [[]] rather than [].

That means the current xfail is hiding a test expectation mismatch rather than an unresolved product bug.

Error Message

Error Message and Stack Trace (if applicable)

test_stream_error_callback still has an incorrect expectation in the empty streaming-error case.

Root Cause

test_stream_error_callback still has an incorrect expectation in the empty streaming-error case.

After #30228 made the assertions execute, the test now exercises the callback path correctly. In the i == 0 case, the callback receives an LLMResult whose generations preserves the batch dimension, so the observed value is [[]] rather than [].

That means the current xfail is hiding a test expectation mismatch rather than an unresolved product bug.

Fix Action

Fix / Workaround

  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • This is not related to the langchain-community package.
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Code Example

import pytest
from langchain_core.language_models import FakeListChatModel
from langchain_core.language_models.fake_chat_models import FakeListChatModelError
from langchain_core.outputs.llm_result import LLMResult
from tests.unit_tests.fake.callbacks import BaseFakeCallbackHandler, FakeAsyncCallbackHandler, FakeCallbackHandler


async def test_stream_error_callback() -> None:
    message = "test"

    def eval_response(callback: BaseFakeCallbackHandler, i: int) -> None:
        assert callback.errors == 1
        assert len(callback.errors_args) == 1
        llm_result: LLMResult = callback.errors_args[0]["kwargs"]["response"]
        if i == 0:
            assert llm_result.generations == []
        else:
            assert llm_result.generations[0][0].text == message[:i]

    for i in range(len(message)):
        llm = FakeListChatModel(
            responses=[message],
            error_on_chunk_number=i,
        )
        cb_async = FakeAsyncCallbackHandler()
        llm_astream = llm.astream("Dummy message", config={"callbacks": [cb_async]})
        for _ in range(i):
            await anext(llm_astream)
        with pytest.raises(FakeListChatModelError):
            await anext(llm_astream)
        eval_response(cb_async, i)

        cb_sync = FakeCallbackHandler()
        llm_stream = llm.stream("Dumy message", config={"callbacks": [cb_sync]})
        for _ in range(i):
            next(llm_stream)
        with pytest.raises(FakeListChatModelError):
            next(llm_stream)
        eval_response(cb_sync, i)

---

E           assert [[]] == []
E             
E             Left contains one more item: []
RAW_BUFFERClick to expand / collapse

Checked other resources

  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • This is not related to the langchain-community package.
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Package

  • langchain-core

Related Issues / PRs

  • Follow-up to #29436 and #30228.

Reproduction Steps / Example Code (Python)

import pytest
from langchain_core.language_models import FakeListChatModel
from langchain_core.language_models.fake_chat_models import FakeListChatModelError
from langchain_core.outputs.llm_result import LLMResult
from tests.unit_tests.fake.callbacks import BaseFakeCallbackHandler, FakeAsyncCallbackHandler, FakeCallbackHandler


async def test_stream_error_callback() -> None:
    message = "test"

    def eval_response(callback: BaseFakeCallbackHandler, i: int) -> None:
        assert callback.errors == 1
        assert len(callback.errors_args) == 1
        llm_result: LLMResult = callback.errors_args[0]["kwargs"]["response"]
        if i == 0:
            assert llm_result.generations == []
        else:
            assert llm_result.generations[0][0].text == message[:i]

    for i in range(len(message)):
        llm = FakeListChatModel(
            responses=[message],
            error_on_chunk_number=i,
        )
        cb_async = FakeAsyncCallbackHandler()
        llm_astream = llm.astream("Dummy message", config={"callbacks": [cb_async]})
        for _ in range(i):
            await anext(llm_astream)
        with pytest.raises(FakeListChatModelError):
            await anext(llm_astream)
        eval_response(cb_async, i)

        cb_sync = FakeCallbackHandler()
        llm_stream = llm.stream("Dumy message", config={"callbacks": [cb_sync]})
        for _ in range(i):
            next(llm_stream)
        with pytest.raises(FakeListChatModelError):
            next(llm_stream)
        eval_response(cb_sync, i)

Error Message and Stack Trace (if applicable)

E           assert [[]] == []
E             
E             Left contains one more item: []

Description

test_stream_error_callback still has an incorrect expectation in the empty streaming-error case.

After #30228 made the assertions execute, the test now exercises the callback path correctly. In the i == 0 case, the callback receives an LLMResult whose generations preserves the batch dimension, so the observed value is [[]] rather than [].

That means the current xfail is hiding a test expectation mismatch rather than an unresolved product bug.

System Info

System Information

OS: Darwin OS Version: Darwin Kernel Version 25.4.0: Thu Mar 19 19:32:36 PDT 2026; root:xnu-12377.101.15~1/RELEASE_ARM64_T8103 Python Version: 3.11.11 (main, Feb 12 2025, 15:06:01) [Clang 19.1.6 ]

Package Information

langchain_core: 1.3.0 langsmith: 0.7.31 langchain_tests: 1.1.6

Other Dependencies

pytest: 9.0.3 pytest-asyncio: 1.3.0

extent analysis

TL;DR

The test test_stream_error_callback has an incorrect expectation for the empty streaming-error case, which can be fixed by updating the assertion to account for the preserved batch dimension in the LLMResult generations.

Guidance

  • The issue arises from the fact that LLMResult generations preserve the batch dimension, resulting in a value of [[]] instead of [] in the i == 0 case.
  • To fix this, update the assertion in the eval_response function to check for [[]] instead of [] when i == 0.
  • Verify that the test passes after making this change to ensure the fix is correct.
  • Consider adding additional test cases to cover other edge cases and ensure the callback handling is working as expected.

Example

def eval_response(callback: BaseFakeCallbackHandler, i: int) -> None:
    assert callback.errors == 1
    assert len(callback.errors_args) == 1
    llm_result: LLMResult = callback.errors_args[0]["kwargs"]["response"]
    if i == 0:
        assert llm_result.generations == [[]]  # Update this line
    else:
        assert llm_result.generations[0][0].text == message[:i]

Notes

This fix assumes that the preserved batch dimension is the intended behavior for LLMResult generations. If this is not the case, further investigation may be needed to determine the correct behavior.

Recommendation

Apply the workaround by updating the assertion in the eval_response function to account for the preserved batch dimension. This should fix the test expectation mismatch and allow the test to pass.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING