
LANGCHAIN — What Are Callbacks Used For?
Talk is cheap. Show me the code. — Linus Torvalds
Callbacks are a crucial aspect of programming, especially in concurrent and asynchronous environments. They are essential for handling events, notifications, and handling the completion of certain tasks. In LangChain, callbacks play a vital role in powering logging, tracing, streaming output, and third-party integrations.
The recent improvements to LangChain’s callbacks system have addressed various issues and introduced new features to better support concurrent runs with independent callbacks, tracing of deeply nested trees of LangChain components, and callback handlers scoped to a single request, which is particularly useful for deploying LangChain on a server.
Let’s dive into the changes and see how callbacks are utilized in LangChain.
Constructor and Request Callbacks
You can now declare which callbacks you want either in constructor arguments or by passing them directly to the run, call, or apply methods that start a run. Constructor callbacks will be used for all calls made on that object and will be scoped to that object only. Request callbacks, on the other hand, will be used for that specific request only and all sub-requests that it contains. Here's an example in TypeScript:
const callbackManager = new CallbackManager();
callbackManager.addHandler(new ConsoleCallbackHandler());
callbackManager.addHandler(new LangChainTracer());
const model = new OpenAI({ temperature: 0, callbackManager });
const executor = await initializeAgentExecutor(tools, model, "zero-shot-react-description", true, callbackManager);Run Manager
Methods on Chains, LLMs, Chat Models, Agents, and Tools now receive a runManager as a second argument, which is bound to that run and contains the logging methods that can be used by that object. This is useful when constructing custom chains.
Verbose Argument
The verbose argument now serves as a shortcut to add a ConsoleCallbackHandler in JavaScript and StdOutCallbackHandler in Python that prints events to stdout. It does not control other callbacks.
Tracing and Concurrency
Tracing and other callbacks now work seamlessly with concurrency, and a context manager has been added to make tracing specific runs even easier.
Deprecations
Some breaking changes and deprecations have been introduced, such as deprecating the use of global callbacks or the global tracer outside of LangChain and attaching a CallbackManager to an object.
In conclusion, callbacks in LangChain are used for various purposes such as logging, tracing, and handling events. The recent improvements have made the system more robust and better suited for concurrent and asynchronous environments.
This tutorial has provided an overview of the improvements to the LangChain callbacks system and how callbacks are used in practice. If you encounter any issues, the LangChain team encourages you to reach out for assistance as these changes represent a significant update to the system.






