LangGraph Streaming Server (FastAPI)
LangGraph Streaming Server (FastAPI)
Section titled “LangGraph Streaming Server (FastAPI)”Latest: 1.0.3 Last verified: 2025-11
from fastapi import FastAPIfrom fastapi.responses import StreamingResponsefrom langgraph.graph import StateGraph, END
app = FastAPI()
def build(): g = StateGraph(dict) g.add_node("echo", lambda s: {**s, "out": s["in"]}) g.set_entry_point("echo") g.add_edge("echo", END) return g.compile()
graph = build()
@app.get("/stream")def stream(q: str): def gen(): for ev in graph.stream({"in": q}): yield f"data: {ev}\n\n" return StreamingResponse(gen(), media_type="text/event-stream")Deployment
Section titled “Deployment”Dockerfile
Section titled “Dockerfile”FROM python:3.11-slimWORKDIR /appCOPY requirements.txt .RUN pip install -r requirements.txtCOPY . .EXPOSE 8080CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8080"]Kubernetes (deployment.yaml)
Section titled “Kubernetes (deployment.yaml)”apiVersion: apps/v1kind: Deploymentmetadata: name: langgraph-streamspec: replicas: 2 selector: matchLabels: app: langgraph-stream template: metadata: labels: app: langgraph-stream spec: containers: - name: app image: ghcr.io/yourorg/langgraph-stream:latest ports: [{ containerPort: 8080 }]---apiVersion: v1kind: Servicemetadata: name: langgraph-streamspec: selector: { app: langgraph-stream } ports: [{ port: 80, targetPort: 8080 }]GitHub Actions
Section titled “GitHub Actions”name: deployon: { push: { branches: [ main ] } }jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: docker/login-action@v3 with: { registry: ghcr.io, username: ${{ github.actor }}, password: ${{ secrets.GITHUB_TOKEN }} } - uses: docker/build-push-action@v5 with: { push: true, tags: ghcr.io/${{ github.repository }}:latest } deploy: needs: build runs-on: ubuntu-latest steps: - uses: azure/k8s-deploy@v5 with: action: deploy manifests: | deployment.yaml images: ghcr.io/${{ github.repository }}:latest namespace: defaultSecurity Best Practices
Section titled “Security Best Practices”- Authenticate clients; use JWT or a signed token for SSE
- Rate limit per user/IP; enforce timeouts and idle disconnect
- Set CORS appropriately; disable caching headers for SSE