LangChain
Build apps and agents on TunAI
Documentation
Use TunAI with LangChain
LangChain's OpenAI integration works with TunAI: set the base URL and your key (in Python, also keep the Responses API off), and every model we serve is available to your chains and agents, billed to your TunAI balance in TND.
Pick one to fill in the samples, or see all supported models.
1Install the package and set your key
pip install langchain-openaiexport TUNAI_API_KEY="YOUR_API_KEY" # your sk-tunai-... key2Make your first call
import os
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="<model-id>",
base_url="https://api.tunai.tn/v1",
api_key=os.environ["TUNAI_API_KEY"],
use_responses_api=False, # TunAI serves /v1/chat/completions
)
print(llm.invoke("Hello!").content)3Stream and call tools
Streaming and tool calling work as with OpenAI. In Python:
for chunk in llm.stream("Write a haiku about Tunis."):
print(chunk.content, end="", flush=True)from langchain_core.tools import tool
@tool
def get_weather(city: str) -> str:
"""Get the weather for a city."""
return f"Sunny in {city}"
reply = llm.bind_tools([get_weather]).invoke("What's the weather in Sfax?")
print(reply.tool_calls)4Embeddings (optional)
import os
from langchain_openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings(
model="text-embedding-3-small",
base_url="https://api.tunai.tn/v1",
api_key=os.environ["TUNAI_API_KEY"],
check_embedding_ctx_length=False, # send plain text, not token ids
)
vectors = embeddings.embed_documents(["Tunis", "Sfax", "Sousse"])Troubleshooting
401 invalid API keyCheck that TUNAI_API_KEY is set in the environment your code runs in.402 insufficient_quotaYour balance is empty or the key reached its monthly budget. Top up on Billing.404 model_not_foundmodel must be the exact TunAI id. The error names the closest one; the full list is on Supported models.404 Not Found when tools are boundWith tools, Python's langchain-openai switches GPT models to OpenAI's Responses API, which TunAI doesn't serve. Keep use_responses_api=False in ChatOpenAI.Embeddings failUse an embedding model (see the list) and keep check_embedding_ctx_length=False.