Home / Function/ test_inline_handlers_share_parent_context() — langchain Function Reference

test_inline_handlers_share_parent_context() — langchain Function Reference

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

Entity Profile

Dependency Diagram

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

Relationship Graph

Source Code

libs/core/tests/unit_tests/callbacks/test_async_callback_manager.py lines 22–75

async def test_inline_handlers_share_parent_context() -> None:
    """Verify that handlers that are configured to run_inline can update parent context.

    This test was created because some of the inline handlers were getting
    their own context as the handling logic was kicked off using asyncio.gather
    which does not automatically propagate the parent context (by design).

    This issue was affecting only a few specific handlers:

    * on_llm_start
    * on_chat_model_start

    which in some cases were triggered with multiple prompts and as a result
    triggering multiple tasks that were launched in parallel.
    """
    some_var: contextvars.ContextVar[str] = contextvars.ContextVar("some_var")

    class CustomHandler(AsyncCallbackHandler):
        """A handler that sets the context variable.

        The handler sets the context variable to the name of the callback that was
        called.
        """

        def __init__(self, *, run_inline: bool) -> None:
            """Initialize the handler."""
            self.run_inline = run_inline

        @override
        async def on_llm_start(self, *args: Any, **kwargs: Any) -> None:
            """Update the callstack with the name of the callback."""
            some_var.set("on_llm_start")

    # The manager serves as a callback dispatcher.
    # It's responsible for dispatching callbacks to all registered handlers.
    manager = AsyncCallbackManager(handlers=[CustomHandler(run_inline=True)])

    # Check on_llm_start
    some_var.set("unset")
    await manager.on_llm_start({}, ["prompt 1"])
    assert some_var.get() == "on_llm_start"

    # Check what happens when run_inline is False
    # We don't expect the context to be updated
    manager2 = AsyncCallbackManager(
        handlers=[
            CustomHandler(run_inline=False),
        ]
    )

    some_var.set("unset")
    await manager2.on_llm_start({}, ["prompt 1"])
    # Will not be updated because the handler is not inline
    assert some_var.get() == "unset"

Domain

Subdomains

Frequently Asked Questions

What does test_inline_handlers_share_parent_context() do?
test_inline_handlers_share_parent_context() 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() defined?
test_inline_handlers_share_parent_context() is defined in libs/core/tests/unit_tests/callbacks/test_async_callback_manager.py at line 22.
What does test_inline_handlers_share_parent_context() call?
test_inline_handlers_share_parent_context() 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