langchain-ai / langserve

LangServe 🦜️🏓
Other
1.75k stars 184 forks source link

Running background tasks #696

Open Sara2823 opened 4 days ago

Sara2823 commented 4 days ago

How to add a FastAPI BackgroundTask to a LangServe endpoint?

We need to save chat history to a database after each request, and not block the user input until the messages are saved.

This is how we add the endpoint to the app:

add_routes(
    app,
    agent,
    path="/agent",
    per_req_config_modifier=per_request_config_modifier,
    config_keys=["configurable", "metadata"]
)

Thanks

eyurtsev commented 3 days ago

Hi @Sara2823 you can use the APIHandler directly if you need fine grained control over the endpoint definition (e.g., to expose background tasks).