How to deploy a LangChain chatbot with FastAPI

A chatbot service needs a stable HTTP boundary before another app, queue worker, or frontend can call it. FastAPI provides that boundary as an ASGI application, while LangChain owns the model invocation behind the request.

The local smoke-test path uses FakeListChatModel so the endpoint can run without provider credentials. The same build_chat_model() function switches to init_chat_model() when LANGCHAIN_MODEL is set and the matching provider package plus API key are available.

Run the service in an activated Python project or container where port 8000 is available. Keep provider API keys in environment variables or a secret store, not in the FastAPI file, because the process loads its model once at startup.

Steps to deploy a LangChain chatbot with FastAPI:

  1. Open an activated Python project environment.
  2. Install LangChain, FastAPI, and Uvicorn.
    $ python3 -m pip install --upgrade langchain fastapi "uvicorn[standard]"

    LangChain provider integrations are separate packages. Install the matching package, such as langchain-openai or langchain-anthropic, before setting LANGCHAIN_MODEL for a real provider.
    Related: How to install LangChain with pip

  3. Create the FastAPI application.
    $ cat > main.py <<'PY'
    import os
     
    from fastapi import FastAPI
    from langchain.chat_models import init_chat_model
    from langchain_core.language_models.fake_chat_models import FakeListChatModel
    from langchain_core.messages import HumanMessage, SystemMessage
    from pydantic import BaseModel
     
     
    class ChatRequest(BaseModel):
        message: str
     
     
    class ChatResponse(BaseModel):
        reply: str
     
     
    def build_chat_model():
        model_name = os.getenv("LANGCHAIN_MODEL")
        if model_name:
            return init_chat_model(model_name)
        return FakeListChatModel(responses=["FastAPI is connected to LangChain."])
     
     
    app = FastAPI(title="LangChain Chatbot API")
    chat_model = build_chat_model()
     
     
    @app.post("/chat", response_model=ChatResponse)
    def chat(request: ChatRequest) -> ChatResponse:
        response = chat_model.invoke([
            SystemMessage(content="Answer as a concise support chatbot."),
            HumanMessage(content=request.message),
        ])
        return ChatResponse(reply=response.text())
    PY

    Leave LANGCHAIN_MODEL unset for the deterministic local smoke test. Set it to a provider-qualified model name, such as openai:gpt-5.5, only after the matching integration package and API key are configured.

  4. Check that Python can load the application file.
    $ python3 -m py_compile main.py

    No output means Python accepted the file syntax.

  5. Start the FastAPI service with Uvicorn.
    $ python3 -m uvicorn main:app --host 127.0.0.1 --port 8000
    INFO:     Started server process [2759]
    INFO:     Waiting for application startup.
    INFO:     Application startup complete.
    INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

    Use --host 0.0.0.0 only when the process must accept connections from outside the local host or container network.

  6. Send a chat request to the endpoint from another terminal.
    $ curl --silent --show-error --request POST http://127.0.0.1:8000/chat \
      --header "Content-Type: application/json" \
      --data '{"message":"Can FastAPI serve this chatbot?"}'
    {"reply":"FastAPI is connected to LangChain."}
  7. Stop the local service after the smoke test.
    Press Ctrl-C in the terminal running Uvicorn.