How to create custom callback handlers
This guide assumes familiarity with the following concepts:
LangChain has some built-in callback handlers, but you will often want to create your own handlers with custom logic.
To create a custom callback handler, we need to determine theevent(s) we want our callback handler to handle as well as what we want our callback handler to do when the event is triggered. Then all we need to do is attach the callback handler to the object, for example viathe constructor orat runtime.
In the example below, we'll implement streaming with a custom handler.
In our custom callback handlerMyCustomHandler
, we implement theon_llm_new_token
handler to print the token we have just received. We then attach our custom handler to the model object as a constructor callback.
from langchain_anthropicimport ChatAnthropic
from langchain_core.callbacksimport BaseCallbackHandler
from langchain_core.promptsimport ChatPromptTemplate
classMyCustomHandler(BaseCallbackHandler):
defon_llm_new_token(self, token:str,**kwargs)->None:
print(f"My custom handler, token:{token}")
prompt= ChatPromptTemplate.from_messages(["Tell me a joke about {animal}"])
# To enable streaming, we pass in `streaming=True` to the ChatModel constructor
# Additionally, we pass in our custom handler as a list to the callbacks parameter
model= ChatAnthropic(
model="claude-3-sonnet-20240229", streaming=True, callbacks=[MyCustomHandler()]
)
chain= prompt| model
response= chain.invoke({"animal":"bears"})
My custom handler, token: Here
My custom handler, token: 's
My custom handler, token: a
My custom handler, token: bear
My custom handler, token: joke
My custom handler, token: for
My custom handler, token: you
My custom handler, token: :
My custom handler, token:
Why
My custom handler, token: di
My custom handler, token: d the
My custom handler, token: bear
My custom handler, token: dissol
My custom handler, token: ve
My custom handler, token: in
My custom handler, token: water
My custom handler, token: ?
My custom handler, token:
Because
My custom handler, token: it
My custom handler, token: was
My custom handler, token: a
My custom handler, token: polar
My custom handler, token: bear
My custom handler, token: !
You can seethis reference page for a list of events you can handle. Note that thehandle_chain_*
events run for most LCEL runnables.
Next steps
You've now learned how to create your own custom callback handlers.
Next, check out the other how-to guides in this section, such ashow to attach callbacks to a runnable.