Home / Function/ test_inline_handlers_share_parent_context_multiple() — langchain Function Reference

test_inline_handlers_share_parent_context_multiple() — langchain Function Reference

Architecture documentation for the test_inline_handlers_share_parent_context_multiple() function in test_async_callback_manager.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  3c7fac12_ce20_c718_b27a_6cfae602fed1["test_inline_handlers_share_parent_context_multiple()"]
  f1a09d1e_2693_1456_76d6_3f34ca061a65["test_async_callback_manager.py"]
  3c7fac12_ce20_c718_b27a_6cfae602fed1 -->|defined in| f1a09d1e_2693_1456_76d6_3f34ca061a65
  2916e273_7429_5c51_38d6_6936f07ce55b["on_llm_start()"]
  3c7fac12_ce20_c718_b27a_6cfae602fed1 -->|calls| 2916e273_7429_5c51_38d6_6936f07ce55b
  style 3c7fac12_ce20_c718_b27a_6cfae602fed1 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/tests/unit_tests/callbacks/test_async_callback_manager.py lines 78–151

async def test_inline_handlers_share_parent_context_multiple() -> None:
    """A slightly more complex variation of the test unit test above.

    This unit test verifies that things work correctly when there are multiple prompts,
    and multiple handlers that are configured to run inline.
    """
    counter_var = contextvars.ContextVar("counter", default=0)

    shared_stack = []

    @asynccontextmanager
    async def set_counter_var() -> Any:
        token = counter_var.set(0)
        try:
            yield
        finally:
            counter_var.reset(token)

    class StatefulAsyncCallbackHandler(AsyncCallbackHandler):
        def __init__(self, name: str, *, run_inline: bool = True):
            self.name = name
            self.run_inline = run_inline

        async def on_llm_start(
            self,
            serialized: dict[str, Any],
            prompts: list[str],
            *,
            run_id: UUID,
            parent_run_id: UUID | None = None,
            **kwargs: Any,
        ) -> None:
            if self.name == "StateModifier":
                current_counter = counter_var.get()
                counter_var.set(current_counter + 1)
                state = counter_var.get()
            elif self.name == "StateReader":
                state = counter_var.get()
            else:
                state = None

            shared_stack.append(state)

            await super().on_llm_start(
                serialized,
                prompts,
                run_id=run_id,
                parent_run_id=parent_run_id,
                **kwargs,
            )

    handlers: list[BaseCallbackHandler] = [
        StatefulAsyncCallbackHandler("StateModifier", run_inline=True),
        StatefulAsyncCallbackHandler("StateReader", run_inline=True),
        StatefulAsyncCallbackHandler("NonInlineHandler", run_inline=False),
    ]

    prompts = ["Prompt1", "Prompt2", "Prompt3"]

    async with set_counter_var():
        shared_stack.clear()
        manager = AsyncCallbackManager(handlers=handlers)
        await manager.on_llm_start({}, prompts)

        # Assert the order of states
        states = [entry for entry in shared_stack if entry is not None]
        assert states == [
            1,
            1,
            2,
            2,
            3,
            3,
        ]

Domain

Subdomains

Frequently Asked Questions

What does test_inline_handlers_share_parent_context_multiple() do?
test_inline_handlers_share_parent_context_multiple() is a function in the langchain codebase, defined in libs/core/tests/unit_tests/callbacks/test_async_callback_manager.py.
Where is test_inline_handlers_share_parent_context_multiple() defined?
test_inline_handlers_share_parent_context_multiple() is defined in libs/core/tests/unit_tests/callbacks/test_async_callback_manager.py at line 78.
What does test_inline_handlers_share_parent_context_multiple() call?
test_inline_handlers_share_parent_context_multiple() calls 1 function(s): on_llm_start.

Analyze Your Own Codebase

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

Try Supermodel Free