The official LangChain integration for DeepInfra — chat models, embeddings, and reranking.
pip install -U langchain-deepinfra
export DEEPINFRA_API_TOKEN="your-api-token"Get an API token from the DeepInfra dashboard.
ChatDeepInfra wraps DeepInfra's OpenAI-compatible Chat Completions endpoint, so it
supports tool calling, structured output, streaming, async, and multimodal inputs.
from langchain_deepinfra import ChatDeepInfra
llm = ChatDeepInfra(model="meta-llama/Llama-3.3-70B-Instruct", temperature=0)
llm.invoke("What is the capital of France?")from langchain_deepinfra import DeepInfraEmbeddings
embeddings = DeepInfraEmbeddings(model="Qwen/Qwen3-Embedding-8B")
embeddings.embed_query("Hello, world!")
embeddings.embed_documents(["doc one", "doc two"])DeepInfraRerank is a BaseDocumentCompressor, so it drops into a
ContextualCompressionRetriever.
from langchain_deepinfra import DeepInfraRerank
reranker = DeepInfraRerank(model="Qwen/Qwen3-Reranker-4B", top_n=3)
reranked = reranker.compress_documents(documents, "my query")
# each returned Document carries metadata["relevance_score"]| Argument | Env var | Default |
|---|---|---|
api_key |
DEEPINFRA_API_TOKEN |
— (required) |
base_url |
DEEPINFRA_API_BASE |
https://api.deepinfra.com/v1/openai |
DeepInfra's chat and embeddings APIs are OpenAI-compatible, so those classes are thin
subclasses of langchain-openai:
ChatDeepInfrasubclassesBaseChatOpenAI. It overrides only the credential / base-URL fields (DEEPINFRA_API_TOKEN,https://api.deepinfra.com/v1/openai) and rebuilds the OpenAI client invalidate_environment. All chat features are inherited.DeepInfraEmbeddingssubclassesOpenAIEmbeddingsthe same way, withcheck_embedding_ctx_length=False(DeepInfra models are not tokenized with tiktoken).DeepInfraRerankis a standaloneBaseDocumentCompressor. Rerank is not OpenAI-compatible: it callsPOST /v1/inference/{model}with parallelqueries/documentsarrays (the query is repeated once per document) and reads one relevancescoreper document from the response.
uv sync # install
make unit_test # offline unit tests
make lint type_check # ruff + mypy
make integration_test # live tests (needs DEEPINFRA_API_TOKEN)MIT