Skip to main content
LangChain is a popular framework for building LLM-powered applications. CometAPI is fully compatible with langchain-openai — just point it at our base URL.

Prerequisites

  • Python 3.8+ and langchain-openai installed (pip install langchain-openai)
  • A CometAPI account with an active API key

Setup

1

Get your CometAPI API key

Log in to the CometAPI dashboard. Click Add API Key and copy your key and base URL (https://api.cometapi.com).
CometAPI dashboard — Add API Key
CometAPI API key and base URL
2

Configure the base URL and API key

Set your CometAPI key and base URL when initializing ChatOpenAI.Option A — Environment variables (recommended):
export OPENAI_API_KEY=<COMETAPI_KEY>
export OPENAI_API_BASE=https://api.cometapi.com/v1
Option B — Inline configuration:
LangChain code showing ChatOpenAI initialized with CometAPI base URL and key
Code example — ChatOpenAI initialization with CometAPI parameters
3

Test the connection

Run a simple chain (e.g., a prompt asking for the current date). A successful response confirms CometAPI is connected.
Use base_url="https://api.cometapi.com/v1" and api_key="<COMETAPI_KEY>" when initializing ChatOpenAI or OpenAI from langchain-openai. Any model name from the CometAPI model list will work.