langchain/libs/core/langchain_core/callbacks
Bharat Ramanathan 931ce8d026
core[patch]: Update AsyncCallbackManager to honor run_inline attribute and prevent context loss (#26885)
## Description

This PR fixes the context loss issue in `AsyncCallbackManager`,
specifically in `on_llm_start` and `on_chat_model_start` methods. It
properly honors the `run_inline` attribute of callback handlers,
preventing race conditions and ordering issues.

Key changes:
1. Separate handlers into inline and non-inline groups.
2. Execute inline handlers sequentially for each prompt.
3. Execute non-inline handlers concurrently across all prompts.
4. Preserve context for stateful handlers.
5. Maintain performance benefits for non-inline handlers.

**These changes are implemented in `AsyncCallbackManager` rather than
`ahandle_event` because the issue occurs at the prompt and message_list
levels, not within individual events.**

## Testing

- Test case implemented in #26857 now passes, verifying execution order
for inline handlers.

## Related Issues

- Fixes issue discussed in #23909 

## Dependencies

No new dependencies are required.

---

@eyurtsev: This PR implements the discussed changes to respect
`run_inline` in `AsyncCallbackManager`. Please review and advise on any
needed changes.

Twitter handle: @parambharat

---------

Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
2024-10-07 14:59:29 -04:00
..
__init__.py core[minor]: Add dispatching for custom events (#24080) 2024-07-11 02:25:12 +00:00
base.py core: Put Python version as a project requirement so it is considered by ruff (#26608) 2024-09-18 14:37:57 +00:00
file.py core[patch]: Add ruff rules for flake8-simplify (SIM) (#26848) 2024-09-27 20:13:23 +00:00
manager.py core[patch]: Update AsyncCallbackManager to honor run_inline attribute and prevent context loss (#26885) 2024-10-07 14:59:29 -04:00
stdout.py core[patch]: On Chain Start Fix for Chain Class (#26593) 2024-09-23 19:30:59 +00:00
streaming_stdout.py core: Add ruff rules for PIE (#26939) 2024-09-27 12:08:35 -04:00