test_async_callback_manager.py — langchain Source File
Architecture documentation for test_async_callback_manager.py, a python file in the langchain codebase. 6 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR 5d2f62dd_acb8_4308_442a_010801b0c5b9["test_async_callback_manager.py"] e7c46dc4_ca3a_87ac_156f_0aa3d9b9d3f4["contextvars"] 5d2f62dd_acb8_4308_442a_010801b0c5b9 --> e7c46dc4_ca3a_87ac_156f_0aa3d9b9d3f4 69e1d8cc_6173_dcd0_bfdf_2132d8e1ce56["contextlib"] 5d2f62dd_acb8_4308_442a_010801b0c5b9 --> 69e1d8cc_6173_dcd0_bfdf_2132d8e1ce56 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"] 5d2f62dd_acb8_4308_442a_010801b0c5b9 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3 8dfa0cac_d802_3ccd_f710_43a5e70da3a5["uuid"] 5d2f62dd_acb8_4308_442a_010801b0c5b9 --> 8dfa0cac_d802_3ccd_f710_43a5e70da3a5 91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"] 5d2f62dd_acb8_4308_442a_010801b0c5b9 --> 91721f45_4909_e489_8c1f_084f8bd87145 f3bc7443_c889_119d_0744_aacc3620d8d2["langchain_core.callbacks"] 5d2f62dd_acb8_4308_442a_010801b0c5b9 --> f3bc7443_c889_119d_0744_aacc3620d8d2 style 5d2f62dd_acb8_4308_442a_010801b0c5b9 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
"""Unit tests for verifying event dispatching.
Much of this code is indirectly tested already through many end-to-end tests
that generate traces based on the callbacks. The traces are all verified
via snapshot testing (e.g., see unit tests for runnables).
"""
import contextvars
from contextlib import asynccontextmanager
from typing import Any
from uuid import UUID
from typing_extensions import override
from langchain_core.callbacks import (
AsyncCallbackHandler,
AsyncCallbackManager,
BaseCallbackHandler,
)
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")
// ... (153 more lines)
Domain
Subdomains
Functions
Dependencies
- contextlib
- contextvars
- langchain_core.callbacks
- typing
- typing_extensions
- uuid
Source
Frequently Asked Questions
What does test_async_callback_manager.py do?
test_async_callback_manager.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, RunnableInterface subdomain.
What functions are defined in test_async_callback_manager.py?
test_async_callback_manager.py defines 3 function(s): test_inline_handlers_share_parent_context, test_inline_handlers_share_parent_context_multiple, test_shielded_callback_context_preservation.
What does test_async_callback_manager.py depend on?
test_async_callback_manager.py imports 6 module(s): contextlib, contextvars, langchain_core.callbacks, typing, typing_extensions, uuid.
Where is test_async_callback_manager.py in the architecture?
test_async_callback_manager.py is located at libs/core/tests/unit_tests/callbacks/test_async_callback_manager.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/core/tests/unit_tests/callbacks).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free