Home / How-To / Llama 3 LangChain integration: OpenAI‑compatible endpoint step‑by‑step
Llama 3 LangChain integration: OpenAI‑compatible endpoint step‑by‑step
To connect Meta’s Llama 3 model via its OpenAI‑compatible REST endpoint, install LangChain’s OpenAI extra, create an OpenAI‑style client with the endpoint URL and API key, and then use that client as any other LangChain LLM – for example inside a RAG chain that retrieves documents, embeds them, and passes the context to Llama 3.

What does the OpenAI‑compatible endpoint look like?
Meta released a REST interface that mirrors the OpenAI Chat Completion schema. The base URL is provided in the Meta developer portal and the endpoint expects a JSON payload with model, messages, and optional parameters like temperature. Authentication is a bearer token passed in the Authorization header, e.g. Authorization: Bearer <YOUR_META_KEY>.
> Note – The endpoint follows the same request/response contract as OpenAI, so any client that can talk to OpenAI can be repointed.
Relevant official docs can be found on Meta’s API reference page (link not provided in the sources).
How do I install and configure LangChain for the Meta endpoint?
- Install LangChain with OpenAI support
bash pip install "langchain[openai]"
The openai extra pulls in the openai Python SDK, which LangChain uses under the hood.
- Set environment variables
bash export OPENAI_API_KEY=YOUR_META_KEY export OPENAI_API_BASE=https://api.meta.com/v1 export OPENAI_API_VERSION=2023-08-15 # if required by Meta
LangChain reads these variables when you call init_chat_model.
- Create the chat model
python from langchain.chat_models import init_chat_model
# The model name matches the one in Meta’s catalog, e.g. "meta-llama3" llm = init_chat_model( "openai", model_name="meta-llama3", )
This call tells LangChain to use the OpenAI client but with the overridden base URL from OPENAI_API_BASE.
- Verify the connection
python response = llm.invoke({"messages": [{"role": "user", "content": "Hello, Llama 3!"}]}) print(response)
A successful response confirms that the wrapper is correctly routing calls to Meta’s service.
Source: LangChain’s OpenAI integration guide [[S1]].

How can I build a simple RAG chain with Llama 3?
Below is a minimal Retrieval‑Augmented Generation (RAG) pipeline that uses a local vector store (FAISS) and the Llama 3 chat model.
python # 1. Install supporting libraries !pip install faiss-cpu sentence-transformers
# 2. Load documents (example uses a web loader) from langchain_community.document_loaders import WebBaseLoader loader = WebBaseLoader("https://docs.smith.langchain.com/user_guide") docs = loader.load()
# 3. Split and embed documents from langchain_text_splitters import RecursiveCharacterTextSplitter splitter = RecursiveCharacterTextSplitter() chunks = splitter.split_documents(docs)
from langchain_community.embeddings import OllamaEmbeddings emb = OllamaEmbeddings(model="llama3") # works because Ollama follows OpenAI schema
from langchain_community.vectorstores import FAISS vector = FAISS.from_documents(chunks, emb)
# 4. Prepare the LLM (configured for Meta endpoint earlier) from langchain.chat_models import init_chat_model llm = init_chat_model("openai", model_name="meta-llama3")
# 5. Create a prompt template from langchain_core.prompts import ChatPromptTemplate prompt = ChatPromptTemplate.from_template( "Answer only using the provided context.\nContext: {context}\nQuestion: {input}" )
# 6. Build the document chain and retrieval chain from langchain.chains.combine_documents import create_stuff_documents_chain doc_chain = create_stuff_documents_chain(llm, prompt)
from langchain.chains import create_retrieval_chain retriever = vector.as_retriever() rag_chain = create_retrieval_chain(retriever, doc_chain)
# 7. Query the RAG system result = rag_chain.invoke({"input": "How does LangChain handle tracing?"}) print(result["answer"])
The code follows the step‑by‑step RAG example from a LangChain tutorial that uses Ollama LLM and FAISS vector store [[S3]]. The only change is that the LLM is now the Meta‑hosted Llama 3 model accessed through the OpenAI wrapper.
You can replace the OllamaEmbeddings with any other embedding model that conforms to the OpenAI‑compatible API if you prefer consistency across the stack.
What to do if it does not work?
- Check environment variables – Ensure
OPENAI_API_KEYandOPENAI_API_BASEare set correctly. A typo in the base URL will cause a 404. - Validate the model name – Meta’s catalog may list the model as
meta-llama3,llama3, or a version suffix likellama3-8b. Use the exact string shown in the dashboard. - Inspect the HTTP response – The
openaiSDK raisesOpenAIErrorwith the response body. Look forinvalid_api_keyormodel_not_foundmessages. - Enable debug logging – Set
export LANGCHAIN_TRACING=trueto see full request/response payloads in the console. - Confirm the OpenAI SDK version – LangChain’s wrapper expects at least
openai>=1.0. Upgrade withpip install --upgrade openai. - Test with a curl command to rule out SDK issues:
bash curl -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -d '{"model":"meta-llama3","messages":[{"role":"user","content":"ping"}]}' \ $OPENAI_API_BASE/v1/chat/completions
If the API returns a valid JSON, the problem is in the Python wrapper.
These steps mirror the troubleshooting advice for LangChain’s OpenAI integration [[S1]].
Menus and names can change as products update. If a step looks different, check the product's own help page.
Frequently asked questions
Do I need an Ollama server when using Meta’s endpoint?
No. The OpenAI‑compatible endpoint is a cloud service; Ollama is only required if you prefer a local model.
Can I use function calling with Llama 3 through this wrapper?
Yes. Because the endpoint follows the OpenAI schema, you can pass a functions array in the request payload just like with ChatGPT.
Is there a rate limit for the Meta Llama 3 API?
Meta publishes quota details in the developer console; the limits are not part of LangChain’s documentation.
How do I switch from Meta’s endpoint to a local Ollama instance?
Replace OPENAI_API_BASE with the local Ollama URL (e.g., http://localhost:11434/v1) and set the model name to the local tag, such as llama3.