servery.py on_event deprecated #116

Open
opened 2026-02-16 00:18:52 -05:00 by yindo · 3 comments
Owner

Originally created by @albertoecf on GitHub (Feb 5, 2024).

Originally assigned to: @eyurtsev on GitHub.

Issue Description:

The on_event decorator used in langserve/server.py is deprecated. As of the current FastAPI version, lifespan event handlers should be used instead of on_event. This change is in line with the deprecation warning message.

Recommendation:

Please consider updating the code to use the new lifespan event handlers. You can find more information about lifespan events in the FastAPI documentation.

Motivation

While writing tests for our Langchain app, we encountered deprecation warnings related to the usage of the on_event decorator in the test setup.

from fastapi.testclient import TestClient
import pytest
from app.server import app

client = TestClient(app)

def test_server_startup():
    """
    Test if the FastAPI server starts correctly and responds to /docs endpoint.
    """
    with client:
        response = client.get("/docs")
        assert response.status_code == 200

warnings:

DeprecationWarning: on_event is deprecated, use lifespan event handlers instead.
Read more about it in the FastAPI docs for Lifespan Events.

Originally created by @albertoecf on GitHub (Feb 5, 2024). Originally assigned to: @eyurtsev on GitHub. ## Issue Description: The on_event decorator used in [langserve/server.py](https://github.com/langchain-ai/langserve/blob/90bf613711c873e640bfa3224d013532744288a4/langserve/server.py#L46) is deprecated. As of the current FastAPI version, lifespan event handlers should be used instead of on_event. This change is in line with the deprecation warning message. ### Recommendation: Please consider updating the code to use the new lifespan event handlers. You can find more information about lifespan events in the [FastAPI documentation](https://fastapi.tiangolo.com/advanced/events/). ### Motivation While writing tests for our Langchain app, we encountered deprecation warnings related to the usage of the on_event decorator in the test setup. ``` from fastapi.testclient import TestClient import pytest from app.server import app client = TestClient(app) def test_server_startup(): """ Test if the FastAPI server starts correctly and responds to /docs endpoint. """ with client: response = client.get("/docs") assert response.status_code == 200 ``` warnings: DeprecationWarning: on_event is deprecated, use lifespan event handlers instead. Read more about it in the [FastAPI docs for Lifespan Events](https://fastapi.tiangolo.com/advanced/events/).
Author
Owner

@eyurtsev commented on GitHub (Feb 5, 2024):

Thanks for reporting the issue!

We unfortunately can't use life-span events. They require the ability to instantiate FastAPI app, which LangServe doesn't have since instantiation is controlled by the user.

We use on_event to display the splash screen when the server is loaded.

We will revisit this issue when the functionality will be deleted from FastAPI (rather than only marked for deprecation).

@eyurtsev commented on GitHub (Feb 5, 2024): Thanks for reporting the issue! We unfortunately can't use life-span events. They require the ability to instantiate FastAPI app, which LangServe doesn't have since instantiation is controlled by the user. We use on_event to display the splash screen when the server is loaded. We will revisit this issue when the functionality will be deleted from FastAPI (rather than only marked for deprecation).
Author
Owner

@albertoecf commented on GitHub (Feb 6, 2024):

Thanks @eyurtsev , make sense!

Should we close the issue and re open it when we are facing deletion from FastAPI?

@albertoecf commented on GitHub (Feb 6, 2024): Thanks @eyurtsev , make sense! Should we close the issue and re open it when we are facing deletion from FastAPI?
Author
Owner

@eyurtsev commented on GitHub (Feb 6, 2024):

hmm let's actually keep open for now -- changed my mind, we should probably start thinking about this before the fast api actually deletes functionality

@eyurtsev commented on GitHub (Feb 6, 2024): hmm let's actually keep open for now -- changed my mind, we should probably start thinking about this before the fast api actually deletes functionality
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langserve#116