Request: tracer equivalent of AsyncCallbackHandler
#19237
Replies: 1 comment
-
I second this FR. One potential use case is with RunnableWithMessageHistory. I have a RAG app that uses Beanie so I got it in my head it'd be a good idea to make an async version of MongoDBChatMessageHistory just for consistency's sake. It works fine if you handle messages manually but if you try to plug in an async ChatMessageHistory to RunnableWithMessageHistory it doesn't work since the _exit_history method callback is attached with the Runnable with_listeners method, which binds a RootListenersTracer as the callback handler:
I'm sure there's an easier way, but one workaround for now is to rewrite the entire BaseTracer class as async then make a corresponding async RootListenersTracer.
Example method:
And for the RootListenersTracer same thing just subclass your AsyncBaseTracer:
Example method:
This lets you pass async Listener functions as your args. To use this with RunnableWithMessageHistory, I had to rewrite that entire class as async too, and change the init method to use the AsyncRootListenersTracer instead of the Runnable with_listeners method:
Then finally, you can use an async add_messages function in your exit method:
This approach does run at least. The callbacks are triggered successfully and the chat history is added to Mongo with Beanie/motor. I figured since BaseTracer inherits from BaseCallbackHandler an async version should work the same as a standard AsyncCallbackHandler, with the coros getting run by the manager with ahandle_event, but I'm not sure if there are any undesirable side-effects from doing this sort of thing. I was just curious if I could get it to work. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked
Feature request
A
BaseTracer
subclass that works with withasyncio
, inlangchain-core
.Think
AsyncCallbackHandler
converted to a tracer, named perhapsAsyncTracer
.Motivation
I am migrating my code base from
BaseCallbackHandler
toBaseTracer
. I have anAsyncCallbackHandler
I would like to migrate, but there is not an async subclass forBaseTracer
.Proposal (If applicable)
No response
Beta Was this translation helpful? Give feedback.
All reactions