Home / Function/ test_callback_handlers() — langchain Function Reference

test_callback_handlers() — langchain Function Reference

Architecture documentation for the test_callback_handlers() function in test_fake_chat_model.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  af4e520f_33c0_5b8c_7bf9_9b0dd5cb04b9["test_callback_handlers()"]
  80c98d4d_3f6a_0197_c2ea_3db4062a1246["test_fake_chat_model.py"]
  af4e520f_33c0_5b8c_7bf9_9b0dd5cb04b9 -->|defined in| 80c98d4d_3f6a_0197_c2ea_3db4062a1246
  7a27e13a_f632_f712_1db5_4d6fd6f822f2["on_chat_model_start()"]
  af4e520f_33c0_5b8c_7bf9_9b0dd5cb04b9 -->|calls| 7a27e13a_f632_f712_1db5_4d6fd6f822f2
  bfc942da_74ef_84cf_c17e_3eb9a75e609b["on_llm_new_token()"]
  af4e520f_33c0_5b8c_7bf9_9b0dd5cb04b9 -->|calls| bfc942da_74ef_84cf_c17e_3eb9a75e609b
  style af4e520f_33c0_5b8c_7bf9_9b0dd5cb04b9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/tests/unit_tests/llms/test_fake_chat_model.py lines 151–206

async def test_callback_handlers() -> None:
    """Verify that model is implemented correctly with handlers working."""

    class MyCustomAsyncHandler(AsyncCallbackHandler):
        def __init__(self, store: list[str]) -> None:
            self.store = store

        async def on_chat_model_start(
            self,
            serialized: dict[str, Any],
            messages: list[list[BaseMessage]],
            *,
            run_id: UUID,
            parent_run_id: UUID | None = None,
            tags: list[str] | None = None,
            metadata: dict[str, Any] | None = None,
            **kwargs: Any,
        ) -> Any:
            # Do nothing
            # Required to implement since this is an abstract method
            pass

        @override
        async def on_llm_new_token(
            self,
            token: str,
            *,
            chunk: GenerationChunk | ChatGenerationChunk | None = None,
            run_id: UUID,
            parent_run_id: UUID | None = None,
            tags: list[str] | None = None,
            **kwargs: Any,
        ) -> None:
            self.store.append(token)

    infinite_cycle = cycle(
        [
            AIMessage(content="hello goodbye"),
        ],
    )
    model = GenericFakeChatModel(messages=infinite_cycle)
    tokens: list[str] = []
    # New model
    results = [
        chunk
        async for chunk in model.astream(
            "meow",
            {"callbacks": [MyCustomAsyncHandler(tokens)]},
        )
    ]
    assert results == [
        _AnyIdAIMessageChunk(content="hello"),
        _AnyIdAIMessageChunk(content=" "),
        _AnyIdAIMessageChunk(content="goodbye", chunk_position="last"),
    ]
    assert tokens == ["hello", " ", "goodbye"]

Domain

Subdomains

Frequently Asked Questions

What does test_callback_handlers() do?
test_callback_handlers() is a function in the langchain codebase, defined in libs/langchain/tests/unit_tests/llms/test_fake_chat_model.py.
Where is test_callback_handlers() defined?
test_callback_handlers() is defined in libs/langchain/tests/unit_tests/llms/test_fake_chat_model.py at line 151.
What does test_callback_handlers() call?
test_callback_handlers() calls 2 function(s): on_chat_model_start, on_llm_new_token.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free