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
  14e65869_1f28_9025_bab4_7fa0e4fc9440["test_callback_handlers()"]
  06318702_96e4_1032_1475_4dde8fe1f643["test_fake_chat_model.py"]
  14e65869_1f28_9025_bab4_7fa0e4fc9440 -->|defined in| 06318702_96e4_1032_1475_4dde8fe1f643
  e79e9024_3d5b_d084_f335_deaf56d8c2bb["on_chat_model_start()"]
  14e65869_1f28_9025_bab4_7fa0e4fc9440 -->|calls| e79e9024_3d5b_d084_f335_deaf56d8c2bb
  2e077bb1_7a76_7fbc_b1df_bf6903ae16e1["on_llm_new_token()"]
  14e65869_1f28_9025_bab4_7fa0e4fc9440 -->|calls| 2e077bb1_7a76_7fbc_b1df_bf6903ae16e1
  style 14e65869_1f28_9025_bab4_7fa0e4fc9440 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/tests/unit_tests/fake/test_fake_chat_model.py lines 160–215

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 == [
        _any_id_ai_message_chunk(content="hello"),
        _any_id_ai_message_chunk(content=" "),
        _any_id_ai_message_chunk(content="goodbye", chunk_position="last"),
    ]
    assert tokens == ["hello", " ", "goodbye"]
    assert len({chunk.id for chunk in results}) == 1

Domain

Subdomains

Frequently Asked Questions

What does test_callback_handlers() do?
test_callback_handlers() is a function in the langchain codebase, defined in libs/core/tests/unit_tests/fake/test_fake_chat_model.py.
Where is test_callback_handlers() defined?
test_callback_handlers() is defined in libs/core/tests/unit_tests/fake/test_fake_chat_model.py at line 160.
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