Skip to main content

An integration package connecting LiteLLM and LangChain

Project description

langchain-litellm

📦 Distribution 🔧 Project 🚀 Activity
PyPI Package Version
PyPI Downloads
License: MIT
Platform
Python
uv
GitHub Issues Closed
GitHub Issues Open
GitHub PRs Open
GitHub PRs Closed

🤔 What is this?

This package contains the LangChain integration with LiteLLM. LiteLLM is a library that simplifies calling Anthropic, Azure, Huggingface, Replicate, etc.

📖 Documentation

For conceptual guides, tutorials, and examples on using these classes, see the LangChain Docs.

Advanced Features

Embeddings

Supported in v0.6.1+

Use LiteLLMEmbeddings to embed text across 100+ providers with a single, consistent interface. All configuration is explicit -- no environment variables required.

from langchain_litellm import LiteLLMEmbeddings

embeddings = LiteLLMEmbeddings(
    model="openai/text-embedding-3-small",
    api_key="sk-...",
)

vectors = embeddings.embed_documents(["hello", "world"])
query_vector = embeddings.embed_query("hello")

Switch providers by changing model -- the interface stays the same:

# Cohere
embeddings = LiteLLMEmbeddings(
    model="cohere/embed-english-v3.0",
    api_key="...",
    document_input_type="search_document",
    query_input_type="search_query",
)

# Azure OpenAI
embeddings = LiteLLMEmbeddings(
    model="azure/my-embedding-deployment",
    api_key="...",
    api_base="https://my-resource.openai.azure.com",
    api_version="2024-02-01",
)

# Bedrock
embeddings = LiteLLMEmbeddings(
    model="bedrock/amazon.titan-embed-text-v1",
)

For load-balancing across multiple deployments of the same model, use LiteLLMEmbeddingsRouter:

from litellm import Router
from langchain_litellm import LiteLLMEmbeddingsRouter

router = Router(model_list=[
    {
        "model_name": "text-embedding-3-small",
        "litellm_params": {
            "model": "openai/text-embedding-3-small",
            "api_key": "sk-key1",
        },
    },
    {
        "model_name": "text-embedding-3-small",
        "litellm_params": {
            "model": "openai/text-embedding-3-small",
            "api_key": "sk-key2",
        },
    },
])

embeddings = LiteLLMEmbeddingsRouter(router=router)
Vertex AI Grounding (Google Search)

Supported in v0.3.5+

You can use Google Search grounding with Vertex AI models (e.g., gemini-2.5-flash). Citations and metadata are returned in response_metadata (Batch) or additional_kwargs (Streaming).

Setup

import os
from langchain_litellm import ChatLiteLLM

os.environ["VERTEX_PROJECT"] = "your-project-id"
os.environ["VERTEX_LOCATION"] = "us-central1"

llm = ChatLiteLLM(model="vertex_ai/gemini-2.5-flash", temperature=0)

Batch Usage

# Invoke with Google Search tool enabled
response = llm.invoke(
    "What is the current stock price of Google?",
    tools=[{"googleSearch": {}}]
)

# Access Citations & Metadata
provider_fields = response.response_metadata.get("provider_specific_fields")
if provider_fields:
    # Vertex returns a list; the first item contains the grounding info
    print(provider_fields[0])

Streaming Usage

stream = llm.stream(
    "What is the current stock price of Google?",
    tools=[{"googleSearch": {}}]
)

for chunk in stream:
    print(chunk.content, end="", flush=True)
    # Metadata is injected into the chunk where it arrives
    if "provider_specific_fields" in chunk.additional_kwargs:
        print("\n[Metadata Found]:", chunk.additional_kwargs["provider_specific_fields"])

📕 Releases & Versioning

See our Releases and Versioning policies.

💁 Contributing

As an open-source project in a rapidly developing field, we are extremely open to contributions, whether it be in the form of a new feature, improved infrastructure, or better documentation.

For detailed information on how to contribute, see the Contributing Guide.

Project details


Download files

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

Source Distribution

langchain_litellm-0.6.5.tar.gz (339.6 kB view details)

Uploaded Source

Built Distribution

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

langchain_litellm-0.6.5-py3-none-any.whl (26.4 kB view details)

Uploaded Python 3

File details

Details for the file langchain_litellm-0.6.5.tar.gz.

File metadata

  • Download URL: langchain_litellm-0.6.5.tar.gz
  • Upload date:
  • Size: 339.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for langchain_litellm-0.6.5.tar.gz
Algorithm Hash digest
SHA256 30741fda59803336d0d39788be441f6ccd2b4e41d7747ff0d2b002950a07453b
MD5 cbe693e60451513d9f4c229affdb2438
BLAKE2b-256 21420b9eea0b57dd225850f9965c1d77d84ad7be1f5101c9040143e8751cfbd6

See more details on using hashes here.

File details

Details for the file langchain_litellm-0.6.5-py3-none-any.whl.

File metadata

  • Download URL: langchain_litellm-0.6.5-py3-none-any.whl
  • Upload date:
  • Size: 26.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for langchain_litellm-0.6.5-py3-none-any.whl
Algorithm Hash digest
SHA256 dce2ebfddddd0dfd6b1ed473399ccc095dd2f5cb6adfe1336d7bbe489ef32b4b
MD5 9d5e66671863a6aa53cf824c0e5a9f50
BLAKE2b-256 5e4899e81a0d33334f3bc7c310d15c19a2a972d0cf8d708c8369258a5db2d74e

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