handle_event() — langchain Function Reference
Architecture documentation for the handle_event() function in manager.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 8964b4e9_440e_18a7_2a15_a54a0b2b733c["handle_event()"] ef55be46_0333_682d_8311_b4dd35c3e34c["manager.py"] 8964b4e9_440e_18a7_2a15_a54a0b2b733c -->|defined in| ef55be46_0333_682d_8311_b4dd35c3e34c 40737de9_fdbb_ebe0_d5f1_fe2a91895e24["on_text()"] 40737de9_fdbb_ebe0_d5f1_fe2a91895e24 -->|calls| 8964b4e9_440e_18a7_2a15_a54a0b2b733c 59c3fe59_2154_bc88_4a8b_47bb9167ce24["on_retry()"] 59c3fe59_2154_bc88_4a8b_47bb9167ce24 -->|calls| 8964b4e9_440e_18a7_2a15_a54a0b2b733c c817e64b_0cff_aea1_66cf_1dde9e8eda41["on_llm_new_token()"] c817e64b_0cff_aea1_66cf_1dde9e8eda41 -->|calls| 8964b4e9_440e_18a7_2a15_a54a0b2b733c bfcf2566_a20a_41fe_25c4_91010073fdeb["on_llm_end()"] bfcf2566_a20a_41fe_25c4_91010073fdeb -->|calls| 8964b4e9_440e_18a7_2a15_a54a0b2b733c 8656516c_f1cc_451d_5ba7_c245448ce87d["on_llm_error()"] 8656516c_f1cc_451d_5ba7_c245448ce87d -->|calls| 8964b4e9_440e_18a7_2a15_a54a0b2b733c 3b7be403_b5f2_f25b_2714_6d61955e1332["on_chain_end()"] 3b7be403_b5f2_f25b_2714_6d61955e1332 -->|calls| 8964b4e9_440e_18a7_2a15_a54a0b2b733c 07a4c44a_3cfb_9b0a_ffde_c1bbfeb4080a["on_chain_error()"] 07a4c44a_3cfb_9b0a_ffde_c1bbfeb4080a -->|calls| 8964b4e9_440e_18a7_2a15_a54a0b2b733c f0cd3c05_aee4_21c7_c860_78b7d129beb7["on_agent_action()"] f0cd3c05_aee4_21c7_c860_78b7d129beb7 -->|calls| 8964b4e9_440e_18a7_2a15_a54a0b2b733c 4e512a22_0568_2bb2_c4ef_7f5bb6e9d351["on_agent_finish()"] 4e512a22_0568_2bb2_c4ef_7f5bb6e9d351 -->|calls| 8964b4e9_440e_18a7_2a15_a54a0b2b733c b9e1e1b1_29a3_f8f4_77f9_b2d38254e1c6["on_tool_end()"] b9e1e1b1_29a3_f8f4_77f9_b2d38254e1c6 -->|calls| 8964b4e9_440e_18a7_2a15_a54a0b2b733c 02639266_0981_41cb_ea80_7d68e6d0a8a5["on_tool_error()"] 02639266_0981_41cb_ea80_7d68e6d0a8a5 -->|calls| 8964b4e9_440e_18a7_2a15_a54a0b2b733c 4d0370de_fc12_b768_9ed2_62d72893e01a["on_retriever_end()"] 4d0370de_fc12_b768_9ed2_62d72893e01a -->|calls| 8964b4e9_440e_18a7_2a15_a54a0b2b733c 74659e38_64dc_e4e3_f73a_4f37bf2e3617["on_retriever_error()"] 74659e38_64dc_e4e3_f73a_4f37bf2e3617 -->|calls| 8964b4e9_440e_18a7_2a15_a54a0b2b733c style 8964b4e9_440e_18a7_2a15_a54a0b2b733c fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/core/langchain_core/callbacks/manager.py lines 256–336
def handle_event(
handlers: list[BaseCallbackHandler],
event_name: str,
ignore_condition_name: str | None,
*args: Any,
**kwargs: Any,
) -> None:
"""Generic event handler for `CallbackManager`.
Args:
handlers: The list of handlers that will handle the event.
event_name: The name of the event (e.g., `'on_llm_start'`).
ignore_condition_name: Name of the attribute defined on handler that if `True`
will cause the handler to be skipped for the given event.
*args: The arguments to pass to the event handler.
**kwargs: The keyword arguments to pass to the event handler
"""
coros: list[Coroutine[Any, Any, Any]] = []
try:
message_strings: list[str] | None = None
for handler in handlers:
try:
if ignore_condition_name is None or not getattr(
handler, ignore_condition_name
):
event = getattr(handler, event_name)(*args, **kwargs)
if asyncio.iscoroutine(event):
coros.append(event)
except NotImplementedError as e:
if event_name == "on_chat_model_start":
if message_strings is None:
message_strings = [get_buffer_string(m) for m in args[1]]
handle_event(
[handler],
"on_llm_start",
"ignore_llm",
args[0],
message_strings,
*args[2:],
**kwargs,
)
else:
handler_name = handler.__class__.__name__
logger.warning(
"NotImplementedError in %s.%s callback: %s",
handler_name,
event_name,
repr(e),
)
except Exception as e:
logger.warning(
"Error in %s.%s callback: %s",
handler.__class__.__name__,
event_name,
repr(e),
)
if handler.raise_error:
raise
finally:
if coros:
try:
# Raises RuntimeError if there is no current event loop.
asyncio.get_running_loop()
loop_running = True
except RuntimeError:
loop_running = False
if loop_running:
# If we try to submit this coroutine to the running loop
# we end up in a deadlock, as we'd have gotten here from a
# running coroutine, which we cannot interrupt to run this one.
# The solution is to run the synchronous function on the globally shared
# thread pool executor to avoid blocking the main event loop.
_executor().submit(
cast("Callable", copy_context().run), _run_coros, coros
).result()
else:
# If there's no running loop, we can run the coroutines directly.
_run_coros(coros)
Domain
Subdomains
Calls
Called By
Source
Frequently Asked Questions
What does handle_event() do?
handle_event() is a function in the langchain codebase, defined in libs/core/langchain_core/callbacks/manager.py.
Where is handle_event() defined?
handle_event() is defined in libs/core/langchain_core/callbacks/manager.py at line 256.
What does handle_event() call?
handle_event() calls 2 function(s): _executor, _run_coros.
What calls handle_event()?
handle_event() is called by 19 function(s): on_agent_action, on_agent_finish, on_chain_end, on_chain_error, on_chain_start, on_chat_model_start, on_custom_event, on_llm_end, and 11 more.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free