Skip to main content

Openai Compatible Langgraph Server

Project description

LangGraph OpenAI Serve

A package that provides an OpenAI-compatible API for LangGraph instances.

Features

  • Expose your LangGraph instances through an OpenAI-compatible API
  • Register multiple graphs and map them to different model names
  • Use with any FastAPI application
  • Support for both streaming and non-streaming completions

Installation

pip install langgraph-openai-serve

Quick Start

Here's a simple example of how to use LangGraph OpenAI Serve:

from fastapi import FastAPI
from langgraph_openai_serve import LangchainOpenaiApiServe

# Import your LangGraph instances
from your_graphs import simple_graph_1, advanced_graph

# Create a FastAPI app
app = FastAPI(
    title="LangGraph OpenAI API",
    version="1.0",
    description="OpenAI API exposing LangGraph agents",
)

graph_serve = LangchainOpenaiApiServe(
    app=app,
    graphs={
        "simple_graph_1": simple_graph_1,
        "advanced_graph": advanced_graph
    },
    default_graph="simple_graph_1"  # Optional: specify which graph to use by default
)

# Bind the OpenAI-compatible endpoints
graph_serve.bind_openai_chat_completion(prefix="/v1")

# Run the app with uvicorn
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(graph_serve.app, host="0.0.0.0", port=8000)

Using with the OpenAI Client

Once your API is running, you can use any OpenAI-compatible client to interact with it:

from openai import OpenAI

# Create a client pointing to your API
client = OpenAI(
    base_url="http://localhost:8000/v1",
    api_key="any-value"  # API key is not verified
)

# Use a specific graph by specifying its name as the model
response = client.chat.completions.create(
    model="simple_graph_1",  # This maps to the graph name in your registry
    messages=[
        {"role": "user", "content": "Hello, how can you help me today?"}
    ]
)

print(response.choices[0].message.content)

# You can also use streaming
stream = client.chat.completions.create(
    model="advanced_graph",
    messages=[
        {"role": "user", "content": "Write a short poem about AI."}
    ],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

Docker Usage

To run with Docker:

# Start the server
docker compose up -d langgraph-openai-serve-dev

# For a complete example with open-webui
docker compose up -d open-webui

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langgraph_openai_serve-0.0.1-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file langgraph_openai_serve-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for langgraph_openai_serve-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b1b86c69271ab45b5713a8793272dbaaf356309eac94e706d7598f3388e58066
MD5 ac0f39eed658870010e2200de98f0492
BLAKE2b-256 3c5d3a93b862e77b46e38a92544ad5efe0b4a73a21c7f746d2f23dbbc03538e4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page