Home / Function/ test_shielded_callback_context_preservation() — langchain Function Reference

test_shielded_callback_context_preservation() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  afa066a7_187e_97a7_9fe8_398637386dee["test_shielded_callback_context_preservation()"]
  f1a09d1e_2693_1456_76d6_3f34ca061a65["test_async_callback_manager.py"]
  afa066a7_187e_97a7_9fe8_398637386dee -->|defined in| f1a09d1e_2693_1456_76d6_3f34ca061a65
  04fc3b21_48f4_024e_026e_0e95026ac119["on_llm_end()"]
  afa066a7_187e_97a7_9fe8_398637386dee -->|calls| 04fc3b21_48f4_024e_026e_0e95026ac119
  000bca7f_11b5_0c40_88ed_3a18292895b8["on_chain_end()"]
  afa066a7_187e_97a7_9fe8_398637386dee -->|calls| 000bca7f_11b5_0c40_88ed_3a18292895b8
  2916e273_7429_5c51_38d6_6936f07ce55b["on_llm_start()"]
  afa066a7_187e_97a7_9fe8_398637386dee -->|calls| 2916e273_7429_5c51_38d6_6936f07ce55b
  style afa066a7_187e_97a7_9fe8_398637386dee fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/tests/unit_tests/callbacks/test_async_callback_manager.py lines 154–212

async def test_shielded_callback_context_preservation() -> None:
    """Verify that shielded callbacks preserve context variables.

    This test specifically addresses the issue where async callbacks decorated
    with @shielded do not properly preserve context variables, breaking
    instrumentation and other context-dependent functionality.

    The issue manifests in callbacks that use the @shielded decorator:
    * on_llm_end
    * on_llm_error
    * on_chain_end
    * on_chain_error
    * And other shielded callback methods
    """
    context_var: contextvars.ContextVar[str] = contextvars.ContextVar("test_context")

    class ContextTestHandler(AsyncCallbackHandler):
        """Handler that reads context variables in shielded callbacks."""

        def __init__(self) -> None:
            self.run_inline = False
            self.context_values: list[str] = []

        @override
        async def on_llm_end(self, response: Any, **kwargs: Any) -> None:
            """This method is decorated with @shielded in the run manager."""
            # This should preserve the context variable value
            self.context_values.append(context_var.get("not_found"))

        @override
        async def on_chain_end(self, outputs: Any, **kwargs: Any) -> None:
            """This method is decorated with @shielded in the run manager."""
            # This should preserve the context variable value
            self.context_values.append(context_var.get("not_found"))

    # Set up the test context
    context_var.set("test_value")
    handler = ContextTestHandler()
    manager = AsyncCallbackManager(handlers=[handler])

    # Create run managers that have the shielded methods
    llm_managers = await manager.on_llm_start({}, ["test prompt"])
    llm_run_manager = llm_managers[0]

    chain_run_manager = await manager.on_chain_start({}, {"test": "input"})

    # Test LLM end callback (which is shielded)
    await llm_run_manager.on_llm_end({"response": "test"})  # type: ignore[arg-type]

    # Test Chain end callback (which is shielded)
    await chain_run_manager.on_chain_end({"output": "test"})

    # The context should be preserved in shielded callbacks
    # This was the main issue - shielded decorators were not preserving context
    assert handler.context_values == ["test_value", "test_value"], (
        f"Expected context values ['test_value', 'test_value'], "
        f"but got {handler.context_values}. "
        f"This indicates the shielded decorator is not preserving context variables."
    )

Domain

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free