test_closing_iterator_doesnt_raise_error() — langchain Function Reference
Architecture documentation for the test_closing_iterator_doesnt_raise_error() function in test_runnable.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD a9077a2a_464d_d853_4eb9_ae6048e61e55["test_closing_iterator_doesnt_raise_error()"] 26df6ad8_0189_51d0_c3c1_6c3248893ff5["test_runnable.py"] a9077a2a_464d_d853_4eb9_ae6048e61e55 -->|defined in| 26df6ad8_0189_51d0_c3c1_6c3248893ff5 a8ab75c8_fdad_c841_f54a_7ddeb43df40d["on_chain_error()"] a9077a2a_464d_d853_4eb9_ae6048e61e55 -->|calls| a8ab75c8_fdad_c841_f54a_7ddeb43df40d e2a2f3d5_061c_bced_7cee_afca636dadd5["on_chain_end()"] a9077a2a_464d_d853_4eb9_ae6048e61e55 -->|calls| e2a2f3d5_061c_bced_7cee_afca636dadd5 style a9077a2a_464d_d853_4eb9_ae6048e61e55 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/core/tests/unit_tests/runnables/test_runnable.py lines 5633–5675
def test_closing_iterator_doesnt_raise_error() -> None:
"""Test that closing an iterator calls on_chain_end rather than on_chain_error."""
on_chain_error_triggered = False
on_chain_end_triggered = False
class MyHandler(BaseCallbackHandler):
@override
def on_chain_error(
self,
error: BaseException,
*,
run_id: UUID,
parent_run_id: UUID | None = None,
tags: list[str] | None = None,
**kwargs: Any,
) -> None:
"""Run when chain errors."""
nonlocal on_chain_error_triggered
on_chain_error_triggered = True
@override
def on_chain_end(
self,
outputs: dict[str, Any],
*,
run_id: UUID,
parent_run_id: UUID | None = None,
**kwargs: Any,
) -> None:
nonlocal on_chain_end_triggered
on_chain_end_triggered = True
llm = GenericFakeChatModel(messages=iter(["hi there"]))
chain = llm | StrOutputParser()
chain_ = chain.with_config({"callbacks": [MyHandler()]})
st = chain_.stream("hello")
next(st)
# This is a generator so close is defined on it.
st.close() # type: ignore[attr-defined]
# Wait for a bit to make sure that the callback is called.
time.sleep(0.05)
assert on_chain_error_triggered is False
assert on_chain_end_triggered is True
Domain
Subdomains
Source
Frequently Asked Questions
What does test_closing_iterator_doesnt_raise_error() do?
test_closing_iterator_doesnt_raise_error() is a function in the langchain codebase, defined in libs/core/tests/unit_tests/runnables/test_runnable.py.
Where is test_closing_iterator_doesnt_raise_error() defined?
test_closing_iterator_doesnt_raise_error() is defined in libs/core/tests/unit_tests/runnables/test_runnable.py at line 5633.
What does test_closing_iterator_doesnt_raise_error() call?
test_closing_iterator_doesnt_raise_error() calls 2 function(s): on_chain_end, on_chain_error.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free