Skip to content

langchain-google-vertexai

LangChain Google Generative AI Integration.

This module contains the LangChain integrations for Vertex AI service - Google foundational models, third-party foundational modela available on Vertex Model Garden and.

Supported integrations

  1. Google's foundational models: Gemini family, Codey, embeddings - ChatVertexAI, VertexAI, VertexAIEmbeddings.
  2. Other Google's foundational models: Imagen - VertexAIImageCaptioning, VertexAIImageCaptioningChat, VertexAIImageEditorChat, VertexAIImageGeneratorChat, VertexAIVisualQnAChat.
  3. Third-party foundational models available as a an API (mdel-as-a-service) on Vertex Model Garden (Mistral, Llama, Anthropic) - model_garden.ChatAnthropicVertex, model_garden_maas.VertexModelGardenLlama, model_garden_maas.VertexModelGardenMistral.
  4. Third-party foundational models deployed on Vertex AI endpoints from Vertex Model Garden or Huggingface - VertexAIModelGarden.
  5. Gemma deployed on Vertex AI endpoints or locally - GemmaChatLocalHF, GemmaChatLocalKaggle, GemmaChatVertexAIModelGarden, GemmaLocalHF, GemmaLocalKaggle, GemmaVertexAIModelGarden.
  6. Vector Search on Vertex AI - VectorSearchVectorStore, VectorSearchVectorStoreDatastore, VectorSearchVectorStoreGCS.
  7. Vertex AI evaluators for generative AI - VertexPairWiseStringEvaluator, VertexStringEvaluator.

Take a look at detailed documentation for each class for further details.

Installation

.. code-block:: bash pip install langchain-google-vertexai

You need to enable required Google Cloud APIs (depending on the integration you're using) and set up credentials by either: - Having credentials configured for your environment (gcloud, workload identity, etc...) - Storing the path to a service account JSON file as the GOOGLE_APPLICATION_CREDENTIALS environment variable

This codebase uses the google.auth library which first looks for the application credentials variable mentioned above, and then looks for system-level auth.

More information:

  • Credential types <https://cloud.google.com/docs/authentication/application-default-credentials#GAC>__
  • google.auth API reference <https://googleapis.dev/python/google-auth/latest/reference/google.auth.html#module-google.auth>__

Modules:

Name Description
callbacks
chains
chat_models

Wrapper around Google VertexAI chat-based models.

embeddings
evaluators
functions_utils
gemma
llms
model_garden
model_garden_maas
utils
vectorstores
vision_models

Classes:

Name Description
ChatVertexAI

Google Cloud Vertex AI chat model integration.

VertexAIEmbeddings

Google Cloud VertexAI embedding models.

VertexPairWiseStringEvaluator

Evaluate the perplexity of a predicted string.

VertexStringEvaluator

Evaluate the perplexity of a predicted string.

PydanticFunctionsOutputParser

Parse an output as a pydantic object.

GemmaChatLocalHF
GemmaChatLocalKaggle
GemmaChatVertexAIModelGarden
GemmaLocalHF

Local gemma model loaded from HuggingFace.

GemmaLocalKaggle

Local gemma chat model loaded from Kaggle.

VertexAI

Google Vertex AI large language models.

VertexAIModelGarden

Large language models served from Vertex AI Model Garden.

DataStoreDocumentStorage

Stores documents in Google Cloud DataStore.

GCSDocumentStorage

Stores documents in Google Cloud Storage.

VectorSearchVectorStore

VertexAI VectorStore that handles the search and indexing using Vector Search

VectorSearchVectorStoreDatastore

VectorSearch with DatasTore document storage.

VectorSearchVectorStoreGCS

Alias of VectorSearchVectorStore for consistency with the rest of vector

VertexAIImageCaptioning

Implementation of the Image Captioning model as an LLM.

VertexAIImageCaptioningChat

Implementation of the Image Captioning model as a chat.

VertexAIImageEditorChat

Given an image and a prompt, edits the image.

VertexAIImageGeneratorChat

Generates an image from a prompt.

VertexAIVisualQnAChat

Chat implementation of a visual QnA model.

Functions:

Name Description
create_structured_runnable

Create a runnable sequence that uses OpenAI functions.

get_vertex_maas_model

Return a corresponding Vertex MaaS instance.

create_context_cache

Creates a cache for content in some model.

ChatVertexAI

Bases: _VertexAICommon, BaseChatModel

Google Cloud Vertex AI chat model integration.

Setup

You must either: - Have credentials configured for your environment (gcloud, workload identity, etc...) - Store the path to a service account JSON file as the GOOGLE_APPLICATION_CREDENTIALS environment variable

This codebase uses the google.auth library which first looks for the application credentials variable mentioned above, and then looks for system-level auth.

More information:

  • Credential types <https://cloud.google.com/docs/authentication/application-default-credentials#GAC>__
  • google.auth API reference <https://googleapis.dev/python/google-auth/latest/reference/google.auth.html#module-google.auth>__

Key init args — completion params: model: str Name of ChatVertexAI model to use. e.g. 'gemini-2.0-flash-001', 'gemini-2.5-pro', etc. temperature: Optional[float] Sampling temperature. seed: Optional[int] Sampling integer to use. max_tokens: Optional[int] Max number of tokens to generate. stop: Optional[List[str]] Default stop sequences. safety_settings: Optional[Dict[vertexai.generative_models.HarmCategory, vertexai.generative_models.HarmBlockThreshold]] The default safety settings to use for all generations.

Key init args — client params: max_retries: int Max number of retries. wait_exponential_kwargs: Optional[dict[str, float]] Optional dictionary with parameters for wait_exponential: - multiplier: Initial wait time multiplier (default: 1.0) - min: Minimum wait time in seconds (default: 4.0) - max: Maximum wait time in seconds (default: 10.0) - exp_base: Exponent base to use (default: 2.0) credentials: Optional[google.auth.credentials.Credentials] The default custom credentials to use when making API calls. If not provided, credentials will be ascertained from the environment. project: Optional[str] The default GCP project to use when making Vertex API calls. location: str = "us-central1" The default location to use when making API calls. request_parallelism: int = 5 The amount of parallelism allowed for requests issued to VertexAI models. (default: 5) base_url: Optional[str] Base URL for API requests.

See full list of supported init args and their descriptions in the params section.

Instantiate

.. code-block:: python

from langchain_google_vertexai import ChatVertexAI

llm = ChatVertexAI(
    model="gemini-1.5-flash-001",
    temperature=0,
    max_tokens=None,
    max_retries=6,
    stop=None,
    # other params...
)
Invoke

.. code-block:: python

messages = [
    (
        "system",
        "You are a helpful translator. Translate the user sentence to French.",
    ),
    ("human", "I love programming."),
]
llm.invoke(messages)

.. code-block:: python

AIMessage(
    content="J'adore programmer. ",
    response_metadata={
        "is_blocked": False,
        "safety_ratings": [
            {
                "category": "HARM_CATEGORY_HATE_SPEECH",
                "probability_label": "NEGLIGIBLE",
                "probability_score": 0.1,
                "blocked": False,
                "severity": "HARM_SEVERITY_NEGLIGIBLE",
                "severity_score": 0.1,
            },
            {
                "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
                "probability_label": "NEGLIGIBLE",
                "probability_score": 0.1,
                "blocked": False,
                "severity": "HARM_SEVERITY_NEGLIGIBLE",
                "severity_score": 0.1,
            },
            {
                "category": "HARM_CATEGORY_HARASSMENT",
                "probability_label": "NEGLIGIBLE",
                "probability_score": 0.1,
                "blocked": False,
                "severity": "HARM_SEVERITY_NEGLIGIBLE",
                "severity_score": 0.1,
            },
            {
                "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
                "probability_label": "NEGLIGIBLE",
                "probability_score": 0.1,
                "blocked": False,
                "severity": "HARM_SEVERITY_NEGLIGIBLE",
                "severity_score": 0.1,
            },
        ],
        "citation_metadata": None,
        "usage_metadata": {
            "prompt_token_count": 17,
            "candidates_token_count": 7,
            "total_token_count": 24,
        },
    },
    id="run-925ce305-2268-44c4-875f-dde9128520ad-0",
)
Stream

.. code-block:: python

for chunk in llm.stream(messages):
    print(chunk)

.. code-block:: python

AIMessageChunk(
    content="J",
    response_metadata={
        "is_blocked": False,
        "safety_ratings": [],
        "citation_metadata": None,
    },
    id="run-9df01d73-84d9-42db-9d6b-b1466a019e89",
)
AIMessageChunk(
    content="'adore programmer. ",
    response_metadata={
        "is_blocked": False,
        "safety_ratings": [
            {
                "category": "HARM_CATEGORY_HATE_SPEECH",
                "probability_label": "NEGLIGIBLE",
                "probability_score": 0.1,
                "blocked": False,
                "severity": "HARM_SEVERITY_NEGLIGIBLE",
                "severity_score": 0.1,
            },
            {
                "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
                "probability_label": "NEGLIGIBLE",
                "probability_score": 0.1,
                "blocked": False,
                "severity": "HARM_SEVERITY_NEGLIGIBLE",
                "severity_score": 0.1,
            },
            {
                "category": "HARM_CATEGORY_HARASSMENT",
                "probability_label": "NEGLIGIBLE",
                "probability_score": 0.1,
                "blocked": False,
                "severity": "HARM_SEVERITY_NEGLIGIBLE",
                "severity_score": 0.1,
            },
            {
                "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
                "probability_label": "NEGLIGIBLE",
                "probability_score": 0.1,
                "blocked": False,
                "severity": "HARM_SEVERITY_NEGLIGIBLE",
                "severity_score": 0.1,
            },
        ],
        "citation_metadata": None,
    },
    id="run-9df01d73-84d9-42db-9d6b-b1466a019e89",
)
AIMessageChunk(
    content="",
    response_metadata={
        "is_blocked": False,
        "safety_ratings": [],
        "citation_metadata": None,
        "usage_metadata": {
            "prompt_token_count": 17,
            "candidates_token_count": 7,
            "total_token_count": 24,
        },
    },
    id="run-9df01d73-84d9-42db-9d6b-b1466a019e89",
)

.. code-block:: python

stream = llm.stream(messages)
full = next(stream)
for chunk in stream:
    full += chunk
full

.. code-block:: python

AIMessageChunk(
    content="J'adore programmer. ",
    response_metadata={
        "is_blocked": False,
        "safety_ratings": [
            {
                "category": "HARM_CATEGORY_HATE_SPEECH",
                "probability_label": "NEGLIGIBLE",
                "probability_score": 0.1,
                "blocked": False,
                "severity": "HARM_SEVERITY_NEGLIGIBLE",
                "severity_score": 0.1,
            },
            {
                "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
                "probability_label": "NEGLIGIBLE",
                "probability_score": 0.1,
                "blocked": False,
                "severity": "HARM_SEVERITY_NEGLIGIBLE",
                "severity_score": 0.1,
            },
            {
                "category": "HARM_CATEGORY_HARASSMENT",
                "probability_label": "NEGLIGIBLE",
                "probability_score": 0.1,
                "blocked": False,
                "severity": "HARM_SEVERITY_NEGLIGIBLE",
                "severity_score": 0.1,
            },
            {
                "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
                "probability_label": "NEGLIGIBLE",
                "probability_score": 0.1,
                "blocked": False,
                "severity": "HARM_SEVERITY_NEGLIGIBLE",
                "severity_score": 0.1,
            },
        ],
        "citation_metadata": None,
        "usage_metadata": {
            "prompt_token_count": 17,
            "candidates_token_count": 7,
            "total_token_count": 24,
        },
    },
    id="run-b7f7492c-4cb5-42d0-8fc3-dce9b293b0fb",
)
Async

.. code-block:: python

await llm.ainvoke(messages)

# stream:
# async for chunk in (await llm.astream(messages))

# batch:
# await llm.abatch([messages])

.. code-block:: python

AIMessage(
    content="J'adore programmer. ",
    response_metadata={
        "is_blocked": False,
        "safety_ratings": [
            {
                "category": "HARM_CATEGORY_HATE_SPEECH",
                "probability_label": "NEGLIGIBLE",
                "probability_score": 0.1,
                "blocked": False,
                "severity": "HARM_SEVERITY_NEGLIGIBLE",
                "severity_score": 0.1,
            },
            {
                "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
                "probability_label": "NEGLIGIBLE",
                "probability_score": 0.1,
                "blocked": False,
                "severity": "HARM_SEVERITY_NEGLIGIBLE",
                "severity_score": 0.1,
            },
            {
                "category": "HARM_CATEGORY_HARASSMENT",
                "probability_label": "NEGLIGIBLE",
                "probability_score": 0.1,
                "blocked": False,
                "severity": "HARM_SEVERITY_NEGLIGIBLE",
                "severity_score": 0.1,
            },
            {
                "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
                "probability_label": "NEGLIGIBLE",
                "probability_score": 0.1,
                "blocked": False,
                "severity": "HARM_SEVERITY_NEGLIGIBLE",
                "severity_score": 0.1,
            },
        ],
        "citation_metadata": None,
        "usage_metadata": {
            "prompt_token_count": 17,
            "candidates_token_count": 7,
            "total_token_count": 24,
        },
    },
    id="run-925ce305-2268-44c4-875f-dde9128520ad-0",
)
Context Caching

Context caching allows you to store and reuse content (e.g., PDFs, images) for faster processing. The cached_content parameter accepts a cache name created via the Google Generative AI API with Vertex AI. Below is an example of caching content from GCS and querying it.

Example

This caches content from GCS and queries it.

.. code-block:: python

from google import genai
from google.genai.types import (
    Content,
    CreateCachedContentConfig,
    HttpOptions,
    Part,
)
from langchain_google_vertexai import ChatVertexAI
from langchain_core.messages import HumanMessage

client = genai.Client(http_options=HttpOptions(api_version="v1beta1"))

contents = [
    Content(
        role="user",
        parts=[
            Part.from_uri(
                file_uri="gs://your-bucket/file1",
                mime_type="application/pdf",
            ),
            Part.from_uri(
                file_uri="gs://your-bucket/file2",
                mime_type="image/jpeg",
            ),
        ],
    )
]

cache = client.caches.create(
    model="gemini-1.5-flash-001",
    config=CreateCachedContentConfig(
        contents=contents,
        system_instruction="You are an expert content analyzer.",
        display_name="content-cache",
        ttl="300s",
    ),
)

llm = ChatVertexAI(
    model_name="gemini-1.5-flash-001",
    cached_content=cache.name,
)
message = HumanMessage(
    content="Provide a summary of the key information across the content."
)
llm.invoke([message])
Tool calling

.. code-block:: python

from pydantic import BaseModel, Field


class GetWeather(BaseModel):
    '''Get the current weather in a given location'''

    location: str = Field(
        ..., description="The city and state, e.g. San Francisco, CA"
    )


class GetPopulation(BaseModel):
    '''Get the current population in a given location'''

    location: str = Field(
        ..., description="The city and state, e.g. San Francisco, CA"
    )


llm_with_tools = llm.bind_tools([GetWeather, GetPopulation])
ai_msg = llm_with_tools.invoke(
    "Which city is hotter today and which is bigger: LA or NY?"
)
ai_msg.tool_calls

.. code-block:: python

[
    {
        "name": "GetWeather",
        "args": {"location": "Los Angeles, CA"},
        "id": "2a2401fa-40db-470d-83ce-4e52de910d9e",
    },
    {
        "name": "GetWeather",
        "args": {"location": "New York City, NY"},
        "id": "96761deb-ab7f-4ef9-b4b4-6d44562fc46e",
    },
    {
        "name": "GetPopulation",
        "args": {"location": "Los Angeles, CA"},
        "id": "9147d532-abee-43a2-adb5-12f164300484",
    },
    {
        "name": "GetPopulation",
        "args": {"location": "New York City, NY"},
        "id": "c43374ea-bde5-49ca-8487-5b83ebeea1e6",
    },
]

See ChatVertexAI.bind_tools() method for more.

Built-in code execution

.. code-block:: python

from google.cloud.aiplatform_v1beta1.types import Tool as VertexTool
from langchain_google_vertexai import ChatVertexAI

llm = ChatVertexAI(model="gemini-2.0-flash-exp")
resp = llm.invoke(
    "What is 3^3?",
    tools=[VertexTool(code_execution={})],
)
Structured output

.. code-block:: python

from typing import Optional

from pydantic import BaseModel, Field


class Joke(BaseModel):
    '''Joke to tell user.'''

    setup: str = Field(description="The setup of the joke")
    punchline: str = Field(description="The punchline to the joke")
    rating: Optional[int] = Field(
        default=None, description="How funny the joke is, from 1 to 10"
    )


structured_llm = llm.with_structured_output(Joke)
structured_llm.invoke("Tell me a joke about cats")

.. code-block:: python

Joke(
    setup="What do you call a cat that loves to bowl?",
    punchline="An alley cat!",
    rating=None,
)

See ChatVertexAI.with_structured_output() for more.

Image input

.. code-block:: python

import base64
import httpx
from langchain_core.messages import HumanMessage

image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
image_data = base64.b64encode(httpx.get(image_url).content).decode("utf-8")
message = HumanMessage(
    content=[
        {"type": "text", "text": "describe the weather in this image"},
        {
            "type": "image_url",
            "image_url": {"url": f"data:image/jpeg;base64,{image_data}"},
        },
    ],
)
ai_msg = llm.invoke([message])
ai_msg.content

.. code-block:: python

"The weather in this image appears to be sunny and pleasant. The sky is a bright blue with scattered white clouds, suggesting a clear and mild day. The lush green grass indicates recent rainfall or sufficient moisture. The absence of strong shadows suggests that the sun is high in the sky, possibly late afternoon. Overall, the image conveys a sense of tranquility and warmth, characteristic of a beautiful summer day."

You can also point to GCS files which is faster / more efficient because bytes are transferred back and forth.

.. code-block:: python

llm.invoke(
    [
        HumanMessage(
            [
                "What's in the image?",
                {
                    "type": "media",
                    "file_uri": "gs://cloud-samples-data/generative-ai/image/scones.jpg",
                    "mime_type": "image/jpeg",
                },
            ]
        )
    ]
).content

.. code-block:: python

'The image is of five blueberry scones arranged on a piece of baking paper. Here is a list of what is in the picture:* **Five blueberry scones:** They are scattered across the parchment paper, dusted with powdered sugar.  * **Two cups of coffee:**  Two white cups with saucers. One appears full, the other partially drunk. * **A bowl of blueberries:** A brown bowl is filled with fresh blueberries, placed near the scones.* **A spoon:**  A silver spoon with the words "Let\'s Jam" rests on the paper.* **Pink peonies:** Several pink peonies lie beside the scones, adding a touch of color.* **Baking paper:** The scones, cups, bowl, and spoon are arranged on a piece of white baking paper, splattered with purple.  The paper is crinkled and sits on a dark surface. The image has a rustic and delicious feel, suggesting a cozy and enjoyable breakfast or brunch setting.'
PDF input

.. code-block:: python

import base64
from langchain_core.messages import HumanMessage

pdf_bytes = open("/path/to/your/test.pdf", "rb").read()
pdf_base64 = base64.b64encode(pdf_bytes).decode("utf-8")
message = HumanMessage(
    content=[
        {"type": "text", "text": "describe the document in a sentence"},
        {
            "type": "file",
            "source_type": "base64",
            "mime_type": "application/pdf",
            "data": pdf_base64,
        },
    ]
)
ai_msg = llm.invoke([message])
ai_msg.content

.. code-block:: python

"This research paper describes a system developed for SemEval-2025 Task 9, which aims to automate the detection of food hazards from recall reports, addressing the class imbalance problem by leveraging LLM-based data augmentation techniques and transformer-based models to improve performance."

You can also point to GCS files.

.. code-block:: python

llm.invoke(
    [
        HumanMessage(
            [
                "describe the document in a sentence",
                {
                    "type": "media",
                    "file_uri": "gs://cloud-samples-data/generative-ai/pdf/1706.03762v7.pdf",
                    "mime_type": "application/pdf",
                },
            ]
        )
    ]
).content

.. code-block:: python

"The article introduces Transformer, a new model architecture for sequence transduction based solely on attention mechanisms, outperforming previous models in machine translation tasks and demonstrating good generalization to English constituency parsing."
Video input

.. code-block:: python

import base64
from langchain_core.messages import HumanMessage

video_bytes = open("/path/to/your/video.mp4", "rb").read()
video_base64 = base64.b64encode(video_bytes).decode("utf-8")

message = HumanMessage(
    content=[
        {
            "type": "text",
            "text": "describe what's in this video in a sentence",
        },
        {
            "type": "file",
            "source_type": "base64",
            "mime_type": "video/mp4",
            "data": video_base64,
        },
    ]
)
ai_msg = llm.invoke([message])
ai_msg.content

.. code-block:: python

"Tom and Jerry, along with a turkey, engage in a chaotic Thanksgiving-themed adventure involving a corn-on-the-cob chase, maze antics, and a disastrous attempt to prepare a turkey dinner."

You can also pass YouTube URLs directly:

.. code-block:: python

from langchain_core.messages import HumanMessage

message = HumanMessage(
    content=[
        {"type": "text", "text": "summarize the video in 3 sentences."},
        {
            "type": "media",
            "file_uri": "https://www.youtube.com/watch?v=9hE5-98ZeCg",
            "mime_type": "video/mp4",
        },
    ]
)
ai_msg = llm.invoke([message])
ai_msg.content

.. code-block:: python

"The video is a demo of multimodal live streaming in Gemini 2.0. The narrator is sharing his screen in AI Studio and asks if the AI can see it. The AI then reads text that is highlighted on the screen, defines the word “multimodal,” and summarizes everything that was seen and heard."

You can also point to GCS files.

.. code-block:: python

llm = ChatVertexAI(model="gemini-1.0-pro-vision")

llm.invoke(
    [
        HumanMessage(
            [
                "What's in the video?",
                {
                    "type": "media",
                    "file_uri": "gs://cloud-samples-data/video/animals.mp4",
                    "mime_type": "video/mp4",
                },
            ]
        )
    ]
).content

.. code-block:: python

 'The video is about a new feature in Google Photos called "Zoomable Selfies". The feature allows users to take selfies with animals at the zoo. The video shows several examples of people taking selfies with animals, including a tiger, an elephant, and a sea otter. The video also shows how the feature works. Users simply need to open the Google Photos app and select the "Zoomable Selfies" option. Then, they need to choose an animal from the list of available animals. The app will then guide the user through the process of taking the selfie.'
Audio input

.. code-block:: python

import base64
from langchain_core.messages import HumanMessage

audio_bytes = open("/path/to/your/audio.mp3", "rb").read()
audio_base64 = base64.b64encode(audio_bytes).decode("utf-8")

message = HumanMessage(
    content=[
        {"type": "text", "text": "summarize this audio in a sentence"},
        {
            "type": "file",
            "source_type": "base64",
            "mime_type": "audio/mp3",
            "data": audio_base64,
        },
    ]
)
ai_msg = llm.invoke([message])
ai_msg.content

.. code-block:: python

"In this episode of the Made by Google podcast, Stephen Johnson and Simon Tokumine discuss NotebookLM, a tool designed to help users understand complex material in various modalities, with a focus on its unexpected uses, the development of audio overviews, and the implementation of new features like mind maps and source discovery."

You can also point to GCS files.

.. code-block:: python

from langchain_core.messages import HumanMessage

llm = ChatVertexAI(model="gemini-1.5-flash-001")

llm.invoke(
    [
        HumanMessage(
            [
                "What's this audio about?",
                {
                    "type": "media",
                    "file_uri": "gs://cloud-samples-data/generative-ai/audio/pixel.mp3",
                    "mime_type": "audio/mpeg",
                },
            ]
        )
    ]
).content

.. code-block:: python

"This audio is an interview with two product managers from Google who work on Pixel feature drops. They discuss how feature drops are important for showcasing how Google devices are constantly improving and getting better. They also discuss some of the highlights of the January feature drop and the new features coming in the March drop for Pixel phones and Pixel watches. The interview concludes with discussion of how user feedback is extremely important to them in deciding which features to include in the feature drops."
Token usage

.. code-block:: python

ai_msg = llm.invoke(messages)
ai_msg.usage_metadata

.. code-block:: python

{"input_tokens": 17, "output_tokens": 7, "total_tokens": 24}
Logprobs

.. code-block:: python

llm = ChatVertexAI(model="gemini-1.5-flash-001", logprobs=True)
ai_msg = llm.invoke(messages)
ai_msg.response_metadata["logprobs_result"]

.. code-block:: python

[
    {"token": "J", "logprob": -1.549651415189146e-06, "top_logprobs": []},
    {"token": "'", "logprob": -1.549651415189146e-06, "top_logprobs": []},
    {"token": "adore", "logprob": 0.0, "top_logprobs": []},
    {
        "token": " programmer",
        "logprob": -1.1922384146600962e-07,
        "top_logprobs": [],
    },
    {"token": ".", "logprob": -4.827636439586058e-05, "top_logprobs": []},
    {"token": " ", "logprob": -0.018011733889579773, "top_logprobs": []},
    {"token": "\\n", "logprob": -0.0008687592926435173, "top_logprobs": []},
]

Response metadata .. code-block:: python

    ai_msg = llm.invoke(messages)
    ai_msg.response_metadata

.. code-block:: python

    {
        "is_blocked": False,
        "safety_ratings": [
            {
                "category": "HARM_CATEGORY_HATE_SPEECH",
                "probability_label": "NEGLIGIBLE",
                "probability_score": 0.1,
                "blocked": False,
                "severity": "HARM_SEVERITY_NEGLIGIBLE",
                "severity_score": 0.1,
            },
            {
                "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
                "probability_label": "NEGLIGIBLE",
                "probability_score": 0.1,
                "blocked": False,
                "severity": "HARM_SEVERITY_NEGLIGIBLE",
                "severity_score": 0.1,
            },
            {
                "category": "HARM_CATEGORY_HARASSMENT",
                "probability_label": "NEGLIGIBLE",
                "probability_score": 0.1,
                "blocked": False,
                "severity": "HARM_SEVERITY_NEGLIGIBLE",
                "severity_score": 0.1,
            },
            {
                "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
                "probability_label": "NEGLIGIBLE",
                "probability_score": 0.1,
                "blocked": False,
                "severity": "HARM_SEVERITY_NEGLIGIBLE",
                "severity_score": 0.1,
            },
        ],
        "usage_metadata": {
            "prompt_token_count": 17,
            "candidates_token_count": 7,
            "total_token_count": 24,
        },
    }

Safety settings .. code-block:: python

    from langchain_google_vertexai import HarmBlockThreshold, HarmCategory

    llm = ChatVertexAI(
        model="gemini-1.5-pro",
        safety_settings={
            HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
            HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
            HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
            HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_ONLY_HIGH,
        },
    )

    llm.invoke(messages).response_metadata

.. code-block:: python

    {
        "is_blocked": False,
        "safety_ratings": [
            {
                "category": "HARM_CATEGORY_HATE_SPEECH",
                "probability_label": "NEGLIGIBLE",
                "probability_score": 0.1,
                "blocked": False,
                "severity": "HARM_SEVERITY_NEGLIGIBLE",
                "severity_score": 0.1,
            },
            {
                "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
                "probability_label": "NEGLIGIBLE",
                "probability_score": 0.1,
                "blocked": False,
                "severity": "HARM_SEVERITY_NEGLIGIBLE",
                "severity_score": 0.1,
            },
            {
                "category": "HARM_CATEGORY_HARASSMENT",
                "probability_label": "NEGLIGIBLE",
                "probability_score": 0.1,
                "blocked": False,
                "severity": "HARM_SEVERITY_NEGLIGIBLE",
                "severity_score": 0.1,
            },
            {
                "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
                "probability_label": "NEGLIGIBLE",
                "probability_score": 0.1,
                "blocked": False,
                "severity": "HARM_SEVERITY_NEGLIGIBLE",
                "severity_score": 0.1,
            },
        ],
        "usage_metadata": {
            "prompt_token_count": 17,
            "candidates_token_count": 7,
            "total_token_count": 24,
        },
    }

Methods:

Name Description
get_name

Get the name of the Runnable.

get_input_schema

Get a pydantic model that can be used to validate input to the Runnable.

get_input_jsonschema

Get a JSON schema that represents the input to the Runnable.

get_output_schema

Get a pydantic model that can be used to validate output to the Runnable.

get_output_jsonschema

Get a JSON schema that represents the output of the Runnable.

config_schema

The type of config this Runnable accepts specified as a pydantic model.

get_config_jsonschema

Get a JSON schema that represents the config of the Runnable.

get_graph

Return a graph representation of this Runnable.

get_prompts

Return a list of prompts used by this Runnable.

__or__

Runnable "or" operator.

__ror__

Runnable "reverse-or" operator.

pipe

Pipe runnables.

pick

Pick keys from the output dict of this Runnable.

assign

Assigns new fields to the dict output of this Runnable.

batch

Default implementation runs invoke in parallel using a thread pool executor.

batch_as_completed

Run invoke in parallel on a list of inputs.

abatch

Default implementation runs ainvoke in parallel using asyncio.gather.

abatch_as_completed

Run ainvoke in parallel on a list of inputs.

astream_log

Stream all output from a Runnable, as reported to the callback system.

astream_events

Generate a stream of events.

transform

Transform inputs to outputs.

atransform

Transform inputs to outputs.

bind

Bind arguments to a Runnable, returning a new Runnable.

with_config

Bind config to a Runnable, returning a new Runnable.

with_listeners

Bind lifecycle listeners to a Runnable, returning a new Runnable.

with_alisteners

Bind async lifecycle listeners to a Runnable.

with_types

Bind input and output types to a Runnable, returning a new Runnable.

with_retry

Create a new Runnable that retries the original Runnable on exceptions.

map

Return a new Runnable that maps a list of inputs to a list of outputs.

with_fallbacks

Add fallbacks to a Runnable, returning a new Runnable.

as_tool

Create a BaseTool from a Runnable.

lc_id

Return a unique identifier for this class for serialization purposes.

to_json

Serialize the Runnable to JSON.

to_json_not_implemented

Serialize a "not implemented" object.

configurable_fields

Configure particular Runnable fields at runtime.

configurable_alternatives

Configure alternatives for Runnables that can be set at runtime.

set_verbose

If verbose is None, set it.

get_token_ids

Return the ordered ids of the tokens in a text.

get_num_tokens_from_messages

Get the number of tokens in the messages.

generate

Pass a sequence of prompts to the model and return model generations.

agenerate

Asynchronously pass a sequence of prompts to a model and return generations.

dict

Return a dictionary of the LLM.

__init__

Needed for mypy typing to recognize model_name as a valid arg

get_lc_namespace

Get the namespace of the langchain object.

build_extra

Build extra kwargs from additional params that were passed in.

validate_environment

Validate that the python package exists in environment.

get_num_tokens

Get the number of tokens present in the text.

with_structured_output

Model wrapper that returns outputs formatted to match the given schema.

bind_tools

Bind tool-like objects to this chat model.

Attributes:

Name Type Description
InputType TypeAlias

Get the input type for this runnable.

OutputType Any

Get the output type for this runnable.

input_schema type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema type[BaseModel]

Output schema.

config_specs list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets dict[str, str]

A map of constructor argument names to secret ids.

lc_attributes dict

List of attribute names that should be included in the serialized kwargs.

cache BaseCache | bool | None

Whether to cache the response.

verbose bool

Whether to print out response text.

callbacks Callbacks

Callbacks to add to the run trace.

tags list[str] | None

Tags to add to the run trace.

metadata dict[str, Any] | None

Metadata to add to the run trace.

custom_get_token_ids Callable[[str], list[int]] | None

Optional encoder to use for counting tokens.

rate_limiter BaseRateLimiter | None

An optional rate limiter to use for limiting the number of requests.

disable_streaming bool | Literal['tool_calling']

Whether to disable streaming for this model.

output_version str | None

Version of AIMessage output format to store in message content.

project Optional[str]

The default GCP project to use when making Vertex API calls.

location str

The default location to use when making API calls.

request_parallelism int

The amount of parallelism allowed for requests issued to VertexAI models.

max_retries int

The maximum number of retries to make when generating.

stop Optional[List[str]]

Optional list of stop words to use when generating.

full_model_name Optional[str]

The full name of the model's endpoint.

api_endpoint Optional[str]

Desired API endpoint, e.g., us-central1-aiplatform.googleapis.com

api_transport Optional[str]

The desired API transport method, can be either 'grpc' or 'rest'.

additional_headers Optional[Dict[str, str]]

A key-value dictionary representing additional headers for the model call

client_cert_source Optional[Callable[[], Tuple[bytes, bytes]]]

A callback which returns client certificate bytes and private key bytes both

credentials Any

The default custom credentials (google.auth.credentials.Credentials) to use

endpoint_version Literal['v1', 'v1beta1']

Whether to use v1 or v1beta1 endpoint.

prediction_client Union[PredictionServiceClient, PredictionServiceClient]

Returns PredictionServiceClient.

async_prediction_client Union[PredictionServiceAsyncClient, PredictionServiceAsyncClient]

Returns PredictionServiceClient.

temperature Optional[float]

Sampling temperature, it controls the degree of randomness in token selection.

frequency_penalty Optional[float]

Positive values penalize tokens that repeatedly appear in the generated text,

presence_penalty Optional[float]

Positive values penalize tokens that already appear in the generated text,

max_output_tokens Optional[int]

Token limit determines the maximum amount of text output from one prompt.

top_p Optional[float]

Tokens are selected from most probable to least until the sum of their

top_k Optional[int]

How the model selects tokens for output, the next token is selected from

n int

How many completions to generate for each prompt.

seed Optional[int]

Random seed for the generation.

streaming bool

Whether to stream the results or not.

safety_settings Optional[SafetySettingsType]

The default safety settings to use for all generations.

tuned_model_name Optional[str]

The name of a tuned model.

model_name str

Underlying model name.

response_mime_type Optional[str]

Optional. Output response mimetype of the generated candidate text. Only

response_schema Optional[Dict[str, Any]]

Optional. Enforce an schema to the output.

cached_content Optional[str]

Optional. Use the model in cache mode. Only supported in Gemini 1.5 and later

logprobs Union[bool, int]

Whether to return logprobs as part of AIMessage.response_metadata.

labels Optional[Dict[str, str]]

Optional tag llm calls with metadata to help in tracebility and biling.

perform_literal_eval_on_string_raw_content bool

Whether to perform literal eval on string raw content.

wait_exponential_kwargs Optional[dict[str, float]]

Optional dictionary with parameters for wait_exponential:

model_kwargs dict[str, Any]

Holds any unexpected initialization parameters.

InputType property

InputType: TypeAlias

Get the input type for this runnable.

OutputType property

OutputType: Any

Get the output type for this runnable.

input_schema property

input_schema: type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema property

output_schema: type[BaseModel]

Output schema.

The type of output this Runnable produces specified as a pydantic model.

config_specs property

config_specs: list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets property

lc_secrets: dict[str, str]

A map of constructor argument names to secret ids.

For example,

lc_attributes property

lc_attributes: dict

List of attribute names that should be included in the serialized kwargs.

These attributes must be accepted by the constructor. Default is an empty dictionary.

cache class-attribute instance-attribute

cache: BaseCache | bool | None = Field(
    default=None, exclude=True
)

Whether to cache the response.

  • If true, will use the global cache.
  • If false, will not use a cache
  • If None, will use the global cache if it's set, otherwise no cache.
  • If instance of BaseCache, will use the provided cache.

Caching is not currently supported for streaming methods of models.

verbose class-attribute instance-attribute

verbose: bool = Field(
    default_factory=_get_verbosity, exclude=True, repr=False
)

Whether to print out response text.

callbacks class-attribute instance-attribute

callbacks: Callbacks = Field(default=None, exclude=True)

Callbacks to add to the run trace.

tags class-attribute instance-attribute

tags: list[str] | None = Field(default=None, exclude=True)

Tags to add to the run trace.

metadata class-attribute instance-attribute

metadata: dict[str, Any] | None = Field(
    default=None, exclude=True
)

Metadata to add to the run trace.

custom_get_token_ids class-attribute instance-attribute

custom_get_token_ids: Callable[[str], list[int]] | None = (
    Field(default=None, exclude=True)
)

Optional encoder to use for counting tokens.

rate_limiter class-attribute instance-attribute

rate_limiter: BaseRateLimiter | None = Field(
    default=None, exclude=True
)

An optional rate limiter to use for limiting the number of requests.

disable_streaming class-attribute instance-attribute

disable_streaming: bool | Literal['tool_calling'] = False

Whether to disable streaming for this model.

If streaming is bypassed, then stream()/astream()/astream_events() will defer to invoke()/ainvoke().

  • If True, will always bypass streaming case.
  • If 'tool_calling', will bypass streaming case only when the model is called with a tools keyword argument. In other words, LangChain will automatically switch to non-streaming behavior (invoke()) only when the tools argument is provided. This offers the best of both worlds.
  • If False (default), will always use streaming case if available.

The main reason for this flag is that code might be written using stream() and a user may want to swap out a given model for another model whose the implementation does not properly support streaming.

output_version class-attribute instance-attribute

output_version: str | None = Field(
    default_factory=from_env(
        "LC_OUTPUT_VERSION", default=None
    )
)

Version of AIMessage output format to store in message content.

AIMessage.content_blocks will lazily parse the contents of content into a standard format. This flag can be used to additionally store the standard format in message content, e.g., for serialization purposes.

Supported values:

  • "v0": provider-specific format in content (can lazily-parse with .content_blocks)
  • "v1": standardized format in content (consistent with .content_blocks)

Partner packages (e.g., langchain-openai) can also use this field to roll out new content formats in a backward-compatible way.

Added in version 1.0

project class-attribute instance-attribute

project: Optional[str] = None

The default GCP project to use when making Vertex API calls.

location class-attribute instance-attribute

location: str = Field(default=_DEFAULT_LOCATION)

The default location to use when making API calls.

request_parallelism class-attribute instance-attribute

request_parallelism: int = 5

The amount of parallelism allowed for requests issued to VertexAI models.

max_retries class-attribute instance-attribute

max_retries: int = 6

The maximum number of retries to make when generating.

stop class-attribute instance-attribute

stop: Optional[List[str]] = Field(
    default=None, alias="stop_sequences"
)

Optional list of stop words to use when generating.

full_model_name class-attribute instance-attribute

full_model_name: Optional[str] = Field(
    default=None, exclude=True
)

The full name of the model's endpoint.

api_endpoint class-attribute instance-attribute

api_endpoint: Optional[str] = Field(
    default=None, alias="base_url"
)

Desired API endpoint, e.g., us-central1-aiplatform.googleapis.com

api_transport class-attribute instance-attribute

api_transport: Optional[str] = None

The desired API transport method, can be either 'grpc' or 'rest'. Uses the default parameter in vertexai.init if defined.

additional_headers class-attribute instance-attribute

additional_headers: Optional[Dict[str, str]] = Field(
    default=None
)

A key-value dictionary representing additional headers for the model call

client_cert_source class-attribute instance-attribute

client_cert_source: Optional[
    Callable[[], Tuple[bytes, bytes]]
] = None

A callback which returns client certificate bytes and private key bytes both

credentials class-attribute instance-attribute

credentials: Any = Field(default=None, exclude=True)

The default custom credentials (google.auth.credentials.Credentials) to use

endpoint_version class-attribute instance-attribute

endpoint_version: Literal['v1', 'v1beta1'] = 'v1beta1'

Whether to use v1 or v1beta1 endpoint.

v1 is more performant, but v1beta1 might have some new features.

prediction_client property

prediction_client: Union[
    PredictionServiceClient, PredictionServiceClient
]

Returns PredictionServiceClient.

async_prediction_client property

async_prediction_client: Union[
    PredictionServiceAsyncClient,
    PredictionServiceAsyncClient,
]

Returns PredictionServiceClient.

temperature class-attribute instance-attribute

temperature: Optional[float] = None

Sampling temperature, it controls the degree of randomness in token selection.

frequency_penalty class-attribute instance-attribute

frequency_penalty: Optional[float] = None

Positive values penalize tokens that repeatedly appear in the generated text,

presence_penalty class-attribute instance-attribute

presence_penalty: Optional[float] = None

Positive values penalize tokens that already appear in the generated text,

max_output_tokens class-attribute instance-attribute

max_output_tokens: Optional[int] = Field(
    default=None, alias="max_tokens"
)

Token limit determines the maximum amount of text output from one prompt.

top_p class-attribute instance-attribute

top_p: Optional[float] = None

Tokens are selected from most probable to least until the sum of their

top_k class-attribute instance-attribute

top_k: Optional[int] = None

How the model selects tokens for output, the next token is selected from

n class-attribute instance-attribute

n: int = 1

How many completions to generate for each prompt.

seed class-attribute instance-attribute

seed: Optional[int] = None

Random seed for the generation.

streaming class-attribute instance-attribute

streaming: bool = False

Whether to stream the results or not.

safety_settings class-attribute instance-attribute

safety_settings: Optional[SafetySettingsType] = None

The default safety settings to use for all generations.

For example:

from langchain_google_vertexai import HarmBlockThreshold, HarmCategory

safety_settings = {
    HarmCategory.HARM_CATEGORY_UNSPECIFIED: HarmBlockThreshold.BLOCK_NONE,
    HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
    HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_ONLY_HIGH,
    HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
    HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
}

tuned_model_name class-attribute instance-attribute

tuned_model_name: Optional[str] = None

The name of a tuned model.

model_name class-attribute instance-attribute

model_name: str = Field(alias='model')

Underlying model name.

response_mime_type class-attribute instance-attribute

response_mime_type: Optional[str] = None

Optional. Output response mimetype of the generated candidate text. Only supported in Gemini 1.5 and later models. Supported mimetype: * 'text/plain': (default) Text output. * 'application/json': JSON response in the candidates. * 'text/x.enum': Enum in plain text. The model also needs to be prompted to output the appropriate response type, otherwise the behavior is undefined. This is a preview feature.

response_schema class-attribute instance-attribute

response_schema: Optional[Dict[str, Any]] = None

Optional. Enforce an schema to the output. The format of the dictionary should follow Open API schema.

cached_content class-attribute instance-attribute

cached_content: Optional[str] = None

Optional. Use the model in cache mode. Only supported in Gemini 1.5 and later models. Must be a string containing the cache name (A sequence of numbers)

logprobs class-attribute instance-attribute

logprobs: Union[bool, int] = False

Whether to return logprobs as part of AIMessage.response_metadata.

If False, don't return logprobs. If True, return logprobs for top candidate. If int, return logprobs for top logprobs candidates.

.. note:: As of 2024-10-28 this is only supported for gemini-1.5-flash models.

.. versionadded: 2.0.6

labels class-attribute instance-attribute

labels: Optional[Dict[str, str]] = None

Optional tag llm calls with metadata to help in tracebility and biling.

perform_literal_eval_on_string_raw_content class-attribute instance-attribute

perform_literal_eval_on_string_raw_content: bool = False

Whether to perform literal eval on string raw content.

wait_exponential_kwargs class-attribute instance-attribute

wait_exponential_kwargs: Optional[dict[str, float]] = None

Optional dictionary with parameters for wait_exponential: - multiplier: Initial wait time multiplier (default: 1.0) - min: Minimum wait time in seconds (default: 4.0) - max: Maximum wait time in seconds (default: 10.0) - exp_base: Exponent base to use (default: 2.0)

model_kwargs class-attribute instance-attribute

model_kwargs: dict[str, Any] = Field(default_factory=dict)

Holds any unexpected initialization parameters.

get_name

get_name(
    suffix: str | None = None, *, name: str | None = None
) -> str

Get the name of the Runnable.

Parameters:

Name Type Description Default
suffix str | None

An optional suffix to append to the name.

None
name str | None

An optional name to use instead of the Runnable's name.

None

Returns:

Type Description
str

The name of the Runnable.

get_input_schema

get_input_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate input to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic input schema that depends on which configuration the Runnable is invoked with.

This method allows to get an input schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate input.

get_input_jsonschema

get_input_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the input to the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the input to the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_input_jsonschema())

Added in version 0.3.0

get_output_schema

get_output_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate output to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic output schema that depends on which configuration the Runnable is invoked with.

This method allows to get an output schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate output.

get_output_jsonschema

get_output_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the output of the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the output of the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_output_jsonschema())

Added in version 0.3.0

config_schema

config_schema(
    *, include: Sequence[str] | None = None
) -> type[BaseModel]

The type of config this Runnable accepts specified as a pydantic model.

To mark a field as configurable, see the configurable_fields and configurable_alternatives methods.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate config.

get_config_jsonschema

get_config_jsonschema(
    *, include: Sequence[str] | None = None
) -> dict[str, Any]

Get a JSON schema that represents the config of the Runnable.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the config of the Runnable.

Added in version 0.3.0

get_graph

get_graph(config: RunnableConfig | None = None) -> Graph

Return a graph representation of this Runnable.

get_prompts

get_prompts(
    config: RunnableConfig | None = None,
) -> list[BasePromptTemplate]

Return a list of prompts used by this Runnable.

__or__

__or__(
    other: (
        Runnable[Any, Other]
        | Callable[[Iterator[Any]], Iterator[Other]]
        | Callable[
            [AsyncIterator[Any]], AsyncIterator[Other]
        ]
        | Callable[[Any], Other]
        | Mapping[
            str,
            Runnable[Any, Other]
            | Callable[[Any], Other]
            | Any,
        ]
    ),
) -> RunnableSerializable[Input, Other]

Runnable "or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Any, Other] | Callable[[Iterator[Any]], Iterator[Other]] | Callable[[AsyncIterator[Any]], AsyncIterator[Other]] | Callable[[Any], Other] | Mapping[str, Runnable[Any, Other] | Callable[[Any], Other] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

__ror__

__ror__(
    other: (
        Runnable[Other, Any]
        | Callable[[Iterator[Other]], Iterator[Any]]
        | Callable[
            [AsyncIterator[Other]], AsyncIterator[Any]
        ]
        | Callable[[Other], Any]
        | Mapping[
            str,
            Runnable[Other, Any]
            | Callable[[Other], Any]
            | Any,
        ]
    ),
) -> RunnableSerializable[Other, Output]

Runnable "reverse-or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Other, Any] | Callable[[Iterator[Other]], Iterator[Any]] | Callable[[AsyncIterator[Other]], AsyncIterator[Any]] | Callable[[Other], Any] | Mapping[str, Runnable[Other, Any] | Callable[[Other], Any] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Other, Output]

A new Runnable.

pipe

pipe(
    *others: Runnable[Any, Other] | Callable[[Any], Other],
    name: str | None = None
) -> RunnableSerializable[Input, Other]

Pipe runnables.

Compose this Runnable with Runnable-like objects to make a RunnableSequence.

Equivalent to RunnableSequence(self, *others) or self | others[0] | ...

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


def mul_two(x: int) -> int:
    return x * 2


runnable_1 = RunnableLambda(add_one)
runnable_2 = RunnableLambda(mul_two)
sequence = runnable_1.pipe(runnable_2)
# Or equivalently:
# sequence = runnable_1 | runnable_2
# sequence = RunnableSequence(first=runnable_1, last=runnable_2)
sequence.invoke(1)
await sequence.ainvoke(1)
# -> 4

sequence.batch([1, 2, 3])
await sequence.abatch([1, 2, 3])
# -> [4, 6, 8]

Parameters:

Name Type Description Default
*others Runnable[Any, Other] | Callable[[Any], Other]

Other Runnable or Runnable-like objects to compose

()
name str | None

An optional name for the resulting RunnableSequence.

None

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

pick

pick(
    keys: str | list[str],
) -> RunnableSerializable[Any, Any]

Pick keys from the output dict of this Runnable.

Pick single key:

```python
import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)
chain = RunnableMap(str=as_str, json=as_json)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3]}

json_only_chain = chain.pick("json")
json_only_chain.invoke("[1, 2, 3]")
# -> [1, 2, 3]
```

Pick list of keys:

```python
from typing import Any

import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)


def as_bytes(x: Any) -> bytes:
    return bytes(x, "utf-8")


chain = RunnableMap(
    str=as_str, json=as_json, bytes=RunnableLambda(as_bytes)
)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3], "bytes": b"[1, 2, 3]"}

json_and_bytes_chain = chain.pick(["json", "bytes"])
json_and_bytes_chain.invoke("[1, 2, 3]")
# -> {"json": [1, 2, 3], "bytes": b"[1, 2, 3]"}
```

Parameters:

Name Type Description Default
keys str | list[str]

A key or list of keys to pick from the output dict.

required

Returns:

Type Description
RunnableSerializable[Any, Any]

a new Runnable.

assign

assign(
    **kwargs: (
        Runnable[dict[str, Any], Any]
        | Callable[[dict[str, Any]], Any]
        | Mapping[
            str,
            Runnable[dict[str, Any], Any]
            | Callable[[dict[str, Any]], Any],
        ]
    ),
) -> RunnableSerializable[Any, Any]

Assigns new fields to the dict output of this Runnable.

from langchain_community.llms.fake import FakeStreamingListLLM
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import SystemMessagePromptTemplate
from langchain_core.runnables import Runnable
from operator import itemgetter

prompt = (
    SystemMessagePromptTemplate.from_template("You are a nice assistant.")
    + "{question}"
)
llm = FakeStreamingListLLM(responses=["foo-lish"])

chain: Runnable = prompt | llm | {"str": StrOutputParser()}

chain_with_assign = chain.assign(hello=itemgetter("str") | llm)

print(chain_with_assign.input_schema.model_json_schema())
# {'title': 'PromptInput', 'type': 'object', 'properties':
{'question': {'title': 'Question', 'type': 'string'}}}
print(chain_with_assign.output_schema.model_json_schema())
# {'title': 'RunnableSequenceOutput', 'type': 'object', 'properties':
{'str': {'title': 'Str',
'type': 'string'}, 'hello': {'title': 'Hello', 'type': 'string'}}}

Parameters:

Name Type Description Default
**kwargs Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any] | Mapping[str, Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any]]

A mapping of keys to Runnable or Runnable-like objects that will be invoked with the entire output dict of this Runnable.

{}

Returns:

Type Description
RunnableSerializable[Any, Any]

A new Runnable.

batch

batch(
    inputs: list[Input],
    config: (
        RunnableConfig | list[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> list[Output]

Default implementation runs invoke in parallel using a thread pool executor.

The default implementation of batch works well for IO bound runnables.

Subclasses should override this method if they can batch more efficiently; e.g., if the underlying Runnable uses an API which supports a batch mode.

Parameters:

Name Type Description Default
inputs list[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | list[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
list[Output]

A list of outputs from the Runnable.

batch_as_completed

batch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> Iterator[tuple[int, Output | Exception]]

Run invoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
tuple[int, Output | Exception]

Tuples of the index of the input and the output from the Runnable.

abatch async

abatch(
    inputs: list[Input],
    config: (
        RunnableConfig | list[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> list[Output]

Default implementation runs ainvoke in parallel using asyncio.gather.

The default implementation of batch works well for IO bound runnables.

Subclasses should override this method if they can batch more efficiently; e.g., if the underlying Runnable uses an API which supports a batch mode.

Parameters:

Name Type Description Default
inputs list[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | list[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
list[Output]

A list of outputs from the Runnable.

abatch_as_completed async

abatch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> AsyncIterator[tuple[int, Output | Exception]]

Run ainvoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[tuple[int, Output | Exception]]

A tuple of the index of the input and the output from the Runnable.

astream_log async

astream_log(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    diff: bool = True,
    with_streamed_output_list: bool = True,
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

Stream all output from a Runnable, as reported to the callback system.

This includes all inner runs of LLMs, Retrievers, Tools, etc.

Output is streamed as Log objects, which include a list of Jsonpatch ops that describe how the state of the run has changed in each step, and the final state of the run.

The Jsonpatch ops can be applied in order to construct state.

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
diff bool

Whether to yield diffs between each step or the current state.

True
with_streamed_output_list bool

Whether to yield the streamed_output list.

True
include_names Sequence[str] | None

Only include logs with these names.

None
include_types Sequence[str] | None

Only include logs with these types.

None
include_tags Sequence[str] | None

Only include logs with these tags.

None
exclude_names Sequence[str] | None

Exclude logs with these names.

None
exclude_types Sequence[str] | None

Exclude logs with these types.

None
exclude_tags Sequence[str] | None

Exclude logs with these tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

A RunLogPatch or RunLog object.

astream_events async

astream_events(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    version: Literal["v1", "v2"] = "v2",
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[StreamEvent]

Generate a stream of events.

Use to create an iterator over StreamEvents that provide real-time information about the progress of the Runnable, including StreamEvents from intermediate results.

A StreamEvent is a dictionary with the following schema:

  • event: str - Event names are of the format: on_[runnable_type]_(start|stream|end).
  • name: str - The name of the Runnable that generated the event.
  • run_id: str - randomly generated ID associated with the given execution of the Runnable that emitted the event. A child Runnable that gets invoked as part of the execution of a parent Runnable is assigned its own unique ID.
  • parent_ids: list[str] - The IDs of the parent runnables that generated the event. The root Runnable will have an empty list. The order of the parent IDs is from the root to the immediate parent. Only available for v2 version of the API. The v1 version of the API will return an empty list.
  • tags: Optional[list[str]] - The tags of the Runnable that generated the event.
  • metadata: Optional[dict[str, Any]] - The metadata of the Runnable that generated the event.
  • data: dict[str, Any]

Below is a table that illustrates some events that might be emitted by various chains. Metadata fields have been omitted from the table for brevity. Chain definitions have been included after the table.

Note

This reference table is for the v2 version of the schema.

+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | event | name | chunk | input | output | +==========================+==================+=====================================+===================================================+=====================================================+ | on_chat_model_start | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_stream | [model name] | AIMessageChunk(content="hello") | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_end | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | AIMessageChunk(content="hello world") | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_start | [model name] | | {'input': 'hello'} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_stream | [model name] | 'Hello' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_end | [model name] | | 'Hello human!' | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_start | format_docs | | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_stream | format_docs | 'hello world!, goodbye world!' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_end | format_docs | | [Document(...)] | 'hello world!, goodbye world!' | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_start | some_tool | | {"x": 1, "y": "2"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_end | some_tool | | | {"x": 1, "y": "2"} | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_start | [retriever name] | | {"query": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_end | [retriever name] | | {"query": "hello"} | [Document(...), ..] | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_start | [template_name] | | {"question": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_end | [template_name] | | {"question": "hello"} | ChatPromptValue(messages: [SystemMessage, ...]) | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+

In addition to the standard events, users can also dispatch custom events (see example below).

Custom events will be only be surfaced with in the v2 version of the API!

A custom event has following format:

+-----------+------+-----------------------------------------------------------------------------------------------------------+ | Attribute | Type | Description | +===========+======+===========================================================================================================+ | name | str | A user defined name for the event. | +-----------+------+-----------------------------------------------------------------------------------------------------------+ | data | Any | The data associated with the event. This can be anything, though we suggest making it JSON serializable. | +-----------+------+-----------------------------------------------------------------------------------------------------------+

Here are declarations associated with the standard events shown above:

format_docs:

def format_docs(docs: list[Document]) -> str:
    '''Format the docs.'''
    return ", ".join([doc.page_content for doc in docs])


format_docs = RunnableLambda(format_docs)

some_tool:

@tool
def some_tool(x: int, y: str) -> dict:
    '''Some_tool.'''
    return {"x": x, "y": y}

prompt:

template = ChatPromptTemplate.from_messages(
    [
        ("system", "You are Cat Agent 007"),
        ("human", "{question}"),
    ]
).with_config({"run_name": "my_template", "tags": ["my_template"]})
Example:

from langchain_core.runnables import RunnableLambda


async def reverse(s: str) -> str:
    return s[::-1]


chain = RunnableLambda(func=reverse)

events = [event async for event in chain.astream_events("hello", version="v2")]

# will produce the following events (run_id, and parent_ids
# has been omitted for brevity):
[
    {
        "data": {"input": "hello"},
        "event": "on_chain_start",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"chunk": "olleh"},
        "event": "on_chain_stream",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"output": "olleh"},
        "event": "on_chain_end",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
]

Example: Dispatch Custom Event

from langchain_core.callbacks.manager import (
    adispatch_custom_event,
)
from langchain_core.runnables import RunnableLambda, RunnableConfig
import asyncio


async def slow_thing(some_input: str, config: RunnableConfig) -> str:
    """Do something that takes a long time."""
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 1 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 2 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    return "Done"

slow_thing = RunnableLambda(slow_thing)

async for event in slow_thing.astream_events("some_input", version="v2"):
    print(event)

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
version Literal['v1', 'v2']

The version of the schema to use either 'v2' or 'v1'. Users should use 'v2'. 'v1' is for backwards compatibility and will be deprecated in 0.4.0. No default will be assigned until the API is stabilized. custom events will only be surfaced in 'v2'.

'v2'
include_names Sequence[str] | None

Only include events from Runnables with matching names.

None
include_types Sequence[str] | None

Only include events from Runnables with matching types.

None
include_tags Sequence[str] | None

Only include events from Runnables with matching tags.

None
exclude_names Sequence[str] | None

Exclude events from Runnables with matching names.

None
exclude_types Sequence[str] | None

Exclude events from Runnables with matching types.

None
exclude_tags Sequence[str] | None

Exclude events from Runnables with matching tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable. These will be passed to astream_log as this implementation of astream_events is built on top of astream_log.

{}

Yields:

Type Description
AsyncIterator[StreamEvent]

An async stream of StreamEvents.

Raises:

Type Description
NotImplementedError

If the version is not 'v1' or 'v2'.

transform

transform(
    input: Iterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> Iterator[Output]

Transform inputs to outputs.

Default implementation of transform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input Iterator[Input]

An iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
Output

The output of the Runnable.

atransform async

atransform(
    input: AsyncIterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> AsyncIterator[Output]

Transform inputs to outputs.

Default implementation of atransform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input AsyncIterator[Input]

An async iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[Output]

The output of the Runnable.

bind

bind(**kwargs: Any) -> Runnable[Input, Output]

Bind arguments to a Runnable, returning a new Runnable.

Useful when a Runnable in a chain requires an argument that is not in the output of the previous Runnable or included in the user input.

Parameters:

Name Type Description Default
kwargs Any

The arguments to bind to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the arguments bound.

Example
from langchain_ollama import ChatOllama
from langchain_core.output_parsers import StrOutputParser

llm = ChatOllama(model="llama3.1")

# Without bind.
chain = llm | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two three four five.'

# With bind.
chain = llm.bind(stop=["three"]) | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two'

with_config

with_config(
    config: RunnableConfig | None = None, **kwargs: Any
) -> Runnable[Input, Output]

Bind config to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

The config to bind to the Runnable.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the config bound.

with_listeners

with_listeners(
    *,
    on_start: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_end: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_error: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None
) -> Runnable[Input, Output]

Bind lifecycle listeners to a Runnable, returning a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called before the Runnable starts running, with the Run object. Defaults to None.

None
on_end Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda
from langchain_core.tracers.schemas import Run

import time


def test_runnable(time_to_sleep: int):
    time.sleep(time_to_sleep)


def fn_start(run_obj: Run):
    print("start_time:", run_obj.start_time)


def fn_end(run_obj: Run):
    print("end_time:", run_obj.end_time)


chain = RunnableLambda(test_runnable).with_listeners(
    on_start=fn_start, on_end=fn_end
)
chain.invoke(2)

with_alisteners

with_alisteners(
    *,
    on_start: AsyncListener | None = None,
    on_end: AsyncListener | None = None,
    on_error: AsyncListener | None = None
) -> Runnable[Input, Output]

Bind async lifecycle listeners to a Runnable.

Returns a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start AsyncListener | None

Called asynchronously before the Runnable starts running, with the Run object. Defaults to None.

None
on_end AsyncListener | None

Called asynchronously after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error AsyncListener | None

Called asynchronously if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda, Runnable
from datetime import datetime, timezone
import time
import asyncio

def format_t(timestamp: float) -> str:
    return datetime.fromtimestamp(timestamp, tz=timezone.utc).isoformat()

async def test_runnable(time_to_sleep: int):
    print(f"Runnable[{time_to_sleep}s]: starts at {format_t(time.time())}")
    await asyncio.sleep(time_to_sleep)
    print(f"Runnable[{time_to_sleep}s]: ends at {format_t(time.time())}")

async def fn_start(run_obj: Runnable):
    print(f"on start callback starts at {format_t(time.time())}")
    await asyncio.sleep(3)
    print(f"on start callback ends at {format_t(time.time())}")

async def fn_end(run_obj: Runnable):
    print(f"on end callback starts at {format_t(time.time())}")
    await asyncio.sleep(2)
    print(f"on end callback ends at {format_t(time.time())}")

runnable = RunnableLambda(test_runnable).with_alisteners(
    on_start=fn_start,
    on_end=fn_end
)
async def concurrent_runs():
    await asyncio.gather(runnable.ainvoke(2), runnable.ainvoke(3))

asyncio.run(concurrent_runs())
Result:
on start callback starts at 2025-03-01T07:05:22.875378+00:00
on start callback starts at 2025-03-01T07:05:22.875495+00:00
on start callback ends at 2025-03-01T07:05:25.878862+00:00
on start callback ends at 2025-03-01T07:05:25.878947+00:00
Runnable[2s]: starts at 2025-03-01T07:05:25.879392+00:00
Runnable[3s]: starts at 2025-03-01T07:05:25.879804+00:00
Runnable[2s]: ends at 2025-03-01T07:05:27.881998+00:00
on end callback starts at 2025-03-01T07:05:27.882360+00:00
Runnable[3s]: ends at 2025-03-01T07:05:28.881737+00:00
on end callback starts at 2025-03-01T07:05:28.882428+00:00
on end callback ends at 2025-03-01T07:05:29.883893+00:00
on end callback ends at 2025-03-01T07:05:30.884831+00:00

with_types

with_types(
    *,
    input_type: type[Input] | None = None,
    output_type: type[Output] | None = None
) -> Runnable[Input, Output]

Bind input and output types to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
input_type type[Input] | None

The input type to bind to the Runnable. Defaults to None.

None
output_type type[Output] | None

The output type to bind to the Runnable. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the types bound.

with_retry

with_retry(
    *,
    retry_if_exception_type: tuple[
        type[BaseException], ...
    ] = (Exception,),
    wait_exponential_jitter: bool = True,
    exponential_jitter_params: (
        ExponentialJitterParams | None
    ) = None,
    stop_after_attempt: int = 3
) -> Runnable[Input, Output]

Create a new Runnable that retries the original Runnable on exceptions.

Parameters:

Name Type Description Default
retry_if_exception_type tuple[type[BaseException], ...]

A tuple of exception types to retry on. Defaults to (Exception,).

(Exception,)
wait_exponential_jitter bool

Whether to add jitter to the wait time between retries. Defaults to True.

True
stop_after_attempt int

The maximum number of attempts to make before giving up. Defaults to 3.

3
exponential_jitter_params ExponentialJitterParams | None

Parameters for tenacity.wait_exponential_jitter. Namely: initial, max, exp_base, and jitter (all float values).

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable that retries the original Runnable on exceptions.

Example
from langchain_core.runnables import RunnableLambda

count = 0


def _lambda(x: int) -> None:
    global count
    count = count + 1
    if x == 1:
        raise ValueError("x is 1")
    else:
        pass


runnable = RunnableLambda(_lambda)
try:
    runnable.with_retry(
        stop_after_attempt=2,
        retry_if_exception_type=(ValueError,),
    ).invoke(1)
except ValueError:
    pass

assert count == 2

map

map() -> Runnable[list[Input], list[Output]]

Return a new Runnable that maps a list of inputs to a list of outputs.

Calls invoke with each input.

Returns:

Type Description
Runnable[list[Input], list[Output]]

A new Runnable that maps a list of inputs to a list of outputs.

Example
from langchain_core.runnables import RunnableLambda


def _lambda(x: int) -> int:
    return x + 1


runnable = RunnableLambda(_lambda)
print(runnable.map().invoke([1, 2, 3]))  # [2, 3, 4]

with_fallbacks

with_fallbacks(
    fallbacks: Sequence[Runnable[Input, Output]],
    *,
    exceptions_to_handle: tuple[
        type[BaseException], ...
    ] = (Exception,),
    exception_key: str | None = None
) -> RunnableWithFallbacks[Input, Output]

Add fallbacks to a Runnable, returning a new Runnable.

The new Runnable will try the original Runnable, and then each fallback in order, upon failures.

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle. Defaults to (Exception,).

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input. Defaults to None.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

Example
from typing import Iterator

from langchain_core.runnables import RunnableGenerator


def _generate_immediate_error(input: Iterator) -> Iterator[str]:
    raise ValueError()
    yield ""


def _generate(input: Iterator) -> Iterator[str]:
    yield from "foo bar"


runnable = RunnableGenerator(_generate_immediate_error).with_fallbacks(
    [RunnableGenerator(_generate)]
)
print("".join(runnable.stream({})))  # foo bar

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle.

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

as_tool

as_tool(
    args_schema: type[BaseModel] | None = None,
    *,
    name: str | None = None,
    description: str | None = None,
    arg_types: dict[str, type] | None = None
) -> BaseTool

Create a BaseTool from a Runnable.

as_tool will instantiate a BaseTool with a name, description, and args_schema from a Runnable. Where possible, schemas are inferred from runnable.get_input_schema. Alternatively (e.g., if the Runnable takes a dict as input and the specific dict keys are not typed), the schema can be specified directly with args_schema. You can also pass arg_types to just specify the required arguments and their types.

Parameters:

Name Type Description Default
args_schema type[BaseModel] | None

The schema for the tool. Defaults to None.

None
name str | None

The name of the tool. Defaults to None.

None
description str | None

The description of the tool. Defaults to None.

None
arg_types dict[str, type] | None

A dictionary of argument names to types. Defaults to None.

None

Returns:

Type Description
BaseTool

A BaseTool instance.

Typed dict input:

from typing_extensions import TypedDict
from langchain_core.runnables import RunnableLambda


class Args(TypedDict):
    a: int
    b: list[int]


def f(x: Args) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool()
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via args_schema:

from typing import Any
from pydantic import BaseModel, Field
from langchain_core.runnables import RunnableLambda

def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))

class FSchema(BaseModel):
    """Apply a function to an integer and list of integers."""

    a: int = Field(..., description="Integer")
    b: list[int] = Field(..., description="List of ints")

runnable = RunnableLambda(f)
as_tool = runnable.as_tool(FSchema)
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via arg_types:

from typing import Any
from langchain_core.runnables import RunnableLambda


def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool(arg_types={"a": int, "b": list[int]})
as_tool.invoke({"a": 3, "b": [1, 2]})

String input:

from langchain_core.runnables import RunnableLambda


def f(x: str) -> str:
    return x + "a"


def g(x: str) -> str:
    return x + "z"


runnable = RunnableLambda(f) | g
as_tool = runnable.as_tool()
as_tool.invoke("b")

Added in version 0.2.14

lc_id classmethod

lc_id() -> list[str]

Return a unique identifier for this class for serialization purposes.

The unique identifier is a list of strings that describes the path to the object. For example, for the class langchain.llms.openai.OpenAI, the id is ["langchain", "llms", "openai", "OpenAI"].

to_json

to_json() -> (
    SerializedConstructor | SerializedNotImplemented
)

Serialize the Runnable to JSON.

Returns:

Type Description
SerializedConstructor | SerializedNotImplemented

A JSON-serializable representation of the Runnable.

to_json_not_implemented

to_json_not_implemented() -> SerializedNotImplemented

Serialize a "not implemented" object.

Returns:

Type Description
SerializedNotImplemented

SerializedNotImplemented.

configurable_fields

configurable_fields(
    **kwargs: AnyConfigurableField,
) -> RunnableSerializable[Input, Output]

Configure particular Runnable fields at runtime.

Parameters:

Name Type Description Default
**kwargs AnyConfigurableField

A dictionary of ConfigurableField instances to configure.

{}

Raises:

Type Description
ValueError

If a configuration key is not found in the Runnable.

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the fields configured.

from langchain_core.runnables import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatOpenAI(max_tokens=20).configurable_fields(
    max_tokens=ConfigurableField(
        id="output_token_number",
        name="Max tokens in the output",
        description="The maximum number of tokens in the output",
    )
)

# max_tokens = 20
print("max_tokens_20: ", model.invoke("tell me something about chess").content)

# max_tokens = 200
print(
    "max_tokens_200: ",
    model.with_config(configurable={"output_token_number": 200})
    .invoke("tell me something about chess")
    .content,
)

configurable_alternatives

configurable_alternatives(
    which: ConfigurableField,
    *,
    default_key: str = "default",
    prefix_keys: bool = False,
    **kwargs: (
        Runnable[Input, Output]
        | Callable[[], Runnable[Input, Output]]
    )
) -> RunnableSerializable[Input, Output]

Configure alternatives for Runnables that can be set at runtime.

Parameters:

Name Type Description Default
which ConfigurableField

The ConfigurableField instance that will be used to select the alternative.

required
default_key str

The default key to use if no alternative is selected. Defaults to 'default'.

'default'
prefix_keys bool

Whether to prefix the keys with the ConfigurableField id. Defaults to False.

False
**kwargs Runnable[Input, Output] | Callable[[], Runnable[Input, Output]]

A dictionary of keys to Runnable instances or callables that return Runnable instances.

{}

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the alternatives configured.

from langchain_anthropic import ChatAnthropic
from langchain_core.runnables.utils import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatAnthropic(
    model_name="claude-3-7-sonnet-20250219"
).configurable_alternatives(
    ConfigurableField(id="llm"),
    default_key="anthropic",
    openai=ChatOpenAI(),
)

# uses the default model ChatAnthropic
print(model.invoke("which organization created you?").content)

# uses ChatOpenAI
print(
    model.with_config(configurable={"llm": "openai"})
    .invoke("which organization created you?")
    .content
)

set_verbose

set_verbose(verbose: bool | None) -> bool

If verbose is None, set it.

This allows users to pass in None as verbose to access the global setting.

Parameters:

Name Type Description Default
verbose bool | None

The verbosity setting to use.

required

Returns:

Type Description
bool

The verbosity setting to use.

get_token_ids

get_token_ids(text: str) -> list[int]

Return the ordered ids of the tokens in a text.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
list[int]

A list of ids corresponding to the tokens in the text, in order they occur

list[int]

in the text.

get_num_tokens_from_messages

get_num_tokens_from_messages(
    messages: list[BaseMessage],
    tools: Sequence | None = None,
) -> int

Get the number of tokens in the messages.

Useful for checking if an input fits in a model's context window.

Note

The base implementation of get_num_tokens_from_messages ignores tool schemas.

Parameters:

Name Type Description Default
messages list[BaseMessage]

The message inputs to tokenize.

required
tools Sequence | None

If provided, sequence of dict, BaseModel, function, or BaseTools to be converted to tool schemas.

None

Returns:

Type Description
int

The sum of the number of tokens across the messages.

generate

generate(
    messages: list[list[BaseMessage]],
    stop: list[str] | None = None,
    callbacks: Callbacks = None,
    *,
    tags: list[str] | None = None,
    metadata: dict[str, Any] | None = None,
    run_name: str | None = None,
    run_id: UUID | None = None,
    **kwargs: Any
) -> LLMResult

Pass a sequence of prompts to the model and return model generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
messages list[list[BaseMessage]]

List of list of messages.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | None

The tags to apply.

None
metadata dict[str, Any] | None

The metadata to apply.

None
run_name str | None

The name of the run.

None
run_id UUID | None

The ID of the run.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input

LLMResult

prompt and additional model provider-specific output.

agenerate async

agenerate(
    messages: list[list[BaseMessage]],
    stop: list[str] | None = None,
    callbacks: Callbacks = None,
    *,
    tags: list[str] | None = None,
    metadata: dict[str, Any] | None = None,
    run_name: str | None = None,
    run_id: UUID | None = None,
    **kwargs: Any
) -> LLMResult

Asynchronously pass a sequence of prompts to a model and return generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
messages list[list[BaseMessage]]

List of list of messages.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | None

The tags to apply.

None
metadata dict[str, Any] | None

The metadata to apply.

None
run_name str | None

The name of the run.

None
run_id UUID | None

The ID of the run.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input

LLMResult

prompt and additional model provider-specific output.

dict

dict(**kwargs: Any) -> dict

Return a dictionary of the LLM.

__init__

__init__(
    *, model_name: Optional[str] = None, **kwargs: Any
) -> None

Needed for mypy typing to recognize model_name as a valid arg and for arg validation.

get_lc_namespace classmethod

get_lc_namespace() -> List[str]

Get the namespace of the langchain object.

build_extra classmethod

build_extra(values: dict[str, Any]) -> Any

Build extra kwargs from additional params that were passed in.

validate_environment

validate_environment() -> Self

Validate that the python package exists in environment.

get_num_tokens

get_num_tokens(text: str) -> int

Get the number of tokens present in the text.

More info <https://cloud.google.com/vertex-ai/docs/reference/rpc/google.cloud.aiplatform.v1beta1#counttokensrequest>__

with_structured_output

with_structured_output(
    schema: Union[Dict, Type[BaseModel], Type],
    *,
    include_raw: bool = False,
    method: Optional[Literal["json_mode"]] = None,
    **kwargs: Any
) -> Runnable[LanguageModelInput, Union[Dict, BaseModel]]

Model wrapper that returns outputs formatted to match the given schema.

.. versionchanged:: 1.1.0

Return type corrected in version 1.1.0. Previously if a dict schema
was provided then the output had the form
``[{"args": {}, "name": "schema_name"}]`` where the output was a list with
a single dict and the "args" of the one dict corresponded to the schema.
As of ``1.1.0`` this has been fixed so that the schema (the value
corresponding to the old "args" key) is returned directly.

Parameters:

Name Type Description Default
schema Union[Dict, Type[BaseModel], Type]

The output schema as a dict or a Pydantic class. If a Pydantic class then the model output will be an object of that class. If a dict then the model output will be a dict. With a Pydantic class the returned attributes will be validated, whereas with a dict they will not be. If method is 'function_calling' and schema is a dict, then the dict must match the OpenAI function-calling spec.

required
include_raw bool

If False then only the parsed structured output is returned. If an error occurs during model output parsing it will be raised. If True then both the raw model response (a BaseMessage) and the parsed model response will be returned. If an error occurs during output parsing it will be caught and returned as well. The final output is always a dict with keys 'raw', 'parsed', and 'parsing_error'.

False
method Optional[Literal['json_mode']]

If set to 'json_schema' it will use controlled genetration to generate the response rather than function calling. Does not work with schemas with references or Pydantic models with self-references.

None

Returns:

Name Type Description
Runnable[LanguageModelInput, Union[Dict, BaseModel]]

A Runnable that takes any ChatModel input. If 'include_raw' is True then a

Runnable[LanguageModelInput, Union[Dict, BaseModel]]

dict with keys — raw: BaseMessage, parsed: Optional[_DictOrPydantic],

parsing_error Runnable[LanguageModelInput, Union[Dict, BaseModel]]

Optional[BaseException]. If 'include_raw' is False then just

Runnable[LanguageModelInput, Union[Dict, BaseModel]]

_DictOrPydantic is returned, where _DictOrPydantic depends on the schema.

Runnable[LanguageModelInput, Union[Dict, BaseModel]]

If schema is a Pydantic class then _DictOrPydantic is the Pydantic class.

Runnable[LanguageModelInput, Union[Dict, BaseModel]]

If schema is a dict then _DictOrPydantic is a dict.

Pydantic schema, exclude raw:

.. code-block:: python

from pydantic import BaseModel
from langchain_google_vertexai import ChatVertexAI


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


llm = ChatVertexAI(model_name="gemini-2.0-flash-001", temperature=0)
structured_llm = llm.with_structured_output(AnswerWithJustification)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)
# -> AnswerWithJustification(
#     answer='They weigh the same.', justification='A pound is a pound.'
# )
Pydantic schema, include raw:

.. code-block:: python

from pydantic import BaseModel
from langchain_google_vertexai import ChatVertexAI


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


llm = ChatVertexAI(model_name="gemini-2.0-flash-001", temperature=0)
structured_llm = llm.with_structured_output(
    AnswerWithJustification, include_raw=True
)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)
# -> {
#     'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),
#     'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),
#     'parsing_error': None
# }
Dict schema, exclude raw:

.. code-block:: python

from pydantic import BaseModel
from langchain_core.utils.function_calling import (
    convert_to_openai_function,
)
from langchain_google_vertexai import ChatVertexAI


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


dict_schema = convert_to_openai_function(AnswerWithJustification)
llm = ChatVertexAI(model_name="gemini-2.0-flash-001", temperature=0)
structured_llm = llm.with_structured_output(dict_schema)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)
# -> {
#     'answer': 'They weigh the same',
#     'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
# }
Pydantic schema, streaming:

.. code-block:: python

from pydantic import BaseModel, Field
from langchain_google_vertexai import ChatVertexAI


class Explanation(BaseModel):
    '''A topic explanation with examples.'''

    description: str = Field(
        description="A brief description of the topic."
    )
    examples: str = Field(
        description="Two examples related to the topic."
    )


llm = ChatVertexAI(model_name="gemini-2.0-flash", temperature=0)
structured_llm = llm.with_structured_output(
    Explanation, method="json_mode"
)

for chunk in structured_llm.stream("Tell me about transformer models"):
    print(chunk)
    print("-------------------------")
# -> description='Transformer models are a type of neural network architecture that have revolutionized the field of natural language processing (NLP) and are also increasingly used in computer vision and other domains. They rely on the self-attention mechanism to weigh the importance of different parts of the input data, allowing them to effectively capture long-range dependencies. Unlike recurrent neural networks (RNNs), transformers can process the entire input sequence in parallel, leading to significantly faster training times. Key components of transformer models include: the self-attention mechanism (calculates attention weights between different parts of the input), multi-head attention (performs self-attention multiple times with different learned parameters), positional encoding (adds information about the position of tokens in the input sequence), feedforward networks (applies a non-linear transformation to each position), and encoder-decoder structure (used for sequence-to-sequence tasks).' examples='1. BERT (Bidirectional Encoder Representations from Transformers): A pre-trained transformer'
#    -------------------------
#    description='Transformer models are a type of neural network architecture that have revolutionized the field of natural language processing (NLP) and are also increasingly used in computer vision and other domains. They rely on the self-attention mechanism to weigh the importance of different parts of the input data, allowing them to effectively capture long-range dependencies. Unlike recurrent neural networks (RNNs), transformers can process the entire input sequence in parallel, leading to significantly faster training times. Key components of transformer models include: the self-attention mechanism (calculates attention weights between different parts of the input), multi-head attention (performs self-attention multiple times with different learned parameters), positional encoding (adds information about the position of tokens in the input sequence), feedforward networks (applies a non-linear transformation to each position), and encoder-decoder structure (used for sequence-to-sequence tasks).' examples='1. BERT (Bidirectional Encoder Representations from Transformers): A pre-trained transformer model that can be fine-tuned for various NLP tasks like text classification, question answering, and named entity recognition. 2. GPT (Generative Pre-trained Transformer): A language model that uses transformers to generate coherent and contextually relevant text. GPT models are used in chatbots, content creation, and code generation.'
#    -------------------------

bind_tools

bind_tools(
    tools: _ToolsType,
    tool_config: Optional[_ToolConfigDict] = None,
    *,
    tool_choice: Optional[
        Union[_ToolChoiceType, bool]
    ] = None,
    **kwargs: Any
) -> Runnable[LanguageModelInput, BaseMessage]

Bind tool-like objects to this chat model.

Assumes model is compatible with Vertex tool-calling API.

Parameters:

Name Type Description Default
tools _ToolsType

A list of tool definitions to bind to this chat model. Can be a pydantic model, callable, or BaseTool. Pydantic models, callables, and BaseTools will be automatically converted to their schema dictionary representation. Tools with Union types in their arguments are now supported and converted to anyOf schemas.

required
**kwargs Any

Any additional parameters to pass to the :class:~langchain.runnable.Runnable constructor.

{}

VertexAIEmbeddings

Bases: _VertexAICommon, Embeddings

Google Cloud VertexAI embedding models.

Methods:

Name Description
aembed_documents

Asynchronous Embed search docs.

aembed_query

Asynchronous Embed query text.

validate_environment

Validates that the python package exists in environment.

__init__

Initialize the sentence_transformer.

embed

Embed a list of strings.

embed_documents

Embed a list of documents.

embed_query

Embed a text.

embed_image

Embed an image.

embed_images

Embed a list of images.

Attributes:

Name Type Description
project Optional[str]

The default GCP project to use when making Vertex API calls.

location str

The default location to use when making API calls.

request_parallelism int

The amount of parallelism allowed for requests issued to VertexAI models.

max_retries int

The maximum number of retries to make when generating.

stop Optional[List[str]]

Optional list of stop words to use when generating.

model_name Optional[str]

Underlying model name.

full_model_name Optional[str]

The full name of the model's endpoint.

api_endpoint Optional[str]

Desired API endpoint, e.g., us-central1-aiplatform.googleapis.com

api_transport Optional[str]

The desired API transport method, can be either 'grpc' or 'rest'.

additional_headers Optional[Dict[str, str]]

A key-value dictionary representing additional headers for the model call

client_cert_source Optional[Callable[[], Tuple[bytes, bytes]]]

A callback which returns client certificate bytes and private key bytes both

credentials Any

The default custom credentials (google.auth.credentials.Credentials) to use

endpoint_version Literal['v1', 'v1beta1']

Whether to use v1 or v1beta1 endpoint.

prediction_client Union[PredictionServiceClient, PredictionServiceClient]

Returns PredictionServiceClient.

async_prediction_client Union[PredictionServiceAsyncClient, PredictionServiceAsyncClient]

Returns PredictionServiceClient.

temperature Optional[float]

Sampling temperature, it controls the degree of randomness in token selection.

frequency_penalty Optional[float]

Positive values penalize tokens that repeatedly appear in the generated text,

presence_penalty Optional[float]

Positive values penalize tokens that already appear in the generated text,

max_output_tokens Optional[int]

Token limit determines the maximum amount of text output from one prompt.

top_p Optional[float]

Tokens are selected from most probable to least until the sum of their

top_k Optional[int]

How the model selects tokens for output, the next token is selected from

n int

How many completions to generate for each prompt.

seed Optional[int]

Random seed for the generation.

streaming bool

Whether to stream the results or not.

safety_settings Optional[SafetySettingsType]

The default safety settings to use for all generations.

tuned_model_name Optional[str]

The name of a tuned model.

project class-attribute instance-attribute

project: Optional[str] = None

The default GCP project to use when making Vertex API calls.

location class-attribute instance-attribute

location: str = Field(default=_DEFAULT_LOCATION)

The default location to use when making API calls.

request_parallelism class-attribute instance-attribute

request_parallelism: int = 5

The amount of parallelism allowed for requests issued to VertexAI models.

max_retries class-attribute instance-attribute

max_retries: int = 6

The maximum number of retries to make when generating.

stop class-attribute instance-attribute

stop: Optional[List[str]] = Field(
    default=None, alias="stop_sequences"
)

Optional list of stop words to use when generating.

model_name class-attribute instance-attribute

model_name: Optional[str] = Field(
    default=None, alias="model"
)

Underlying model name.

full_model_name class-attribute instance-attribute

full_model_name: Optional[str] = Field(
    default=None, exclude=True
)

The full name of the model's endpoint.

api_endpoint class-attribute instance-attribute

api_endpoint: Optional[str] = Field(
    default=None, alias="base_url"
)

Desired API endpoint, e.g., us-central1-aiplatform.googleapis.com

api_transport class-attribute instance-attribute

api_transport: Optional[str] = None

The desired API transport method, can be either 'grpc' or 'rest'. Uses the default parameter in vertexai.init if defined.

additional_headers class-attribute instance-attribute

additional_headers: Optional[Dict[str, str]] = Field(
    default=None
)

A key-value dictionary representing additional headers for the model call

client_cert_source class-attribute instance-attribute

client_cert_source: Optional[
    Callable[[], Tuple[bytes, bytes]]
] = None

A callback which returns client certificate bytes and private key bytes both

credentials class-attribute instance-attribute

credentials: Any = Field(default=None, exclude=True)

The default custom credentials (google.auth.credentials.Credentials) to use

endpoint_version class-attribute instance-attribute

endpoint_version: Literal['v1', 'v1beta1'] = 'v1beta1'

Whether to use v1 or v1beta1 endpoint.

v1 is more performant, but v1beta1 might have some new features.

prediction_client property

prediction_client: Union[
    PredictionServiceClient, PredictionServiceClient
]

Returns PredictionServiceClient.

async_prediction_client property

async_prediction_client: Union[
    PredictionServiceAsyncClient,
    PredictionServiceAsyncClient,
]

Returns PredictionServiceClient.

temperature class-attribute instance-attribute

temperature: Optional[float] = None

Sampling temperature, it controls the degree of randomness in token selection.

frequency_penalty class-attribute instance-attribute

frequency_penalty: Optional[float] = None

Positive values penalize tokens that repeatedly appear in the generated text,

presence_penalty class-attribute instance-attribute

presence_penalty: Optional[float] = None

Positive values penalize tokens that already appear in the generated text,

max_output_tokens class-attribute instance-attribute

max_output_tokens: Optional[int] = Field(
    default=None, alias="max_tokens"
)

Token limit determines the maximum amount of text output from one prompt.

top_p class-attribute instance-attribute

top_p: Optional[float] = None

Tokens are selected from most probable to least until the sum of their

top_k class-attribute instance-attribute

top_k: Optional[int] = None

How the model selects tokens for output, the next token is selected from

n class-attribute instance-attribute

n: int = 1

How many completions to generate for each prompt.

seed class-attribute instance-attribute

seed: Optional[int] = None

Random seed for the generation.

streaming class-attribute instance-attribute

streaming: bool = False

Whether to stream the results or not.

safety_settings class-attribute instance-attribute

safety_settings: Optional[SafetySettingsType] = None

The default safety settings to use for all generations.

For example:

from langchain_google_vertexai import HarmBlockThreshold, HarmCategory

safety_settings = {
    HarmCategory.HARM_CATEGORY_UNSPECIFIED: HarmBlockThreshold.BLOCK_NONE,
    HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
    HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_ONLY_HIGH,
    HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
    HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
}

tuned_model_name class-attribute instance-attribute

tuned_model_name: Optional[str] = None

The name of a tuned model.

aembed_documents async

aembed_documents(texts: list[str]) -> list[list[float]]

Asynchronous Embed search docs.

Parameters:

Name Type Description Default
texts list[str]

List of text to embed.

required

Returns:

Type Description
list[list[float]]

List of embeddings.

aembed_query async

aembed_query(text: str) -> list[float]

Asynchronous Embed query text.

Parameters:

Name Type Description Default
text str

Text to embed.

required

Returns:

Type Description
list[float]

Embedding.

validate_environment

validate_environment() -> Self

Validates that the python package exists in environment.

__init__

__init__(
    model_name: Optional[str] = None,
    project: Optional[str] = None,
    location: str = "us-central1",
    request_parallelism: int = 5,
    max_retries: int = 6,
    credentials: Optional[Any] = None,
    **kwargs: Any
) -> None

Initialize the sentence_transformer.

embed

embed(
    texts: List[str],
    batch_size: int = 0,
    embeddings_task_type: Optional[
        EmbeddingTaskTypes
    ] = None,
    dimensions: Optional[int] = None,
) -> List[List[float]]

Embed a list of strings.

Parameters:

Name Type Description Default
texts List[str]

List[str] The list of strings to embed.

required
batch_size int

[int] The batch size of embeddings to send to the model. If zero, then the largest batch size will be detected dynamically at the first request, starting from 250, down to 5.

0
embeddings_task_type Optional[EmbeddingTaskTypes]

[str] optional embeddings task type, one of the following RETRIEVAL_QUERY - Text is a query in a search/retrieval setting. RETRIEVAL_DOCUMENT - Text is a document in a search/retrieval setting. SEMANTIC_SIMILARITY - Embeddings will be used for Semantic Textual Similarity (STS). CLASSIFICATION - Embeddings will be used for classification. CLUSTERING - Embeddings will be used for clustering. CODE_RETRIEVAL_QUERY - Embeddings will be used for code retrieval for Java and Python. The following are only supported on preview models: QUESTION_ANSWERING FACT_VERIFICATION

None
dimensions Optional[int]

[int] optional. Output embeddings dimensions. Only supported on preview models.

None

Returns:

Type Description
List[List[float]]

List of embeddings, one for each text.

embed_documents

embed_documents(
    texts: List[str],
    batch_size: int = 0,
    *,
    embeddings_task_type: EmbeddingTaskTypes = "RETRIEVAL_DOCUMENT"
) -> List[List[float]]

Embed a list of documents.

Parameters:

Name Type Description Default
texts List[str]

List[str] The list of texts to embed.

required
batch_size int

[int] The batch size of embeddings to send to the model. If zero, then the largest batch size will be detected dynamically at the first request, starting from 250, down to 5.

0

Returns:

Type Description
List[List[float]]

List of embeddings, one for each text.

embed_query

embed_query(
    text: str,
    *,
    embeddings_task_type: EmbeddingTaskTypes = "RETRIEVAL_QUERY"
) -> List[float]

Embed a text.

Parameters:

Name Type Description Default
text str

The text to embed.

required

Returns:

Type Description
List[float]

Embedding for the text.

embed_image

embed_image(
    image_path: str,
    contextual_text: Optional[str] = None,
    dimensions: Optional[int] = None,
) -> List[float]

Embed an image.

Parameters:

Name Type Description Default
image_path str

Path to image (Google Cloud Storage or web) to generate

required
contextual_text Optional[str]

Text to generate embeddings for.

None

Returns:

Type Description
List[float]

Embedding for the image.

embed_images

embed_images(
    uris: List[str],
    contextual_text: Optional[str] = None,
    dimensions: Optional[int] = None,
) -> List[List[float]]

Embed a list of images.

Parameters:

Name Type Description Default
uris List[str]

Paths to image (local, Google Cloud Storage or web) to generate

required
contextual_text Optional[str]

Text to generate embeddings for.

None

Returns:

Type Description
List[List[float]]

Embedding for the image.

VertexPairWiseStringEvaluator

Bases: _EvaluatorBase, PairwiseStringEvaluator

Evaluate the perplexity of a predicted string.

Methods:

Name Description
evaluate_string_pairs

Evaluate the output string pairs.

aevaluate_string_pairs

Asynchronously evaluate the output string pairs.

Attributes:

Name Type Description
requires_input bool

Whether this evaluator requires an input string.

requires_reference bool

Whether this evaluator requires a reference label.

requires_input property

requires_input: bool

Whether this evaluator requires an input string.

requires_reference property

requires_reference: bool

Whether this evaluator requires a reference label.

evaluate_string_pairs

evaluate_string_pairs(
    *,
    prediction: str,
    prediction_b: str,
    reference: Optional[str] = None,
    input: Optional[str] = None,
    **kwargs: Any
) -> dict

Evaluate the output string pairs.

Parameters:

Name Type Description Default
prediction str

The output string from the first model.

required
prediction_b str

The output string from the second model.

required
reference Optional[str]

The expected output / reference string.

None
input Optional[str]

The input string.

None
**kwargs Any

Additional keyword arguments, such as callbacks and optional reference strings.

{}

Returns:

Name Type Description
dict dict

A dictionary containing the preference, scores, and/or other information.

aevaluate_string_pairs async

aevaluate_string_pairs(
    *,
    prediction: str,
    prediction_b: str,
    reference: Optional[str] = None,
    input: Optional[str] = None,
    **kwargs: Any
) -> dict

Asynchronously evaluate the output string pairs.

Parameters:

Name Type Description Default
prediction str

The output string from the first model.

required
prediction_b str

The output string from the second model.

required
reference Optional[str]

The expected output / reference string.

None
input Optional[str]

The input string.

None
**kwargs Any

Additional keyword arguments, such as callbacks and optional reference strings.

{}

Returns:

Name Type Description
dict dict

A dictionary containing the preference, scores, and/or other information.

VertexStringEvaluator

Bases: _EvaluatorBase, StringEvaluator

Evaluate the perplexity of a predicted string.

Methods:

Name Description
evaluate_strings

Evaluate Chain or LLM output, based on optional input and label.

aevaluate_strings

Asynchronously evaluate Chain or LLM output, based on optional input and label.

Attributes:

Name Type Description
requires_reference bool

Whether this evaluator requires a reference label.

requires_input bool

Whether this evaluator requires an input string.

evaluation_name str

The name of the evaluation.

requires_reference property

requires_reference: bool

Whether this evaluator requires a reference label.

requires_input property

requires_input: bool

Whether this evaluator requires an input string.

evaluation_name property

evaluation_name: str

The name of the evaluation.

evaluate_strings

evaluate_strings(
    *,
    prediction: str,
    reference: Optional[str] = None,
    input: Optional[str] = None,
    **kwargs: Any
) -> dict

Evaluate Chain or LLM output, based on optional input and label.

Parameters:

Name Type Description Default
prediction str

The LLM or chain prediction to evaluate.

required
reference Optional[str]

The reference label to evaluate against.

None
input Optional[str]

The input to consider during evaluation.

None
**kwargs Any

Additional keyword arguments, including callbacks, tags, etc.

{}

Returns:

Name Type Description
dict dict

The evaluation results containing the score or value.

aevaluate_strings async

aevaluate_strings(
    *,
    prediction: str,
    reference: Optional[str] = None,
    input: Optional[str] = None,
    **kwargs: Any
) -> dict

Asynchronously evaluate Chain or LLM output, based on optional input and label.

Parameters:

Name Type Description Default
prediction str

The LLM or chain prediction to evaluate.

required
reference Optional[str]

The reference label to evaluate against.

None
input Optional[str]

The input to consider during evaluation.

None
**kwargs Any

Additional keyword arguments, including callbacks, tags, etc.

{}

Returns:

Name Type Description
dict dict

The evaluation results containing the score or value.

PydanticFunctionsOutputParser

Bases: BaseOutputParser

Parse an output as a pydantic object.

This parser is used to parse the output of a ChatModel that uses Google Vertex function format to invoke functions.

The parser extracts the function call invocation and matches them to the pydantic schema provided.

An exception will be raised if the function call does not match the provided schema.

Example

... code-block:: python

message = AIMessage(
    content="This is a test message",
    additional_kwargs={
        "function_call": {
            "name": "cookie",
            "arguments": json.dumps({"name": "value", "age": 10}),
        }
    },
)
chat_generation = ChatGeneration(message=message)

class Cookie(BaseModel):
    name: str
    age: int

class Dog(BaseModel):
    species: str

# Full output
parser = PydanticOutputFunctionsParser(
    pydantic_schema={"cookie": Cookie, "dog": Dog}
)
result = parser.parse_result([chat_generation])

Methods:

Name Description
get_name

Get the name of the Runnable.

get_input_schema

Get a pydantic model that can be used to validate input to the Runnable.

get_input_jsonschema

Get a JSON schema that represents the input to the Runnable.

get_output_schema

Get a pydantic model that can be used to validate output to the Runnable.

get_output_jsonschema

Get a JSON schema that represents the output of the Runnable.

config_schema

The type of config this Runnable accepts specified as a pydantic model.

get_config_jsonschema

Get a JSON schema that represents the config of the Runnable.

get_graph

Return a graph representation of this Runnable.

get_prompts

Return a list of prompts used by this Runnable.

__or__

Runnable "or" operator.

__ror__

Runnable "reverse-or" operator.

pipe

Pipe runnables.

pick

Pick keys from the output dict of this Runnable.

assign

Assigns new fields to the dict output of this Runnable.

batch

Default implementation runs invoke in parallel using a thread pool executor.

batch_as_completed

Run invoke in parallel on a list of inputs.

abatch

Default implementation runs ainvoke in parallel using asyncio.gather.

abatch_as_completed

Run ainvoke in parallel on a list of inputs.

stream

Default implementation of stream, which calls invoke.

astream

Default implementation of astream, which calls ainvoke.

astream_log

Stream all output from a Runnable, as reported to the callback system.

astream_events

Generate a stream of events.

transform

Transform inputs to outputs.

atransform

Transform inputs to outputs.

bind

Bind arguments to a Runnable, returning a new Runnable.

with_config

Bind config to a Runnable, returning a new Runnable.

with_listeners

Bind lifecycle listeners to a Runnable, returning a new Runnable.

with_alisteners

Bind async lifecycle listeners to a Runnable.

with_types

Bind input and output types to a Runnable, returning a new Runnable.

with_retry

Create a new Runnable that retries the original Runnable on exceptions.

map

Return a new Runnable that maps a list of inputs to a list of outputs.

with_fallbacks

Add fallbacks to a Runnable, returning a new Runnable.

as_tool

Create a BaseTool from a Runnable.

__init__
is_lc_serializable

Is this class serializable?

get_lc_namespace

Get the namespace of the langchain object.

lc_id

Return a unique identifier for this class for serialization purposes.

to_json

Serialize the Runnable to JSON.

to_json_not_implemented

Serialize a "not implemented" object.

configurable_fields

Configure particular Runnable fields at runtime.

configurable_alternatives

Configure alternatives for Runnables that can be set at runtime.

aparse_result

Async parse a list of candidate model Generations into a specific format.

aparse

Async parse a single string model output into some structure.

parse_with_prompt

Parse the output of an LLM call with the input prompt for context.

get_format_instructions

Instructions on how the LLM output should be formatted.

dict

Return dictionary representation of output parser.

Attributes:

Name Type Description
InputType Any

Return the input type for the parser.

OutputType type[T]

Return the output type for the parser.

input_schema type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema type[BaseModel]

Output schema.

config_specs list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets dict[str, str]

A map of constructor argument names to secret ids.

lc_attributes dict

List of attribute names that should be included in the serialized kwargs.

InputType property

InputType: Any

Return the input type for the parser.

OutputType property

OutputType: type[T]

Return the output type for the parser.

This property is inferred from the first type argument of the class.

Raises:

Type Description
TypeError

If the class doesn't have an inferable OutputType.

input_schema property

input_schema: type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema property

output_schema: type[BaseModel]

Output schema.

The type of output this Runnable produces specified as a pydantic model.

config_specs property

config_specs: list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets property

lc_secrets: dict[str, str]

A map of constructor argument names to secret ids.

For example,

lc_attributes property

lc_attributes: dict

List of attribute names that should be included in the serialized kwargs.

These attributes must be accepted by the constructor. Default is an empty dictionary.

get_name

get_name(
    suffix: str | None = None, *, name: str | None = None
) -> str

Get the name of the Runnable.

Parameters:

Name Type Description Default
suffix str | None

An optional suffix to append to the name.

None
name str | None

An optional name to use instead of the Runnable's name.

None

Returns:

Type Description
str

The name of the Runnable.

get_input_schema

get_input_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate input to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic input schema that depends on which configuration the Runnable is invoked with.

This method allows to get an input schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate input.

get_input_jsonschema

get_input_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the input to the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the input to the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_input_jsonschema())

Added in version 0.3.0

get_output_schema

get_output_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate output to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic output schema that depends on which configuration the Runnable is invoked with.

This method allows to get an output schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate output.

get_output_jsonschema

get_output_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the output of the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the output of the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_output_jsonschema())

Added in version 0.3.0

config_schema

config_schema(
    *, include: Sequence[str] | None = None
) -> type[BaseModel]

The type of config this Runnable accepts specified as a pydantic model.

To mark a field as configurable, see the configurable_fields and configurable_alternatives methods.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate config.

get_config_jsonschema

get_config_jsonschema(
    *, include: Sequence[str] | None = None
) -> dict[str, Any]

Get a JSON schema that represents the config of the Runnable.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the config of the Runnable.

Added in version 0.3.0

get_graph

get_graph(config: RunnableConfig | None = None) -> Graph

Return a graph representation of this Runnable.

get_prompts

get_prompts(
    config: RunnableConfig | None = None,
) -> list[BasePromptTemplate]

Return a list of prompts used by this Runnable.

__or__

__or__(
    other: (
        Runnable[Any, Other]
        | Callable[[Iterator[Any]], Iterator[Other]]
        | Callable[
            [AsyncIterator[Any]], AsyncIterator[Other]
        ]
        | Callable[[Any], Other]
        | Mapping[
            str,
            Runnable[Any, Other]
            | Callable[[Any], Other]
            | Any,
        ]
    ),
) -> RunnableSerializable[Input, Other]

Runnable "or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Any, Other] | Callable[[Iterator[Any]], Iterator[Other]] | Callable[[AsyncIterator[Any]], AsyncIterator[Other]] | Callable[[Any], Other] | Mapping[str, Runnable[Any, Other] | Callable[[Any], Other] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

__ror__

__ror__(
    other: (
        Runnable[Other, Any]
        | Callable[[Iterator[Other]], Iterator[Any]]
        | Callable[
            [AsyncIterator[Other]], AsyncIterator[Any]
        ]
        | Callable[[Other], Any]
        | Mapping[
            str,
            Runnable[Other, Any]
            | Callable[[Other], Any]
            | Any,
        ]
    ),
) -> RunnableSerializable[Other, Output]

Runnable "reverse-or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Other, Any] | Callable[[Iterator[Other]], Iterator[Any]] | Callable[[AsyncIterator[Other]], AsyncIterator[Any]] | Callable[[Other], Any] | Mapping[str, Runnable[Other, Any] | Callable[[Other], Any] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Other, Output]

A new Runnable.

pipe

pipe(
    *others: Runnable[Any, Other] | Callable[[Any], Other],
    name: str | None = None
) -> RunnableSerializable[Input, Other]

Pipe runnables.

Compose this Runnable with Runnable-like objects to make a RunnableSequence.

Equivalent to RunnableSequence(self, *others) or self | others[0] | ...

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


def mul_two(x: int) -> int:
    return x * 2


runnable_1 = RunnableLambda(add_one)
runnable_2 = RunnableLambda(mul_two)
sequence = runnable_1.pipe(runnable_2)
# Or equivalently:
# sequence = runnable_1 | runnable_2
# sequence = RunnableSequence(first=runnable_1, last=runnable_2)
sequence.invoke(1)
await sequence.ainvoke(1)
# -> 4

sequence.batch([1, 2, 3])
await sequence.abatch([1, 2, 3])
# -> [4, 6, 8]

Parameters:

Name Type Description Default
*others Runnable[Any, Other] | Callable[[Any], Other]

Other Runnable or Runnable-like objects to compose

()
name str | None

An optional name for the resulting RunnableSequence.

None

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

pick

pick(
    keys: str | list[str],
) -> RunnableSerializable[Any, Any]

Pick keys from the output dict of this Runnable.

Pick single key:

```python
import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)
chain = RunnableMap(str=as_str, json=as_json)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3]}

json_only_chain = chain.pick("json")
json_only_chain.invoke("[1, 2, 3]")
# -> [1, 2, 3]
```

Pick list of keys:

```python
from typing import Any

import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)


def as_bytes(x: Any) -> bytes:
    return bytes(x, "utf-8")


chain = RunnableMap(
    str=as_str, json=as_json, bytes=RunnableLambda(as_bytes)
)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3], "bytes": b"[1, 2, 3]"}

json_and_bytes_chain = chain.pick(["json", "bytes"])
json_and_bytes_chain.invoke("[1, 2, 3]")
# -> {"json": [1, 2, 3], "bytes": b"[1, 2, 3]"}
```

Parameters:

Name Type Description Default
keys str | list[str]

A key or list of keys to pick from the output dict.

required

Returns:

Type Description
RunnableSerializable[Any, Any]

a new Runnable.

assign

assign(
    **kwargs: (
        Runnable[dict[str, Any], Any]
        | Callable[[dict[str, Any]], Any]
        | Mapping[
            str,
            Runnable[dict[str, Any], Any]
            | Callable[[dict[str, Any]], Any],
        ]
    ),
) -> RunnableSerializable[Any, Any]

Assigns new fields to the dict output of this Runnable.

from langchain_community.llms.fake import FakeStreamingListLLM
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import SystemMessagePromptTemplate
from langchain_core.runnables import Runnable
from operator import itemgetter

prompt = (
    SystemMessagePromptTemplate.from_template("You are a nice assistant.")
    + "{question}"
)
llm = FakeStreamingListLLM(responses=["foo-lish"])

chain: Runnable = prompt | llm | {"str": StrOutputParser()}

chain_with_assign = chain.assign(hello=itemgetter("str") | llm)

print(chain_with_assign.input_schema.model_json_schema())
# {'title': 'PromptInput', 'type': 'object', 'properties':
{'question': {'title': 'Question', 'type': 'string'}}}
print(chain_with_assign.output_schema.model_json_schema())
# {'title': 'RunnableSequenceOutput', 'type': 'object', 'properties':
{'str': {'title': 'Str',
'type': 'string'}, 'hello': {'title': 'Hello', 'type': 'string'}}}

Parameters:

Name Type Description Default
**kwargs Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any] | Mapping[str, Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any]]

A mapping of keys to Runnable or Runnable-like objects that will be invoked with the entire output dict of this Runnable.

{}

Returns:

Type Description
RunnableSerializable[Any, Any]

A new Runnable.

batch

batch(
    inputs: list[Input],
    config: (
        RunnableConfig | list[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> list[Output]

Default implementation runs invoke in parallel using a thread pool executor.

The default implementation of batch works well for IO bound runnables.

Subclasses should override this method if they can batch more efficiently; e.g., if the underlying Runnable uses an API which supports a batch mode.

Parameters:

Name Type Description Default
inputs list[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | list[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
list[Output]

A list of outputs from the Runnable.

batch_as_completed

batch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> Iterator[tuple[int, Output | Exception]]

Run invoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
tuple[int, Output | Exception]

Tuples of the index of the input and the output from the Runnable.

abatch async

abatch(
    inputs: list[Input],
    config: (
        RunnableConfig | list[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> list[Output]

Default implementation runs ainvoke in parallel using asyncio.gather.

The default implementation of batch works well for IO bound runnables.

Subclasses should override this method if they can batch more efficiently; e.g., if the underlying Runnable uses an API which supports a batch mode.

Parameters:

Name Type Description Default
inputs list[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | list[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
list[Output]

A list of outputs from the Runnable.

abatch_as_completed async

abatch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> AsyncIterator[tuple[int, Output | Exception]]

Run ainvoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[tuple[int, Output | Exception]]

A tuple of the index of the input and the output from the Runnable.

stream

stream(
    input: Input,
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> Iterator[Output]

Default implementation of stream, which calls invoke.

Subclasses should override this method if they support streaming output.

Parameters:

Name Type Description Default
input Input

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
Output

The output of the Runnable.

astream async

astream(
    input: Input,
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> AsyncIterator[Output]

Default implementation of astream, which calls ainvoke.

Subclasses should override this method if they support streaming output.

Parameters:

Name Type Description Default
input Input

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[Output]

The output of the Runnable.

astream_log async

astream_log(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    diff: bool = True,
    with_streamed_output_list: bool = True,
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

Stream all output from a Runnable, as reported to the callback system.

This includes all inner runs of LLMs, Retrievers, Tools, etc.

Output is streamed as Log objects, which include a list of Jsonpatch ops that describe how the state of the run has changed in each step, and the final state of the run.

The Jsonpatch ops can be applied in order to construct state.

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
diff bool

Whether to yield diffs between each step or the current state.

True
with_streamed_output_list bool

Whether to yield the streamed_output list.

True
include_names Sequence[str] | None

Only include logs with these names.

None
include_types Sequence[str] | None

Only include logs with these types.

None
include_tags Sequence[str] | None

Only include logs with these tags.

None
exclude_names Sequence[str] | None

Exclude logs with these names.

None
exclude_types Sequence[str] | None

Exclude logs with these types.

None
exclude_tags Sequence[str] | None

Exclude logs with these tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

A RunLogPatch or RunLog object.

astream_events async

astream_events(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    version: Literal["v1", "v2"] = "v2",
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[StreamEvent]

Generate a stream of events.

Use to create an iterator over StreamEvents that provide real-time information about the progress of the Runnable, including StreamEvents from intermediate results.

A StreamEvent is a dictionary with the following schema:

  • event: str - Event names are of the format: on_[runnable_type]_(start|stream|end).
  • name: str - The name of the Runnable that generated the event.
  • run_id: str - randomly generated ID associated with the given execution of the Runnable that emitted the event. A child Runnable that gets invoked as part of the execution of a parent Runnable is assigned its own unique ID.
  • parent_ids: list[str] - The IDs of the parent runnables that generated the event. The root Runnable will have an empty list. The order of the parent IDs is from the root to the immediate parent. Only available for v2 version of the API. The v1 version of the API will return an empty list.
  • tags: Optional[list[str]] - The tags of the Runnable that generated the event.
  • metadata: Optional[dict[str, Any]] - The metadata of the Runnable that generated the event.
  • data: dict[str, Any]

Below is a table that illustrates some events that might be emitted by various chains. Metadata fields have been omitted from the table for brevity. Chain definitions have been included after the table.

Note

This reference table is for the v2 version of the schema.

+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | event | name | chunk | input | output | +==========================+==================+=====================================+===================================================+=====================================================+ | on_chat_model_start | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_stream | [model name] | AIMessageChunk(content="hello") | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_end | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | AIMessageChunk(content="hello world") | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_start | [model name] | | {'input': 'hello'} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_stream | [model name] | 'Hello' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_end | [model name] | | 'Hello human!' | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_start | format_docs | | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_stream | format_docs | 'hello world!, goodbye world!' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_end | format_docs | | [Document(...)] | 'hello world!, goodbye world!' | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_start | some_tool | | {"x": 1, "y": "2"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_end | some_tool | | | {"x": 1, "y": "2"} | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_start | [retriever name] | | {"query": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_end | [retriever name] | | {"query": "hello"} | [Document(...), ..] | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_start | [template_name] | | {"question": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_end | [template_name] | | {"question": "hello"} | ChatPromptValue(messages: [SystemMessage, ...]) | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+

In addition to the standard events, users can also dispatch custom events (see example below).

Custom events will be only be surfaced with in the v2 version of the API!

A custom event has following format:

+-----------+------+-----------------------------------------------------------------------------------------------------------+ | Attribute | Type | Description | +===========+======+===========================================================================================================+ | name | str | A user defined name for the event. | +-----------+------+-----------------------------------------------------------------------------------------------------------+ | data | Any | The data associated with the event. This can be anything, though we suggest making it JSON serializable. | +-----------+------+-----------------------------------------------------------------------------------------------------------+

Here are declarations associated with the standard events shown above:

format_docs:

def format_docs(docs: list[Document]) -> str:
    '''Format the docs.'''
    return ", ".join([doc.page_content for doc in docs])


format_docs = RunnableLambda(format_docs)

some_tool:

@tool
def some_tool(x: int, y: str) -> dict:
    '''Some_tool.'''
    return {"x": x, "y": y}

prompt:

template = ChatPromptTemplate.from_messages(
    [
        ("system", "You are Cat Agent 007"),
        ("human", "{question}"),
    ]
).with_config({"run_name": "my_template", "tags": ["my_template"]})
Example:

from langchain_core.runnables import RunnableLambda


async def reverse(s: str) -> str:
    return s[::-1]


chain = RunnableLambda(func=reverse)

events = [event async for event in chain.astream_events("hello", version="v2")]

# will produce the following events (run_id, and parent_ids
# has been omitted for brevity):
[
    {
        "data": {"input": "hello"},
        "event": "on_chain_start",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"chunk": "olleh"},
        "event": "on_chain_stream",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"output": "olleh"},
        "event": "on_chain_end",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
]

Example: Dispatch Custom Event

from langchain_core.callbacks.manager import (
    adispatch_custom_event,
)
from langchain_core.runnables import RunnableLambda, RunnableConfig
import asyncio


async def slow_thing(some_input: str, config: RunnableConfig) -> str:
    """Do something that takes a long time."""
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 1 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 2 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    return "Done"

slow_thing = RunnableLambda(slow_thing)

async for event in slow_thing.astream_events("some_input", version="v2"):
    print(event)

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
version Literal['v1', 'v2']

The version of the schema to use either 'v2' or 'v1'. Users should use 'v2'. 'v1' is for backwards compatibility and will be deprecated in 0.4.0. No default will be assigned until the API is stabilized. custom events will only be surfaced in 'v2'.

'v2'
include_names Sequence[str] | None

Only include events from Runnables with matching names.

None
include_types Sequence[str] | None

Only include events from Runnables with matching types.

None
include_tags Sequence[str] | None

Only include events from Runnables with matching tags.

None
exclude_names Sequence[str] | None

Exclude events from Runnables with matching names.

None
exclude_types Sequence[str] | None

Exclude events from Runnables with matching types.

None
exclude_tags Sequence[str] | None

Exclude events from Runnables with matching tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable. These will be passed to astream_log as this implementation of astream_events is built on top of astream_log.

{}

Yields:

Type Description
AsyncIterator[StreamEvent]

An async stream of StreamEvents.

Raises:

Type Description
NotImplementedError

If the version is not 'v1' or 'v2'.

transform

transform(
    input: Iterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> Iterator[Output]

Transform inputs to outputs.

Default implementation of transform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input Iterator[Input]

An iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
Output

The output of the Runnable.

atransform async

atransform(
    input: AsyncIterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> AsyncIterator[Output]

Transform inputs to outputs.

Default implementation of atransform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input AsyncIterator[Input]

An async iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[Output]

The output of the Runnable.

bind

bind(**kwargs: Any) -> Runnable[Input, Output]

Bind arguments to a Runnable, returning a new Runnable.

Useful when a Runnable in a chain requires an argument that is not in the output of the previous Runnable or included in the user input.

Parameters:

Name Type Description Default
kwargs Any

The arguments to bind to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the arguments bound.

Example
from langchain_ollama import ChatOllama
from langchain_core.output_parsers import StrOutputParser

llm = ChatOllama(model="llama3.1")

# Without bind.
chain = llm | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two three four five.'

# With bind.
chain = llm.bind(stop=["three"]) | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two'

with_config

with_config(
    config: RunnableConfig | None = None, **kwargs: Any
) -> Runnable[Input, Output]

Bind config to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

The config to bind to the Runnable.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the config bound.

with_listeners

with_listeners(
    *,
    on_start: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_end: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_error: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None
) -> Runnable[Input, Output]

Bind lifecycle listeners to a Runnable, returning a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called before the Runnable starts running, with the Run object. Defaults to None.

None
on_end Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda
from langchain_core.tracers.schemas import Run

import time


def test_runnable(time_to_sleep: int):
    time.sleep(time_to_sleep)


def fn_start(run_obj: Run):
    print("start_time:", run_obj.start_time)


def fn_end(run_obj: Run):
    print("end_time:", run_obj.end_time)


chain = RunnableLambda(test_runnable).with_listeners(
    on_start=fn_start, on_end=fn_end
)
chain.invoke(2)

with_alisteners

with_alisteners(
    *,
    on_start: AsyncListener | None = None,
    on_end: AsyncListener | None = None,
    on_error: AsyncListener | None = None
) -> Runnable[Input, Output]

Bind async lifecycle listeners to a Runnable.

Returns a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start AsyncListener | None

Called asynchronously before the Runnable starts running, with the Run object. Defaults to None.

None
on_end AsyncListener | None

Called asynchronously after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error AsyncListener | None

Called asynchronously if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda, Runnable
from datetime import datetime, timezone
import time
import asyncio

def format_t(timestamp: float) -> str:
    return datetime.fromtimestamp(timestamp, tz=timezone.utc).isoformat()

async def test_runnable(time_to_sleep: int):
    print(f"Runnable[{time_to_sleep}s]: starts at {format_t(time.time())}")
    await asyncio.sleep(time_to_sleep)
    print(f"Runnable[{time_to_sleep}s]: ends at {format_t(time.time())}")

async def fn_start(run_obj: Runnable):
    print(f"on start callback starts at {format_t(time.time())}")
    await asyncio.sleep(3)
    print(f"on start callback ends at {format_t(time.time())}")

async def fn_end(run_obj: Runnable):
    print(f"on end callback starts at {format_t(time.time())}")
    await asyncio.sleep(2)
    print(f"on end callback ends at {format_t(time.time())}")

runnable = RunnableLambda(test_runnable).with_alisteners(
    on_start=fn_start,
    on_end=fn_end
)
async def concurrent_runs():
    await asyncio.gather(runnable.ainvoke(2), runnable.ainvoke(3))

asyncio.run(concurrent_runs())
Result:
on start callback starts at 2025-03-01T07:05:22.875378+00:00
on start callback starts at 2025-03-01T07:05:22.875495+00:00
on start callback ends at 2025-03-01T07:05:25.878862+00:00
on start callback ends at 2025-03-01T07:05:25.878947+00:00
Runnable[2s]: starts at 2025-03-01T07:05:25.879392+00:00
Runnable[3s]: starts at 2025-03-01T07:05:25.879804+00:00
Runnable[2s]: ends at 2025-03-01T07:05:27.881998+00:00
on end callback starts at 2025-03-01T07:05:27.882360+00:00
Runnable[3s]: ends at 2025-03-01T07:05:28.881737+00:00
on end callback starts at 2025-03-01T07:05:28.882428+00:00
on end callback ends at 2025-03-01T07:05:29.883893+00:00
on end callback ends at 2025-03-01T07:05:30.884831+00:00

with_types

with_types(
    *,
    input_type: type[Input] | None = None,
    output_type: type[Output] | None = None
) -> Runnable[Input, Output]

Bind input and output types to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
input_type type[Input] | None

The input type to bind to the Runnable. Defaults to None.

None
output_type type[Output] | None

The output type to bind to the Runnable. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the types bound.

with_retry

with_retry(
    *,
    retry_if_exception_type: tuple[
        type[BaseException], ...
    ] = (Exception,),
    wait_exponential_jitter: bool = True,
    exponential_jitter_params: (
        ExponentialJitterParams | None
    ) = None,
    stop_after_attempt: int = 3
) -> Runnable[Input, Output]

Create a new Runnable that retries the original Runnable on exceptions.

Parameters:

Name Type Description Default
retry_if_exception_type tuple[type[BaseException], ...]

A tuple of exception types to retry on. Defaults to (Exception,).

(Exception,)
wait_exponential_jitter bool

Whether to add jitter to the wait time between retries. Defaults to True.

True
stop_after_attempt int

The maximum number of attempts to make before giving up. Defaults to 3.

3
exponential_jitter_params ExponentialJitterParams | None

Parameters for tenacity.wait_exponential_jitter. Namely: initial, max, exp_base, and jitter (all float values).

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable that retries the original Runnable on exceptions.

Example
from langchain_core.runnables import RunnableLambda

count = 0


def _lambda(x: int) -> None:
    global count
    count = count + 1
    if x == 1:
        raise ValueError("x is 1")
    else:
        pass


runnable = RunnableLambda(_lambda)
try:
    runnable.with_retry(
        stop_after_attempt=2,
        retry_if_exception_type=(ValueError,),
    ).invoke(1)
except ValueError:
    pass

assert count == 2

map

map() -> Runnable[list[Input], list[Output]]

Return a new Runnable that maps a list of inputs to a list of outputs.

Calls invoke with each input.

Returns:

Type Description
Runnable[list[Input], list[Output]]

A new Runnable that maps a list of inputs to a list of outputs.

Example
from langchain_core.runnables import RunnableLambda


def _lambda(x: int) -> int:
    return x + 1


runnable = RunnableLambda(_lambda)
print(runnable.map().invoke([1, 2, 3]))  # [2, 3, 4]

with_fallbacks

with_fallbacks(
    fallbacks: Sequence[Runnable[Input, Output]],
    *,
    exceptions_to_handle: tuple[
        type[BaseException], ...
    ] = (Exception,),
    exception_key: str | None = None
) -> RunnableWithFallbacks[Input, Output]

Add fallbacks to a Runnable, returning a new Runnable.

The new Runnable will try the original Runnable, and then each fallback in order, upon failures.

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle. Defaults to (Exception,).

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input. Defaults to None.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

Example
from typing import Iterator

from langchain_core.runnables import RunnableGenerator


def _generate_immediate_error(input: Iterator) -> Iterator[str]:
    raise ValueError()
    yield ""


def _generate(input: Iterator) -> Iterator[str]:
    yield from "foo bar"


runnable = RunnableGenerator(_generate_immediate_error).with_fallbacks(
    [RunnableGenerator(_generate)]
)
print("".join(runnable.stream({})))  # foo bar

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle.

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

as_tool

as_tool(
    args_schema: type[BaseModel] | None = None,
    *,
    name: str | None = None,
    description: str | None = None,
    arg_types: dict[str, type] | None = None
) -> BaseTool

Create a BaseTool from a Runnable.

as_tool will instantiate a BaseTool with a name, description, and args_schema from a Runnable. Where possible, schemas are inferred from runnable.get_input_schema. Alternatively (e.g., if the Runnable takes a dict as input and the specific dict keys are not typed), the schema can be specified directly with args_schema. You can also pass arg_types to just specify the required arguments and their types.

Parameters:

Name Type Description Default
args_schema type[BaseModel] | None

The schema for the tool. Defaults to None.

None
name str | None

The name of the tool. Defaults to None.

None
description str | None

The description of the tool. Defaults to None.

None
arg_types dict[str, type] | None

A dictionary of argument names to types. Defaults to None.

None

Returns:

Type Description
BaseTool

A BaseTool instance.

Typed dict input:

from typing_extensions import TypedDict
from langchain_core.runnables import RunnableLambda


class Args(TypedDict):
    a: int
    b: list[int]


def f(x: Args) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool()
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via args_schema:

from typing import Any
from pydantic import BaseModel, Field
from langchain_core.runnables import RunnableLambda

def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))

class FSchema(BaseModel):
    """Apply a function to an integer and list of integers."""

    a: int = Field(..., description="Integer")
    b: list[int] = Field(..., description="List of ints")

runnable = RunnableLambda(f)
as_tool = runnable.as_tool(FSchema)
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via arg_types:

from typing import Any
from langchain_core.runnables import RunnableLambda


def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool(arg_types={"a": int, "b": list[int]})
as_tool.invoke({"a": 3, "b": [1, 2]})

String input:

from langchain_core.runnables import RunnableLambda


def f(x: str) -> str:
    return x + "a"


def g(x: str) -> str:
    return x + "z"


runnable = RunnableLambda(f) | g
as_tool = runnable.as_tool()
as_tool.invoke("b")

Added in version 0.2.14

__init__

__init__(*args: Any, **kwargs: Any) -> None

is_lc_serializable classmethod

is_lc_serializable() -> bool

Is this class serializable?

By design, even if a class inherits from Serializable, it is not serializable by default. This is to prevent accidental serialization of objects that should not be serialized.

Returns:

Type Description
bool

Whether the class is serializable. Default is False.

get_lc_namespace classmethod

get_lc_namespace() -> list[str]

Get the namespace of the langchain object.

For example, if the class is langchain.llms.openai.OpenAI, then the namespace is ["langchain", "llms", "openai"]

Returns:

Type Description
list[str]

The namespace as a list of strings.

lc_id classmethod

lc_id() -> list[str]

Return a unique identifier for this class for serialization purposes.

The unique identifier is a list of strings that describes the path to the object. For example, for the class langchain.llms.openai.OpenAI, the id is ["langchain", "llms", "openai", "OpenAI"].

to_json

to_json() -> (
    SerializedConstructor | SerializedNotImplemented
)

Serialize the Runnable to JSON.

Returns:

Type Description
SerializedConstructor | SerializedNotImplemented

A JSON-serializable representation of the Runnable.

to_json_not_implemented

to_json_not_implemented() -> SerializedNotImplemented

Serialize a "not implemented" object.

Returns:

Type Description
SerializedNotImplemented

SerializedNotImplemented.

configurable_fields

configurable_fields(
    **kwargs: AnyConfigurableField,
) -> RunnableSerializable[Input, Output]

Configure particular Runnable fields at runtime.

Parameters:

Name Type Description Default
**kwargs AnyConfigurableField

A dictionary of ConfigurableField instances to configure.

{}

Raises:

Type Description
ValueError

If a configuration key is not found in the Runnable.

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the fields configured.

from langchain_core.runnables import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatOpenAI(max_tokens=20).configurable_fields(
    max_tokens=ConfigurableField(
        id="output_token_number",
        name="Max tokens in the output",
        description="The maximum number of tokens in the output",
    )
)

# max_tokens = 20
print("max_tokens_20: ", model.invoke("tell me something about chess").content)

# max_tokens = 200
print(
    "max_tokens_200: ",
    model.with_config(configurable={"output_token_number": 200})
    .invoke("tell me something about chess")
    .content,
)

configurable_alternatives

configurable_alternatives(
    which: ConfigurableField,
    *,
    default_key: str = "default",
    prefix_keys: bool = False,
    **kwargs: (
        Runnable[Input, Output]
        | Callable[[], Runnable[Input, Output]]
    )
) -> RunnableSerializable[Input, Output]

Configure alternatives for Runnables that can be set at runtime.

Parameters:

Name Type Description Default
which ConfigurableField

The ConfigurableField instance that will be used to select the alternative.

required
default_key str

The default key to use if no alternative is selected. Defaults to 'default'.

'default'
prefix_keys bool

Whether to prefix the keys with the ConfigurableField id. Defaults to False.

False
**kwargs Runnable[Input, Output] | Callable[[], Runnable[Input, Output]]

A dictionary of keys to Runnable instances or callables that return Runnable instances.

{}

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the alternatives configured.

from langchain_anthropic import ChatAnthropic
from langchain_core.runnables.utils import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatAnthropic(
    model_name="claude-3-7-sonnet-20250219"
).configurable_alternatives(
    ConfigurableField(id="llm"),
    default_key="anthropic",
    openai=ChatOpenAI(),
)

# uses the default model ChatAnthropic
print(model.invoke("which organization created you?").content)

# uses ChatOpenAI
print(
    model.with_config(configurable={"llm": "openai"})
    .invoke("which organization created you?")
    .content
)

aparse_result async

aparse_result(
    result: list[Generation], *, partial: bool = False
) -> T

Async parse a list of candidate model Generations into a specific format.

The return value is parsed from only the first Generation in the result, which is assumed to be the highest-likelihood Generation.

Parameters:

Name Type Description Default
result list[Generation]

A list of Generations to be parsed. The Generations are assumed to be different candidate outputs for a single model input.

required
partial bool

Whether to parse the output as a partial result. This is useful for parsers that can parse partial results. Default is False.

False

Returns:

Type Description
T

Structured output.

aparse async

aparse(text: str) -> T

Async parse a single string model output into some structure.

Parameters:

Name Type Description Default
text str

String output of a language model.

required

Returns:

Type Description
T

Structured output.

parse_with_prompt

parse_with_prompt(
    completion: str, prompt: PromptValue
) -> Any

Parse the output of an LLM call with the input prompt for context.

The prompt is largely provided in the event the OutputParser wants to retry or fix the output in some way, and needs information from the prompt to do so.

Parameters:

Name Type Description Default
completion str

String output of a language model.

required
prompt PromptValue

Input PromptValue.

required

Returns:

Type Description
Any

Structured output.

get_format_instructions

get_format_instructions() -> str

Instructions on how the LLM output should be formatted.

dict

dict(**kwargs: Any) -> dict

Return dictionary representation of output parser.

GemmaChatLocalHF

Bases: _GemmaLocalHFBase, BaseChatModel

Methods:

Name Description
get_name

Get the name of the Runnable.

get_input_schema

Get a pydantic model that can be used to validate input to the Runnable.

get_input_jsonschema

Get a JSON schema that represents the input to the Runnable.

get_output_schema

Get a pydantic model that can be used to validate output to the Runnable.

get_output_jsonschema

Get a JSON schema that represents the output of the Runnable.

config_schema

The type of config this Runnable accepts specified as a pydantic model.

get_config_jsonschema

Get a JSON schema that represents the config of the Runnable.

get_graph

Return a graph representation of this Runnable.

get_prompts

Return a list of prompts used by this Runnable.

__or__

Runnable "or" operator.

__ror__

Runnable "reverse-or" operator.

pipe

Pipe runnables.

pick

Pick keys from the output dict of this Runnable.

assign

Assigns new fields to the dict output of this Runnable.

batch

Default implementation runs invoke in parallel using a thread pool executor.

batch_as_completed

Run invoke in parallel on a list of inputs.

abatch

Default implementation runs ainvoke in parallel using asyncio.gather.

abatch_as_completed

Run ainvoke in parallel on a list of inputs.

astream_log

Stream all output from a Runnable, as reported to the callback system.

astream_events

Generate a stream of events.

transform

Transform inputs to outputs.

atransform

Transform inputs to outputs.

bind

Bind arguments to a Runnable, returning a new Runnable.

with_config

Bind config to a Runnable, returning a new Runnable.

with_listeners

Bind lifecycle listeners to a Runnable, returning a new Runnable.

with_alisteners

Bind async lifecycle listeners to a Runnable.

with_types

Bind input and output types to a Runnable, returning a new Runnable.

with_retry

Create a new Runnable that retries the original Runnable on exceptions.

map

Return a new Runnable that maps a list of inputs to a list of outputs.

with_fallbacks

Add fallbacks to a Runnable, returning a new Runnable.

as_tool

Create a BaseTool from a Runnable.

__init__
is_lc_serializable

Is this class serializable?

get_lc_namespace

Get the namespace of the langchain object.

lc_id

Return a unique identifier for this class for serialization purposes.

to_json

Serialize the Runnable to JSON.

to_json_not_implemented

Serialize a "not implemented" object.

configurable_fields

Configure particular Runnable fields at runtime.

configurable_alternatives

Configure alternatives for Runnables that can be set at runtime.

set_verbose

If verbose is None, set it.

with_structured_output

Model wrapper that returns outputs formatted to match the given schema.

get_token_ids

Return the ordered ids of the tokens in a text.

get_num_tokens

Get the number of tokens present in the text.

get_num_tokens_from_messages

Get the number of tokens in the messages.

generate

Pass a sequence of prompts to the model and return model generations.

agenerate

Asynchronously pass a sequence of prompts to a model and return generations.

dict

Return a dictionary of the LLM.

bind_tools

Bind tools to the model.

validate_environment

Validate that llama-cpp-python library is installed.

Attributes:

Name Type Description
InputType TypeAlias

Get the input type for this runnable.

OutputType Any

Get the output type for this runnable.

input_schema type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema type[BaseModel]

Output schema.

config_specs list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets dict[str, str]

A map of constructor argument names to secret ids.

lc_attributes dict

List of attribute names that should be included in the serialized kwargs.

cache BaseCache | bool | None

Whether to cache the response.

verbose bool

Whether to print out response text.

callbacks Callbacks

Callbacks to add to the run trace.

tags list[str] | None

Tags to add to the run trace.

metadata dict[str, Any] | None

Metadata to add to the run trace.

custom_get_token_ids Callable[[str], list[int]] | None

Optional encoder to use for counting tokens.

rate_limiter BaseRateLimiter | None

An optional rate limiter to use for limiting the number of requests.

disable_streaming bool | Literal['tool_calling']

Whether to disable streaming for this model.

output_version str | None

Version of AIMessage output format to store in message content.

max_tokens Optional[int]

The maximum number of tokens to generate.

temperature Optional[float]

The temperature to use for sampling.

top_p Optional[float]

The top-p value to use for sampling.

top_k Optional[int]

The top-k value to use for sampling.

model_name str

Gemma model name.

parse_response bool

Whether to post-process the chat response and clean repeations

InputType property

InputType: TypeAlias

Get the input type for this runnable.

OutputType property

OutputType: Any

Get the output type for this runnable.

input_schema property

input_schema: type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema property

output_schema: type[BaseModel]

Output schema.

The type of output this Runnable produces specified as a pydantic model.

config_specs property

config_specs: list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets property

lc_secrets: dict[str, str]

A map of constructor argument names to secret ids.

For example,

lc_attributes property

lc_attributes: dict

List of attribute names that should be included in the serialized kwargs.

These attributes must be accepted by the constructor. Default is an empty dictionary.

cache class-attribute instance-attribute

cache: BaseCache | bool | None = Field(
    default=None, exclude=True
)

Whether to cache the response.

  • If true, will use the global cache.
  • If false, will not use a cache
  • If None, will use the global cache if it's set, otherwise no cache.
  • If instance of BaseCache, will use the provided cache.

Caching is not currently supported for streaming methods of models.

verbose class-attribute instance-attribute

verbose: bool = Field(
    default_factory=_get_verbosity, exclude=True, repr=False
)

Whether to print out response text.

callbacks class-attribute instance-attribute

callbacks: Callbacks = Field(default=None, exclude=True)

Callbacks to add to the run trace.

tags class-attribute instance-attribute

tags: list[str] | None = Field(default=None, exclude=True)

Tags to add to the run trace.

metadata class-attribute instance-attribute

metadata: dict[str, Any] | None = Field(
    default=None, exclude=True
)

Metadata to add to the run trace.

custom_get_token_ids class-attribute instance-attribute

custom_get_token_ids: Callable[[str], list[int]] | None = (
    Field(default=None, exclude=True)
)

Optional encoder to use for counting tokens.

rate_limiter class-attribute instance-attribute

rate_limiter: BaseRateLimiter | None = Field(
    default=None, exclude=True
)

An optional rate limiter to use for limiting the number of requests.

disable_streaming class-attribute instance-attribute

disable_streaming: bool | Literal['tool_calling'] = False

Whether to disable streaming for this model.

If streaming is bypassed, then stream()/astream()/astream_events() will defer to invoke()/ainvoke().

  • If True, will always bypass streaming case.
  • If 'tool_calling', will bypass streaming case only when the model is called with a tools keyword argument. In other words, LangChain will automatically switch to non-streaming behavior (invoke()) only when the tools argument is provided. This offers the best of both worlds.
  • If False (default), will always use streaming case if available.

The main reason for this flag is that code might be written using stream() and a user may want to swap out a given model for another model whose the implementation does not properly support streaming.

output_version class-attribute instance-attribute

output_version: str | None = Field(
    default_factory=from_env(
        "LC_OUTPUT_VERSION", default=None
    )
)

Version of AIMessage output format to store in message content.

AIMessage.content_blocks will lazily parse the contents of content into a standard format. This flag can be used to additionally store the standard format in message content, e.g., for serialization purposes.

Supported values:

  • "v0": provider-specific format in content (can lazily-parse with .content_blocks)
  • "v1": standardized format in content (consistent with .content_blocks)

Partner packages (e.g., langchain-openai) can also use this field to roll out new content formats in a backward-compatible way.

Added in version 1.0

max_tokens class-attribute instance-attribute

max_tokens: Optional[int] = None

The maximum number of tokens to generate.

temperature class-attribute instance-attribute

temperature: Optional[float] = None

The temperature to use for sampling.

top_p class-attribute instance-attribute

top_p: Optional[float] = None

The top-p value to use for sampling.

top_k class-attribute instance-attribute

top_k: Optional[int] = None

The top-k value to use for sampling.

model_name class-attribute instance-attribute

model_name: str = Field(
    default="google/gemma-2b", alias="model"
)

Gemma model name.

parse_response class-attribute instance-attribute

parse_response: bool = False

Whether to post-process the chat response and clean repeations

get_name

get_name(
    suffix: str | None = None, *, name: str | None = None
) -> str

Get the name of the Runnable.

Parameters:

Name Type Description Default
suffix str | None

An optional suffix to append to the name.

None
name str | None

An optional name to use instead of the Runnable's name.

None

Returns:

Type Description
str

The name of the Runnable.

get_input_schema

get_input_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate input to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic input schema that depends on which configuration the Runnable is invoked with.

This method allows to get an input schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate input.

get_input_jsonschema

get_input_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the input to the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the input to the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_input_jsonschema())

Added in version 0.3.0

get_output_schema

get_output_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate output to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic output schema that depends on which configuration the Runnable is invoked with.

This method allows to get an output schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate output.

get_output_jsonschema

get_output_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the output of the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the output of the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_output_jsonschema())

Added in version 0.3.0

config_schema

config_schema(
    *, include: Sequence[str] | None = None
) -> type[BaseModel]

The type of config this Runnable accepts specified as a pydantic model.

To mark a field as configurable, see the configurable_fields and configurable_alternatives methods.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate config.

get_config_jsonschema

get_config_jsonschema(
    *, include: Sequence[str] | None = None
) -> dict[str, Any]

Get a JSON schema that represents the config of the Runnable.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the config of the Runnable.

Added in version 0.3.0

get_graph

get_graph(config: RunnableConfig | None = None) -> Graph

Return a graph representation of this Runnable.

get_prompts

get_prompts(
    config: RunnableConfig | None = None,
) -> list[BasePromptTemplate]

Return a list of prompts used by this Runnable.

__or__

__or__(
    other: (
        Runnable[Any, Other]
        | Callable[[Iterator[Any]], Iterator[Other]]
        | Callable[
            [AsyncIterator[Any]], AsyncIterator[Other]
        ]
        | Callable[[Any], Other]
        | Mapping[
            str,
            Runnable[Any, Other]
            | Callable[[Any], Other]
            | Any,
        ]
    ),
) -> RunnableSerializable[Input, Other]

Runnable "or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Any, Other] | Callable[[Iterator[Any]], Iterator[Other]] | Callable[[AsyncIterator[Any]], AsyncIterator[Other]] | Callable[[Any], Other] | Mapping[str, Runnable[Any, Other] | Callable[[Any], Other] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

__ror__

__ror__(
    other: (
        Runnable[Other, Any]
        | Callable[[Iterator[Other]], Iterator[Any]]
        | Callable[
            [AsyncIterator[Other]], AsyncIterator[Any]
        ]
        | Callable[[Other], Any]
        | Mapping[
            str,
            Runnable[Other, Any]
            | Callable[[Other], Any]
            | Any,
        ]
    ),
) -> RunnableSerializable[Other, Output]

Runnable "reverse-or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Other, Any] | Callable[[Iterator[Other]], Iterator[Any]] | Callable[[AsyncIterator[Other]], AsyncIterator[Any]] | Callable[[Other], Any] | Mapping[str, Runnable[Other, Any] | Callable[[Other], Any] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Other, Output]

A new Runnable.

pipe

pipe(
    *others: Runnable[Any, Other] | Callable[[Any], Other],
    name: str | None = None
) -> RunnableSerializable[Input, Other]

Pipe runnables.

Compose this Runnable with Runnable-like objects to make a RunnableSequence.

Equivalent to RunnableSequence(self, *others) or self | others[0] | ...

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


def mul_two(x: int) -> int:
    return x * 2


runnable_1 = RunnableLambda(add_one)
runnable_2 = RunnableLambda(mul_two)
sequence = runnable_1.pipe(runnable_2)
# Or equivalently:
# sequence = runnable_1 | runnable_2
# sequence = RunnableSequence(first=runnable_1, last=runnable_2)
sequence.invoke(1)
await sequence.ainvoke(1)
# -> 4

sequence.batch([1, 2, 3])
await sequence.abatch([1, 2, 3])
# -> [4, 6, 8]

Parameters:

Name Type Description Default
*others Runnable[Any, Other] | Callable[[Any], Other]

Other Runnable or Runnable-like objects to compose

()
name str | None

An optional name for the resulting RunnableSequence.

None

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

pick

pick(
    keys: str | list[str],
) -> RunnableSerializable[Any, Any]

Pick keys from the output dict of this Runnable.

Pick single key:

```python
import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)
chain = RunnableMap(str=as_str, json=as_json)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3]}

json_only_chain = chain.pick("json")
json_only_chain.invoke("[1, 2, 3]")
# -> [1, 2, 3]
```

Pick list of keys:

```python
from typing import Any

import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)


def as_bytes(x: Any) -> bytes:
    return bytes(x, "utf-8")


chain = RunnableMap(
    str=as_str, json=as_json, bytes=RunnableLambda(as_bytes)
)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3], "bytes": b"[1, 2, 3]"}

json_and_bytes_chain = chain.pick(["json", "bytes"])
json_and_bytes_chain.invoke("[1, 2, 3]")
# -> {"json": [1, 2, 3], "bytes": b"[1, 2, 3]"}
```

Parameters:

Name Type Description Default
keys str | list[str]

A key or list of keys to pick from the output dict.

required

Returns:

Type Description
RunnableSerializable[Any, Any]

a new Runnable.

assign

assign(
    **kwargs: (
        Runnable[dict[str, Any], Any]
        | Callable[[dict[str, Any]], Any]
        | Mapping[
            str,
            Runnable[dict[str, Any], Any]
            | Callable[[dict[str, Any]], Any],
        ]
    ),
) -> RunnableSerializable[Any, Any]

Assigns new fields to the dict output of this Runnable.

from langchain_community.llms.fake import FakeStreamingListLLM
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import SystemMessagePromptTemplate
from langchain_core.runnables import Runnable
from operator import itemgetter

prompt = (
    SystemMessagePromptTemplate.from_template("You are a nice assistant.")
    + "{question}"
)
llm = FakeStreamingListLLM(responses=["foo-lish"])

chain: Runnable = prompt | llm | {"str": StrOutputParser()}

chain_with_assign = chain.assign(hello=itemgetter("str") | llm)

print(chain_with_assign.input_schema.model_json_schema())
# {'title': 'PromptInput', 'type': 'object', 'properties':
{'question': {'title': 'Question', 'type': 'string'}}}
print(chain_with_assign.output_schema.model_json_schema())
# {'title': 'RunnableSequenceOutput', 'type': 'object', 'properties':
{'str': {'title': 'Str',
'type': 'string'}, 'hello': {'title': 'Hello', 'type': 'string'}}}

Parameters:

Name Type Description Default
**kwargs Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any] | Mapping[str, Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any]]

A mapping of keys to Runnable or Runnable-like objects that will be invoked with the entire output dict of this Runnable.

{}

Returns:

Type Description
RunnableSerializable[Any, Any]

A new Runnable.

batch

batch(
    inputs: list[Input],
    config: (
        RunnableConfig | list[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> list[Output]

Default implementation runs invoke in parallel using a thread pool executor.

The default implementation of batch works well for IO bound runnables.

Subclasses should override this method if they can batch more efficiently; e.g., if the underlying Runnable uses an API which supports a batch mode.

Parameters:

Name Type Description Default
inputs list[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | list[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
list[Output]

A list of outputs from the Runnable.

batch_as_completed

batch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> Iterator[tuple[int, Output | Exception]]

Run invoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
tuple[int, Output | Exception]

Tuples of the index of the input and the output from the Runnable.

abatch async

abatch(
    inputs: list[Input],
    config: (
        RunnableConfig | list[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> list[Output]

Default implementation runs ainvoke in parallel using asyncio.gather.

The default implementation of batch works well for IO bound runnables.

Subclasses should override this method if they can batch more efficiently; e.g., if the underlying Runnable uses an API which supports a batch mode.

Parameters:

Name Type Description Default
inputs list[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | list[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
list[Output]

A list of outputs from the Runnable.

abatch_as_completed async

abatch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> AsyncIterator[tuple[int, Output | Exception]]

Run ainvoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[tuple[int, Output | Exception]]

A tuple of the index of the input and the output from the Runnable.

astream_log async

astream_log(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    diff: bool = True,
    with_streamed_output_list: bool = True,
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

Stream all output from a Runnable, as reported to the callback system.

This includes all inner runs of LLMs, Retrievers, Tools, etc.

Output is streamed as Log objects, which include a list of Jsonpatch ops that describe how the state of the run has changed in each step, and the final state of the run.

The Jsonpatch ops can be applied in order to construct state.

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
diff bool

Whether to yield diffs between each step or the current state.

True
with_streamed_output_list bool

Whether to yield the streamed_output list.

True
include_names Sequence[str] | None

Only include logs with these names.

None
include_types Sequence[str] | None

Only include logs with these types.

None
include_tags Sequence[str] | None

Only include logs with these tags.

None
exclude_names Sequence[str] | None

Exclude logs with these names.

None
exclude_types Sequence[str] | None

Exclude logs with these types.

None
exclude_tags Sequence[str] | None

Exclude logs with these tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

A RunLogPatch or RunLog object.

astream_events async

astream_events(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    version: Literal["v1", "v2"] = "v2",
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[StreamEvent]

Generate a stream of events.

Use to create an iterator over StreamEvents that provide real-time information about the progress of the Runnable, including StreamEvents from intermediate results.

A StreamEvent is a dictionary with the following schema:

  • event: str - Event names are of the format: on_[runnable_type]_(start|stream|end).
  • name: str - The name of the Runnable that generated the event.
  • run_id: str - randomly generated ID associated with the given execution of the Runnable that emitted the event. A child Runnable that gets invoked as part of the execution of a parent Runnable is assigned its own unique ID.
  • parent_ids: list[str] - The IDs of the parent runnables that generated the event. The root Runnable will have an empty list. The order of the parent IDs is from the root to the immediate parent. Only available for v2 version of the API. The v1 version of the API will return an empty list.
  • tags: Optional[list[str]] - The tags of the Runnable that generated the event.
  • metadata: Optional[dict[str, Any]] - The metadata of the Runnable that generated the event.
  • data: dict[str, Any]

Below is a table that illustrates some events that might be emitted by various chains. Metadata fields have been omitted from the table for brevity. Chain definitions have been included after the table.

Note

This reference table is for the v2 version of the schema.

+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | event | name | chunk | input | output | +==========================+==================+=====================================+===================================================+=====================================================+ | on_chat_model_start | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_stream | [model name] | AIMessageChunk(content="hello") | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_end | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | AIMessageChunk(content="hello world") | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_start | [model name] | | {'input': 'hello'} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_stream | [model name] | 'Hello' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_end | [model name] | | 'Hello human!' | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_start | format_docs | | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_stream | format_docs | 'hello world!, goodbye world!' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_end | format_docs | | [Document(...)] | 'hello world!, goodbye world!' | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_start | some_tool | | {"x": 1, "y": "2"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_end | some_tool | | | {"x": 1, "y": "2"} | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_start | [retriever name] | | {"query": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_end | [retriever name] | | {"query": "hello"} | [Document(...), ..] | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_start | [template_name] | | {"question": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_end | [template_name] | | {"question": "hello"} | ChatPromptValue(messages: [SystemMessage, ...]) | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+

In addition to the standard events, users can also dispatch custom events (see example below).

Custom events will be only be surfaced with in the v2 version of the API!

A custom event has following format:

+-----------+------+-----------------------------------------------------------------------------------------------------------+ | Attribute | Type | Description | +===========+======+===========================================================================================================+ | name | str | A user defined name for the event. | +-----------+------+-----------------------------------------------------------------------------------------------------------+ | data | Any | The data associated with the event. This can be anything, though we suggest making it JSON serializable. | +-----------+------+-----------------------------------------------------------------------------------------------------------+

Here are declarations associated with the standard events shown above:

format_docs:

def format_docs(docs: list[Document]) -> str:
    '''Format the docs.'''
    return ", ".join([doc.page_content for doc in docs])


format_docs = RunnableLambda(format_docs)

some_tool:

@tool
def some_tool(x: int, y: str) -> dict:
    '''Some_tool.'''
    return {"x": x, "y": y}

prompt:

template = ChatPromptTemplate.from_messages(
    [
        ("system", "You are Cat Agent 007"),
        ("human", "{question}"),
    ]
).with_config({"run_name": "my_template", "tags": ["my_template"]})
Example:

from langchain_core.runnables import RunnableLambda


async def reverse(s: str) -> str:
    return s[::-1]


chain = RunnableLambda(func=reverse)

events = [event async for event in chain.astream_events("hello", version="v2")]

# will produce the following events (run_id, and parent_ids
# has been omitted for brevity):
[
    {
        "data": {"input": "hello"},
        "event": "on_chain_start",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"chunk": "olleh"},
        "event": "on_chain_stream",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"output": "olleh"},
        "event": "on_chain_end",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
]

Example: Dispatch Custom Event

from langchain_core.callbacks.manager import (
    adispatch_custom_event,
)
from langchain_core.runnables import RunnableLambda, RunnableConfig
import asyncio


async def slow_thing(some_input: str, config: RunnableConfig) -> str:
    """Do something that takes a long time."""
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 1 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 2 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    return "Done"

slow_thing = RunnableLambda(slow_thing)

async for event in slow_thing.astream_events("some_input", version="v2"):
    print(event)

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
version Literal['v1', 'v2']

The version of the schema to use either 'v2' or 'v1'. Users should use 'v2'. 'v1' is for backwards compatibility and will be deprecated in 0.4.0. No default will be assigned until the API is stabilized. custom events will only be surfaced in 'v2'.

'v2'
include_names Sequence[str] | None

Only include events from Runnables with matching names.

None
include_types Sequence[str] | None

Only include events from Runnables with matching types.

None
include_tags Sequence[str] | None

Only include events from Runnables with matching tags.

None
exclude_names Sequence[str] | None

Exclude events from Runnables with matching names.

None
exclude_types Sequence[str] | None

Exclude events from Runnables with matching types.

None
exclude_tags Sequence[str] | None

Exclude events from Runnables with matching tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable. These will be passed to astream_log as this implementation of astream_events is built on top of astream_log.

{}

Yields:

Type Description
AsyncIterator[StreamEvent]

An async stream of StreamEvents.

Raises:

Type Description
NotImplementedError

If the version is not 'v1' or 'v2'.

transform

transform(
    input: Iterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> Iterator[Output]

Transform inputs to outputs.

Default implementation of transform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input Iterator[Input]

An iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
Output

The output of the Runnable.

atransform async

atransform(
    input: AsyncIterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> AsyncIterator[Output]

Transform inputs to outputs.

Default implementation of atransform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input AsyncIterator[Input]

An async iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[Output]

The output of the Runnable.

bind

bind(**kwargs: Any) -> Runnable[Input, Output]

Bind arguments to a Runnable, returning a new Runnable.

Useful when a Runnable in a chain requires an argument that is not in the output of the previous Runnable or included in the user input.

Parameters:

Name Type Description Default
kwargs Any

The arguments to bind to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the arguments bound.

Example
from langchain_ollama import ChatOllama
from langchain_core.output_parsers import StrOutputParser

llm = ChatOllama(model="llama3.1")

# Without bind.
chain = llm | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two three four five.'

# With bind.
chain = llm.bind(stop=["three"]) | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two'

with_config

with_config(
    config: RunnableConfig | None = None, **kwargs: Any
) -> Runnable[Input, Output]

Bind config to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

The config to bind to the Runnable.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the config bound.

with_listeners

with_listeners(
    *,
    on_start: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_end: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_error: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None
) -> Runnable[Input, Output]

Bind lifecycle listeners to a Runnable, returning a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called before the Runnable starts running, with the Run object. Defaults to None.

None
on_end Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda
from langchain_core.tracers.schemas import Run

import time


def test_runnable(time_to_sleep: int):
    time.sleep(time_to_sleep)


def fn_start(run_obj: Run):
    print("start_time:", run_obj.start_time)


def fn_end(run_obj: Run):
    print("end_time:", run_obj.end_time)


chain = RunnableLambda(test_runnable).with_listeners(
    on_start=fn_start, on_end=fn_end
)
chain.invoke(2)

with_alisteners

with_alisteners(
    *,
    on_start: AsyncListener | None = None,
    on_end: AsyncListener | None = None,
    on_error: AsyncListener | None = None
) -> Runnable[Input, Output]

Bind async lifecycle listeners to a Runnable.

Returns a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start AsyncListener | None

Called asynchronously before the Runnable starts running, with the Run object. Defaults to None.

None
on_end AsyncListener | None

Called asynchronously after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error AsyncListener | None

Called asynchronously if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda, Runnable
from datetime import datetime, timezone
import time
import asyncio

def format_t(timestamp: float) -> str:
    return datetime.fromtimestamp(timestamp, tz=timezone.utc).isoformat()

async def test_runnable(time_to_sleep: int):
    print(f"Runnable[{time_to_sleep}s]: starts at {format_t(time.time())}")
    await asyncio.sleep(time_to_sleep)
    print(f"Runnable[{time_to_sleep}s]: ends at {format_t(time.time())}")

async def fn_start(run_obj: Runnable):
    print(f"on start callback starts at {format_t(time.time())}")
    await asyncio.sleep(3)
    print(f"on start callback ends at {format_t(time.time())}")

async def fn_end(run_obj: Runnable):
    print(f"on end callback starts at {format_t(time.time())}")
    await asyncio.sleep(2)
    print(f"on end callback ends at {format_t(time.time())}")

runnable = RunnableLambda(test_runnable).with_alisteners(
    on_start=fn_start,
    on_end=fn_end
)
async def concurrent_runs():
    await asyncio.gather(runnable.ainvoke(2), runnable.ainvoke(3))

asyncio.run(concurrent_runs())
Result:
on start callback starts at 2025-03-01T07:05:22.875378+00:00
on start callback starts at 2025-03-01T07:05:22.875495+00:00
on start callback ends at 2025-03-01T07:05:25.878862+00:00
on start callback ends at 2025-03-01T07:05:25.878947+00:00
Runnable[2s]: starts at 2025-03-01T07:05:25.879392+00:00
Runnable[3s]: starts at 2025-03-01T07:05:25.879804+00:00
Runnable[2s]: ends at 2025-03-01T07:05:27.881998+00:00
on end callback starts at 2025-03-01T07:05:27.882360+00:00
Runnable[3s]: ends at 2025-03-01T07:05:28.881737+00:00
on end callback starts at 2025-03-01T07:05:28.882428+00:00
on end callback ends at 2025-03-01T07:05:29.883893+00:00
on end callback ends at 2025-03-01T07:05:30.884831+00:00

with_types

with_types(
    *,
    input_type: type[Input] | None = None,
    output_type: type[Output] | None = None
) -> Runnable[Input, Output]

Bind input and output types to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
input_type type[Input] | None

The input type to bind to the Runnable. Defaults to None.

None
output_type type[Output] | None

The output type to bind to the Runnable. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the types bound.

with_retry

with_retry(
    *,
    retry_if_exception_type: tuple[
        type[BaseException], ...
    ] = (Exception,),
    wait_exponential_jitter: bool = True,
    exponential_jitter_params: (
        ExponentialJitterParams | None
    ) = None,
    stop_after_attempt: int = 3
) -> Runnable[Input, Output]

Create a new Runnable that retries the original Runnable on exceptions.

Parameters:

Name Type Description Default
retry_if_exception_type tuple[type[BaseException], ...]

A tuple of exception types to retry on. Defaults to (Exception,).

(Exception,)
wait_exponential_jitter bool

Whether to add jitter to the wait time between retries. Defaults to True.

True
stop_after_attempt int

The maximum number of attempts to make before giving up. Defaults to 3.

3
exponential_jitter_params ExponentialJitterParams | None

Parameters for tenacity.wait_exponential_jitter. Namely: initial, max, exp_base, and jitter (all float values).

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable that retries the original Runnable on exceptions.

Example
from langchain_core.runnables import RunnableLambda

count = 0


def _lambda(x: int) -> None:
    global count
    count = count + 1
    if x == 1:
        raise ValueError("x is 1")
    else:
        pass


runnable = RunnableLambda(_lambda)
try:
    runnable.with_retry(
        stop_after_attempt=2,
        retry_if_exception_type=(ValueError,),
    ).invoke(1)
except ValueError:
    pass

assert count == 2

map

map() -> Runnable[list[Input], list[Output]]

Return a new Runnable that maps a list of inputs to a list of outputs.

Calls invoke with each input.

Returns:

Type Description
Runnable[list[Input], list[Output]]

A new Runnable that maps a list of inputs to a list of outputs.

Example
from langchain_core.runnables import RunnableLambda


def _lambda(x: int) -> int:
    return x + 1


runnable = RunnableLambda(_lambda)
print(runnable.map().invoke([1, 2, 3]))  # [2, 3, 4]

with_fallbacks

with_fallbacks(
    fallbacks: Sequence[Runnable[Input, Output]],
    *,
    exceptions_to_handle: tuple[
        type[BaseException], ...
    ] = (Exception,),
    exception_key: str | None = None
) -> RunnableWithFallbacks[Input, Output]

Add fallbacks to a Runnable, returning a new Runnable.

The new Runnable will try the original Runnable, and then each fallback in order, upon failures.

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle. Defaults to (Exception,).

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input. Defaults to None.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

Example
from typing import Iterator

from langchain_core.runnables import RunnableGenerator


def _generate_immediate_error(input: Iterator) -> Iterator[str]:
    raise ValueError()
    yield ""


def _generate(input: Iterator) -> Iterator[str]:
    yield from "foo bar"


runnable = RunnableGenerator(_generate_immediate_error).with_fallbacks(
    [RunnableGenerator(_generate)]
)
print("".join(runnable.stream({})))  # foo bar

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle.

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

as_tool

as_tool(
    args_schema: type[BaseModel] | None = None,
    *,
    name: str | None = None,
    description: str | None = None,
    arg_types: dict[str, type] | None = None
) -> BaseTool

Create a BaseTool from a Runnable.

as_tool will instantiate a BaseTool with a name, description, and args_schema from a Runnable. Where possible, schemas are inferred from runnable.get_input_schema. Alternatively (e.g., if the Runnable takes a dict as input and the specific dict keys are not typed), the schema can be specified directly with args_schema. You can also pass arg_types to just specify the required arguments and their types.

Parameters:

Name Type Description Default
args_schema type[BaseModel] | None

The schema for the tool. Defaults to None.

None
name str | None

The name of the tool. Defaults to None.

None
description str | None

The description of the tool. Defaults to None.

None
arg_types dict[str, type] | None

A dictionary of argument names to types. Defaults to None.

None

Returns:

Type Description
BaseTool

A BaseTool instance.

Typed dict input:

from typing_extensions import TypedDict
from langchain_core.runnables import RunnableLambda


class Args(TypedDict):
    a: int
    b: list[int]


def f(x: Args) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool()
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via args_schema:

from typing import Any
from pydantic import BaseModel, Field
from langchain_core.runnables import RunnableLambda

def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))

class FSchema(BaseModel):
    """Apply a function to an integer and list of integers."""

    a: int = Field(..., description="Integer")
    b: list[int] = Field(..., description="List of ints")

runnable = RunnableLambda(f)
as_tool = runnable.as_tool(FSchema)
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via arg_types:

from typing import Any
from langchain_core.runnables import RunnableLambda


def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool(arg_types={"a": int, "b": list[int]})
as_tool.invoke({"a": 3, "b": [1, 2]})

String input:

from langchain_core.runnables import RunnableLambda


def f(x: str) -> str:
    return x + "a"


def g(x: str) -> str:
    return x + "z"


runnable = RunnableLambda(f) | g
as_tool = runnable.as_tool()
as_tool.invoke("b")

Added in version 0.2.14

__init__

__init__(*args: Any, **kwargs: Any) -> None

is_lc_serializable classmethod

is_lc_serializable() -> bool

Is this class serializable?

By design, even if a class inherits from Serializable, it is not serializable by default. This is to prevent accidental serialization of objects that should not be serialized.

Returns:

Type Description
bool

Whether the class is serializable. Default is False.

get_lc_namespace classmethod

get_lc_namespace() -> list[str]

Get the namespace of the langchain object.

For example, if the class is langchain.llms.openai.OpenAI, then the namespace is ["langchain", "llms", "openai"]

Returns:

Type Description
list[str]

The namespace as a list of strings.

lc_id classmethod

lc_id() -> list[str]

Return a unique identifier for this class for serialization purposes.

The unique identifier is a list of strings that describes the path to the object. For example, for the class langchain.llms.openai.OpenAI, the id is ["langchain", "llms", "openai", "OpenAI"].

to_json

to_json() -> (
    SerializedConstructor | SerializedNotImplemented
)

Serialize the Runnable to JSON.

Returns:

Type Description
SerializedConstructor | SerializedNotImplemented

A JSON-serializable representation of the Runnable.

to_json_not_implemented

to_json_not_implemented() -> SerializedNotImplemented

Serialize a "not implemented" object.

Returns:

Type Description
SerializedNotImplemented

SerializedNotImplemented.

configurable_fields

configurable_fields(
    **kwargs: AnyConfigurableField,
) -> RunnableSerializable[Input, Output]

Configure particular Runnable fields at runtime.

Parameters:

Name Type Description Default
**kwargs AnyConfigurableField

A dictionary of ConfigurableField instances to configure.

{}

Raises:

Type Description
ValueError

If a configuration key is not found in the Runnable.

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the fields configured.

from langchain_core.runnables import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatOpenAI(max_tokens=20).configurable_fields(
    max_tokens=ConfigurableField(
        id="output_token_number",
        name="Max tokens in the output",
        description="The maximum number of tokens in the output",
    )
)

# max_tokens = 20
print("max_tokens_20: ", model.invoke("tell me something about chess").content)

# max_tokens = 200
print(
    "max_tokens_200: ",
    model.with_config(configurable={"output_token_number": 200})
    .invoke("tell me something about chess")
    .content,
)

configurable_alternatives

configurable_alternatives(
    which: ConfigurableField,
    *,
    default_key: str = "default",
    prefix_keys: bool = False,
    **kwargs: (
        Runnable[Input, Output]
        | Callable[[], Runnable[Input, Output]]
    )
) -> RunnableSerializable[Input, Output]

Configure alternatives for Runnables that can be set at runtime.

Parameters:

Name Type Description Default
which ConfigurableField

The ConfigurableField instance that will be used to select the alternative.

required
default_key str

The default key to use if no alternative is selected. Defaults to 'default'.

'default'
prefix_keys bool

Whether to prefix the keys with the ConfigurableField id. Defaults to False.

False
**kwargs Runnable[Input, Output] | Callable[[], Runnable[Input, Output]]

A dictionary of keys to Runnable instances or callables that return Runnable instances.

{}

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the alternatives configured.

from langchain_anthropic import ChatAnthropic
from langchain_core.runnables.utils import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatAnthropic(
    model_name="claude-3-7-sonnet-20250219"
).configurable_alternatives(
    ConfigurableField(id="llm"),
    default_key="anthropic",
    openai=ChatOpenAI(),
)

# uses the default model ChatAnthropic
print(model.invoke("which organization created you?").content)

# uses ChatOpenAI
print(
    model.with_config(configurable={"llm": "openai"})
    .invoke("which organization created you?")
    .content
)

set_verbose

set_verbose(verbose: bool | None) -> bool

If verbose is None, set it.

This allows users to pass in None as verbose to access the global setting.

Parameters:

Name Type Description Default
verbose bool | None

The verbosity setting to use.

required

Returns:

Type Description
bool

The verbosity setting to use.

with_structured_output

with_structured_output(
    schema: Dict | type,
    *,
    include_raw: bool = False,
    **kwargs: Any
) -> Runnable[LanguageModelInput, Dict | BaseModel]

Model wrapper that returns outputs formatted to match the given schema.

Parameters:

Name Type Description Default
schema Dict | type

The output schema. Can be passed in as:

  • an OpenAI function/tool schema,
  • a JSON Schema,
  • a TypedDict class,
  • or a Pydantic class.

If schema is a Pydantic class then the model output will be a Pydantic instance of that class, and the model-generated fields will be validated by the Pydantic class. Otherwise the model output will be a dict and will not be validated. See langchain_core.utils.function_calling.convert_to_openai_tool for more on how to properly specify types and descriptions of schema fields when specifying a Pydantic or TypedDict class.

required
include_raw bool

If False then only the parsed structured output is returned. If an error occurs during model output parsing it will be raised. If True then both the raw model response (a BaseMessage) and the parsed model response will be returned. If an error occurs during output parsing it will be caught and returned as well. The final output is always a dict with keys 'raw', 'parsed', and 'parsing_error'.

False

Raises:

Type Description
ValueError

If there are any unsupported kwargs.

NotImplementedError

If the model does not implement with_structured_output().

Returns:

Type Description
Runnable[LanguageModelInput, Dict | BaseModel]

A Runnable that takes same inputs as a langchain_core.language_models.chat.BaseChatModel.

Runnable[LanguageModelInput, Dict | BaseModel]

If include_raw is False and schema is a Pydantic class, Runnable outputs

Runnable[LanguageModelInput, Dict | BaseModel]

an instance of schema (i.e., a Pydantic object).

Runnable[LanguageModelInput, Dict | BaseModel]

Otherwise, if include_raw is False then Runnable outputs a dict.

Runnable[LanguageModelInput, Dict | BaseModel]

If include_raw is True, then Runnable outputs a dict with keys:

Runnable[LanguageModelInput, Dict | BaseModel]
  • 'raw': BaseMessage
Runnable[LanguageModelInput, Dict | BaseModel]
  • 'parsed': None if there was a parsing error, otherwise the type depends on the schema as described above.
Runnable[LanguageModelInput, Dict | BaseModel]
  • 'parsing_error': Optional[BaseException]
Pydantic schema (include_raw=False):

.. code-block:: python

from pydantic import BaseModel


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


llm = ChatModel(model="model-name", temperature=0)
structured_llm = llm.with_structured_output(AnswerWithJustification)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)

# -> AnswerWithJustification(
#     answer='They weigh the same',
#     justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'
# )
Pydantic schema (include_raw=True):

.. code-block:: python

from pydantic import BaseModel


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


llm = ChatModel(model="model-name", temperature=0)
structured_llm = llm.with_structured_output(
    AnswerWithJustification, include_raw=True
)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)
# -> {
#     'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),
#     'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),
#     'parsing_error': None
# }
Dict schema (include_raw=False):

.. code-block:: python

from pydantic import BaseModel
from langchain_core.utils.function_calling import convert_to_openai_tool


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


dict_schema = convert_to_openai_tool(AnswerWithJustification)
llm = ChatModel(model="model-name", temperature=0)
structured_llm = llm.with_structured_output(dict_schema)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)
# -> {
#     'answer': 'They weigh the same',
#     'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
# }

Behavior changed in 0.2.26

Added support for TypedDict class.

get_token_ids

get_token_ids(text: str) -> list[int]

Return the ordered ids of the tokens in a text.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
list[int]

A list of ids corresponding to the tokens in the text, in order they occur

list[int]

in the text.

get_num_tokens

get_num_tokens(text: str) -> int

Get the number of tokens present in the text.

Useful for checking if an input fits in a model's context window.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
int

The integer number of tokens in the text.

get_num_tokens_from_messages

get_num_tokens_from_messages(
    messages: list[BaseMessage],
    tools: Sequence | None = None,
) -> int

Get the number of tokens in the messages.

Useful for checking if an input fits in a model's context window.

Note

The base implementation of get_num_tokens_from_messages ignores tool schemas.

Parameters:

Name Type Description Default
messages list[BaseMessage]

The message inputs to tokenize.

required
tools Sequence | None

If provided, sequence of dict, BaseModel, function, or BaseTools to be converted to tool schemas.

None

Returns:

Type Description
int

The sum of the number of tokens across the messages.

generate

generate(
    messages: list[list[BaseMessage]],
    stop: list[str] | None = None,
    callbacks: Callbacks = None,
    *,
    tags: list[str] | None = None,
    metadata: dict[str, Any] | None = None,
    run_name: str | None = None,
    run_id: UUID | None = None,
    **kwargs: Any
) -> LLMResult

Pass a sequence of prompts to the model and return model generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
messages list[list[BaseMessage]]

List of list of messages.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | None

The tags to apply.

None
metadata dict[str, Any] | None

The metadata to apply.

None
run_name str | None

The name of the run.

None
run_id UUID | None

The ID of the run.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input

LLMResult

prompt and additional model provider-specific output.

agenerate async

agenerate(
    messages: list[list[BaseMessage]],
    stop: list[str] | None = None,
    callbacks: Callbacks = None,
    *,
    tags: list[str] | None = None,
    metadata: dict[str, Any] | None = None,
    run_name: str | None = None,
    run_id: UUID | None = None,
    **kwargs: Any
) -> LLMResult

Asynchronously pass a sequence of prompts to a model and return generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
messages list[list[BaseMessage]]

List of list of messages.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | None

The tags to apply.

None
metadata dict[str, Any] | None

The metadata to apply.

None
run_name str | None

The name of the run.

None
run_id UUID | None

The ID of the run.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input

LLMResult

prompt and additional model provider-specific output.

dict

dict(**kwargs: Any) -> dict

Return a dictionary of the LLM.

bind_tools

bind_tools(
    tools: Sequence[
        Dict[str, Any] | type | Callable | BaseTool
    ],
    *,
    tool_choice: str | None = None,
    **kwargs: Any
) -> Runnable[LanguageModelInput, AIMessage]

Bind tools to the model.

Parameters:

Name Type Description Default
tools Sequence[Dict[str, Any] | type | Callable | BaseTool]

Sequence of tools to bind to the model.

required
tool_choice str | None

The tool to use. If "any" then any tool can be used.

None

Returns:

Type Description
Runnable[LanguageModelInput, AIMessage]

A Runnable that returns a message.

validate_environment

validate_environment() -> Self

Validate that llama-cpp-python library is installed.

GemmaChatLocalKaggle

Bases: _GemmaLocalKaggleBase, BaseChatModel

Methods:

Name Description
get_name

Get the name of the Runnable.

get_input_schema

Get a pydantic model that can be used to validate input to the Runnable.

get_input_jsonschema

Get a JSON schema that represents the input to the Runnable.

get_output_schema

Get a pydantic model that can be used to validate output to the Runnable.

get_output_jsonschema

Get a JSON schema that represents the output of the Runnable.

config_schema

The type of config this Runnable accepts specified as a pydantic model.

get_config_jsonschema

Get a JSON schema that represents the config of the Runnable.

get_graph

Return a graph representation of this Runnable.

get_prompts

Return a list of prompts used by this Runnable.

__or__

Runnable "or" operator.

__ror__

Runnable "reverse-or" operator.

pipe

Pipe runnables.

pick

Pick keys from the output dict of this Runnable.

assign

Assigns new fields to the dict output of this Runnable.

batch

Default implementation runs invoke in parallel using a thread pool executor.

batch_as_completed

Run invoke in parallel on a list of inputs.

abatch

Default implementation runs ainvoke in parallel using asyncio.gather.

abatch_as_completed

Run ainvoke in parallel on a list of inputs.

astream_log

Stream all output from a Runnable, as reported to the callback system.

astream_events

Generate a stream of events.

transform

Transform inputs to outputs.

atransform

Transform inputs to outputs.

bind

Bind arguments to a Runnable, returning a new Runnable.

with_config

Bind config to a Runnable, returning a new Runnable.

with_listeners

Bind lifecycle listeners to a Runnable, returning a new Runnable.

with_alisteners

Bind async lifecycle listeners to a Runnable.

with_types

Bind input and output types to a Runnable, returning a new Runnable.

with_retry

Create a new Runnable that retries the original Runnable on exceptions.

map

Return a new Runnable that maps a list of inputs to a list of outputs.

with_fallbacks

Add fallbacks to a Runnable, returning a new Runnable.

as_tool

Create a BaseTool from a Runnable.

is_lc_serializable

Is this class serializable?

get_lc_namespace

Get the namespace of the langchain object.

lc_id

Return a unique identifier for this class for serialization purposes.

to_json

Serialize the Runnable to JSON.

to_json_not_implemented

Serialize a "not implemented" object.

configurable_fields

Configure particular Runnable fields at runtime.

configurable_alternatives

Configure alternatives for Runnables that can be set at runtime.

set_verbose

If verbose is None, set it.

with_structured_output

Model wrapper that returns outputs formatted to match the given schema.

get_token_ids

Return the ordered ids of the tokens in a text.

get_num_tokens

Get the number of tokens present in the text.

get_num_tokens_from_messages

Get the number of tokens in the messages.

generate

Pass a sequence of prompts to the model and return model generations.

agenerate

Asynchronously pass a sequence of prompts to a model and return generations.

dict

Return a dictionary of the LLM.

bind_tools

Bind tools to the model.

validate_environment

Validate that llama-cpp-python library is installed.

__init__

Needed for mypy typing to recognize model_name as a valid arg.

Attributes:

Name Type Description
InputType TypeAlias

Get the input type for this runnable.

OutputType Any

Get the output type for this runnable.

input_schema type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema type[BaseModel]

Output schema.

config_specs list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets dict[str, str]

A map of constructor argument names to secret ids.

lc_attributes dict

List of attribute names that should be included in the serialized kwargs.

cache BaseCache | bool | None

Whether to cache the response.

verbose bool

Whether to print out response text.

callbacks Callbacks

Callbacks to add to the run trace.

tags list[str] | None

Tags to add to the run trace.

metadata dict[str, Any] | None

Metadata to add to the run trace.

custom_get_token_ids Callable[[str], list[int]] | None

Optional encoder to use for counting tokens.

rate_limiter BaseRateLimiter | None

An optional rate limiter to use for limiting the number of requests.

disable_streaming bool | Literal['tool_calling']

Whether to disable streaming for this model.

output_version str | None

Version of AIMessage output format to store in message content.

max_tokens Optional[int]

The maximum number of tokens to generate.

temperature Optional[float]

The temperature to use for sampling.

top_p Optional[float]

The top-p value to use for sampling.

top_k Optional[int]

The top-k value to use for sampling.

model_name str

Gemma model name.

parse_response bool

Whether to post-process the chat response and clean repeations

InputType property

InputType: TypeAlias

Get the input type for this runnable.

OutputType property

OutputType: Any

Get the output type for this runnable.

input_schema property

input_schema: type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema property

output_schema: type[BaseModel]

Output schema.

The type of output this Runnable produces specified as a pydantic model.

config_specs property

config_specs: list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets property

lc_secrets: dict[str, str]

A map of constructor argument names to secret ids.

For example,

lc_attributes property

lc_attributes: dict

List of attribute names that should be included in the serialized kwargs.

These attributes must be accepted by the constructor. Default is an empty dictionary.

cache class-attribute instance-attribute

cache: BaseCache | bool | None = Field(
    default=None, exclude=True
)

Whether to cache the response.

  • If true, will use the global cache.
  • If false, will not use a cache
  • If None, will use the global cache if it's set, otherwise no cache.
  • If instance of BaseCache, will use the provided cache.

Caching is not currently supported for streaming methods of models.

verbose class-attribute instance-attribute

verbose: bool = Field(
    default_factory=_get_verbosity, exclude=True, repr=False
)

Whether to print out response text.

callbacks class-attribute instance-attribute

callbacks: Callbacks = Field(default=None, exclude=True)

Callbacks to add to the run trace.

tags class-attribute instance-attribute

tags: list[str] | None = Field(default=None, exclude=True)

Tags to add to the run trace.

metadata class-attribute instance-attribute

metadata: dict[str, Any] | None = Field(
    default=None, exclude=True
)

Metadata to add to the run trace.

custom_get_token_ids class-attribute instance-attribute

custom_get_token_ids: Callable[[str], list[int]] | None = (
    Field(default=None, exclude=True)
)

Optional encoder to use for counting tokens.

rate_limiter class-attribute instance-attribute

rate_limiter: BaseRateLimiter | None = Field(
    default=None, exclude=True
)

An optional rate limiter to use for limiting the number of requests.

disable_streaming class-attribute instance-attribute

disable_streaming: bool | Literal['tool_calling'] = False

Whether to disable streaming for this model.

If streaming is bypassed, then stream()/astream()/astream_events() will defer to invoke()/ainvoke().

  • If True, will always bypass streaming case.
  • If 'tool_calling', will bypass streaming case only when the model is called with a tools keyword argument. In other words, LangChain will automatically switch to non-streaming behavior (invoke()) only when the tools argument is provided. This offers the best of both worlds.
  • If False (default), will always use streaming case if available.

The main reason for this flag is that code might be written using stream() and a user may want to swap out a given model for another model whose the implementation does not properly support streaming.

output_version class-attribute instance-attribute

output_version: str | None = Field(
    default_factory=from_env(
        "LC_OUTPUT_VERSION", default=None
    )
)

Version of AIMessage output format to store in message content.

AIMessage.content_blocks will lazily parse the contents of content into a standard format. This flag can be used to additionally store the standard format in message content, e.g., for serialization purposes.

Supported values:

  • "v0": provider-specific format in content (can lazily-parse with .content_blocks)
  • "v1": standardized format in content (consistent with .content_blocks)

Partner packages (e.g., langchain-openai) can also use this field to roll out new content formats in a backward-compatible way.

Added in version 1.0

max_tokens class-attribute instance-attribute

max_tokens: Optional[int] = None

The maximum number of tokens to generate.

temperature class-attribute instance-attribute

temperature: Optional[float] = None

The temperature to use for sampling.

top_p class-attribute instance-attribute

top_p: Optional[float] = None

The top-p value to use for sampling.

top_k class-attribute instance-attribute

top_k: Optional[int] = None

The top-k value to use for sampling.

model_name class-attribute instance-attribute

model_name: str = Field(
    default="gemma_2b_en", alias="model"
)

Gemma model name.

parse_response class-attribute instance-attribute

parse_response: bool = False

Whether to post-process the chat response and clean repeations

get_name

get_name(
    suffix: str | None = None, *, name: str | None = None
) -> str

Get the name of the Runnable.

Parameters:

Name Type Description Default
suffix str | None

An optional suffix to append to the name.

None
name str | None

An optional name to use instead of the Runnable's name.

None

Returns:

Type Description
str

The name of the Runnable.

get_input_schema

get_input_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate input to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic input schema that depends on which configuration the Runnable is invoked with.

This method allows to get an input schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate input.

get_input_jsonschema

get_input_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the input to the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the input to the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_input_jsonschema())

Added in version 0.3.0

get_output_schema

get_output_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate output to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic output schema that depends on which configuration the Runnable is invoked with.

This method allows to get an output schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate output.

get_output_jsonschema

get_output_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the output of the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the output of the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_output_jsonschema())

Added in version 0.3.0

config_schema

config_schema(
    *, include: Sequence[str] | None = None
) -> type[BaseModel]

The type of config this Runnable accepts specified as a pydantic model.

To mark a field as configurable, see the configurable_fields and configurable_alternatives methods.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate config.

get_config_jsonschema

get_config_jsonschema(
    *, include: Sequence[str] | None = None
) -> dict[str, Any]

Get a JSON schema that represents the config of the Runnable.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the config of the Runnable.

Added in version 0.3.0

get_graph

get_graph(config: RunnableConfig | None = None) -> Graph

Return a graph representation of this Runnable.

get_prompts

get_prompts(
    config: RunnableConfig | None = None,
) -> list[BasePromptTemplate]

Return a list of prompts used by this Runnable.

__or__

__or__(
    other: (
        Runnable[Any, Other]
        | Callable[[Iterator[Any]], Iterator[Other]]
        | Callable[
            [AsyncIterator[Any]], AsyncIterator[Other]
        ]
        | Callable[[Any], Other]
        | Mapping[
            str,
            Runnable[Any, Other]
            | Callable[[Any], Other]
            | Any,
        ]
    ),
) -> RunnableSerializable[Input, Other]

Runnable "or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Any, Other] | Callable[[Iterator[Any]], Iterator[Other]] | Callable[[AsyncIterator[Any]], AsyncIterator[Other]] | Callable[[Any], Other] | Mapping[str, Runnable[Any, Other] | Callable[[Any], Other] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

__ror__

__ror__(
    other: (
        Runnable[Other, Any]
        | Callable[[Iterator[Other]], Iterator[Any]]
        | Callable[
            [AsyncIterator[Other]], AsyncIterator[Any]
        ]
        | Callable[[Other], Any]
        | Mapping[
            str,
            Runnable[Other, Any]
            | Callable[[Other], Any]
            | Any,
        ]
    ),
) -> RunnableSerializable[Other, Output]

Runnable "reverse-or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Other, Any] | Callable[[Iterator[Other]], Iterator[Any]] | Callable[[AsyncIterator[Other]], AsyncIterator[Any]] | Callable[[Other], Any] | Mapping[str, Runnable[Other, Any] | Callable[[Other], Any] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Other, Output]

A new Runnable.

pipe

pipe(
    *others: Runnable[Any, Other] | Callable[[Any], Other],
    name: str | None = None
) -> RunnableSerializable[Input, Other]

Pipe runnables.

Compose this Runnable with Runnable-like objects to make a RunnableSequence.

Equivalent to RunnableSequence(self, *others) or self | others[0] | ...

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


def mul_two(x: int) -> int:
    return x * 2


runnable_1 = RunnableLambda(add_one)
runnable_2 = RunnableLambda(mul_two)
sequence = runnable_1.pipe(runnable_2)
# Or equivalently:
# sequence = runnable_1 | runnable_2
# sequence = RunnableSequence(first=runnable_1, last=runnable_2)
sequence.invoke(1)
await sequence.ainvoke(1)
# -> 4

sequence.batch([1, 2, 3])
await sequence.abatch([1, 2, 3])
# -> [4, 6, 8]

Parameters:

Name Type Description Default
*others Runnable[Any, Other] | Callable[[Any], Other]

Other Runnable or Runnable-like objects to compose

()
name str | None

An optional name for the resulting RunnableSequence.

None

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

pick

pick(
    keys: str | list[str],
) -> RunnableSerializable[Any, Any]

Pick keys from the output dict of this Runnable.

Pick single key:

```python
import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)
chain = RunnableMap(str=as_str, json=as_json)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3]}

json_only_chain = chain.pick("json")
json_only_chain.invoke("[1, 2, 3]")
# -> [1, 2, 3]
```

Pick list of keys:

```python
from typing import Any

import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)


def as_bytes(x: Any) -> bytes:
    return bytes(x, "utf-8")


chain = RunnableMap(
    str=as_str, json=as_json, bytes=RunnableLambda(as_bytes)
)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3], "bytes": b"[1, 2, 3]"}

json_and_bytes_chain = chain.pick(["json", "bytes"])
json_and_bytes_chain.invoke("[1, 2, 3]")
# -> {"json": [1, 2, 3], "bytes": b"[1, 2, 3]"}
```

Parameters:

Name Type Description Default
keys str | list[str]

A key or list of keys to pick from the output dict.

required

Returns:

Type Description
RunnableSerializable[Any, Any]

a new Runnable.

assign

assign(
    **kwargs: (
        Runnable[dict[str, Any], Any]
        | Callable[[dict[str, Any]], Any]
        | Mapping[
            str,
            Runnable[dict[str, Any], Any]
            | Callable[[dict[str, Any]], Any],
        ]
    ),
) -> RunnableSerializable[Any, Any]

Assigns new fields to the dict output of this Runnable.

from langchain_community.llms.fake import FakeStreamingListLLM
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import SystemMessagePromptTemplate
from langchain_core.runnables import Runnable
from operator import itemgetter

prompt = (
    SystemMessagePromptTemplate.from_template("You are a nice assistant.")
    + "{question}"
)
llm = FakeStreamingListLLM(responses=["foo-lish"])

chain: Runnable = prompt | llm | {"str": StrOutputParser()}

chain_with_assign = chain.assign(hello=itemgetter("str") | llm)

print(chain_with_assign.input_schema.model_json_schema())
# {'title': 'PromptInput', 'type': 'object', 'properties':
{'question': {'title': 'Question', 'type': 'string'}}}
print(chain_with_assign.output_schema.model_json_schema())
# {'title': 'RunnableSequenceOutput', 'type': 'object', 'properties':
{'str': {'title': 'Str',
'type': 'string'}, 'hello': {'title': 'Hello', 'type': 'string'}}}

Parameters:

Name Type Description Default
**kwargs Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any] | Mapping[str, Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any]]

A mapping of keys to Runnable or Runnable-like objects that will be invoked with the entire output dict of this Runnable.

{}

Returns:

Type Description
RunnableSerializable[Any, Any]

A new Runnable.

batch

batch(
    inputs: list[Input],
    config: (
        RunnableConfig | list[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> list[Output]

Default implementation runs invoke in parallel using a thread pool executor.

The default implementation of batch works well for IO bound runnables.

Subclasses should override this method if they can batch more efficiently; e.g., if the underlying Runnable uses an API which supports a batch mode.

Parameters:

Name Type Description Default
inputs list[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | list[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
list[Output]

A list of outputs from the Runnable.

batch_as_completed

batch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> Iterator[tuple[int, Output | Exception]]

Run invoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
tuple[int, Output | Exception]

Tuples of the index of the input and the output from the Runnable.

abatch async

abatch(
    inputs: list[Input],
    config: (
        RunnableConfig | list[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> list[Output]

Default implementation runs ainvoke in parallel using asyncio.gather.

The default implementation of batch works well for IO bound runnables.

Subclasses should override this method if they can batch more efficiently; e.g., if the underlying Runnable uses an API which supports a batch mode.

Parameters:

Name Type Description Default
inputs list[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | list[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
list[Output]

A list of outputs from the Runnable.

abatch_as_completed async

abatch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> AsyncIterator[tuple[int, Output | Exception]]

Run ainvoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[tuple[int, Output | Exception]]

A tuple of the index of the input and the output from the Runnable.

astream_log async

astream_log(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    diff: bool = True,
    with_streamed_output_list: bool = True,
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

Stream all output from a Runnable, as reported to the callback system.

This includes all inner runs of LLMs, Retrievers, Tools, etc.

Output is streamed as Log objects, which include a list of Jsonpatch ops that describe how the state of the run has changed in each step, and the final state of the run.

The Jsonpatch ops can be applied in order to construct state.

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
diff bool

Whether to yield diffs between each step or the current state.

True
with_streamed_output_list bool

Whether to yield the streamed_output list.

True
include_names Sequence[str] | None

Only include logs with these names.

None
include_types Sequence[str] | None

Only include logs with these types.

None
include_tags Sequence[str] | None

Only include logs with these tags.

None
exclude_names Sequence[str] | None

Exclude logs with these names.

None
exclude_types Sequence[str] | None

Exclude logs with these types.

None
exclude_tags Sequence[str] | None

Exclude logs with these tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

A RunLogPatch or RunLog object.

astream_events async

astream_events(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    version: Literal["v1", "v2"] = "v2",
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[StreamEvent]

Generate a stream of events.

Use to create an iterator over StreamEvents that provide real-time information about the progress of the Runnable, including StreamEvents from intermediate results.

A StreamEvent is a dictionary with the following schema:

  • event: str - Event names are of the format: on_[runnable_type]_(start|stream|end).
  • name: str - The name of the Runnable that generated the event.
  • run_id: str - randomly generated ID associated with the given execution of the Runnable that emitted the event. A child Runnable that gets invoked as part of the execution of a parent Runnable is assigned its own unique ID.
  • parent_ids: list[str] - The IDs of the parent runnables that generated the event. The root Runnable will have an empty list. The order of the parent IDs is from the root to the immediate parent. Only available for v2 version of the API. The v1 version of the API will return an empty list.
  • tags: Optional[list[str]] - The tags of the Runnable that generated the event.
  • metadata: Optional[dict[str, Any]] - The metadata of the Runnable that generated the event.
  • data: dict[str, Any]

Below is a table that illustrates some events that might be emitted by various chains. Metadata fields have been omitted from the table for brevity. Chain definitions have been included after the table.

Note

This reference table is for the v2 version of the schema.

+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | event | name | chunk | input | output | +==========================+==================+=====================================+===================================================+=====================================================+ | on_chat_model_start | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_stream | [model name] | AIMessageChunk(content="hello") | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_end | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | AIMessageChunk(content="hello world") | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_start | [model name] | | {'input': 'hello'} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_stream | [model name] | 'Hello' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_end | [model name] | | 'Hello human!' | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_start | format_docs | | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_stream | format_docs | 'hello world!, goodbye world!' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_end | format_docs | | [Document(...)] | 'hello world!, goodbye world!' | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_start | some_tool | | {"x": 1, "y": "2"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_end | some_tool | | | {"x": 1, "y": "2"} | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_start | [retriever name] | | {"query": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_end | [retriever name] | | {"query": "hello"} | [Document(...), ..] | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_start | [template_name] | | {"question": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_end | [template_name] | | {"question": "hello"} | ChatPromptValue(messages: [SystemMessage, ...]) | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+

In addition to the standard events, users can also dispatch custom events (see example below).

Custom events will be only be surfaced with in the v2 version of the API!

A custom event has following format:

+-----------+------+-----------------------------------------------------------------------------------------------------------+ | Attribute | Type | Description | +===========+======+===========================================================================================================+ | name | str | A user defined name for the event. | +-----------+------+-----------------------------------------------------------------------------------------------------------+ | data | Any | The data associated with the event. This can be anything, though we suggest making it JSON serializable. | +-----------+------+-----------------------------------------------------------------------------------------------------------+

Here are declarations associated with the standard events shown above:

format_docs:

def format_docs(docs: list[Document]) -> str:
    '''Format the docs.'''
    return ", ".join([doc.page_content for doc in docs])


format_docs = RunnableLambda(format_docs)

some_tool:

@tool
def some_tool(x: int, y: str) -> dict:
    '''Some_tool.'''
    return {"x": x, "y": y}

prompt:

template = ChatPromptTemplate.from_messages(
    [
        ("system", "You are Cat Agent 007"),
        ("human", "{question}"),
    ]
).with_config({"run_name": "my_template", "tags": ["my_template"]})
Example:

from langchain_core.runnables import RunnableLambda


async def reverse(s: str) -> str:
    return s[::-1]


chain = RunnableLambda(func=reverse)

events = [event async for event in chain.astream_events("hello", version="v2")]

# will produce the following events (run_id, and parent_ids
# has been omitted for brevity):
[
    {
        "data": {"input": "hello"},
        "event": "on_chain_start",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"chunk": "olleh"},
        "event": "on_chain_stream",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"output": "olleh"},
        "event": "on_chain_end",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
]

Example: Dispatch Custom Event

from langchain_core.callbacks.manager import (
    adispatch_custom_event,
)
from langchain_core.runnables import RunnableLambda, RunnableConfig
import asyncio


async def slow_thing(some_input: str, config: RunnableConfig) -> str:
    """Do something that takes a long time."""
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 1 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 2 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    return "Done"

slow_thing = RunnableLambda(slow_thing)

async for event in slow_thing.astream_events("some_input", version="v2"):
    print(event)

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
version Literal['v1', 'v2']

The version of the schema to use either 'v2' or 'v1'. Users should use 'v2'. 'v1' is for backwards compatibility and will be deprecated in 0.4.0. No default will be assigned until the API is stabilized. custom events will only be surfaced in 'v2'.

'v2'
include_names Sequence[str] | None

Only include events from Runnables with matching names.

None
include_types Sequence[str] | None

Only include events from Runnables with matching types.

None
include_tags Sequence[str] | None

Only include events from Runnables with matching tags.

None
exclude_names Sequence[str] | None

Exclude events from Runnables with matching names.

None
exclude_types Sequence[str] | None

Exclude events from Runnables with matching types.

None
exclude_tags Sequence[str] | None

Exclude events from Runnables with matching tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable. These will be passed to astream_log as this implementation of astream_events is built on top of astream_log.

{}

Yields:

Type Description
AsyncIterator[StreamEvent]

An async stream of StreamEvents.

Raises:

Type Description
NotImplementedError

If the version is not 'v1' or 'v2'.

transform

transform(
    input: Iterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> Iterator[Output]

Transform inputs to outputs.

Default implementation of transform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input Iterator[Input]

An iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
Output

The output of the Runnable.

atransform async

atransform(
    input: AsyncIterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> AsyncIterator[Output]

Transform inputs to outputs.

Default implementation of atransform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input AsyncIterator[Input]

An async iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[Output]

The output of the Runnable.

bind

bind(**kwargs: Any) -> Runnable[Input, Output]

Bind arguments to a Runnable, returning a new Runnable.

Useful when a Runnable in a chain requires an argument that is not in the output of the previous Runnable or included in the user input.

Parameters:

Name Type Description Default
kwargs Any

The arguments to bind to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the arguments bound.

Example
from langchain_ollama import ChatOllama
from langchain_core.output_parsers import StrOutputParser

llm = ChatOllama(model="llama3.1")

# Without bind.
chain = llm | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two three four five.'

# With bind.
chain = llm.bind(stop=["three"]) | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two'

with_config

with_config(
    config: RunnableConfig | None = None, **kwargs: Any
) -> Runnable[Input, Output]

Bind config to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

The config to bind to the Runnable.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the config bound.

with_listeners

with_listeners(
    *,
    on_start: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_end: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_error: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None
) -> Runnable[Input, Output]

Bind lifecycle listeners to a Runnable, returning a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called before the Runnable starts running, with the Run object. Defaults to None.

None
on_end Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda
from langchain_core.tracers.schemas import Run

import time


def test_runnable(time_to_sleep: int):
    time.sleep(time_to_sleep)


def fn_start(run_obj: Run):
    print("start_time:", run_obj.start_time)


def fn_end(run_obj: Run):
    print("end_time:", run_obj.end_time)


chain = RunnableLambda(test_runnable).with_listeners(
    on_start=fn_start, on_end=fn_end
)
chain.invoke(2)

with_alisteners

with_alisteners(
    *,
    on_start: AsyncListener | None = None,
    on_end: AsyncListener | None = None,
    on_error: AsyncListener | None = None
) -> Runnable[Input, Output]

Bind async lifecycle listeners to a Runnable.

Returns a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start AsyncListener | None

Called asynchronously before the Runnable starts running, with the Run object. Defaults to None.

None
on_end AsyncListener | None

Called asynchronously after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error AsyncListener | None

Called asynchronously if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda, Runnable
from datetime import datetime, timezone
import time
import asyncio

def format_t(timestamp: float) -> str:
    return datetime.fromtimestamp(timestamp, tz=timezone.utc).isoformat()

async def test_runnable(time_to_sleep: int):
    print(f"Runnable[{time_to_sleep}s]: starts at {format_t(time.time())}")
    await asyncio.sleep(time_to_sleep)
    print(f"Runnable[{time_to_sleep}s]: ends at {format_t(time.time())}")

async def fn_start(run_obj: Runnable):
    print(f"on start callback starts at {format_t(time.time())}")
    await asyncio.sleep(3)
    print(f"on start callback ends at {format_t(time.time())}")

async def fn_end(run_obj: Runnable):
    print(f"on end callback starts at {format_t(time.time())}")
    await asyncio.sleep(2)
    print(f"on end callback ends at {format_t(time.time())}")

runnable = RunnableLambda(test_runnable).with_alisteners(
    on_start=fn_start,
    on_end=fn_end
)
async def concurrent_runs():
    await asyncio.gather(runnable.ainvoke(2), runnable.ainvoke(3))

asyncio.run(concurrent_runs())
Result:
on start callback starts at 2025-03-01T07:05:22.875378+00:00
on start callback starts at 2025-03-01T07:05:22.875495+00:00
on start callback ends at 2025-03-01T07:05:25.878862+00:00
on start callback ends at 2025-03-01T07:05:25.878947+00:00
Runnable[2s]: starts at 2025-03-01T07:05:25.879392+00:00
Runnable[3s]: starts at 2025-03-01T07:05:25.879804+00:00
Runnable[2s]: ends at 2025-03-01T07:05:27.881998+00:00
on end callback starts at 2025-03-01T07:05:27.882360+00:00
Runnable[3s]: ends at 2025-03-01T07:05:28.881737+00:00
on end callback starts at 2025-03-01T07:05:28.882428+00:00
on end callback ends at 2025-03-01T07:05:29.883893+00:00
on end callback ends at 2025-03-01T07:05:30.884831+00:00

with_types

with_types(
    *,
    input_type: type[Input] | None = None,
    output_type: type[Output] | None = None
) -> Runnable[Input, Output]

Bind input and output types to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
input_type type[Input] | None

The input type to bind to the Runnable. Defaults to None.

None
output_type type[Output] | None

The output type to bind to the Runnable. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the types bound.

with_retry

with_retry(
    *,
    retry_if_exception_type: tuple[
        type[BaseException], ...
    ] = (Exception,),
    wait_exponential_jitter: bool = True,
    exponential_jitter_params: (
        ExponentialJitterParams | None
    ) = None,
    stop_after_attempt: int = 3
) -> Runnable[Input, Output]

Create a new Runnable that retries the original Runnable on exceptions.

Parameters:

Name Type Description Default
retry_if_exception_type tuple[type[BaseException], ...]

A tuple of exception types to retry on. Defaults to (Exception,).

(Exception,)
wait_exponential_jitter bool

Whether to add jitter to the wait time between retries. Defaults to True.

True
stop_after_attempt int

The maximum number of attempts to make before giving up. Defaults to 3.

3
exponential_jitter_params ExponentialJitterParams | None

Parameters for tenacity.wait_exponential_jitter. Namely: initial, max, exp_base, and jitter (all float values).

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable that retries the original Runnable on exceptions.

Example
from langchain_core.runnables import RunnableLambda

count = 0


def _lambda(x: int) -> None:
    global count
    count = count + 1
    if x == 1:
        raise ValueError("x is 1")
    else:
        pass


runnable = RunnableLambda(_lambda)
try:
    runnable.with_retry(
        stop_after_attempt=2,
        retry_if_exception_type=(ValueError,),
    ).invoke(1)
except ValueError:
    pass

assert count == 2

map

map() -> Runnable[list[Input], list[Output]]

Return a new Runnable that maps a list of inputs to a list of outputs.

Calls invoke with each input.

Returns:

Type Description
Runnable[list[Input], list[Output]]

A new Runnable that maps a list of inputs to a list of outputs.

Example
from langchain_core.runnables import RunnableLambda


def _lambda(x: int) -> int:
    return x + 1


runnable = RunnableLambda(_lambda)
print(runnable.map().invoke([1, 2, 3]))  # [2, 3, 4]

with_fallbacks

with_fallbacks(
    fallbacks: Sequence[Runnable[Input, Output]],
    *,
    exceptions_to_handle: tuple[
        type[BaseException], ...
    ] = (Exception,),
    exception_key: str | None = None
) -> RunnableWithFallbacks[Input, Output]

Add fallbacks to a Runnable, returning a new Runnable.

The new Runnable will try the original Runnable, and then each fallback in order, upon failures.

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle. Defaults to (Exception,).

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input. Defaults to None.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

Example
from typing import Iterator

from langchain_core.runnables import RunnableGenerator


def _generate_immediate_error(input: Iterator) -> Iterator[str]:
    raise ValueError()
    yield ""


def _generate(input: Iterator) -> Iterator[str]:
    yield from "foo bar"


runnable = RunnableGenerator(_generate_immediate_error).with_fallbacks(
    [RunnableGenerator(_generate)]
)
print("".join(runnable.stream({})))  # foo bar

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle.

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

as_tool

as_tool(
    args_schema: type[BaseModel] | None = None,
    *,
    name: str | None = None,
    description: str | None = None,
    arg_types: dict[str, type] | None = None
) -> BaseTool

Create a BaseTool from a Runnable.

as_tool will instantiate a BaseTool with a name, description, and args_schema from a Runnable. Where possible, schemas are inferred from runnable.get_input_schema. Alternatively (e.g., if the Runnable takes a dict as input and the specific dict keys are not typed), the schema can be specified directly with args_schema. You can also pass arg_types to just specify the required arguments and their types.

Parameters:

Name Type Description Default
args_schema type[BaseModel] | None

The schema for the tool. Defaults to None.

None
name str | None

The name of the tool. Defaults to None.

None
description str | None

The description of the tool. Defaults to None.

None
arg_types dict[str, type] | None

A dictionary of argument names to types. Defaults to None.

None

Returns:

Type Description
BaseTool

A BaseTool instance.

Typed dict input:

from typing_extensions import TypedDict
from langchain_core.runnables import RunnableLambda


class Args(TypedDict):
    a: int
    b: list[int]


def f(x: Args) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool()
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via args_schema:

from typing import Any
from pydantic import BaseModel, Field
from langchain_core.runnables import RunnableLambda

def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))

class FSchema(BaseModel):
    """Apply a function to an integer and list of integers."""

    a: int = Field(..., description="Integer")
    b: list[int] = Field(..., description="List of ints")

runnable = RunnableLambda(f)
as_tool = runnable.as_tool(FSchema)
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via arg_types:

from typing import Any
from langchain_core.runnables import RunnableLambda


def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool(arg_types={"a": int, "b": list[int]})
as_tool.invoke({"a": 3, "b": [1, 2]})

String input:

from langchain_core.runnables import RunnableLambda


def f(x: str) -> str:
    return x + "a"


def g(x: str) -> str:
    return x + "z"


runnable = RunnableLambda(f) | g
as_tool = runnable.as_tool()
as_tool.invoke("b")

Added in version 0.2.14

is_lc_serializable classmethod

is_lc_serializable() -> bool

Is this class serializable?

By design, even if a class inherits from Serializable, it is not serializable by default. This is to prevent accidental serialization of objects that should not be serialized.

Returns:

Type Description
bool

Whether the class is serializable. Default is False.

get_lc_namespace classmethod

get_lc_namespace() -> list[str]

Get the namespace of the langchain object.

For example, if the class is langchain.llms.openai.OpenAI, then the namespace is ["langchain", "llms", "openai"]

Returns:

Type Description
list[str]

The namespace as a list of strings.

lc_id classmethod

lc_id() -> list[str]

Return a unique identifier for this class for serialization purposes.

The unique identifier is a list of strings that describes the path to the object. For example, for the class langchain.llms.openai.OpenAI, the id is ["langchain", "llms", "openai", "OpenAI"].

to_json

to_json() -> (
    SerializedConstructor | SerializedNotImplemented
)

Serialize the Runnable to JSON.

Returns:

Type Description
SerializedConstructor | SerializedNotImplemented

A JSON-serializable representation of the Runnable.

to_json_not_implemented

to_json_not_implemented() -> SerializedNotImplemented

Serialize a "not implemented" object.

Returns:

Type Description
SerializedNotImplemented

SerializedNotImplemented.

configurable_fields

configurable_fields(
    **kwargs: AnyConfigurableField,
) -> RunnableSerializable[Input, Output]

Configure particular Runnable fields at runtime.

Parameters:

Name Type Description Default
**kwargs AnyConfigurableField

A dictionary of ConfigurableField instances to configure.

{}

Raises:

Type Description
ValueError

If a configuration key is not found in the Runnable.

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the fields configured.

from langchain_core.runnables import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatOpenAI(max_tokens=20).configurable_fields(
    max_tokens=ConfigurableField(
        id="output_token_number",
        name="Max tokens in the output",
        description="The maximum number of tokens in the output",
    )
)

# max_tokens = 20
print("max_tokens_20: ", model.invoke("tell me something about chess").content)

# max_tokens = 200
print(
    "max_tokens_200: ",
    model.with_config(configurable={"output_token_number": 200})
    .invoke("tell me something about chess")
    .content,
)

configurable_alternatives

configurable_alternatives(
    which: ConfigurableField,
    *,
    default_key: str = "default",
    prefix_keys: bool = False,
    **kwargs: (
        Runnable[Input, Output]
        | Callable[[], Runnable[Input, Output]]
    )
) -> RunnableSerializable[Input, Output]

Configure alternatives for Runnables that can be set at runtime.

Parameters:

Name Type Description Default
which ConfigurableField

The ConfigurableField instance that will be used to select the alternative.

required
default_key str

The default key to use if no alternative is selected. Defaults to 'default'.

'default'
prefix_keys bool

Whether to prefix the keys with the ConfigurableField id. Defaults to False.

False
**kwargs Runnable[Input, Output] | Callable[[], Runnable[Input, Output]]

A dictionary of keys to Runnable instances or callables that return Runnable instances.

{}

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the alternatives configured.

from langchain_anthropic import ChatAnthropic
from langchain_core.runnables.utils import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatAnthropic(
    model_name="claude-3-7-sonnet-20250219"
).configurable_alternatives(
    ConfigurableField(id="llm"),
    default_key="anthropic",
    openai=ChatOpenAI(),
)

# uses the default model ChatAnthropic
print(model.invoke("which organization created you?").content)

# uses ChatOpenAI
print(
    model.with_config(configurable={"llm": "openai"})
    .invoke("which organization created you?")
    .content
)

set_verbose

set_verbose(verbose: bool | None) -> bool

If verbose is None, set it.

This allows users to pass in None as verbose to access the global setting.

Parameters:

Name Type Description Default
verbose bool | None

The verbosity setting to use.

required

Returns:

Type Description
bool

The verbosity setting to use.

with_structured_output

with_structured_output(
    schema: Dict | type,
    *,
    include_raw: bool = False,
    **kwargs: Any
) -> Runnable[LanguageModelInput, Dict | BaseModel]

Model wrapper that returns outputs formatted to match the given schema.

Parameters:

Name Type Description Default
schema Dict | type

The output schema. Can be passed in as:

  • an OpenAI function/tool schema,
  • a JSON Schema,
  • a TypedDict class,
  • or a Pydantic class.

If schema is a Pydantic class then the model output will be a Pydantic instance of that class, and the model-generated fields will be validated by the Pydantic class. Otherwise the model output will be a dict and will not be validated. See langchain_core.utils.function_calling.convert_to_openai_tool for more on how to properly specify types and descriptions of schema fields when specifying a Pydantic or TypedDict class.

required
include_raw bool

If False then only the parsed structured output is returned. If an error occurs during model output parsing it will be raised. If True then both the raw model response (a BaseMessage) and the parsed model response will be returned. If an error occurs during output parsing it will be caught and returned as well. The final output is always a dict with keys 'raw', 'parsed', and 'parsing_error'.

False

Raises:

Type Description
ValueError

If there are any unsupported kwargs.

NotImplementedError

If the model does not implement with_structured_output().

Returns:

Type Description
Runnable[LanguageModelInput, Dict | BaseModel]

A Runnable that takes same inputs as a langchain_core.language_models.chat.BaseChatModel.

Runnable[LanguageModelInput, Dict | BaseModel]

If include_raw is False and schema is a Pydantic class, Runnable outputs

Runnable[LanguageModelInput, Dict | BaseModel]

an instance of schema (i.e., a Pydantic object).

Runnable[LanguageModelInput, Dict | BaseModel]

Otherwise, if include_raw is False then Runnable outputs a dict.

Runnable[LanguageModelInput, Dict | BaseModel]

If include_raw is True, then Runnable outputs a dict with keys:

Runnable[LanguageModelInput, Dict | BaseModel]
  • 'raw': BaseMessage
Runnable[LanguageModelInput, Dict | BaseModel]
  • 'parsed': None if there was a parsing error, otherwise the type depends on the schema as described above.
Runnable[LanguageModelInput, Dict | BaseModel]
  • 'parsing_error': Optional[BaseException]
Pydantic schema (include_raw=False):

.. code-block:: python

from pydantic import BaseModel


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


llm = ChatModel(model="model-name", temperature=0)
structured_llm = llm.with_structured_output(AnswerWithJustification)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)

# -> AnswerWithJustification(
#     answer='They weigh the same',
#     justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'
# )
Pydantic schema (include_raw=True):

.. code-block:: python

from pydantic import BaseModel


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


llm = ChatModel(model="model-name", temperature=0)
structured_llm = llm.with_structured_output(
    AnswerWithJustification, include_raw=True
)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)
# -> {
#     'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),
#     'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),
#     'parsing_error': None
# }
Dict schema (include_raw=False):

.. code-block:: python

from pydantic import BaseModel
from langchain_core.utils.function_calling import convert_to_openai_tool


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


dict_schema = convert_to_openai_tool(AnswerWithJustification)
llm = ChatModel(model="model-name", temperature=0)
structured_llm = llm.with_structured_output(dict_schema)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)
# -> {
#     'answer': 'They weigh the same',
#     'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
# }

Behavior changed in 0.2.26

Added support for TypedDict class.

get_token_ids

get_token_ids(text: str) -> list[int]

Return the ordered ids of the tokens in a text.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
list[int]

A list of ids corresponding to the tokens in the text, in order they occur

list[int]

in the text.

get_num_tokens

get_num_tokens(text: str) -> int

Get the number of tokens present in the text.

Useful for checking if an input fits in a model's context window.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
int

The integer number of tokens in the text.

get_num_tokens_from_messages

get_num_tokens_from_messages(
    messages: list[BaseMessage],
    tools: Sequence | None = None,
) -> int

Get the number of tokens in the messages.

Useful for checking if an input fits in a model's context window.

Note

The base implementation of get_num_tokens_from_messages ignores tool schemas.

Parameters:

Name Type Description Default
messages list[BaseMessage]

The message inputs to tokenize.

required
tools Sequence | None

If provided, sequence of dict, BaseModel, function, or BaseTools to be converted to tool schemas.

None

Returns:

Type Description
int

The sum of the number of tokens across the messages.

generate

generate(
    messages: list[list[BaseMessage]],
    stop: list[str] | None = None,
    callbacks: Callbacks = None,
    *,
    tags: list[str] | None = None,
    metadata: dict[str, Any] | None = None,
    run_name: str | None = None,
    run_id: UUID | None = None,
    **kwargs: Any
) -> LLMResult

Pass a sequence of prompts to the model and return model generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
messages list[list[BaseMessage]]

List of list of messages.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | None

The tags to apply.

None
metadata dict[str, Any] | None

The metadata to apply.

None
run_name str | None

The name of the run.

None
run_id UUID | None

The ID of the run.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input

LLMResult

prompt and additional model provider-specific output.

agenerate async

agenerate(
    messages: list[list[BaseMessage]],
    stop: list[str] | None = None,
    callbacks: Callbacks = None,
    *,
    tags: list[str] | None = None,
    metadata: dict[str, Any] | None = None,
    run_name: str | None = None,
    run_id: UUID | None = None,
    **kwargs: Any
) -> LLMResult

Asynchronously pass a sequence of prompts to a model and return generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
messages list[list[BaseMessage]]

List of list of messages.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | None

The tags to apply.

None
metadata dict[str, Any] | None

The metadata to apply.

None
run_name str | None

The name of the run.

None
run_id UUID | None

The ID of the run.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input

LLMResult

prompt and additional model provider-specific output.

dict

dict(**kwargs: Any) -> dict

Return a dictionary of the LLM.

bind_tools

bind_tools(
    tools: Sequence[
        Dict[str, Any] | type | Callable | BaseTool
    ],
    *,
    tool_choice: str | None = None,
    **kwargs: Any
) -> Runnable[LanguageModelInput, AIMessage]

Bind tools to the model.

Parameters:

Name Type Description Default
tools Sequence[Dict[str, Any] | type | Callable | BaseTool]

Sequence of tools to bind to the model.

required
tool_choice str | None

The tool to use. If "any" then any tool can be used.

None

Returns:

Type Description
Runnable[LanguageModelInput, AIMessage]

A Runnable that returns a message.

validate_environment

validate_environment() -> Self

Validate that llama-cpp-python library is installed.

__init__

__init__(
    *, model_name: Optional[str] = None, **kwargs: Any
) -> None

Needed for mypy typing to recognize model_name as a valid arg.

GemmaChatVertexAIModelGarden

Bases: _GemmaBase, _BaseVertexAIModelGarden, BaseChatModel

Methods:

Name Description
get_name

Get the name of the Runnable.

get_input_schema

Get a pydantic model that can be used to validate input to the Runnable.

get_input_jsonschema

Get a JSON schema that represents the input to the Runnable.

get_output_schema

Get a pydantic model that can be used to validate output to the Runnable.

get_output_jsonschema

Get a JSON schema that represents the output of the Runnable.

config_schema

The type of config this Runnable accepts specified as a pydantic model.

get_config_jsonschema

Get a JSON schema that represents the config of the Runnable.

get_graph

Return a graph representation of this Runnable.

get_prompts

Return a list of prompts used by this Runnable.

__or__

Runnable "or" operator.

__ror__

Runnable "reverse-or" operator.

pipe

Pipe runnables.

pick

Pick keys from the output dict of this Runnable.

assign

Assigns new fields to the dict output of this Runnable.

batch

Default implementation runs invoke in parallel using a thread pool executor.

batch_as_completed

Run invoke in parallel on a list of inputs.

abatch

Default implementation runs ainvoke in parallel using asyncio.gather.

abatch_as_completed

Run ainvoke in parallel on a list of inputs.

astream_log

Stream all output from a Runnable, as reported to the callback system.

astream_events

Generate a stream of events.

transform

Transform inputs to outputs.

atransform

Transform inputs to outputs.

bind

Bind arguments to a Runnable, returning a new Runnable.

with_config

Bind config to a Runnable, returning a new Runnable.

with_listeners

Bind lifecycle listeners to a Runnable, returning a new Runnable.

with_alisteners

Bind async lifecycle listeners to a Runnable.

with_types

Bind input and output types to a Runnable, returning a new Runnable.

with_retry

Create a new Runnable that retries the original Runnable on exceptions.

map

Return a new Runnable that maps a list of inputs to a list of outputs.

with_fallbacks

Add fallbacks to a Runnable, returning a new Runnable.

as_tool

Create a BaseTool from a Runnable.

is_lc_serializable

Is this class serializable?

get_lc_namespace

Get the namespace of the langchain object.

lc_id

Return a unique identifier for this class for serialization purposes.

to_json

Serialize the Runnable to JSON.

to_json_not_implemented

Serialize a "not implemented" object.

configurable_fields

Configure particular Runnable fields at runtime.

configurable_alternatives

Configure alternatives for Runnables that can be set at runtime.

set_verbose

If verbose is None, set it.

with_structured_output

Model wrapper that returns outputs formatted to match the given schema.

get_token_ids

Return the ordered ids of the tokens in a text.

get_num_tokens

Get the number of tokens present in the text.

get_num_tokens_from_messages

Get the number of tokens in the messages.

generate

Pass a sequence of prompts to the model and return model generations.

agenerate

Asynchronously pass a sequence of prompts to a model and return generations.

dict

Return a dictionary of the LLM.

bind_tools

Bind tools to the model.

validate_environment

Validate that the python package exists in environment.

__init__

Needed for mypy typing to recognize model_name as a valid arg.

Attributes:

Name Type Description
InputType TypeAlias

Get the input type for this runnable.

OutputType Any

Get the output type for this runnable.

input_schema type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema type[BaseModel]

Output schema.

config_specs list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets dict[str, str]

A map of constructor argument names to secret ids.

lc_attributes dict

List of attribute names that should be included in the serialized kwargs.

cache BaseCache | bool | None

Whether to cache the response.

verbose bool

Whether to print out response text.

callbacks Callbacks

Callbacks to add to the run trace.

tags list[str] | None

Tags to add to the run trace.

metadata dict[str, Any] | None

Metadata to add to the run trace.

custom_get_token_ids Callable[[str], list[int]] | None

Optional encoder to use for counting tokens.

rate_limiter BaseRateLimiter | None

An optional rate limiter to use for limiting the number of requests.

disable_streaming bool | Literal['tool_calling']

Whether to disable streaming for this model.

output_version str | None

Version of AIMessage output format to store in message content.

project Optional[str]

The default GCP project to use when making Vertex API calls.

location str

The default location to use when making API calls.

request_parallelism int

The amount of parallelism allowed for requests issued to VertexAI models.

max_retries int

The maximum number of retries to make when generating.

stop Optional[List[str]]

Optional list of stop words to use when generating.

model_name Optional[str]

Underlying model name.

full_model_name Optional[str]

The full name of the model's endpoint.

api_endpoint Optional[str]

Desired API endpoint, e.g., us-central1-aiplatform.googleapis.com

api_transport Optional[str]

The desired API transport method, can be either 'grpc' or 'rest'.

additional_headers Optional[Dict[str, str]]

A key-value dictionary representing additional headers for the model call

client_cert_source Optional[Callable[[], Tuple[bytes, bytes]]]

A callback which returns client certificate bytes and private key bytes both

credentials Any

The default custom credentials (google.auth.credentials.Credentials) to use

endpoint_version Literal['v1', 'v1beta1']

Whether to use v1 or v1beta1 endpoint.

prediction_client Union[PredictionServiceClient, PredictionServiceClient]

Returns PredictionServiceClient.

async_prediction_client Union[PredictionServiceAsyncClient, PredictionServiceAsyncClient]

Returns PredictionServiceClient.

endpoint_id str

A name of an endpoint where the model has been deployed.

result_arg Optional[str]

Set result_arg to None if output of the model is expected to be a string.

single_example_per_request bool

LLM endpoint currently serves only the first example in the request

max_tokens Optional[int]

The maximum number of tokens to generate.

temperature Optional[float]

The temperature to use for sampling.

top_p Optional[float]

The top-p value to use for sampling.

top_k Optional[int]

The top-k value to use for sampling.

parse_response bool

Whether to post-process the chat response and clean repeations

InputType property

InputType: TypeAlias

Get the input type for this runnable.

OutputType property

OutputType: Any

Get the output type for this runnable.

input_schema property

input_schema: type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema property

output_schema: type[BaseModel]

Output schema.

The type of output this Runnable produces specified as a pydantic model.

config_specs property

config_specs: list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets property

lc_secrets: dict[str, str]

A map of constructor argument names to secret ids.

For example,

lc_attributes property

lc_attributes: dict

List of attribute names that should be included in the serialized kwargs.

These attributes must be accepted by the constructor. Default is an empty dictionary.

cache class-attribute instance-attribute

cache: BaseCache | bool | None = Field(
    default=None, exclude=True
)

Whether to cache the response.

  • If true, will use the global cache.
  • If false, will not use a cache
  • If None, will use the global cache if it's set, otherwise no cache.
  • If instance of BaseCache, will use the provided cache.

Caching is not currently supported for streaming methods of models.

verbose class-attribute instance-attribute

verbose: bool = Field(
    default_factory=_get_verbosity, exclude=True, repr=False
)

Whether to print out response text.

callbacks class-attribute instance-attribute

callbacks: Callbacks = Field(default=None, exclude=True)

Callbacks to add to the run trace.

tags class-attribute instance-attribute

tags: list[str] | None = Field(default=None, exclude=True)

Tags to add to the run trace.

metadata class-attribute instance-attribute

metadata: dict[str, Any] | None = Field(
    default=None, exclude=True
)

Metadata to add to the run trace.

custom_get_token_ids class-attribute instance-attribute

custom_get_token_ids: Callable[[str], list[int]] | None = (
    Field(default=None, exclude=True)
)

Optional encoder to use for counting tokens.

rate_limiter class-attribute instance-attribute

rate_limiter: BaseRateLimiter | None = Field(
    default=None, exclude=True
)

An optional rate limiter to use for limiting the number of requests.

disable_streaming class-attribute instance-attribute

disable_streaming: bool | Literal['tool_calling'] = False

Whether to disable streaming for this model.

If streaming is bypassed, then stream()/astream()/astream_events() will defer to invoke()/ainvoke().

  • If True, will always bypass streaming case.
  • If 'tool_calling', will bypass streaming case only when the model is called with a tools keyword argument. In other words, LangChain will automatically switch to non-streaming behavior (invoke()) only when the tools argument is provided. This offers the best of both worlds.
  • If False (default), will always use streaming case if available.

The main reason for this flag is that code might be written using stream() and a user may want to swap out a given model for another model whose the implementation does not properly support streaming.

output_version class-attribute instance-attribute

output_version: str | None = Field(
    default_factory=from_env(
        "LC_OUTPUT_VERSION", default=None
    )
)

Version of AIMessage output format to store in message content.

AIMessage.content_blocks will lazily parse the contents of content into a standard format. This flag can be used to additionally store the standard format in message content, e.g., for serialization purposes.

Supported values:

  • "v0": provider-specific format in content (can lazily-parse with .content_blocks)
  • "v1": standardized format in content (consistent with .content_blocks)

Partner packages (e.g., langchain-openai) can also use this field to roll out new content formats in a backward-compatible way.

Added in version 1.0

project class-attribute instance-attribute

project: Optional[str] = None

The default GCP project to use when making Vertex API calls.

location class-attribute instance-attribute

location: str = Field(default=_DEFAULT_LOCATION)

The default location to use when making API calls.

request_parallelism class-attribute instance-attribute

request_parallelism: int = 5

The amount of parallelism allowed for requests issued to VertexAI models.

max_retries class-attribute instance-attribute

max_retries: int = 6

The maximum number of retries to make when generating.

stop class-attribute instance-attribute

stop: Optional[List[str]] = Field(
    default=None, alias="stop_sequences"
)

Optional list of stop words to use when generating.

model_name class-attribute instance-attribute

model_name: Optional[str] = Field(
    default=None, alias="model"
)

Underlying model name.

full_model_name class-attribute instance-attribute

full_model_name: Optional[str] = Field(
    default=None, exclude=True
)

The full name of the model's endpoint.

api_endpoint class-attribute instance-attribute

api_endpoint: Optional[str] = Field(
    default=None, alias="base_url"
)

Desired API endpoint, e.g., us-central1-aiplatform.googleapis.com

api_transport class-attribute instance-attribute

api_transport: Optional[str] = None

The desired API transport method, can be either 'grpc' or 'rest'. Uses the default parameter in vertexai.init if defined.

additional_headers class-attribute instance-attribute

additional_headers: Optional[Dict[str, str]] = Field(
    default=None
)

A key-value dictionary representing additional headers for the model call

client_cert_source class-attribute instance-attribute

client_cert_source: Optional[
    Callable[[], Tuple[bytes, bytes]]
] = None

A callback which returns client certificate bytes and private key bytes both

credentials class-attribute instance-attribute

credentials: Any = Field(default=None, exclude=True)

The default custom credentials (google.auth.credentials.Credentials) to use

endpoint_version class-attribute instance-attribute

endpoint_version: Literal['v1', 'v1beta1'] = 'v1beta1'

Whether to use v1 or v1beta1 endpoint.

v1 is more performant, but v1beta1 might have some new features.

prediction_client property

prediction_client: Union[
    PredictionServiceClient, PredictionServiceClient
]

Returns PredictionServiceClient.

async_prediction_client property

async_prediction_client: Union[
    PredictionServiceAsyncClient,
    PredictionServiceAsyncClient,
]

Returns PredictionServiceClient.

endpoint_id instance-attribute

endpoint_id: str

A name of an endpoint where the model has been deployed.

result_arg class-attribute instance-attribute

result_arg: Optional[str] = 'generated_text'

Set result_arg to None if output of the model is expected to be a string.

single_example_per_request class-attribute instance-attribute

single_example_per_request: bool = True

LLM endpoint currently serves only the first example in the request

max_tokens class-attribute instance-attribute

max_tokens: Optional[int] = None

The maximum number of tokens to generate.

temperature class-attribute instance-attribute

temperature: Optional[float] = None

The temperature to use for sampling.

top_p class-attribute instance-attribute

top_p: Optional[float] = None

The top-p value to use for sampling.

top_k class-attribute instance-attribute

top_k: Optional[int] = None

The top-k value to use for sampling.

parse_response class-attribute instance-attribute

parse_response: bool = False

Whether to post-process the chat response and clean repeations

get_name

get_name(
    suffix: str | None = None, *, name: str | None = None
) -> str

Get the name of the Runnable.

Parameters:

Name Type Description Default
suffix str | None

An optional suffix to append to the name.

None
name str | None

An optional name to use instead of the Runnable's name.

None

Returns:

Type Description
str

The name of the Runnable.

get_input_schema

get_input_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate input to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic input schema that depends on which configuration the Runnable is invoked with.

This method allows to get an input schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate input.

get_input_jsonschema

get_input_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the input to the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the input to the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_input_jsonschema())

Added in version 0.3.0

get_output_schema

get_output_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate output to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic output schema that depends on which configuration the Runnable is invoked with.

This method allows to get an output schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate output.

get_output_jsonschema

get_output_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the output of the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the output of the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_output_jsonschema())

Added in version 0.3.0

config_schema

config_schema(
    *, include: Sequence[str] | None = None
) -> type[BaseModel]

The type of config this Runnable accepts specified as a pydantic model.

To mark a field as configurable, see the configurable_fields and configurable_alternatives methods.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate config.

get_config_jsonschema

get_config_jsonschema(
    *, include: Sequence[str] | None = None
) -> dict[str, Any]

Get a JSON schema that represents the config of the Runnable.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the config of the Runnable.

Added in version 0.3.0

get_graph

get_graph(config: RunnableConfig | None = None) -> Graph

Return a graph representation of this Runnable.

get_prompts

get_prompts(
    config: RunnableConfig | None = None,
) -> list[BasePromptTemplate]

Return a list of prompts used by this Runnable.

__or__

__or__(
    other: (
        Runnable[Any, Other]
        | Callable[[Iterator[Any]], Iterator[Other]]
        | Callable[
            [AsyncIterator[Any]], AsyncIterator[Other]
        ]
        | Callable[[Any], Other]
        | Mapping[
            str,
            Runnable[Any, Other]
            | Callable[[Any], Other]
            | Any,
        ]
    ),
) -> RunnableSerializable[Input, Other]

Runnable "or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Any, Other] | Callable[[Iterator[Any]], Iterator[Other]] | Callable[[AsyncIterator[Any]], AsyncIterator[Other]] | Callable[[Any], Other] | Mapping[str, Runnable[Any, Other] | Callable[[Any], Other] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

__ror__

__ror__(
    other: (
        Runnable[Other, Any]
        | Callable[[Iterator[Other]], Iterator[Any]]
        | Callable[
            [AsyncIterator[Other]], AsyncIterator[Any]
        ]
        | Callable[[Other], Any]
        | Mapping[
            str,
            Runnable[Other, Any]
            | Callable[[Other], Any]
            | Any,
        ]
    ),
) -> RunnableSerializable[Other, Output]

Runnable "reverse-or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Other, Any] | Callable[[Iterator[Other]], Iterator[Any]] | Callable[[AsyncIterator[Other]], AsyncIterator[Any]] | Callable[[Other], Any] | Mapping[str, Runnable[Other, Any] | Callable[[Other], Any] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Other, Output]

A new Runnable.

pipe

pipe(
    *others: Runnable[Any, Other] | Callable[[Any], Other],
    name: str | None = None
) -> RunnableSerializable[Input, Other]

Pipe runnables.

Compose this Runnable with Runnable-like objects to make a RunnableSequence.

Equivalent to RunnableSequence(self, *others) or self | others[0] | ...

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


def mul_two(x: int) -> int:
    return x * 2


runnable_1 = RunnableLambda(add_one)
runnable_2 = RunnableLambda(mul_two)
sequence = runnable_1.pipe(runnable_2)
# Or equivalently:
# sequence = runnable_1 | runnable_2
# sequence = RunnableSequence(first=runnable_1, last=runnable_2)
sequence.invoke(1)
await sequence.ainvoke(1)
# -> 4

sequence.batch([1, 2, 3])
await sequence.abatch([1, 2, 3])
# -> [4, 6, 8]

Parameters:

Name Type Description Default
*others Runnable[Any, Other] | Callable[[Any], Other]

Other Runnable or Runnable-like objects to compose

()
name str | None

An optional name for the resulting RunnableSequence.

None

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

pick

pick(
    keys: str | list[str],
) -> RunnableSerializable[Any, Any]

Pick keys from the output dict of this Runnable.

Pick single key:

```python
import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)
chain = RunnableMap(str=as_str, json=as_json)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3]}

json_only_chain = chain.pick("json")
json_only_chain.invoke("[1, 2, 3]")
# -> [1, 2, 3]
```

Pick list of keys:

```python
from typing import Any

import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)


def as_bytes(x: Any) -> bytes:
    return bytes(x, "utf-8")


chain = RunnableMap(
    str=as_str, json=as_json, bytes=RunnableLambda(as_bytes)
)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3], "bytes": b"[1, 2, 3]"}

json_and_bytes_chain = chain.pick(["json", "bytes"])
json_and_bytes_chain.invoke("[1, 2, 3]")
# -> {"json": [1, 2, 3], "bytes": b"[1, 2, 3]"}
```

Parameters:

Name Type Description Default
keys str | list[str]

A key or list of keys to pick from the output dict.

required

Returns:

Type Description
RunnableSerializable[Any, Any]

a new Runnable.

assign

assign(
    **kwargs: (
        Runnable[dict[str, Any], Any]
        | Callable[[dict[str, Any]], Any]
        | Mapping[
            str,
            Runnable[dict[str, Any], Any]
            | Callable[[dict[str, Any]], Any],
        ]
    ),
) -> RunnableSerializable[Any, Any]

Assigns new fields to the dict output of this Runnable.

from langchain_community.llms.fake import FakeStreamingListLLM
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import SystemMessagePromptTemplate
from langchain_core.runnables import Runnable
from operator import itemgetter

prompt = (
    SystemMessagePromptTemplate.from_template("You are a nice assistant.")
    + "{question}"
)
llm = FakeStreamingListLLM(responses=["foo-lish"])

chain: Runnable = prompt | llm | {"str": StrOutputParser()}

chain_with_assign = chain.assign(hello=itemgetter("str") | llm)

print(chain_with_assign.input_schema.model_json_schema())
# {'title': 'PromptInput', 'type': 'object', 'properties':
{'question': {'title': 'Question', 'type': 'string'}}}
print(chain_with_assign.output_schema.model_json_schema())
# {'title': 'RunnableSequenceOutput', 'type': 'object', 'properties':
{'str': {'title': 'Str',
'type': 'string'}, 'hello': {'title': 'Hello', 'type': 'string'}}}

Parameters:

Name Type Description Default
**kwargs Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any] | Mapping[str, Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any]]

A mapping of keys to Runnable or Runnable-like objects that will be invoked with the entire output dict of this Runnable.

{}

Returns:

Type Description
RunnableSerializable[Any, Any]

A new Runnable.

batch

batch(
    inputs: list[Input],
    config: (
        RunnableConfig | list[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> list[Output]

Default implementation runs invoke in parallel using a thread pool executor.

The default implementation of batch works well for IO bound runnables.

Subclasses should override this method if they can batch more efficiently; e.g., if the underlying Runnable uses an API which supports a batch mode.

Parameters:

Name Type Description Default
inputs list[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | list[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
list[Output]

A list of outputs from the Runnable.

batch_as_completed

batch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> Iterator[tuple[int, Output | Exception]]

Run invoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
tuple[int, Output | Exception]

Tuples of the index of the input and the output from the Runnable.

abatch async

abatch(
    inputs: list[Input],
    config: (
        RunnableConfig | list[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> list[Output]

Default implementation runs ainvoke in parallel using asyncio.gather.

The default implementation of batch works well for IO bound runnables.

Subclasses should override this method if they can batch more efficiently; e.g., if the underlying Runnable uses an API which supports a batch mode.

Parameters:

Name Type Description Default
inputs list[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | list[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
list[Output]

A list of outputs from the Runnable.

abatch_as_completed async

abatch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> AsyncIterator[tuple[int, Output | Exception]]

Run ainvoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[tuple[int, Output | Exception]]

A tuple of the index of the input and the output from the Runnable.

astream_log async

astream_log(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    diff: bool = True,
    with_streamed_output_list: bool = True,
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

Stream all output from a Runnable, as reported to the callback system.

This includes all inner runs of LLMs, Retrievers, Tools, etc.

Output is streamed as Log objects, which include a list of Jsonpatch ops that describe how the state of the run has changed in each step, and the final state of the run.

The Jsonpatch ops can be applied in order to construct state.

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
diff bool

Whether to yield diffs between each step or the current state.

True
with_streamed_output_list bool

Whether to yield the streamed_output list.

True
include_names Sequence[str] | None

Only include logs with these names.

None
include_types Sequence[str] | None

Only include logs with these types.

None
include_tags Sequence[str] | None

Only include logs with these tags.

None
exclude_names Sequence[str] | None

Exclude logs with these names.

None
exclude_types Sequence[str] | None

Exclude logs with these types.

None
exclude_tags Sequence[str] | None

Exclude logs with these tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

A RunLogPatch or RunLog object.

astream_events async

astream_events(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    version: Literal["v1", "v2"] = "v2",
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[StreamEvent]

Generate a stream of events.

Use to create an iterator over StreamEvents that provide real-time information about the progress of the Runnable, including StreamEvents from intermediate results.

A StreamEvent is a dictionary with the following schema:

  • event: str - Event names are of the format: on_[runnable_type]_(start|stream|end).
  • name: str - The name of the Runnable that generated the event.
  • run_id: str - randomly generated ID associated with the given execution of the Runnable that emitted the event. A child Runnable that gets invoked as part of the execution of a parent Runnable is assigned its own unique ID.
  • parent_ids: list[str] - The IDs of the parent runnables that generated the event. The root Runnable will have an empty list. The order of the parent IDs is from the root to the immediate parent. Only available for v2 version of the API. The v1 version of the API will return an empty list.
  • tags: Optional[list[str]] - The tags of the Runnable that generated the event.
  • metadata: Optional[dict[str, Any]] - The metadata of the Runnable that generated the event.
  • data: dict[str, Any]

Below is a table that illustrates some events that might be emitted by various chains. Metadata fields have been omitted from the table for brevity. Chain definitions have been included after the table.

Note

This reference table is for the v2 version of the schema.

+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | event | name | chunk | input | output | +==========================+==================+=====================================+===================================================+=====================================================+ | on_chat_model_start | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_stream | [model name] | AIMessageChunk(content="hello") | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_end | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | AIMessageChunk(content="hello world") | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_start | [model name] | | {'input': 'hello'} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_stream | [model name] | 'Hello' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_end | [model name] | | 'Hello human!' | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_start | format_docs | | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_stream | format_docs | 'hello world!, goodbye world!' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_end | format_docs | | [Document(...)] | 'hello world!, goodbye world!' | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_start | some_tool | | {"x": 1, "y": "2"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_end | some_tool | | | {"x": 1, "y": "2"} | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_start | [retriever name] | | {"query": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_end | [retriever name] | | {"query": "hello"} | [Document(...), ..] | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_start | [template_name] | | {"question": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_end | [template_name] | | {"question": "hello"} | ChatPromptValue(messages: [SystemMessage, ...]) | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+

In addition to the standard events, users can also dispatch custom events (see example below).

Custom events will be only be surfaced with in the v2 version of the API!

A custom event has following format:

+-----------+------+-----------------------------------------------------------------------------------------------------------+ | Attribute | Type | Description | +===========+======+===========================================================================================================+ | name | str | A user defined name for the event. | +-----------+------+-----------------------------------------------------------------------------------------------------------+ | data | Any | The data associated with the event. This can be anything, though we suggest making it JSON serializable. | +-----------+------+-----------------------------------------------------------------------------------------------------------+

Here are declarations associated with the standard events shown above:

format_docs:

def format_docs(docs: list[Document]) -> str:
    '''Format the docs.'''
    return ", ".join([doc.page_content for doc in docs])


format_docs = RunnableLambda(format_docs)

some_tool:

@tool
def some_tool(x: int, y: str) -> dict:
    '''Some_tool.'''
    return {"x": x, "y": y}

prompt:

template = ChatPromptTemplate.from_messages(
    [
        ("system", "You are Cat Agent 007"),
        ("human", "{question}"),
    ]
).with_config({"run_name": "my_template", "tags": ["my_template"]})
Example:

from langchain_core.runnables import RunnableLambda


async def reverse(s: str) -> str:
    return s[::-1]


chain = RunnableLambda(func=reverse)

events = [event async for event in chain.astream_events("hello", version="v2")]

# will produce the following events (run_id, and parent_ids
# has been omitted for brevity):
[
    {
        "data": {"input": "hello"},
        "event": "on_chain_start",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"chunk": "olleh"},
        "event": "on_chain_stream",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"output": "olleh"},
        "event": "on_chain_end",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
]

Example: Dispatch Custom Event

from langchain_core.callbacks.manager import (
    adispatch_custom_event,
)
from langchain_core.runnables import RunnableLambda, RunnableConfig
import asyncio


async def slow_thing(some_input: str, config: RunnableConfig) -> str:
    """Do something that takes a long time."""
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 1 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 2 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    return "Done"

slow_thing = RunnableLambda(slow_thing)

async for event in slow_thing.astream_events("some_input", version="v2"):
    print(event)

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
version Literal['v1', 'v2']

The version of the schema to use either 'v2' or 'v1'. Users should use 'v2'. 'v1' is for backwards compatibility and will be deprecated in 0.4.0. No default will be assigned until the API is stabilized. custom events will only be surfaced in 'v2'.

'v2'
include_names Sequence[str] | None

Only include events from Runnables with matching names.

None
include_types Sequence[str] | None

Only include events from Runnables with matching types.

None
include_tags Sequence[str] | None

Only include events from Runnables with matching tags.

None
exclude_names Sequence[str] | None

Exclude events from Runnables with matching names.

None
exclude_types Sequence[str] | None

Exclude events from Runnables with matching types.

None
exclude_tags Sequence[str] | None

Exclude events from Runnables with matching tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable. These will be passed to astream_log as this implementation of astream_events is built on top of astream_log.

{}

Yields:

Type Description
AsyncIterator[StreamEvent]

An async stream of StreamEvents.

Raises:

Type Description
NotImplementedError

If the version is not 'v1' or 'v2'.

transform

transform(
    input: Iterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> Iterator[Output]

Transform inputs to outputs.

Default implementation of transform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input Iterator[Input]

An iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
Output

The output of the Runnable.

atransform async

atransform(
    input: AsyncIterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> AsyncIterator[Output]

Transform inputs to outputs.

Default implementation of atransform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input AsyncIterator[Input]

An async iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[Output]

The output of the Runnable.

bind

bind(**kwargs: Any) -> Runnable[Input, Output]

Bind arguments to a Runnable, returning a new Runnable.

Useful when a Runnable in a chain requires an argument that is not in the output of the previous Runnable or included in the user input.

Parameters:

Name Type Description Default
kwargs Any

The arguments to bind to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the arguments bound.

Example
from langchain_ollama import ChatOllama
from langchain_core.output_parsers import StrOutputParser

llm = ChatOllama(model="llama3.1")

# Without bind.
chain = llm | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two three four five.'

# With bind.
chain = llm.bind(stop=["three"]) | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two'

with_config

with_config(
    config: RunnableConfig | None = None, **kwargs: Any
) -> Runnable[Input, Output]

Bind config to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

The config to bind to the Runnable.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the config bound.

with_listeners

with_listeners(
    *,
    on_start: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_end: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_error: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None
) -> Runnable[Input, Output]

Bind lifecycle listeners to a Runnable, returning a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called before the Runnable starts running, with the Run object. Defaults to None.

None
on_end Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda
from langchain_core.tracers.schemas import Run

import time


def test_runnable(time_to_sleep: int):
    time.sleep(time_to_sleep)


def fn_start(run_obj: Run):
    print("start_time:", run_obj.start_time)


def fn_end(run_obj: Run):
    print("end_time:", run_obj.end_time)


chain = RunnableLambda(test_runnable).with_listeners(
    on_start=fn_start, on_end=fn_end
)
chain.invoke(2)

with_alisteners

with_alisteners(
    *,
    on_start: AsyncListener | None = None,
    on_end: AsyncListener | None = None,
    on_error: AsyncListener | None = None
) -> Runnable[Input, Output]

Bind async lifecycle listeners to a Runnable.

Returns a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start AsyncListener | None

Called asynchronously before the Runnable starts running, with the Run object. Defaults to None.

None
on_end AsyncListener | None

Called asynchronously after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error AsyncListener | None

Called asynchronously if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda, Runnable
from datetime import datetime, timezone
import time
import asyncio

def format_t(timestamp: float) -> str:
    return datetime.fromtimestamp(timestamp, tz=timezone.utc).isoformat()

async def test_runnable(time_to_sleep: int):
    print(f"Runnable[{time_to_sleep}s]: starts at {format_t(time.time())}")
    await asyncio.sleep(time_to_sleep)
    print(f"Runnable[{time_to_sleep}s]: ends at {format_t(time.time())}")

async def fn_start(run_obj: Runnable):
    print(f"on start callback starts at {format_t(time.time())}")
    await asyncio.sleep(3)
    print(f"on start callback ends at {format_t(time.time())}")

async def fn_end(run_obj: Runnable):
    print(f"on end callback starts at {format_t(time.time())}")
    await asyncio.sleep(2)
    print(f"on end callback ends at {format_t(time.time())}")

runnable = RunnableLambda(test_runnable).with_alisteners(
    on_start=fn_start,
    on_end=fn_end
)
async def concurrent_runs():
    await asyncio.gather(runnable.ainvoke(2), runnable.ainvoke(3))

asyncio.run(concurrent_runs())
Result:
on start callback starts at 2025-03-01T07:05:22.875378+00:00
on start callback starts at 2025-03-01T07:05:22.875495+00:00
on start callback ends at 2025-03-01T07:05:25.878862+00:00
on start callback ends at 2025-03-01T07:05:25.878947+00:00
Runnable[2s]: starts at 2025-03-01T07:05:25.879392+00:00
Runnable[3s]: starts at 2025-03-01T07:05:25.879804+00:00
Runnable[2s]: ends at 2025-03-01T07:05:27.881998+00:00
on end callback starts at 2025-03-01T07:05:27.882360+00:00
Runnable[3s]: ends at 2025-03-01T07:05:28.881737+00:00
on end callback starts at 2025-03-01T07:05:28.882428+00:00
on end callback ends at 2025-03-01T07:05:29.883893+00:00
on end callback ends at 2025-03-01T07:05:30.884831+00:00

with_types

with_types(
    *,
    input_type: type[Input] | None = None,
    output_type: type[Output] | None = None
) -> Runnable[Input, Output]

Bind input and output types to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
input_type type[Input] | None

The input type to bind to the Runnable. Defaults to None.

None
output_type type[Output] | None

The output type to bind to the Runnable. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the types bound.

with_retry

with_retry(
    *,
    retry_if_exception_type: tuple[
        type[BaseException], ...
    ] = (Exception,),
    wait_exponential_jitter: bool = True,
    exponential_jitter_params: (
        ExponentialJitterParams | None
    ) = None,
    stop_after_attempt: int = 3
) -> Runnable[Input, Output]

Create a new Runnable that retries the original Runnable on exceptions.

Parameters:

Name Type Description Default
retry_if_exception_type tuple[type[BaseException], ...]

A tuple of exception types to retry on. Defaults to (Exception,).

(Exception,)
wait_exponential_jitter bool

Whether to add jitter to the wait time between retries. Defaults to True.

True
stop_after_attempt int

The maximum number of attempts to make before giving up. Defaults to 3.

3
exponential_jitter_params ExponentialJitterParams | None

Parameters for tenacity.wait_exponential_jitter. Namely: initial, max, exp_base, and jitter (all float values).

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable that retries the original Runnable on exceptions.

Example
from langchain_core.runnables import RunnableLambda

count = 0


def _lambda(x: int) -> None:
    global count
    count = count + 1
    if x == 1:
        raise ValueError("x is 1")
    else:
        pass


runnable = RunnableLambda(_lambda)
try:
    runnable.with_retry(
        stop_after_attempt=2,
        retry_if_exception_type=(ValueError,),
    ).invoke(1)
except ValueError:
    pass

assert count == 2

map

map() -> Runnable[list[Input], list[Output]]

Return a new Runnable that maps a list of inputs to a list of outputs.

Calls invoke with each input.

Returns:

Type Description
Runnable[list[Input], list[Output]]

A new Runnable that maps a list of inputs to a list of outputs.

Example
from langchain_core.runnables import RunnableLambda


def _lambda(x: int) -> int:
    return x + 1


runnable = RunnableLambda(_lambda)
print(runnable.map().invoke([1, 2, 3]))  # [2, 3, 4]

with_fallbacks

with_fallbacks(
    fallbacks: Sequence[Runnable[Input, Output]],
    *,
    exceptions_to_handle: tuple[
        type[BaseException], ...
    ] = (Exception,),
    exception_key: str | None = None
) -> RunnableWithFallbacks[Input, Output]

Add fallbacks to a Runnable, returning a new Runnable.

The new Runnable will try the original Runnable, and then each fallback in order, upon failures.

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle. Defaults to (Exception,).

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input. Defaults to None.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

Example
from typing import Iterator

from langchain_core.runnables import RunnableGenerator


def _generate_immediate_error(input: Iterator) -> Iterator[str]:
    raise ValueError()
    yield ""


def _generate(input: Iterator) -> Iterator[str]:
    yield from "foo bar"


runnable = RunnableGenerator(_generate_immediate_error).with_fallbacks(
    [RunnableGenerator(_generate)]
)
print("".join(runnable.stream({})))  # foo bar

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle.

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

as_tool

as_tool(
    args_schema: type[BaseModel] | None = None,
    *,
    name: str | None = None,
    description: str | None = None,
    arg_types: dict[str, type] | None = None
) -> BaseTool

Create a BaseTool from a Runnable.

as_tool will instantiate a BaseTool with a name, description, and args_schema from a Runnable. Where possible, schemas are inferred from runnable.get_input_schema. Alternatively (e.g., if the Runnable takes a dict as input and the specific dict keys are not typed), the schema can be specified directly with args_schema. You can also pass arg_types to just specify the required arguments and their types.

Parameters:

Name Type Description Default
args_schema type[BaseModel] | None

The schema for the tool. Defaults to None.

None
name str | None

The name of the tool. Defaults to None.

None
description str | None

The description of the tool. Defaults to None.

None
arg_types dict[str, type] | None

A dictionary of argument names to types. Defaults to None.

None

Returns:

Type Description
BaseTool

A BaseTool instance.

Typed dict input:

from typing_extensions import TypedDict
from langchain_core.runnables import RunnableLambda


class Args(TypedDict):
    a: int
    b: list[int]


def f(x: Args) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool()
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via args_schema:

from typing import Any
from pydantic import BaseModel, Field
from langchain_core.runnables import RunnableLambda

def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))

class FSchema(BaseModel):
    """Apply a function to an integer and list of integers."""

    a: int = Field(..., description="Integer")
    b: list[int] = Field(..., description="List of ints")

runnable = RunnableLambda(f)
as_tool = runnable.as_tool(FSchema)
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via arg_types:

from typing import Any
from langchain_core.runnables import RunnableLambda


def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool(arg_types={"a": int, "b": list[int]})
as_tool.invoke({"a": 3, "b": [1, 2]})

String input:

from langchain_core.runnables import RunnableLambda


def f(x: str) -> str:
    return x + "a"


def g(x: str) -> str:
    return x + "z"


runnable = RunnableLambda(f) | g
as_tool = runnable.as_tool()
as_tool.invoke("b")

Added in version 0.2.14

is_lc_serializable classmethod

is_lc_serializable() -> bool

Is this class serializable?

By design, even if a class inherits from Serializable, it is not serializable by default. This is to prevent accidental serialization of objects that should not be serialized.

Returns:

Type Description
bool

Whether the class is serializable. Default is False.

get_lc_namespace classmethod

get_lc_namespace() -> list[str]

Get the namespace of the langchain object.

For example, if the class is langchain.llms.openai.OpenAI, then the namespace is ["langchain", "llms", "openai"]

Returns:

Type Description
list[str]

The namespace as a list of strings.

lc_id classmethod

lc_id() -> list[str]

Return a unique identifier for this class for serialization purposes.

The unique identifier is a list of strings that describes the path to the object. For example, for the class langchain.llms.openai.OpenAI, the id is ["langchain", "llms", "openai", "OpenAI"].

to_json

to_json() -> (
    SerializedConstructor | SerializedNotImplemented
)

Serialize the Runnable to JSON.

Returns:

Type Description
SerializedConstructor | SerializedNotImplemented

A JSON-serializable representation of the Runnable.

to_json_not_implemented

to_json_not_implemented() -> SerializedNotImplemented

Serialize a "not implemented" object.

Returns:

Type Description
SerializedNotImplemented

SerializedNotImplemented.

configurable_fields

configurable_fields(
    **kwargs: AnyConfigurableField,
) -> RunnableSerializable[Input, Output]

Configure particular Runnable fields at runtime.

Parameters:

Name Type Description Default
**kwargs AnyConfigurableField

A dictionary of ConfigurableField instances to configure.

{}

Raises:

Type Description
ValueError

If a configuration key is not found in the Runnable.

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the fields configured.

from langchain_core.runnables import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatOpenAI(max_tokens=20).configurable_fields(
    max_tokens=ConfigurableField(
        id="output_token_number",
        name="Max tokens in the output",
        description="The maximum number of tokens in the output",
    )
)

# max_tokens = 20
print("max_tokens_20: ", model.invoke("tell me something about chess").content)

# max_tokens = 200
print(
    "max_tokens_200: ",
    model.with_config(configurable={"output_token_number": 200})
    .invoke("tell me something about chess")
    .content,
)

configurable_alternatives

configurable_alternatives(
    which: ConfigurableField,
    *,
    default_key: str = "default",
    prefix_keys: bool = False,
    **kwargs: (
        Runnable[Input, Output]
        | Callable[[], Runnable[Input, Output]]
    )
) -> RunnableSerializable[Input, Output]

Configure alternatives for Runnables that can be set at runtime.

Parameters:

Name Type Description Default
which ConfigurableField

The ConfigurableField instance that will be used to select the alternative.

required
default_key str

The default key to use if no alternative is selected. Defaults to 'default'.

'default'
prefix_keys bool

Whether to prefix the keys with the ConfigurableField id. Defaults to False.

False
**kwargs Runnable[Input, Output] | Callable[[], Runnable[Input, Output]]

A dictionary of keys to Runnable instances or callables that return Runnable instances.

{}

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the alternatives configured.

from langchain_anthropic import ChatAnthropic
from langchain_core.runnables.utils import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatAnthropic(
    model_name="claude-3-7-sonnet-20250219"
).configurable_alternatives(
    ConfigurableField(id="llm"),
    default_key="anthropic",
    openai=ChatOpenAI(),
)

# uses the default model ChatAnthropic
print(model.invoke("which organization created you?").content)

# uses ChatOpenAI
print(
    model.with_config(configurable={"llm": "openai"})
    .invoke("which organization created you?")
    .content
)

set_verbose

set_verbose(verbose: bool | None) -> bool

If verbose is None, set it.

This allows users to pass in None as verbose to access the global setting.

Parameters:

Name Type Description Default
verbose bool | None

The verbosity setting to use.

required

Returns:

Type Description
bool

The verbosity setting to use.

with_structured_output

with_structured_output(
    schema: Dict | type,
    *,
    include_raw: bool = False,
    **kwargs: Any
) -> Runnable[LanguageModelInput, Dict | BaseModel]

Model wrapper that returns outputs formatted to match the given schema.

Parameters:

Name Type Description Default
schema Dict | type

The output schema. Can be passed in as:

  • an OpenAI function/tool schema,
  • a JSON Schema,
  • a TypedDict class,
  • or a Pydantic class.

If schema is a Pydantic class then the model output will be a Pydantic instance of that class, and the model-generated fields will be validated by the Pydantic class. Otherwise the model output will be a dict and will not be validated. See langchain_core.utils.function_calling.convert_to_openai_tool for more on how to properly specify types and descriptions of schema fields when specifying a Pydantic or TypedDict class.

required
include_raw bool

If False then only the parsed structured output is returned. If an error occurs during model output parsing it will be raised. If True then both the raw model response (a BaseMessage) and the parsed model response will be returned. If an error occurs during output parsing it will be caught and returned as well. The final output is always a dict with keys 'raw', 'parsed', and 'parsing_error'.

False

Raises:

Type Description
ValueError

If there are any unsupported kwargs.

NotImplementedError

If the model does not implement with_structured_output().

Returns:

Type Description
Runnable[LanguageModelInput, Dict | BaseModel]

A Runnable that takes same inputs as a langchain_core.language_models.chat.BaseChatModel.

Runnable[LanguageModelInput, Dict | BaseModel]

If include_raw is False and schema is a Pydantic class, Runnable outputs

Runnable[LanguageModelInput, Dict | BaseModel]

an instance of schema (i.e., a Pydantic object).

Runnable[LanguageModelInput, Dict | BaseModel]

Otherwise, if include_raw is False then Runnable outputs a dict.

Runnable[LanguageModelInput, Dict | BaseModel]

If include_raw is True, then Runnable outputs a dict with keys:

Runnable[LanguageModelInput, Dict | BaseModel]
  • 'raw': BaseMessage
Runnable[LanguageModelInput, Dict | BaseModel]
  • 'parsed': None if there was a parsing error, otherwise the type depends on the schema as described above.
Runnable[LanguageModelInput, Dict | BaseModel]
  • 'parsing_error': Optional[BaseException]
Pydantic schema (include_raw=False):

.. code-block:: python

from pydantic import BaseModel


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


llm = ChatModel(model="model-name", temperature=0)
structured_llm = llm.with_structured_output(AnswerWithJustification)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)

# -> AnswerWithJustification(
#     answer='They weigh the same',
#     justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'
# )
Pydantic schema (include_raw=True):

.. code-block:: python

from pydantic import BaseModel


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


llm = ChatModel(model="model-name", temperature=0)
structured_llm = llm.with_structured_output(
    AnswerWithJustification, include_raw=True
)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)
# -> {
#     'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),
#     'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),
#     'parsing_error': None
# }
Dict schema (include_raw=False):

.. code-block:: python

from pydantic import BaseModel
from langchain_core.utils.function_calling import convert_to_openai_tool


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


dict_schema = convert_to_openai_tool(AnswerWithJustification)
llm = ChatModel(model="model-name", temperature=0)
structured_llm = llm.with_structured_output(dict_schema)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)
# -> {
#     'answer': 'They weigh the same',
#     'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
# }

Behavior changed in 0.2.26

Added support for TypedDict class.

get_token_ids

get_token_ids(text: str) -> list[int]

Return the ordered ids of the tokens in a text.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
list[int]

A list of ids corresponding to the tokens in the text, in order they occur

list[int]

in the text.

get_num_tokens

get_num_tokens(text: str) -> int

Get the number of tokens present in the text.

Useful for checking if an input fits in a model's context window.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
int

The integer number of tokens in the text.

get_num_tokens_from_messages

get_num_tokens_from_messages(
    messages: list[BaseMessage],
    tools: Sequence | None = None,
) -> int

Get the number of tokens in the messages.

Useful for checking if an input fits in a model's context window.

Note

The base implementation of get_num_tokens_from_messages ignores tool schemas.

Parameters:

Name Type Description Default
messages list[BaseMessage]

The message inputs to tokenize.

required
tools Sequence | None

If provided, sequence of dict, BaseModel, function, or BaseTools to be converted to tool schemas.

None

Returns:

Type Description
int

The sum of the number of tokens across the messages.

generate

generate(
    messages: list[list[BaseMessage]],
    stop: list[str] | None = None,
    callbacks: Callbacks = None,
    *,
    tags: list[str] | None = None,
    metadata: dict[str, Any] | None = None,
    run_name: str | None = None,
    run_id: UUID | None = None,
    **kwargs: Any
) -> LLMResult

Pass a sequence of prompts to the model and return model generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
messages list[list[BaseMessage]]

List of list of messages.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | None

The tags to apply.

None
metadata dict[str, Any] | None

The metadata to apply.

None
run_name str | None

The name of the run.

None
run_id UUID | None

The ID of the run.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input

LLMResult

prompt and additional model provider-specific output.

agenerate async

agenerate(
    messages: list[list[BaseMessage]],
    stop: list[str] | None = None,
    callbacks: Callbacks = None,
    *,
    tags: list[str] | None = None,
    metadata: dict[str, Any] | None = None,
    run_name: str | None = None,
    run_id: UUID | None = None,
    **kwargs: Any
) -> LLMResult

Asynchronously pass a sequence of prompts to a model and return generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
messages list[list[BaseMessage]]

List of list of messages.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | None

The tags to apply.

None
metadata dict[str, Any] | None

The metadata to apply.

None
run_name str | None

The name of the run.

None
run_id UUID | None

The ID of the run.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input

LLMResult

prompt and additional model provider-specific output.

dict

dict(**kwargs: Any) -> dict

Return a dictionary of the LLM.

bind_tools

bind_tools(
    tools: Sequence[
        Dict[str, Any] | type | Callable | BaseTool
    ],
    *,
    tool_choice: str | None = None,
    **kwargs: Any
) -> Runnable[LanguageModelInput, AIMessage]

Bind tools to the model.

Parameters:

Name Type Description Default
tools Sequence[Dict[str, Any] | type | Callable | BaseTool]

Sequence of tools to bind to the model.

required
tool_choice str | None

The tool to use. If "any" then any tool can be used.

None

Returns:

Type Description
Runnable[LanguageModelInput, AIMessage]

A Runnable that returns a message.

validate_environment

validate_environment() -> Self

Validate that the python package exists in environment.

__init__

__init__(
    *, model_name: Optional[str] = None, **kwargs: Any
) -> None

Needed for mypy typing to recognize model_name as a valid arg.

GemmaLocalHF

Bases: _GemmaLocalHFBase, BaseLLM

Local gemma model loaded from HuggingFace.

Methods:

Name Description
get_name

Get the name of the Runnable.

get_input_schema

Get a pydantic model that can be used to validate input to the Runnable.

get_input_jsonschema

Get a JSON schema that represents the input to the Runnable.

get_output_schema

Get a pydantic model that can be used to validate output to the Runnable.

get_output_jsonschema

Get a JSON schema that represents the output of the Runnable.

config_schema

The type of config this Runnable accepts specified as a pydantic model.

get_config_jsonschema

Get a JSON schema that represents the config of the Runnable.

get_graph

Return a graph representation of this Runnable.

get_prompts

Return a list of prompts used by this Runnable.

__or__

Runnable "or" operator.

__ror__

Runnable "reverse-or" operator.

pipe

Pipe runnables.

pick

Pick keys from the output dict of this Runnable.

assign

Assigns new fields to the dict output of this Runnable.

batch_as_completed

Run invoke in parallel on a list of inputs.

abatch_as_completed

Run ainvoke in parallel on a list of inputs.

astream_log

Stream all output from a Runnable, as reported to the callback system.

astream_events

Generate a stream of events.

transform

Transform inputs to outputs.

atransform

Transform inputs to outputs.

bind

Bind arguments to a Runnable, returning a new Runnable.

with_config

Bind config to a Runnable, returning a new Runnable.

with_listeners

Bind lifecycle listeners to a Runnable, returning a new Runnable.

with_alisteners

Bind async lifecycle listeners to a Runnable.

with_types

Bind input and output types to a Runnable, returning a new Runnable.

with_retry

Create a new Runnable that retries the original Runnable on exceptions.

map

Return a new Runnable that maps a list of inputs to a list of outputs.

with_fallbacks

Add fallbacks to a Runnable, returning a new Runnable.

as_tool

Create a BaseTool from a Runnable.

__init__
is_lc_serializable

Is this class serializable?

get_lc_namespace

Get the namespace of the langchain object.

lc_id

Return a unique identifier for this class for serialization purposes.

to_json

Serialize the Runnable to JSON.

to_json_not_implemented

Serialize a "not implemented" object.

configurable_fields

Configure particular Runnable fields at runtime.

configurable_alternatives

Configure alternatives for Runnables that can be set at runtime.

set_verbose

If verbose is None, set it.

with_structured_output

Not implemented on this class.

get_token_ids

Return the ordered ids of the tokens in a text.

get_num_tokens

Get the number of tokens present in the text.

get_num_tokens_from_messages

Get the number of tokens in the messages.

generate

Pass a sequence of prompts to a model and return generations.

agenerate

Asynchronously pass a sequence of prompts to a model and return generations.

__str__

Return a string representation of the object for printing.

dict

Return a dictionary of the LLM.

save

Save the LLM.

validate_environment

Validate that llama-cpp-python library is installed.

Attributes:

Name Type Description
InputType TypeAlias

Get the input type for this runnable.

OutputType type[str]

Get the input type for this runnable.

input_schema type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema type[BaseModel]

Output schema.

config_specs list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets dict[str, str]

A map of constructor argument names to secret ids.

lc_attributes dict

List of attribute names that should be included in the serialized kwargs.

cache BaseCache | bool | None

Whether to cache the response.

verbose bool

Whether to print out response text.

callbacks Callbacks

Callbacks to add to the run trace.

tags list[str] | None

Tags to add to the run trace.

metadata dict[str, Any] | None

Metadata to add to the run trace.

custom_get_token_ids Callable[[str], list[int]] | None

Optional encoder to use for counting tokens.

max_tokens Optional[int]

The maximum number of tokens to generate.

temperature Optional[float]

The temperature to use for sampling.

top_p Optional[float]

The top-p value to use for sampling.

top_k Optional[int]

The top-k value to use for sampling.

model_name str

Gemma model name.

InputType property

InputType: TypeAlias

Get the input type for this runnable.

OutputType property

OutputType: type[str]

Get the input type for this runnable.

input_schema property

input_schema: type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema property

output_schema: type[BaseModel]

Output schema.

The type of output this Runnable produces specified as a pydantic model.

config_specs property

config_specs: list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets property

lc_secrets: dict[str, str]

A map of constructor argument names to secret ids.

For example,

lc_attributes property

lc_attributes: dict

List of attribute names that should be included in the serialized kwargs.

These attributes must be accepted by the constructor. Default is an empty dictionary.

cache class-attribute instance-attribute

cache: BaseCache | bool | None = Field(
    default=None, exclude=True
)

Whether to cache the response.

  • If true, will use the global cache.
  • If false, will not use a cache
  • If None, will use the global cache if it's set, otherwise no cache.
  • If instance of BaseCache, will use the provided cache.

Caching is not currently supported for streaming methods of models.

verbose class-attribute instance-attribute

verbose: bool = Field(
    default_factory=_get_verbosity, exclude=True, repr=False
)

Whether to print out response text.

callbacks class-attribute instance-attribute

callbacks: Callbacks = Field(default=None, exclude=True)

Callbacks to add to the run trace.

tags class-attribute instance-attribute

tags: list[str] | None = Field(default=None, exclude=True)

Tags to add to the run trace.

metadata class-attribute instance-attribute

metadata: dict[str, Any] | None = Field(
    default=None, exclude=True
)

Metadata to add to the run trace.

custom_get_token_ids class-attribute instance-attribute

custom_get_token_ids: Callable[[str], list[int]] | None = (
    Field(default=None, exclude=True)
)

Optional encoder to use for counting tokens.

max_tokens class-attribute instance-attribute

max_tokens: Optional[int] = None

The maximum number of tokens to generate.

temperature class-attribute instance-attribute

temperature: Optional[float] = None

The temperature to use for sampling.

top_p class-attribute instance-attribute

top_p: Optional[float] = None

The top-p value to use for sampling.

top_k class-attribute instance-attribute

top_k: Optional[int] = None

The top-k value to use for sampling.

model_name class-attribute instance-attribute

model_name: str = Field(
    default="google/gemma-2b", alias="model"
)

Gemma model name.

get_name

get_name(
    suffix: str | None = None, *, name: str | None = None
) -> str

Get the name of the Runnable.

Parameters:

Name Type Description Default
suffix str | None

An optional suffix to append to the name.

None
name str | None

An optional name to use instead of the Runnable's name.

None

Returns:

Type Description
str

The name of the Runnable.

get_input_schema

get_input_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate input to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic input schema that depends on which configuration the Runnable is invoked with.

This method allows to get an input schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate input.

get_input_jsonschema

get_input_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the input to the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the input to the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_input_jsonschema())

Added in version 0.3.0

get_output_schema

get_output_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate output to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic output schema that depends on which configuration the Runnable is invoked with.

This method allows to get an output schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate output.

get_output_jsonschema

get_output_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the output of the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the output of the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_output_jsonschema())

Added in version 0.3.0

config_schema

config_schema(
    *, include: Sequence[str] | None = None
) -> type[BaseModel]

The type of config this Runnable accepts specified as a pydantic model.

To mark a field as configurable, see the configurable_fields and configurable_alternatives methods.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate config.

get_config_jsonschema

get_config_jsonschema(
    *, include: Sequence[str] | None = None
) -> dict[str, Any]

Get a JSON schema that represents the config of the Runnable.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the config of the Runnable.

Added in version 0.3.0

get_graph

get_graph(config: RunnableConfig | None = None) -> Graph

Return a graph representation of this Runnable.

get_prompts

get_prompts(
    config: RunnableConfig | None = None,
) -> list[BasePromptTemplate]

Return a list of prompts used by this Runnable.

__or__

__or__(
    other: (
        Runnable[Any, Other]
        | Callable[[Iterator[Any]], Iterator[Other]]
        | Callable[
            [AsyncIterator[Any]], AsyncIterator[Other]
        ]
        | Callable[[Any], Other]
        | Mapping[
            str,
            Runnable[Any, Other]
            | Callable[[Any], Other]
            | Any,
        ]
    ),
) -> RunnableSerializable[Input, Other]

Runnable "or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Any, Other] | Callable[[Iterator[Any]], Iterator[Other]] | Callable[[AsyncIterator[Any]], AsyncIterator[Other]] | Callable[[Any], Other] | Mapping[str, Runnable[Any, Other] | Callable[[Any], Other] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

__ror__

__ror__(
    other: (
        Runnable[Other, Any]
        | Callable[[Iterator[Other]], Iterator[Any]]
        | Callable[
            [AsyncIterator[Other]], AsyncIterator[Any]
        ]
        | Callable[[Other], Any]
        | Mapping[
            str,
            Runnable[Other, Any]
            | Callable[[Other], Any]
            | Any,
        ]
    ),
) -> RunnableSerializable[Other, Output]

Runnable "reverse-or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Other, Any] | Callable[[Iterator[Other]], Iterator[Any]] | Callable[[AsyncIterator[Other]], AsyncIterator[Any]] | Callable[[Other], Any] | Mapping[str, Runnable[Other, Any] | Callable[[Other], Any] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Other, Output]

A new Runnable.

pipe

pipe(
    *others: Runnable[Any, Other] | Callable[[Any], Other],
    name: str | None = None
) -> RunnableSerializable[Input, Other]

Pipe runnables.

Compose this Runnable with Runnable-like objects to make a RunnableSequence.

Equivalent to RunnableSequence(self, *others) or self | others[0] | ...

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


def mul_two(x: int) -> int:
    return x * 2


runnable_1 = RunnableLambda(add_one)
runnable_2 = RunnableLambda(mul_two)
sequence = runnable_1.pipe(runnable_2)
# Or equivalently:
# sequence = runnable_1 | runnable_2
# sequence = RunnableSequence(first=runnable_1, last=runnable_2)
sequence.invoke(1)
await sequence.ainvoke(1)
# -> 4

sequence.batch([1, 2, 3])
await sequence.abatch([1, 2, 3])
# -> [4, 6, 8]

Parameters:

Name Type Description Default
*others Runnable[Any, Other] | Callable[[Any], Other]

Other Runnable or Runnable-like objects to compose

()
name str | None

An optional name for the resulting RunnableSequence.

None

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

pick

pick(
    keys: str | list[str],
) -> RunnableSerializable[Any, Any]

Pick keys from the output dict of this Runnable.

Pick single key:

```python
import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)
chain = RunnableMap(str=as_str, json=as_json)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3]}

json_only_chain = chain.pick("json")
json_only_chain.invoke("[1, 2, 3]")
# -> [1, 2, 3]
```

Pick list of keys:

```python
from typing import Any

import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)


def as_bytes(x: Any) -> bytes:
    return bytes(x, "utf-8")


chain = RunnableMap(
    str=as_str, json=as_json, bytes=RunnableLambda(as_bytes)
)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3], "bytes": b"[1, 2, 3]"}

json_and_bytes_chain = chain.pick(["json", "bytes"])
json_and_bytes_chain.invoke("[1, 2, 3]")
# -> {"json": [1, 2, 3], "bytes": b"[1, 2, 3]"}
```

Parameters:

Name Type Description Default
keys str | list[str]

A key or list of keys to pick from the output dict.

required

Returns:

Type Description
RunnableSerializable[Any, Any]

a new Runnable.

assign

assign(
    **kwargs: (
        Runnable[dict[str, Any], Any]
        | Callable[[dict[str, Any]], Any]
        | Mapping[
            str,
            Runnable[dict[str, Any], Any]
            | Callable[[dict[str, Any]], Any],
        ]
    ),
) -> RunnableSerializable[Any, Any]

Assigns new fields to the dict output of this Runnable.

from langchain_community.llms.fake import FakeStreamingListLLM
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import SystemMessagePromptTemplate
from langchain_core.runnables import Runnable
from operator import itemgetter

prompt = (
    SystemMessagePromptTemplate.from_template("You are a nice assistant.")
    + "{question}"
)
llm = FakeStreamingListLLM(responses=["foo-lish"])

chain: Runnable = prompt | llm | {"str": StrOutputParser()}

chain_with_assign = chain.assign(hello=itemgetter("str") | llm)

print(chain_with_assign.input_schema.model_json_schema())
# {'title': 'PromptInput', 'type': 'object', 'properties':
{'question': {'title': 'Question', 'type': 'string'}}}
print(chain_with_assign.output_schema.model_json_schema())
# {'title': 'RunnableSequenceOutput', 'type': 'object', 'properties':
{'str': {'title': 'Str',
'type': 'string'}, 'hello': {'title': 'Hello', 'type': 'string'}}}

Parameters:

Name Type Description Default
**kwargs Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any] | Mapping[str, Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any]]

A mapping of keys to Runnable or Runnable-like objects that will be invoked with the entire output dict of this Runnable.

{}

Returns:

Type Description
RunnableSerializable[Any, Any]

A new Runnable.

batch_as_completed

batch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> Iterator[tuple[int, Output | Exception]]

Run invoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
tuple[int, Output | Exception]

Tuples of the index of the input and the output from the Runnable.

abatch_as_completed async

abatch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> AsyncIterator[tuple[int, Output | Exception]]

Run ainvoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[tuple[int, Output | Exception]]

A tuple of the index of the input and the output from the Runnable.

astream_log async

astream_log(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    diff: bool = True,
    with_streamed_output_list: bool = True,
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

Stream all output from a Runnable, as reported to the callback system.

This includes all inner runs of LLMs, Retrievers, Tools, etc.

Output is streamed as Log objects, which include a list of Jsonpatch ops that describe how the state of the run has changed in each step, and the final state of the run.

The Jsonpatch ops can be applied in order to construct state.

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
diff bool

Whether to yield diffs between each step or the current state.

True
with_streamed_output_list bool

Whether to yield the streamed_output list.

True
include_names Sequence[str] | None

Only include logs with these names.

None
include_types Sequence[str] | None

Only include logs with these types.

None
include_tags Sequence[str] | None

Only include logs with these tags.

None
exclude_names Sequence[str] | None

Exclude logs with these names.

None
exclude_types Sequence[str] | None

Exclude logs with these types.

None
exclude_tags Sequence[str] | None

Exclude logs with these tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

A RunLogPatch or RunLog object.

astream_events async

astream_events(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    version: Literal["v1", "v2"] = "v2",
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[StreamEvent]

Generate a stream of events.

Use to create an iterator over StreamEvents that provide real-time information about the progress of the Runnable, including StreamEvents from intermediate results.

A StreamEvent is a dictionary with the following schema:

  • event: str - Event names are of the format: on_[runnable_type]_(start|stream|end).
  • name: str - The name of the Runnable that generated the event.
  • run_id: str - randomly generated ID associated with the given execution of the Runnable that emitted the event. A child Runnable that gets invoked as part of the execution of a parent Runnable is assigned its own unique ID.
  • parent_ids: list[str] - The IDs of the parent runnables that generated the event. The root Runnable will have an empty list. The order of the parent IDs is from the root to the immediate parent. Only available for v2 version of the API. The v1 version of the API will return an empty list.
  • tags: Optional[list[str]] - The tags of the Runnable that generated the event.
  • metadata: Optional[dict[str, Any]] - The metadata of the Runnable that generated the event.
  • data: dict[str, Any]

Below is a table that illustrates some events that might be emitted by various chains. Metadata fields have been omitted from the table for brevity. Chain definitions have been included after the table.

Note

This reference table is for the v2 version of the schema.

+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | event | name | chunk | input | output | +==========================+==================+=====================================+===================================================+=====================================================+ | on_chat_model_start | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_stream | [model name] | AIMessageChunk(content="hello") | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_end | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | AIMessageChunk(content="hello world") | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_start | [model name] | | {'input': 'hello'} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_stream | [model name] | 'Hello' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_end | [model name] | | 'Hello human!' | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_start | format_docs | | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_stream | format_docs | 'hello world!, goodbye world!' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_end | format_docs | | [Document(...)] | 'hello world!, goodbye world!' | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_start | some_tool | | {"x": 1, "y": "2"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_end | some_tool | | | {"x": 1, "y": "2"} | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_start | [retriever name] | | {"query": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_end | [retriever name] | | {"query": "hello"} | [Document(...), ..] | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_start | [template_name] | | {"question": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_end | [template_name] | | {"question": "hello"} | ChatPromptValue(messages: [SystemMessage, ...]) | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+

In addition to the standard events, users can also dispatch custom events (see example below).

Custom events will be only be surfaced with in the v2 version of the API!

A custom event has following format:

+-----------+------+-----------------------------------------------------------------------------------------------------------+ | Attribute | Type | Description | +===========+======+===========================================================================================================+ | name | str | A user defined name for the event. | +-----------+------+-----------------------------------------------------------------------------------------------------------+ | data | Any | The data associated with the event. This can be anything, though we suggest making it JSON serializable. | +-----------+------+-----------------------------------------------------------------------------------------------------------+

Here are declarations associated with the standard events shown above:

format_docs:

def format_docs(docs: list[Document]) -> str:
    '''Format the docs.'''
    return ", ".join([doc.page_content for doc in docs])


format_docs = RunnableLambda(format_docs)

some_tool:

@tool
def some_tool(x: int, y: str) -> dict:
    '''Some_tool.'''
    return {"x": x, "y": y}

prompt:

template = ChatPromptTemplate.from_messages(
    [
        ("system", "You are Cat Agent 007"),
        ("human", "{question}"),
    ]
).with_config({"run_name": "my_template", "tags": ["my_template"]})
Example:

from langchain_core.runnables import RunnableLambda


async def reverse(s: str) -> str:
    return s[::-1]


chain = RunnableLambda(func=reverse)

events = [event async for event in chain.astream_events("hello", version="v2")]

# will produce the following events (run_id, and parent_ids
# has been omitted for brevity):
[
    {
        "data": {"input": "hello"},
        "event": "on_chain_start",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"chunk": "olleh"},
        "event": "on_chain_stream",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"output": "olleh"},
        "event": "on_chain_end",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
]

Example: Dispatch Custom Event

from langchain_core.callbacks.manager import (
    adispatch_custom_event,
)
from langchain_core.runnables import RunnableLambda, RunnableConfig
import asyncio


async def slow_thing(some_input: str, config: RunnableConfig) -> str:
    """Do something that takes a long time."""
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 1 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 2 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    return "Done"

slow_thing = RunnableLambda(slow_thing)

async for event in slow_thing.astream_events("some_input", version="v2"):
    print(event)

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
version Literal['v1', 'v2']

The version of the schema to use either 'v2' or 'v1'. Users should use 'v2'. 'v1' is for backwards compatibility and will be deprecated in 0.4.0. No default will be assigned until the API is stabilized. custom events will only be surfaced in 'v2'.

'v2'
include_names Sequence[str] | None

Only include events from Runnables with matching names.

None
include_types Sequence[str] | None

Only include events from Runnables with matching types.

None
include_tags Sequence[str] | None

Only include events from Runnables with matching tags.

None
exclude_names Sequence[str] | None

Exclude events from Runnables with matching names.

None
exclude_types Sequence[str] | None

Exclude events from Runnables with matching types.

None
exclude_tags Sequence[str] | None

Exclude events from Runnables with matching tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable. These will be passed to astream_log as this implementation of astream_events is built on top of astream_log.

{}

Yields:

Type Description
AsyncIterator[StreamEvent]

An async stream of StreamEvents.

Raises:

Type Description
NotImplementedError

If the version is not 'v1' or 'v2'.

transform

transform(
    input: Iterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> Iterator[Output]

Transform inputs to outputs.

Default implementation of transform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input Iterator[Input]

An iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
Output

The output of the Runnable.

atransform async

atransform(
    input: AsyncIterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> AsyncIterator[Output]

Transform inputs to outputs.

Default implementation of atransform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input AsyncIterator[Input]

An async iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[Output]

The output of the Runnable.

bind

bind(**kwargs: Any) -> Runnable[Input, Output]

Bind arguments to a Runnable, returning a new Runnable.

Useful when a Runnable in a chain requires an argument that is not in the output of the previous Runnable or included in the user input.

Parameters:

Name Type Description Default
kwargs Any

The arguments to bind to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the arguments bound.

Example
from langchain_ollama import ChatOllama
from langchain_core.output_parsers import StrOutputParser

llm = ChatOllama(model="llama3.1")

# Without bind.
chain = llm | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two three four five.'

# With bind.
chain = llm.bind(stop=["three"]) | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two'

with_config

with_config(
    config: RunnableConfig | None = None, **kwargs: Any
) -> Runnable[Input, Output]

Bind config to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

The config to bind to the Runnable.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the config bound.

with_listeners

with_listeners(
    *,
    on_start: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_end: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_error: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None
) -> Runnable[Input, Output]

Bind lifecycle listeners to a Runnable, returning a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called before the Runnable starts running, with the Run object. Defaults to None.

None
on_end Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda
from langchain_core.tracers.schemas import Run

import time


def test_runnable(time_to_sleep: int):
    time.sleep(time_to_sleep)


def fn_start(run_obj: Run):
    print("start_time:", run_obj.start_time)


def fn_end(run_obj: Run):
    print("end_time:", run_obj.end_time)


chain = RunnableLambda(test_runnable).with_listeners(
    on_start=fn_start, on_end=fn_end
)
chain.invoke(2)

with_alisteners

with_alisteners(
    *,
    on_start: AsyncListener | None = None,
    on_end: AsyncListener | None = None,
    on_error: AsyncListener | None = None
) -> Runnable[Input, Output]

Bind async lifecycle listeners to a Runnable.

Returns a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start AsyncListener | None

Called asynchronously before the Runnable starts running, with the Run object. Defaults to None.

None
on_end AsyncListener | None

Called asynchronously after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error AsyncListener | None

Called asynchronously if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda, Runnable
from datetime import datetime, timezone
import time
import asyncio

def format_t(timestamp: float) -> str:
    return datetime.fromtimestamp(timestamp, tz=timezone.utc).isoformat()

async def test_runnable(time_to_sleep: int):
    print(f"Runnable[{time_to_sleep}s]: starts at {format_t(time.time())}")
    await asyncio.sleep(time_to_sleep)
    print(f"Runnable[{time_to_sleep}s]: ends at {format_t(time.time())}")

async def fn_start(run_obj: Runnable):
    print(f"on start callback starts at {format_t(time.time())}")
    await asyncio.sleep(3)
    print(f"on start callback ends at {format_t(time.time())}")

async def fn_end(run_obj: Runnable):
    print(f"on end callback starts at {format_t(time.time())}")
    await asyncio.sleep(2)
    print(f"on end callback ends at {format_t(time.time())}")

runnable = RunnableLambda(test_runnable).with_alisteners(
    on_start=fn_start,
    on_end=fn_end
)
async def concurrent_runs():
    await asyncio.gather(runnable.ainvoke(2), runnable.ainvoke(3))

asyncio.run(concurrent_runs())
Result:
on start callback starts at 2025-03-01T07:05:22.875378+00:00
on start callback starts at 2025-03-01T07:05:22.875495+00:00
on start callback ends at 2025-03-01T07:05:25.878862+00:00
on start callback ends at 2025-03-01T07:05:25.878947+00:00
Runnable[2s]: starts at 2025-03-01T07:05:25.879392+00:00
Runnable[3s]: starts at 2025-03-01T07:05:25.879804+00:00
Runnable[2s]: ends at 2025-03-01T07:05:27.881998+00:00
on end callback starts at 2025-03-01T07:05:27.882360+00:00
Runnable[3s]: ends at 2025-03-01T07:05:28.881737+00:00
on end callback starts at 2025-03-01T07:05:28.882428+00:00
on end callback ends at 2025-03-01T07:05:29.883893+00:00
on end callback ends at 2025-03-01T07:05:30.884831+00:00

with_types

with_types(
    *,
    input_type: type[Input] | None = None,
    output_type: type[Output] | None = None
) -> Runnable[Input, Output]

Bind input and output types to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
input_type type[Input] | None

The input type to bind to the Runnable. Defaults to None.

None
output_type type[Output] | None

The output type to bind to the Runnable. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the types bound.

with_retry

with_retry(
    *,
    retry_if_exception_type: tuple[
        type[BaseException], ...
    ] = (Exception,),
    wait_exponential_jitter: bool = True,
    exponential_jitter_params: (
        ExponentialJitterParams | None
    ) = None,
    stop_after_attempt: int = 3
) -> Runnable[Input, Output]

Create a new Runnable that retries the original Runnable on exceptions.

Parameters:

Name Type Description Default
retry_if_exception_type tuple[type[BaseException], ...]

A tuple of exception types to retry on. Defaults to (Exception,).

(Exception,)
wait_exponential_jitter bool

Whether to add jitter to the wait time between retries. Defaults to True.

True
stop_after_attempt int

The maximum number of attempts to make before giving up. Defaults to 3.

3
exponential_jitter_params ExponentialJitterParams | None

Parameters for tenacity.wait_exponential_jitter. Namely: initial, max, exp_base, and jitter (all float values).

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable that retries the original Runnable on exceptions.

Example
from langchain_core.runnables import RunnableLambda

count = 0


def _lambda(x: int) -> None:
    global count
    count = count + 1
    if x == 1:
        raise ValueError("x is 1")
    else:
        pass


runnable = RunnableLambda(_lambda)
try:
    runnable.with_retry(
        stop_after_attempt=2,
        retry_if_exception_type=(ValueError,),
    ).invoke(1)
except ValueError:
    pass

assert count == 2

map

map() -> Runnable[list[Input], list[Output]]

Return a new Runnable that maps a list of inputs to a list of outputs.

Calls invoke with each input.

Returns:

Type Description
Runnable[list[Input], list[Output]]

A new Runnable that maps a list of inputs to a list of outputs.

Example
from langchain_core.runnables import RunnableLambda


def _lambda(x: int) -> int:
    return x + 1


runnable = RunnableLambda(_lambda)
print(runnable.map().invoke([1, 2, 3]))  # [2, 3, 4]

with_fallbacks

with_fallbacks(
    fallbacks: Sequence[Runnable[Input, Output]],
    *,
    exceptions_to_handle: tuple[
        type[BaseException], ...
    ] = (Exception,),
    exception_key: str | None = None
) -> RunnableWithFallbacks[Input, Output]

Add fallbacks to a Runnable, returning a new Runnable.

The new Runnable will try the original Runnable, and then each fallback in order, upon failures.

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle. Defaults to (Exception,).

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input. Defaults to None.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

Example
from typing import Iterator

from langchain_core.runnables import RunnableGenerator


def _generate_immediate_error(input: Iterator) -> Iterator[str]:
    raise ValueError()
    yield ""


def _generate(input: Iterator) -> Iterator[str]:
    yield from "foo bar"


runnable = RunnableGenerator(_generate_immediate_error).with_fallbacks(
    [RunnableGenerator(_generate)]
)
print("".join(runnable.stream({})))  # foo bar

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle.

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

as_tool

as_tool(
    args_schema: type[BaseModel] | None = None,
    *,
    name: str | None = None,
    description: str | None = None,
    arg_types: dict[str, type] | None = None
) -> BaseTool

Create a BaseTool from a Runnable.

as_tool will instantiate a BaseTool with a name, description, and args_schema from a Runnable. Where possible, schemas are inferred from runnable.get_input_schema. Alternatively (e.g., if the Runnable takes a dict as input and the specific dict keys are not typed), the schema can be specified directly with args_schema. You can also pass arg_types to just specify the required arguments and their types.

Parameters:

Name Type Description Default
args_schema type[BaseModel] | None

The schema for the tool. Defaults to None.

None
name str | None

The name of the tool. Defaults to None.

None
description str | None

The description of the tool. Defaults to None.

None
arg_types dict[str, type] | None

A dictionary of argument names to types. Defaults to None.

None

Returns:

Type Description
BaseTool

A BaseTool instance.

Typed dict input:

from typing_extensions import TypedDict
from langchain_core.runnables import RunnableLambda


class Args(TypedDict):
    a: int
    b: list[int]


def f(x: Args) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool()
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via args_schema:

from typing import Any
from pydantic import BaseModel, Field
from langchain_core.runnables import RunnableLambda

def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))

class FSchema(BaseModel):
    """Apply a function to an integer and list of integers."""

    a: int = Field(..., description="Integer")
    b: list[int] = Field(..., description="List of ints")

runnable = RunnableLambda(f)
as_tool = runnable.as_tool(FSchema)
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via arg_types:

from typing import Any
from langchain_core.runnables import RunnableLambda


def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool(arg_types={"a": int, "b": list[int]})
as_tool.invoke({"a": 3, "b": [1, 2]})

String input:

from langchain_core.runnables import RunnableLambda


def f(x: str) -> str:
    return x + "a"


def g(x: str) -> str:
    return x + "z"


runnable = RunnableLambda(f) | g
as_tool = runnable.as_tool()
as_tool.invoke("b")

Added in version 0.2.14

__init__

__init__(*args: Any, **kwargs: Any) -> None

is_lc_serializable classmethod

is_lc_serializable() -> bool

Is this class serializable?

By design, even if a class inherits from Serializable, it is not serializable by default. This is to prevent accidental serialization of objects that should not be serialized.

Returns:

Type Description
bool

Whether the class is serializable. Default is False.

get_lc_namespace classmethod

get_lc_namespace() -> list[str]

Get the namespace of the langchain object.

For example, if the class is langchain.llms.openai.OpenAI, then the namespace is ["langchain", "llms", "openai"]

Returns:

Type Description
list[str]

The namespace as a list of strings.

lc_id classmethod

lc_id() -> list[str]

Return a unique identifier for this class for serialization purposes.

The unique identifier is a list of strings that describes the path to the object. For example, for the class langchain.llms.openai.OpenAI, the id is ["langchain", "llms", "openai", "OpenAI"].

to_json

to_json() -> (
    SerializedConstructor | SerializedNotImplemented
)

Serialize the Runnable to JSON.

Returns:

Type Description
SerializedConstructor | SerializedNotImplemented

A JSON-serializable representation of the Runnable.

to_json_not_implemented

to_json_not_implemented() -> SerializedNotImplemented

Serialize a "not implemented" object.

Returns:

Type Description
SerializedNotImplemented

SerializedNotImplemented.

configurable_fields

configurable_fields(
    **kwargs: AnyConfigurableField,
) -> RunnableSerializable[Input, Output]

Configure particular Runnable fields at runtime.

Parameters:

Name Type Description Default
**kwargs AnyConfigurableField

A dictionary of ConfigurableField instances to configure.

{}

Raises:

Type Description
ValueError

If a configuration key is not found in the Runnable.

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the fields configured.

from langchain_core.runnables import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatOpenAI(max_tokens=20).configurable_fields(
    max_tokens=ConfigurableField(
        id="output_token_number",
        name="Max tokens in the output",
        description="The maximum number of tokens in the output",
    )
)

# max_tokens = 20
print("max_tokens_20: ", model.invoke("tell me something about chess").content)

# max_tokens = 200
print(
    "max_tokens_200: ",
    model.with_config(configurable={"output_token_number": 200})
    .invoke("tell me something about chess")
    .content,
)

configurable_alternatives

configurable_alternatives(
    which: ConfigurableField,
    *,
    default_key: str = "default",
    prefix_keys: bool = False,
    **kwargs: (
        Runnable[Input, Output]
        | Callable[[], Runnable[Input, Output]]
    )
) -> RunnableSerializable[Input, Output]

Configure alternatives for Runnables that can be set at runtime.

Parameters:

Name Type Description Default
which ConfigurableField

The ConfigurableField instance that will be used to select the alternative.

required
default_key str

The default key to use if no alternative is selected. Defaults to 'default'.

'default'
prefix_keys bool

Whether to prefix the keys with the ConfigurableField id. Defaults to False.

False
**kwargs Runnable[Input, Output] | Callable[[], Runnable[Input, Output]]

A dictionary of keys to Runnable instances or callables that return Runnable instances.

{}

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the alternatives configured.

from langchain_anthropic import ChatAnthropic
from langchain_core.runnables.utils import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatAnthropic(
    model_name="claude-3-7-sonnet-20250219"
).configurable_alternatives(
    ConfigurableField(id="llm"),
    default_key="anthropic",
    openai=ChatOpenAI(),
)

# uses the default model ChatAnthropic
print(model.invoke("which organization created you?").content)

# uses ChatOpenAI
print(
    model.with_config(configurable={"llm": "openai"})
    .invoke("which organization created you?")
    .content
)

set_verbose

set_verbose(verbose: bool | None) -> bool

If verbose is None, set it.

This allows users to pass in None as verbose to access the global setting.

Parameters:

Name Type Description Default
verbose bool | None

The verbosity setting to use.

required

Returns:

Type Description
bool

The verbosity setting to use.

with_structured_output

with_structured_output(
    schema: dict | type, **kwargs: Any
) -> Runnable[LanguageModelInput, dict | BaseModel]

Not implemented on this class.

get_token_ids

get_token_ids(text: str) -> list[int]

Return the ordered ids of the tokens in a text.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
list[int]

A list of ids corresponding to the tokens in the text, in order they occur

list[int]

in the text.

get_num_tokens

get_num_tokens(text: str) -> int

Get the number of tokens present in the text.

Useful for checking if an input fits in a model's context window.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
int

The integer number of tokens in the text.

get_num_tokens_from_messages

get_num_tokens_from_messages(
    messages: list[BaseMessage],
    tools: Sequence | None = None,
) -> int

Get the number of tokens in the messages.

Useful for checking if an input fits in a model's context window.

Note

The base implementation of get_num_tokens_from_messages ignores tool schemas.

Parameters:

Name Type Description Default
messages list[BaseMessage]

The message inputs to tokenize.

required
tools Sequence | None

If provided, sequence of dict, BaseModel, function, or BaseTools to be converted to tool schemas.

None

Returns:

Type Description
int

The sum of the number of tokens across the messages.

generate

generate(
    prompts: list[str],
    stop: list[str] | None = None,
    callbacks: Callbacks | list[Callbacks] | None = None,
    *,
    tags: list[str] | list[list[str]] | None = None,
    metadata: (
        dict[str, Any] | list[dict[str, Any]] | None
    ) = None,
    run_name: str | list[str] | None = None,
    run_id: UUID | list[UUID | None] | None = None,
    **kwargs: Any
) -> LLMResult

Pass a sequence of prompts to a model and return generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
prompts list[str]

List of string prompts.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks | list[Callbacks] | None

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | list[list[str]] | None

List of tags to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
metadata dict[str, Any] | list[dict[str, Any]] | None

List of metadata dictionaries to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
run_name str | list[str] | None

List of run names to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
run_id UUID | list[UUID | None] | None

List of run IDs to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Raises:

Type Description
ValueError

If prompts is not a list.

ValueError

If the length of callbacks, tags, metadata, or run_name (if provided) does not match the length of prompts.

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input prompt and additional model provider-specific output.

agenerate async

agenerate(
    prompts: list[str],
    stop: list[str] | None = None,
    callbacks: Callbacks | list[Callbacks] | None = None,
    *,
    tags: list[str] | list[list[str]] | None = None,
    metadata: (
        dict[str, Any] | list[dict[str, Any]] | None
    ) = None,
    run_name: str | list[str] | None = None,
    run_id: UUID | list[UUID | None] | None = None,
    **kwargs: Any
) -> LLMResult

Asynchronously pass a sequence of prompts to a model and return generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
prompts list[str]

List of string prompts.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks | list[Callbacks] | None

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | list[list[str]] | None

List of tags to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
metadata dict[str, Any] | list[dict[str, Any]] | None

List of metadata dictionaries to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
run_name str | list[str] | None

List of run names to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
run_id UUID | list[UUID | None] | None

List of run IDs to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Raises:

Type Description
ValueError

If the length of callbacks, tags, metadata, or run_name (if provided) does not match the length of prompts.

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input prompt and additional model provider-specific output.

__str__

__str__() -> str

Return a string representation of the object for printing.

dict

dict(**kwargs: Any) -> dict

Return a dictionary of the LLM.

save

save(file_path: Path | str) -> None

Save the LLM.

Parameters:

Name Type Description Default
file_path Path | str

Path to file to save the LLM to.

required

Raises:

Type Description
ValueError

If the file path is not a string or Path object.

Example:

.. code-block:: python

    llm.save(file_path="path/llm.yaml")

validate_environment

validate_environment() -> Self

Validate that llama-cpp-python library is installed.

GemmaLocalKaggle

Bases: _GemmaLocalKaggleBase, BaseLLM

Local gemma chat model loaded from Kaggle.

Methods:

Name Description
get_name

Get the name of the Runnable.

get_input_schema

Get a pydantic model that can be used to validate input to the Runnable.

get_input_jsonschema

Get a JSON schema that represents the input to the Runnable.

get_output_schema

Get a pydantic model that can be used to validate output to the Runnable.

get_output_jsonschema

Get a JSON schema that represents the output of the Runnable.

config_schema

The type of config this Runnable accepts specified as a pydantic model.

get_config_jsonschema

Get a JSON schema that represents the config of the Runnable.

get_graph

Return a graph representation of this Runnable.

get_prompts

Return a list of prompts used by this Runnable.

__or__

Runnable "or" operator.

__ror__

Runnable "reverse-or" operator.

pipe

Pipe runnables.

pick

Pick keys from the output dict of this Runnable.

assign

Assigns new fields to the dict output of this Runnable.

batch_as_completed

Run invoke in parallel on a list of inputs.

abatch_as_completed

Run ainvoke in parallel on a list of inputs.

astream_log

Stream all output from a Runnable, as reported to the callback system.

astream_events

Generate a stream of events.

transform

Transform inputs to outputs.

atransform

Transform inputs to outputs.

bind

Bind arguments to a Runnable, returning a new Runnable.

with_config

Bind config to a Runnable, returning a new Runnable.

with_listeners

Bind lifecycle listeners to a Runnable, returning a new Runnable.

with_alisteners

Bind async lifecycle listeners to a Runnable.

with_types

Bind input and output types to a Runnable, returning a new Runnable.

with_retry

Create a new Runnable that retries the original Runnable on exceptions.

map

Return a new Runnable that maps a list of inputs to a list of outputs.

with_fallbacks

Add fallbacks to a Runnable, returning a new Runnable.

as_tool

Create a BaseTool from a Runnable.

is_lc_serializable

Is this class serializable?

get_lc_namespace

Get the namespace of the langchain object.

lc_id

Return a unique identifier for this class for serialization purposes.

to_json

Serialize the Runnable to JSON.

to_json_not_implemented

Serialize a "not implemented" object.

configurable_fields

Configure particular Runnable fields at runtime.

configurable_alternatives

Configure alternatives for Runnables that can be set at runtime.

set_verbose

If verbose is None, set it.

with_structured_output

Not implemented on this class.

get_token_ids

Return the ordered ids of the tokens in a text.

get_num_tokens

Get the number of tokens present in the text.

get_num_tokens_from_messages

Get the number of tokens in the messages.

generate

Pass a sequence of prompts to a model and return generations.

agenerate

Asynchronously pass a sequence of prompts to a model and return generations.

__str__

Return a string representation of the object for printing.

dict

Return a dictionary of the LLM.

save

Save the LLM.

validate_environment

Validate that llama-cpp-python library is installed.

__init__

Only needed for typing.

Attributes:

Name Type Description
InputType TypeAlias

Get the input type for this runnable.

OutputType type[str]

Get the input type for this runnable.

input_schema type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema type[BaseModel]

Output schema.

config_specs list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets dict[str, str]

A map of constructor argument names to secret ids.

lc_attributes dict

List of attribute names that should be included in the serialized kwargs.

cache BaseCache | bool | None

Whether to cache the response.

verbose bool

Whether to print out response text.

callbacks Callbacks

Callbacks to add to the run trace.

tags list[str] | None

Tags to add to the run trace.

metadata dict[str, Any] | None

Metadata to add to the run trace.

custom_get_token_ids Callable[[str], list[int]] | None

Optional encoder to use for counting tokens.

max_tokens Optional[int]

The maximum number of tokens to generate.

temperature Optional[float]

The temperature to use for sampling.

top_p Optional[float]

The top-p value to use for sampling.

top_k Optional[int]

The top-k value to use for sampling.

model_name str

Gemma model name.

InputType property

InputType: TypeAlias

Get the input type for this runnable.

OutputType property

OutputType: type[str]

Get the input type for this runnable.

input_schema property

input_schema: type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema property

output_schema: type[BaseModel]

Output schema.

The type of output this Runnable produces specified as a pydantic model.

config_specs property

config_specs: list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets property

lc_secrets: dict[str, str]

A map of constructor argument names to secret ids.

For example,

lc_attributes property

lc_attributes: dict

List of attribute names that should be included in the serialized kwargs.

These attributes must be accepted by the constructor. Default is an empty dictionary.

cache class-attribute instance-attribute

cache: BaseCache | bool | None = Field(
    default=None, exclude=True
)

Whether to cache the response.

  • If true, will use the global cache.
  • If false, will not use a cache
  • If None, will use the global cache if it's set, otherwise no cache.
  • If instance of BaseCache, will use the provided cache.

Caching is not currently supported for streaming methods of models.

verbose class-attribute instance-attribute

verbose: bool = Field(
    default_factory=_get_verbosity, exclude=True, repr=False
)

Whether to print out response text.

callbacks class-attribute instance-attribute

callbacks: Callbacks = Field(default=None, exclude=True)

Callbacks to add to the run trace.

tags class-attribute instance-attribute

tags: list[str] | None = Field(default=None, exclude=True)

Tags to add to the run trace.

metadata class-attribute instance-attribute

metadata: dict[str, Any] | None = Field(
    default=None, exclude=True
)

Metadata to add to the run trace.

custom_get_token_ids class-attribute instance-attribute

custom_get_token_ids: Callable[[str], list[int]] | None = (
    Field(default=None, exclude=True)
)

Optional encoder to use for counting tokens.

max_tokens class-attribute instance-attribute

max_tokens: Optional[int] = None

The maximum number of tokens to generate.

temperature class-attribute instance-attribute

temperature: Optional[float] = None

The temperature to use for sampling.

top_p class-attribute instance-attribute

top_p: Optional[float] = None

The top-p value to use for sampling.

top_k class-attribute instance-attribute

top_k: Optional[int] = None

The top-k value to use for sampling.

model_name class-attribute instance-attribute

model_name: str = Field(
    default="gemma_2b_en", alias="model"
)

Gemma model name.

get_name

get_name(
    suffix: str | None = None, *, name: str | None = None
) -> str

Get the name of the Runnable.

Parameters:

Name Type Description Default
suffix str | None

An optional suffix to append to the name.

None
name str | None

An optional name to use instead of the Runnable's name.

None

Returns:

Type Description
str

The name of the Runnable.

get_input_schema

get_input_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate input to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic input schema that depends on which configuration the Runnable is invoked with.

This method allows to get an input schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate input.

get_input_jsonschema

get_input_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the input to the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the input to the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_input_jsonschema())

Added in version 0.3.0

get_output_schema

get_output_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate output to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic output schema that depends on which configuration the Runnable is invoked with.

This method allows to get an output schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate output.

get_output_jsonschema

get_output_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the output of the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the output of the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_output_jsonschema())

Added in version 0.3.0

config_schema

config_schema(
    *, include: Sequence[str] | None = None
) -> type[BaseModel]

The type of config this Runnable accepts specified as a pydantic model.

To mark a field as configurable, see the configurable_fields and configurable_alternatives methods.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate config.

get_config_jsonschema

get_config_jsonschema(
    *, include: Sequence[str] | None = None
) -> dict[str, Any]

Get a JSON schema that represents the config of the Runnable.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the config of the Runnable.

Added in version 0.3.0

get_graph

get_graph(config: RunnableConfig | None = None) -> Graph

Return a graph representation of this Runnable.

get_prompts

get_prompts(
    config: RunnableConfig | None = None,
) -> list[BasePromptTemplate]

Return a list of prompts used by this Runnable.

__or__

__or__(
    other: (
        Runnable[Any, Other]
        | Callable[[Iterator[Any]], Iterator[Other]]
        | Callable[
            [AsyncIterator[Any]], AsyncIterator[Other]
        ]
        | Callable[[Any], Other]
        | Mapping[
            str,
            Runnable[Any, Other]
            | Callable[[Any], Other]
            | Any,
        ]
    ),
) -> RunnableSerializable[Input, Other]

Runnable "or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Any, Other] | Callable[[Iterator[Any]], Iterator[Other]] | Callable[[AsyncIterator[Any]], AsyncIterator[Other]] | Callable[[Any], Other] | Mapping[str, Runnable[Any, Other] | Callable[[Any], Other] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

__ror__

__ror__(
    other: (
        Runnable[Other, Any]
        | Callable[[Iterator[Other]], Iterator[Any]]
        | Callable[
            [AsyncIterator[Other]], AsyncIterator[Any]
        ]
        | Callable[[Other], Any]
        | Mapping[
            str,
            Runnable[Other, Any]
            | Callable[[Other], Any]
            | Any,
        ]
    ),
) -> RunnableSerializable[Other, Output]

Runnable "reverse-or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Other, Any] | Callable[[Iterator[Other]], Iterator[Any]] | Callable[[AsyncIterator[Other]], AsyncIterator[Any]] | Callable[[Other], Any] | Mapping[str, Runnable[Other, Any] | Callable[[Other], Any] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Other, Output]

A new Runnable.

pipe

pipe(
    *others: Runnable[Any, Other] | Callable[[Any], Other],
    name: str | None = None
) -> RunnableSerializable[Input, Other]

Pipe runnables.

Compose this Runnable with Runnable-like objects to make a RunnableSequence.

Equivalent to RunnableSequence(self, *others) or self | others[0] | ...

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


def mul_two(x: int) -> int:
    return x * 2


runnable_1 = RunnableLambda(add_one)
runnable_2 = RunnableLambda(mul_two)
sequence = runnable_1.pipe(runnable_2)
# Or equivalently:
# sequence = runnable_1 | runnable_2
# sequence = RunnableSequence(first=runnable_1, last=runnable_2)
sequence.invoke(1)
await sequence.ainvoke(1)
# -> 4

sequence.batch([1, 2, 3])
await sequence.abatch([1, 2, 3])
# -> [4, 6, 8]

Parameters:

Name Type Description Default
*others Runnable[Any, Other] | Callable[[Any], Other]

Other Runnable or Runnable-like objects to compose

()
name str | None

An optional name for the resulting RunnableSequence.

None

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

pick

pick(
    keys: str | list[str],
) -> RunnableSerializable[Any, Any]

Pick keys from the output dict of this Runnable.

Pick single key:

```python
import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)
chain = RunnableMap(str=as_str, json=as_json)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3]}

json_only_chain = chain.pick("json")
json_only_chain.invoke("[1, 2, 3]")
# -> [1, 2, 3]
```

Pick list of keys:

```python
from typing import Any

import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)


def as_bytes(x: Any) -> bytes:
    return bytes(x, "utf-8")


chain = RunnableMap(
    str=as_str, json=as_json, bytes=RunnableLambda(as_bytes)
)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3], "bytes": b"[1, 2, 3]"}

json_and_bytes_chain = chain.pick(["json", "bytes"])
json_and_bytes_chain.invoke("[1, 2, 3]")
# -> {"json": [1, 2, 3], "bytes": b"[1, 2, 3]"}
```

Parameters:

Name Type Description Default
keys str | list[str]

A key or list of keys to pick from the output dict.

required

Returns:

Type Description
RunnableSerializable[Any, Any]

a new Runnable.

assign

assign(
    **kwargs: (
        Runnable[dict[str, Any], Any]
        | Callable[[dict[str, Any]], Any]
        | Mapping[
            str,
            Runnable[dict[str, Any], Any]
            | Callable[[dict[str, Any]], Any],
        ]
    ),
) -> RunnableSerializable[Any, Any]

Assigns new fields to the dict output of this Runnable.

from langchain_community.llms.fake import FakeStreamingListLLM
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import SystemMessagePromptTemplate
from langchain_core.runnables import Runnable
from operator import itemgetter

prompt = (
    SystemMessagePromptTemplate.from_template("You are a nice assistant.")
    + "{question}"
)
llm = FakeStreamingListLLM(responses=["foo-lish"])

chain: Runnable = prompt | llm | {"str": StrOutputParser()}

chain_with_assign = chain.assign(hello=itemgetter("str") | llm)

print(chain_with_assign.input_schema.model_json_schema())
# {'title': 'PromptInput', 'type': 'object', 'properties':
{'question': {'title': 'Question', 'type': 'string'}}}
print(chain_with_assign.output_schema.model_json_schema())
# {'title': 'RunnableSequenceOutput', 'type': 'object', 'properties':
{'str': {'title': 'Str',
'type': 'string'}, 'hello': {'title': 'Hello', 'type': 'string'}}}

Parameters:

Name Type Description Default
**kwargs Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any] | Mapping[str, Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any]]

A mapping of keys to Runnable or Runnable-like objects that will be invoked with the entire output dict of this Runnable.

{}

Returns:

Type Description
RunnableSerializable[Any, Any]

A new Runnable.

batch_as_completed

batch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> Iterator[tuple[int, Output | Exception]]

Run invoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
tuple[int, Output | Exception]

Tuples of the index of the input and the output from the Runnable.

abatch_as_completed async

abatch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> AsyncIterator[tuple[int, Output | Exception]]

Run ainvoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[tuple[int, Output | Exception]]

A tuple of the index of the input and the output from the Runnable.

astream_log async

astream_log(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    diff: bool = True,
    with_streamed_output_list: bool = True,
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

Stream all output from a Runnable, as reported to the callback system.

This includes all inner runs of LLMs, Retrievers, Tools, etc.

Output is streamed as Log objects, which include a list of Jsonpatch ops that describe how the state of the run has changed in each step, and the final state of the run.

The Jsonpatch ops can be applied in order to construct state.

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
diff bool

Whether to yield diffs between each step or the current state.

True
with_streamed_output_list bool

Whether to yield the streamed_output list.

True
include_names Sequence[str] | None

Only include logs with these names.

None
include_types Sequence[str] | None

Only include logs with these types.

None
include_tags Sequence[str] | None

Only include logs with these tags.

None
exclude_names Sequence[str] | None

Exclude logs with these names.

None
exclude_types Sequence[str] | None

Exclude logs with these types.

None
exclude_tags Sequence[str] | None

Exclude logs with these tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

A RunLogPatch or RunLog object.

astream_events async

astream_events(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    version: Literal["v1", "v2"] = "v2",
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[StreamEvent]

Generate a stream of events.

Use to create an iterator over StreamEvents that provide real-time information about the progress of the Runnable, including StreamEvents from intermediate results.

A StreamEvent is a dictionary with the following schema:

  • event: str - Event names are of the format: on_[runnable_type]_(start|stream|end).
  • name: str - The name of the Runnable that generated the event.
  • run_id: str - randomly generated ID associated with the given execution of the Runnable that emitted the event. A child Runnable that gets invoked as part of the execution of a parent Runnable is assigned its own unique ID.
  • parent_ids: list[str] - The IDs of the parent runnables that generated the event. The root Runnable will have an empty list. The order of the parent IDs is from the root to the immediate parent. Only available for v2 version of the API. The v1 version of the API will return an empty list.
  • tags: Optional[list[str]] - The tags of the Runnable that generated the event.
  • metadata: Optional[dict[str, Any]] - The metadata of the Runnable that generated the event.
  • data: dict[str, Any]

Below is a table that illustrates some events that might be emitted by various chains. Metadata fields have been omitted from the table for brevity. Chain definitions have been included after the table.

Note

This reference table is for the v2 version of the schema.

+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | event | name | chunk | input | output | +==========================+==================+=====================================+===================================================+=====================================================+ | on_chat_model_start | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_stream | [model name] | AIMessageChunk(content="hello") | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_end | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | AIMessageChunk(content="hello world") | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_start | [model name] | | {'input': 'hello'} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_stream | [model name] | 'Hello' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_end | [model name] | | 'Hello human!' | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_start | format_docs | | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_stream | format_docs | 'hello world!, goodbye world!' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_end | format_docs | | [Document(...)] | 'hello world!, goodbye world!' | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_start | some_tool | | {"x": 1, "y": "2"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_end | some_tool | | | {"x": 1, "y": "2"} | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_start | [retriever name] | | {"query": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_end | [retriever name] | | {"query": "hello"} | [Document(...), ..] | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_start | [template_name] | | {"question": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_end | [template_name] | | {"question": "hello"} | ChatPromptValue(messages: [SystemMessage, ...]) | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+

In addition to the standard events, users can also dispatch custom events (see example below).

Custom events will be only be surfaced with in the v2 version of the API!

A custom event has following format:

+-----------+------+-----------------------------------------------------------------------------------------------------------+ | Attribute | Type | Description | +===========+======+===========================================================================================================+ | name | str | A user defined name for the event. | +-----------+------+-----------------------------------------------------------------------------------------------------------+ | data | Any | The data associated with the event. This can be anything, though we suggest making it JSON serializable. | +-----------+------+-----------------------------------------------------------------------------------------------------------+

Here are declarations associated with the standard events shown above:

format_docs:

def format_docs(docs: list[Document]) -> str:
    '''Format the docs.'''
    return ", ".join([doc.page_content for doc in docs])


format_docs = RunnableLambda(format_docs)

some_tool:

@tool
def some_tool(x: int, y: str) -> dict:
    '''Some_tool.'''
    return {"x": x, "y": y}

prompt:

template = ChatPromptTemplate.from_messages(
    [
        ("system", "You are Cat Agent 007"),
        ("human", "{question}"),
    ]
).with_config({"run_name": "my_template", "tags": ["my_template"]})
Example:

from langchain_core.runnables import RunnableLambda


async def reverse(s: str) -> str:
    return s[::-1]


chain = RunnableLambda(func=reverse)

events = [event async for event in chain.astream_events("hello", version="v2")]

# will produce the following events (run_id, and parent_ids
# has been omitted for brevity):
[
    {
        "data": {"input": "hello"},
        "event": "on_chain_start",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"chunk": "olleh"},
        "event": "on_chain_stream",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"output": "olleh"},
        "event": "on_chain_end",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
]

Example: Dispatch Custom Event

from langchain_core.callbacks.manager import (
    adispatch_custom_event,
)
from langchain_core.runnables import RunnableLambda, RunnableConfig
import asyncio


async def slow_thing(some_input: str, config: RunnableConfig) -> str:
    """Do something that takes a long time."""
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 1 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 2 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    return "Done"

slow_thing = RunnableLambda(slow_thing)

async for event in slow_thing.astream_events("some_input", version="v2"):
    print(event)

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
version Literal['v1', 'v2']

The version of the schema to use either 'v2' or 'v1'. Users should use 'v2'. 'v1' is for backwards compatibility and will be deprecated in 0.4.0. No default will be assigned until the API is stabilized. custom events will only be surfaced in 'v2'.

'v2'
include_names Sequence[str] | None

Only include events from Runnables with matching names.

None
include_types Sequence[str] | None

Only include events from Runnables with matching types.

None
include_tags Sequence[str] | None

Only include events from Runnables with matching tags.

None
exclude_names Sequence[str] | None

Exclude events from Runnables with matching names.

None
exclude_types Sequence[str] | None

Exclude events from Runnables with matching types.

None
exclude_tags Sequence[str] | None

Exclude events from Runnables with matching tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable. These will be passed to astream_log as this implementation of astream_events is built on top of astream_log.

{}

Yields:

Type Description
AsyncIterator[StreamEvent]

An async stream of StreamEvents.

Raises:

Type Description
NotImplementedError

If the version is not 'v1' or 'v2'.

transform

transform(
    input: Iterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> Iterator[Output]

Transform inputs to outputs.

Default implementation of transform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input Iterator[Input]

An iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
Output

The output of the Runnable.

atransform async

atransform(
    input: AsyncIterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> AsyncIterator[Output]

Transform inputs to outputs.

Default implementation of atransform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input AsyncIterator[Input]

An async iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[Output]

The output of the Runnable.

bind

bind(**kwargs: Any) -> Runnable[Input, Output]

Bind arguments to a Runnable, returning a new Runnable.

Useful when a Runnable in a chain requires an argument that is not in the output of the previous Runnable or included in the user input.

Parameters:

Name Type Description Default
kwargs Any

The arguments to bind to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the arguments bound.

Example
from langchain_ollama import ChatOllama
from langchain_core.output_parsers import StrOutputParser

llm = ChatOllama(model="llama3.1")

# Without bind.
chain = llm | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two three four five.'

# With bind.
chain = llm.bind(stop=["three"]) | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two'

with_config

with_config(
    config: RunnableConfig | None = None, **kwargs: Any
) -> Runnable[Input, Output]

Bind config to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

The config to bind to the Runnable.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the config bound.

with_listeners

with_listeners(
    *,
    on_start: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_end: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_error: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None
) -> Runnable[Input, Output]

Bind lifecycle listeners to a Runnable, returning a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called before the Runnable starts running, with the Run object. Defaults to None.

None
on_end Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda
from langchain_core.tracers.schemas import Run

import time


def test_runnable(time_to_sleep: int):
    time.sleep(time_to_sleep)


def fn_start(run_obj: Run):
    print("start_time:", run_obj.start_time)


def fn_end(run_obj: Run):
    print("end_time:", run_obj.end_time)


chain = RunnableLambda(test_runnable).with_listeners(
    on_start=fn_start, on_end=fn_end
)
chain.invoke(2)

with_alisteners

with_alisteners(
    *,
    on_start: AsyncListener | None = None,
    on_end: AsyncListener | None = None,
    on_error: AsyncListener | None = None
) -> Runnable[Input, Output]

Bind async lifecycle listeners to a Runnable.

Returns a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start AsyncListener | None

Called asynchronously before the Runnable starts running, with the Run object. Defaults to None.

None
on_end AsyncListener | None

Called asynchronously after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error AsyncListener | None

Called asynchronously if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda, Runnable
from datetime import datetime, timezone
import time
import asyncio

def format_t(timestamp: float) -> str:
    return datetime.fromtimestamp(timestamp, tz=timezone.utc).isoformat()

async def test_runnable(time_to_sleep: int):
    print(f"Runnable[{time_to_sleep}s]: starts at {format_t(time.time())}")
    await asyncio.sleep(time_to_sleep)
    print(f"Runnable[{time_to_sleep}s]: ends at {format_t(time.time())}")

async def fn_start(run_obj: Runnable):
    print(f"on start callback starts at {format_t(time.time())}")
    await asyncio.sleep(3)
    print(f"on start callback ends at {format_t(time.time())}")

async def fn_end(run_obj: Runnable):
    print(f"on end callback starts at {format_t(time.time())}")
    await asyncio.sleep(2)
    print(f"on end callback ends at {format_t(time.time())}")

runnable = RunnableLambda(test_runnable).with_alisteners(
    on_start=fn_start,
    on_end=fn_end
)
async def concurrent_runs():
    await asyncio.gather(runnable.ainvoke(2), runnable.ainvoke(3))

asyncio.run(concurrent_runs())
Result:
on start callback starts at 2025-03-01T07:05:22.875378+00:00
on start callback starts at 2025-03-01T07:05:22.875495+00:00
on start callback ends at 2025-03-01T07:05:25.878862+00:00
on start callback ends at 2025-03-01T07:05:25.878947+00:00
Runnable[2s]: starts at 2025-03-01T07:05:25.879392+00:00
Runnable[3s]: starts at 2025-03-01T07:05:25.879804+00:00
Runnable[2s]: ends at 2025-03-01T07:05:27.881998+00:00
on end callback starts at 2025-03-01T07:05:27.882360+00:00
Runnable[3s]: ends at 2025-03-01T07:05:28.881737+00:00
on end callback starts at 2025-03-01T07:05:28.882428+00:00
on end callback ends at 2025-03-01T07:05:29.883893+00:00
on end callback ends at 2025-03-01T07:05:30.884831+00:00

with_types

with_types(
    *,
    input_type: type[Input] | None = None,
    output_type: type[Output] | None = None
) -> Runnable[Input, Output]

Bind input and output types to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
input_type type[Input] | None

The input type to bind to the Runnable. Defaults to None.

None
output_type type[Output] | None

The output type to bind to the Runnable. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the types bound.

with_retry

with_retry(
    *,
    retry_if_exception_type: tuple[
        type[BaseException], ...
    ] = (Exception,),
    wait_exponential_jitter: bool = True,
    exponential_jitter_params: (
        ExponentialJitterParams | None
    ) = None,
    stop_after_attempt: int = 3
) -> Runnable[Input, Output]

Create a new Runnable that retries the original Runnable on exceptions.

Parameters:

Name Type Description Default
retry_if_exception_type tuple[type[BaseException], ...]

A tuple of exception types to retry on. Defaults to (Exception,).

(Exception,)
wait_exponential_jitter bool

Whether to add jitter to the wait time between retries. Defaults to True.

True
stop_after_attempt int

The maximum number of attempts to make before giving up. Defaults to 3.

3
exponential_jitter_params ExponentialJitterParams | None

Parameters for tenacity.wait_exponential_jitter. Namely: initial, max, exp_base, and jitter (all float values).

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable that retries the original Runnable on exceptions.

Example
from langchain_core.runnables import RunnableLambda

count = 0


def _lambda(x: int) -> None:
    global count
    count = count + 1
    if x == 1:
        raise ValueError("x is 1")
    else:
        pass


runnable = RunnableLambda(_lambda)
try:
    runnable.with_retry(
        stop_after_attempt=2,
        retry_if_exception_type=(ValueError,),
    ).invoke(1)
except ValueError:
    pass

assert count == 2

map

map() -> Runnable[list[Input], list[Output]]

Return a new Runnable that maps a list of inputs to a list of outputs.

Calls invoke with each input.

Returns:

Type Description
Runnable[list[Input], list[Output]]

A new Runnable that maps a list of inputs to a list of outputs.

Example
from langchain_core.runnables import RunnableLambda


def _lambda(x: int) -> int:
    return x + 1


runnable = RunnableLambda(_lambda)
print(runnable.map().invoke([1, 2, 3]))  # [2, 3, 4]

with_fallbacks

with_fallbacks(
    fallbacks: Sequence[Runnable[Input, Output]],
    *,
    exceptions_to_handle: tuple[
        type[BaseException], ...
    ] = (Exception,),
    exception_key: str | None = None
) -> RunnableWithFallbacks[Input, Output]

Add fallbacks to a Runnable, returning a new Runnable.

The new Runnable will try the original Runnable, and then each fallback in order, upon failures.

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle. Defaults to (Exception,).

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input. Defaults to None.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

Example
from typing import Iterator

from langchain_core.runnables import RunnableGenerator


def _generate_immediate_error(input: Iterator) -> Iterator[str]:
    raise ValueError()
    yield ""


def _generate(input: Iterator) -> Iterator[str]:
    yield from "foo bar"


runnable = RunnableGenerator(_generate_immediate_error).with_fallbacks(
    [RunnableGenerator(_generate)]
)
print("".join(runnable.stream({})))  # foo bar

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle.

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

as_tool

as_tool(
    args_schema: type[BaseModel] | None = None,
    *,
    name: str | None = None,
    description: str | None = None,
    arg_types: dict[str, type] | None = None
) -> BaseTool

Create a BaseTool from a Runnable.

as_tool will instantiate a BaseTool with a name, description, and args_schema from a Runnable. Where possible, schemas are inferred from runnable.get_input_schema. Alternatively (e.g., if the Runnable takes a dict as input and the specific dict keys are not typed), the schema can be specified directly with args_schema. You can also pass arg_types to just specify the required arguments and their types.

Parameters:

Name Type Description Default
args_schema type[BaseModel] | None

The schema for the tool. Defaults to None.

None
name str | None

The name of the tool. Defaults to None.

None
description str | None

The description of the tool. Defaults to None.

None
arg_types dict[str, type] | None

A dictionary of argument names to types. Defaults to None.

None

Returns:

Type Description
BaseTool

A BaseTool instance.

Typed dict input:

from typing_extensions import TypedDict
from langchain_core.runnables import RunnableLambda


class Args(TypedDict):
    a: int
    b: list[int]


def f(x: Args) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool()
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via args_schema:

from typing import Any
from pydantic import BaseModel, Field
from langchain_core.runnables import RunnableLambda

def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))

class FSchema(BaseModel):
    """Apply a function to an integer and list of integers."""

    a: int = Field(..., description="Integer")
    b: list[int] = Field(..., description="List of ints")

runnable = RunnableLambda(f)
as_tool = runnable.as_tool(FSchema)
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via arg_types:

from typing import Any
from langchain_core.runnables import RunnableLambda


def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool(arg_types={"a": int, "b": list[int]})
as_tool.invoke({"a": 3, "b": [1, 2]})

String input:

from langchain_core.runnables import RunnableLambda


def f(x: str) -> str:
    return x + "a"


def g(x: str) -> str:
    return x + "z"


runnable = RunnableLambda(f) | g
as_tool = runnable.as_tool()
as_tool.invoke("b")

Added in version 0.2.14

is_lc_serializable classmethod

is_lc_serializable() -> bool

Is this class serializable?

By design, even if a class inherits from Serializable, it is not serializable by default. This is to prevent accidental serialization of objects that should not be serialized.

Returns:

Type Description
bool

Whether the class is serializable. Default is False.

get_lc_namespace classmethod

get_lc_namespace() -> list[str]

Get the namespace of the langchain object.

For example, if the class is langchain.llms.openai.OpenAI, then the namespace is ["langchain", "llms", "openai"]

Returns:

Type Description
list[str]

The namespace as a list of strings.

lc_id classmethod

lc_id() -> list[str]

Return a unique identifier for this class for serialization purposes.

The unique identifier is a list of strings that describes the path to the object. For example, for the class langchain.llms.openai.OpenAI, the id is ["langchain", "llms", "openai", "OpenAI"].

to_json

to_json() -> (
    SerializedConstructor | SerializedNotImplemented
)

Serialize the Runnable to JSON.

Returns:

Type Description
SerializedConstructor | SerializedNotImplemented

A JSON-serializable representation of the Runnable.

to_json_not_implemented

to_json_not_implemented() -> SerializedNotImplemented

Serialize a "not implemented" object.

Returns:

Type Description
SerializedNotImplemented

SerializedNotImplemented.

configurable_fields

configurable_fields(
    **kwargs: AnyConfigurableField,
) -> RunnableSerializable[Input, Output]

Configure particular Runnable fields at runtime.

Parameters:

Name Type Description Default
**kwargs AnyConfigurableField

A dictionary of ConfigurableField instances to configure.

{}

Raises:

Type Description
ValueError

If a configuration key is not found in the Runnable.

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the fields configured.

from langchain_core.runnables import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatOpenAI(max_tokens=20).configurable_fields(
    max_tokens=ConfigurableField(
        id="output_token_number",
        name="Max tokens in the output",
        description="The maximum number of tokens in the output",
    )
)

# max_tokens = 20
print("max_tokens_20: ", model.invoke("tell me something about chess").content)

# max_tokens = 200
print(
    "max_tokens_200: ",
    model.with_config(configurable={"output_token_number": 200})
    .invoke("tell me something about chess")
    .content,
)

configurable_alternatives

configurable_alternatives(
    which: ConfigurableField,
    *,
    default_key: str = "default",
    prefix_keys: bool = False,
    **kwargs: (
        Runnable[Input, Output]
        | Callable[[], Runnable[Input, Output]]
    )
) -> RunnableSerializable[Input, Output]

Configure alternatives for Runnables that can be set at runtime.

Parameters:

Name Type Description Default
which ConfigurableField

The ConfigurableField instance that will be used to select the alternative.

required
default_key str

The default key to use if no alternative is selected. Defaults to 'default'.

'default'
prefix_keys bool

Whether to prefix the keys with the ConfigurableField id. Defaults to False.

False
**kwargs Runnable[Input, Output] | Callable[[], Runnable[Input, Output]]

A dictionary of keys to Runnable instances or callables that return Runnable instances.

{}

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the alternatives configured.

from langchain_anthropic import ChatAnthropic
from langchain_core.runnables.utils import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatAnthropic(
    model_name="claude-3-7-sonnet-20250219"
).configurable_alternatives(
    ConfigurableField(id="llm"),
    default_key="anthropic",
    openai=ChatOpenAI(),
)

# uses the default model ChatAnthropic
print(model.invoke("which organization created you?").content)

# uses ChatOpenAI
print(
    model.with_config(configurable={"llm": "openai"})
    .invoke("which organization created you?")
    .content
)

set_verbose

set_verbose(verbose: bool | None) -> bool

If verbose is None, set it.

This allows users to pass in None as verbose to access the global setting.

Parameters:

Name Type Description Default
verbose bool | None

The verbosity setting to use.

required

Returns:

Type Description
bool

The verbosity setting to use.

with_structured_output

with_structured_output(
    schema: dict | type, **kwargs: Any
) -> Runnable[LanguageModelInput, dict | BaseModel]

Not implemented on this class.

get_token_ids

get_token_ids(text: str) -> list[int]

Return the ordered ids of the tokens in a text.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
list[int]

A list of ids corresponding to the tokens in the text, in order they occur

list[int]

in the text.

get_num_tokens

get_num_tokens(text: str) -> int

Get the number of tokens present in the text.

Useful for checking if an input fits in a model's context window.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
int

The integer number of tokens in the text.

get_num_tokens_from_messages

get_num_tokens_from_messages(
    messages: list[BaseMessage],
    tools: Sequence | None = None,
) -> int

Get the number of tokens in the messages.

Useful for checking if an input fits in a model's context window.

Note

The base implementation of get_num_tokens_from_messages ignores tool schemas.

Parameters:

Name Type Description Default
messages list[BaseMessage]

The message inputs to tokenize.

required
tools Sequence | None

If provided, sequence of dict, BaseModel, function, or BaseTools to be converted to tool schemas.

None

Returns:

Type Description
int

The sum of the number of tokens across the messages.

generate

generate(
    prompts: list[str],
    stop: list[str] | None = None,
    callbacks: Callbacks | list[Callbacks] | None = None,
    *,
    tags: list[str] | list[list[str]] | None = None,
    metadata: (
        dict[str, Any] | list[dict[str, Any]] | None
    ) = None,
    run_name: str | list[str] | None = None,
    run_id: UUID | list[UUID | None] | None = None,
    **kwargs: Any
) -> LLMResult

Pass a sequence of prompts to a model and return generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
prompts list[str]

List of string prompts.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks | list[Callbacks] | None

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | list[list[str]] | None

List of tags to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
metadata dict[str, Any] | list[dict[str, Any]] | None

List of metadata dictionaries to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
run_name str | list[str] | None

List of run names to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
run_id UUID | list[UUID | None] | None

List of run IDs to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Raises:

Type Description
ValueError

If prompts is not a list.

ValueError

If the length of callbacks, tags, metadata, or run_name (if provided) does not match the length of prompts.

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input prompt and additional model provider-specific output.

agenerate async

agenerate(
    prompts: list[str],
    stop: list[str] | None = None,
    callbacks: Callbacks | list[Callbacks] | None = None,
    *,
    tags: list[str] | list[list[str]] | None = None,
    metadata: (
        dict[str, Any] | list[dict[str, Any]] | None
    ) = None,
    run_name: str | list[str] | None = None,
    run_id: UUID | list[UUID | None] | None = None,
    **kwargs: Any
) -> LLMResult

Asynchronously pass a sequence of prompts to a model and return generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
prompts list[str]

List of string prompts.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks | list[Callbacks] | None

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | list[list[str]] | None

List of tags to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
metadata dict[str, Any] | list[dict[str, Any]] | None

List of metadata dictionaries to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
run_name str | list[str] | None

List of run names to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
run_id UUID | list[UUID | None] | None

List of run IDs to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Raises:

Type Description
ValueError

If the length of callbacks, tags, metadata, or run_name (if provided) does not match the length of prompts.

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input prompt and additional model provider-specific output.

__str__

__str__() -> str

Return a string representation of the object for printing.

dict

dict(**kwargs: Any) -> dict

Return a dictionary of the LLM.

save

save(file_path: Path | str) -> None

Save the LLM.

Parameters:

Name Type Description Default
file_path Path | str

Path to file to save the LLM to.

required

Raises:

Type Description
ValueError

If the file path is not a string or Path object.

Example:

.. code-block:: python

    llm.save(file_path="path/llm.yaml")

validate_environment

validate_environment() -> Self

Validate that llama-cpp-python library is installed.

__init__

__init__(
    *, model_name: Optional[str] = None, **kwargs: Any
) -> None

Only needed for typing.

VertexAI

Bases: _VertexAICommon, BaseLLM

Google Vertex AI large language models.

Methods:

Name Description
get_name

Get the name of the Runnable.

get_input_schema

Get a pydantic model that can be used to validate input to the Runnable.

get_input_jsonschema

Get a JSON schema that represents the input to the Runnable.

get_output_schema

Get a pydantic model that can be used to validate output to the Runnable.

get_output_jsonschema

Get a JSON schema that represents the output of the Runnable.

config_schema

The type of config this Runnable accepts specified as a pydantic model.

get_config_jsonschema

Get a JSON schema that represents the config of the Runnable.

get_graph

Return a graph representation of this Runnable.

get_prompts

Return a list of prompts used by this Runnable.

__or__

Runnable "or" operator.

__ror__

Runnable "reverse-or" operator.

pipe

Pipe runnables.

pick

Pick keys from the output dict of this Runnable.

assign

Assigns new fields to the dict output of this Runnable.

batch_as_completed

Run invoke in parallel on a list of inputs.

abatch_as_completed

Run ainvoke in parallel on a list of inputs.

astream_log

Stream all output from a Runnable, as reported to the callback system.

astream_events

Generate a stream of events.

transform

Transform inputs to outputs.

atransform

Transform inputs to outputs.

bind

Bind arguments to a Runnable, returning a new Runnable.

with_config

Bind config to a Runnable, returning a new Runnable.

with_listeners

Bind lifecycle listeners to a Runnable, returning a new Runnable.

with_alisteners

Bind async lifecycle listeners to a Runnable.

with_types

Bind input and output types to a Runnable, returning a new Runnable.

with_retry

Create a new Runnable that retries the original Runnable on exceptions.

map

Return a new Runnable that maps a list of inputs to a list of outputs.

with_fallbacks

Add fallbacks to a Runnable, returning a new Runnable.

as_tool

Create a BaseTool from a Runnable.

lc_id

Return a unique identifier for this class for serialization purposes.

to_json

Serialize the Runnable to JSON.

to_json_not_implemented

Serialize a "not implemented" object.

configurable_fields

Configure particular Runnable fields at runtime.

configurable_alternatives

Configure alternatives for Runnables that can be set at runtime.

set_verbose

If verbose is None, set it.

with_structured_output

Not implemented on this class.

get_token_ids

Return the ordered ids of the tokens in a text.

get_num_tokens_from_messages

Get the number of tokens in the messages.

generate

Pass a sequence of prompts to a model and return generations.

agenerate

Asynchronously pass a sequence of prompts to a model and return generations.

__str__

Return a string representation of the object for printing.

dict

Return a dictionary of the LLM.

save

Save the LLM.

__init__

Needed for mypy typing to recognize model_name as a valid arg

get_lc_namespace

Get the namespace of the langchain object.

validate_environment

Validate that the python package exists in environment.

get_num_tokens

Get the number of tokens present in the text.

Attributes:

Name Type Description
InputType TypeAlias

Get the input type for this runnable.

OutputType type[str]

Get the input type for this runnable.

input_schema type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema type[BaseModel]

Output schema.

config_specs list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets dict[str, str]

A map of constructor argument names to secret ids.

lc_attributes dict

List of attribute names that should be included in the serialized kwargs.

cache BaseCache | bool | None

Whether to cache the response.

verbose bool

Whether to print out response text.

callbacks Callbacks

Callbacks to add to the run trace.

tags list[str] | None

Tags to add to the run trace.

metadata dict[str, Any] | None

Metadata to add to the run trace.

custom_get_token_ids Callable[[str], list[int]] | None

Optional encoder to use for counting tokens.

project Optional[str]

The default GCP project to use when making Vertex API calls.

location str

The default location to use when making API calls.

request_parallelism int

The amount of parallelism allowed for requests issued to VertexAI models.

max_retries int

The maximum number of retries to make when generating.

stop Optional[List[str]]

Optional list of stop words to use when generating.

full_model_name Optional[str]

The full name of the model's endpoint.

api_endpoint Optional[str]

Desired API endpoint, e.g., us-central1-aiplatform.googleapis.com

api_transport Optional[str]

The desired API transport method, can be either 'grpc' or 'rest'.

additional_headers Optional[Dict[str, str]]

A key-value dictionary representing additional headers for the model call

client_cert_source Optional[Callable[[], Tuple[bytes, bytes]]]

A callback which returns client certificate bytes and private key bytes both

credentials Any

The default custom credentials (google.auth.credentials.Credentials) to use

endpoint_version Literal['v1', 'v1beta1']

Whether to use v1 or v1beta1 endpoint.

prediction_client Union[PredictionServiceClient, PredictionServiceClient]

Returns PredictionServiceClient.

async_prediction_client Union[PredictionServiceAsyncClient, PredictionServiceAsyncClient]

Returns PredictionServiceClient.

temperature Optional[float]

Sampling temperature, it controls the degree of randomness in token selection.

frequency_penalty Optional[float]

Positive values penalize tokens that repeatedly appear in the generated text,

presence_penalty Optional[float]

Positive values penalize tokens that already appear in the generated text,

max_output_tokens Optional[int]

Token limit determines the maximum amount of text output from one prompt.

top_p Optional[float]

Tokens are selected from most probable to least until the sum of their

top_k Optional[int]

How the model selects tokens for output, the next token is selected from

n int

How many completions to generate for each prompt.

seed Optional[int]

Random seed for the generation.

streaming bool

Whether to stream the results or not.

safety_settings Optional[SafetySettingsType]

The default safety settings to use for all generations.

model_name str

The name of the Vertex AI large language model.

tuned_model_name Optional[str]

The name of a tuned model. If tuned_model_name is passed

response_mime_type Optional[str]

Optional. Output response mimetype of the generated candidate text. Only

response_schema Optional[Dict[str, Any]]

Optional. Enforce an schema to the output.

InputType property

InputType: TypeAlias

Get the input type for this runnable.

OutputType property

OutputType: type[str]

Get the input type for this runnable.

input_schema property

input_schema: type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema property

output_schema: type[BaseModel]

Output schema.

The type of output this Runnable produces specified as a pydantic model.

config_specs property

config_specs: list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets property

lc_secrets: dict[str, str]

A map of constructor argument names to secret ids.

For example,

lc_attributes property

lc_attributes: dict

List of attribute names that should be included in the serialized kwargs.

These attributes must be accepted by the constructor. Default is an empty dictionary.

cache class-attribute instance-attribute

cache: BaseCache | bool | None = Field(
    default=None, exclude=True
)

Whether to cache the response.

  • If true, will use the global cache.
  • If false, will not use a cache
  • If None, will use the global cache if it's set, otherwise no cache.
  • If instance of BaseCache, will use the provided cache.

Caching is not currently supported for streaming methods of models.

verbose class-attribute instance-attribute

verbose: bool = Field(
    default_factory=_get_verbosity, exclude=True, repr=False
)

Whether to print out response text.

callbacks class-attribute instance-attribute

callbacks: Callbacks = Field(default=None, exclude=True)

Callbacks to add to the run trace.

tags class-attribute instance-attribute

tags: list[str] | None = Field(default=None, exclude=True)

Tags to add to the run trace.

metadata class-attribute instance-attribute

metadata: dict[str, Any] | None = Field(
    default=None, exclude=True
)

Metadata to add to the run trace.

custom_get_token_ids class-attribute instance-attribute

custom_get_token_ids: Callable[[str], list[int]] | None = (
    Field(default=None, exclude=True)
)

Optional encoder to use for counting tokens.

project class-attribute instance-attribute

project: Optional[str] = None

The default GCP project to use when making Vertex API calls.

location class-attribute instance-attribute

location: str = Field(default=_DEFAULT_LOCATION)

The default location to use when making API calls.

request_parallelism class-attribute instance-attribute

request_parallelism: int = 5

The amount of parallelism allowed for requests issued to VertexAI models.

max_retries class-attribute instance-attribute

max_retries: int = 6

The maximum number of retries to make when generating.

stop class-attribute instance-attribute

stop: Optional[List[str]] = Field(
    default=None, alias="stop_sequences"
)

Optional list of stop words to use when generating.

full_model_name class-attribute instance-attribute

full_model_name: Optional[str] = Field(
    default=None, exclude=True
)

The full name of the model's endpoint.

api_endpoint class-attribute instance-attribute

api_endpoint: Optional[str] = Field(
    default=None, alias="base_url"
)

Desired API endpoint, e.g., us-central1-aiplatform.googleapis.com

api_transport class-attribute instance-attribute

api_transport: Optional[str] = None

The desired API transport method, can be either 'grpc' or 'rest'. Uses the default parameter in vertexai.init if defined.

additional_headers class-attribute instance-attribute

additional_headers: Optional[Dict[str, str]] = Field(
    default=None
)

A key-value dictionary representing additional headers for the model call

client_cert_source class-attribute instance-attribute

client_cert_source: Optional[
    Callable[[], Tuple[bytes, bytes]]
] = None

A callback which returns client certificate bytes and private key bytes both

credentials class-attribute instance-attribute

credentials: Any = Field(default=None, exclude=True)

The default custom credentials (google.auth.credentials.Credentials) to use

endpoint_version class-attribute instance-attribute

endpoint_version: Literal['v1', 'v1beta1'] = 'v1beta1'

Whether to use v1 or v1beta1 endpoint.

v1 is more performant, but v1beta1 might have some new features.

prediction_client property

prediction_client: Union[
    PredictionServiceClient, PredictionServiceClient
]

Returns PredictionServiceClient.

async_prediction_client property

async_prediction_client: Union[
    PredictionServiceAsyncClient,
    PredictionServiceAsyncClient,
]

Returns PredictionServiceClient.

temperature class-attribute instance-attribute

temperature: Optional[float] = None

Sampling temperature, it controls the degree of randomness in token selection.

frequency_penalty class-attribute instance-attribute

frequency_penalty: Optional[float] = None

Positive values penalize tokens that repeatedly appear in the generated text,

presence_penalty class-attribute instance-attribute

presence_penalty: Optional[float] = None

Positive values penalize tokens that already appear in the generated text,

max_output_tokens class-attribute instance-attribute

max_output_tokens: Optional[int] = Field(
    default=None, alias="max_tokens"
)

Token limit determines the maximum amount of text output from one prompt.

top_p class-attribute instance-attribute

top_p: Optional[float] = None

Tokens are selected from most probable to least until the sum of their

top_k class-attribute instance-attribute

top_k: Optional[int] = None

How the model selects tokens for output, the next token is selected from

n class-attribute instance-attribute

n: int = 1

How many completions to generate for each prompt.

seed class-attribute instance-attribute

seed: Optional[int] = None

Random seed for the generation.

streaming class-attribute instance-attribute

streaming: bool = False

Whether to stream the results or not.

safety_settings class-attribute instance-attribute

safety_settings: Optional[SafetySettingsType] = None

The default safety settings to use for all generations.

For example:

from langchain_google_vertexai import HarmBlockThreshold, HarmCategory

safety_settings = {
    HarmCategory.HARM_CATEGORY_UNSPECIFIED: HarmBlockThreshold.BLOCK_NONE,
    HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
    HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_ONLY_HIGH,
    HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
    HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
}

model_name class-attribute instance-attribute

model_name: str = Field(
    default="gemini-2.0-flash-001", alias="model"
)

The name of the Vertex AI large language model.

tuned_model_name class-attribute instance-attribute

tuned_model_name: Optional[str] = None

The name of a tuned model. If tuned_model_name is passed model_name will be used to determine the model family

response_mime_type class-attribute instance-attribute

response_mime_type: Optional[str] = None

Optional. Output response mimetype of the generated candidate text. Only supported in Gemini 1.5 and later models. Supported mimetype: * "text/plain": (default) Text output. * "application/json": JSON response in the candidates. * "text/x.enum": Enum in plain text. The model also needs to be prompted to output the appropriate response type, otherwise the behavior is undefined. This is a preview feature.

response_schema class-attribute instance-attribute

response_schema: Optional[Dict[str, Any]] = None

Optional. Enforce an schema to the output. The format of the dictionary should follow Open API schema.

get_name

get_name(
    suffix: str | None = None, *, name: str | None = None
) -> str

Get the name of the Runnable.

Parameters:

Name Type Description Default
suffix str | None

An optional suffix to append to the name.

None
name str | None

An optional name to use instead of the Runnable's name.

None

Returns:

Type Description
str

The name of the Runnable.

get_input_schema

get_input_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate input to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic input schema that depends on which configuration the Runnable is invoked with.

This method allows to get an input schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate input.

get_input_jsonschema

get_input_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the input to the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the input to the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_input_jsonschema())

Added in version 0.3.0

get_output_schema

get_output_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate output to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic output schema that depends on which configuration the Runnable is invoked with.

This method allows to get an output schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate output.

get_output_jsonschema

get_output_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the output of the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the output of the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_output_jsonschema())

Added in version 0.3.0

config_schema

config_schema(
    *, include: Sequence[str] | None = None
) -> type[BaseModel]

The type of config this Runnable accepts specified as a pydantic model.

To mark a field as configurable, see the configurable_fields and configurable_alternatives methods.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate config.

get_config_jsonschema

get_config_jsonschema(
    *, include: Sequence[str] | None = None
) -> dict[str, Any]

Get a JSON schema that represents the config of the Runnable.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the config of the Runnable.

Added in version 0.3.0

get_graph

get_graph(config: RunnableConfig | None = None) -> Graph

Return a graph representation of this Runnable.

get_prompts

get_prompts(
    config: RunnableConfig | None = None,
) -> list[BasePromptTemplate]

Return a list of prompts used by this Runnable.

__or__

__or__(
    other: (
        Runnable[Any, Other]
        | Callable[[Iterator[Any]], Iterator[Other]]
        | Callable[
            [AsyncIterator[Any]], AsyncIterator[Other]
        ]
        | Callable[[Any], Other]
        | Mapping[
            str,
            Runnable[Any, Other]
            | Callable[[Any], Other]
            | Any,
        ]
    ),
) -> RunnableSerializable[Input, Other]

Runnable "or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Any, Other] | Callable[[Iterator[Any]], Iterator[Other]] | Callable[[AsyncIterator[Any]], AsyncIterator[Other]] | Callable[[Any], Other] | Mapping[str, Runnable[Any, Other] | Callable[[Any], Other] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

__ror__

__ror__(
    other: (
        Runnable[Other, Any]
        | Callable[[Iterator[Other]], Iterator[Any]]
        | Callable[
            [AsyncIterator[Other]], AsyncIterator[Any]
        ]
        | Callable[[Other], Any]
        | Mapping[
            str,
            Runnable[Other, Any]
            | Callable[[Other], Any]
            | Any,
        ]
    ),
) -> RunnableSerializable[Other, Output]

Runnable "reverse-or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Other, Any] | Callable[[Iterator[Other]], Iterator[Any]] | Callable[[AsyncIterator[Other]], AsyncIterator[Any]] | Callable[[Other], Any] | Mapping[str, Runnable[Other, Any] | Callable[[Other], Any] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Other, Output]

A new Runnable.

pipe

pipe(
    *others: Runnable[Any, Other] | Callable[[Any], Other],
    name: str | None = None
) -> RunnableSerializable[Input, Other]

Pipe runnables.

Compose this Runnable with Runnable-like objects to make a RunnableSequence.

Equivalent to RunnableSequence(self, *others) or self | others[0] | ...

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


def mul_two(x: int) -> int:
    return x * 2


runnable_1 = RunnableLambda(add_one)
runnable_2 = RunnableLambda(mul_two)
sequence = runnable_1.pipe(runnable_2)
# Or equivalently:
# sequence = runnable_1 | runnable_2
# sequence = RunnableSequence(first=runnable_1, last=runnable_2)
sequence.invoke(1)
await sequence.ainvoke(1)
# -> 4

sequence.batch([1, 2, 3])
await sequence.abatch([1, 2, 3])
# -> [4, 6, 8]

Parameters:

Name Type Description Default
*others Runnable[Any, Other] | Callable[[Any], Other]

Other Runnable or Runnable-like objects to compose

()
name str | None

An optional name for the resulting RunnableSequence.

None

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

pick

pick(
    keys: str | list[str],
) -> RunnableSerializable[Any, Any]

Pick keys from the output dict of this Runnable.

Pick single key:

```python
import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)
chain = RunnableMap(str=as_str, json=as_json)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3]}

json_only_chain = chain.pick("json")
json_only_chain.invoke("[1, 2, 3]")
# -> [1, 2, 3]
```

Pick list of keys:

```python
from typing import Any

import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)


def as_bytes(x: Any) -> bytes:
    return bytes(x, "utf-8")


chain = RunnableMap(
    str=as_str, json=as_json, bytes=RunnableLambda(as_bytes)
)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3], "bytes": b"[1, 2, 3]"}

json_and_bytes_chain = chain.pick(["json", "bytes"])
json_and_bytes_chain.invoke("[1, 2, 3]")
# -> {"json": [1, 2, 3], "bytes": b"[1, 2, 3]"}
```

Parameters:

Name Type Description Default
keys str | list[str]

A key or list of keys to pick from the output dict.

required

Returns:

Type Description
RunnableSerializable[Any, Any]

a new Runnable.

assign

assign(
    **kwargs: (
        Runnable[dict[str, Any], Any]
        | Callable[[dict[str, Any]], Any]
        | Mapping[
            str,
            Runnable[dict[str, Any], Any]
            | Callable[[dict[str, Any]], Any],
        ]
    ),
) -> RunnableSerializable[Any, Any]

Assigns new fields to the dict output of this Runnable.

from langchain_community.llms.fake import FakeStreamingListLLM
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import SystemMessagePromptTemplate
from langchain_core.runnables import Runnable
from operator import itemgetter

prompt = (
    SystemMessagePromptTemplate.from_template("You are a nice assistant.")
    + "{question}"
)
llm = FakeStreamingListLLM(responses=["foo-lish"])

chain: Runnable = prompt | llm | {"str": StrOutputParser()}

chain_with_assign = chain.assign(hello=itemgetter("str") | llm)

print(chain_with_assign.input_schema.model_json_schema())
# {'title': 'PromptInput', 'type': 'object', 'properties':
{'question': {'title': 'Question', 'type': 'string'}}}
print(chain_with_assign.output_schema.model_json_schema())
# {'title': 'RunnableSequenceOutput', 'type': 'object', 'properties':
{'str': {'title': 'Str',
'type': 'string'}, 'hello': {'title': 'Hello', 'type': 'string'}}}

Parameters:

Name Type Description Default
**kwargs Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any] | Mapping[str, Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any]]

A mapping of keys to Runnable or Runnable-like objects that will be invoked with the entire output dict of this Runnable.

{}

Returns:

Type Description
RunnableSerializable[Any, Any]

A new Runnable.

batch_as_completed

batch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> Iterator[tuple[int, Output | Exception]]

Run invoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
tuple[int, Output | Exception]

Tuples of the index of the input and the output from the Runnable.

abatch_as_completed async

abatch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> AsyncIterator[tuple[int, Output | Exception]]

Run ainvoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[tuple[int, Output | Exception]]

A tuple of the index of the input and the output from the Runnable.

astream_log async

astream_log(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    diff: bool = True,
    with_streamed_output_list: bool = True,
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

Stream all output from a Runnable, as reported to the callback system.

This includes all inner runs of LLMs, Retrievers, Tools, etc.

Output is streamed as Log objects, which include a list of Jsonpatch ops that describe how the state of the run has changed in each step, and the final state of the run.

The Jsonpatch ops can be applied in order to construct state.

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
diff bool

Whether to yield diffs between each step or the current state.

True
with_streamed_output_list bool

Whether to yield the streamed_output list.

True
include_names Sequence[str] | None

Only include logs with these names.

None
include_types Sequence[str] | None

Only include logs with these types.

None
include_tags Sequence[str] | None

Only include logs with these tags.

None
exclude_names Sequence[str] | None

Exclude logs with these names.

None
exclude_types Sequence[str] | None

Exclude logs with these types.

None
exclude_tags Sequence[str] | None

Exclude logs with these tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

A RunLogPatch or RunLog object.

astream_events async

astream_events(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    version: Literal["v1", "v2"] = "v2",
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[StreamEvent]

Generate a stream of events.

Use to create an iterator over StreamEvents that provide real-time information about the progress of the Runnable, including StreamEvents from intermediate results.

A StreamEvent is a dictionary with the following schema:

  • event: str - Event names are of the format: on_[runnable_type]_(start|stream|end).
  • name: str - The name of the Runnable that generated the event.
  • run_id: str - randomly generated ID associated with the given execution of the Runnable that emitted the event. A child Runnable that gets invoked as part of the execution of a parent Runnable is assigned its own unique ID.
  • parent_ids: list[str] - The IDs of the parent runnables that generated the event. The root Runnable will have an empty list. The order of the parent IDs is from the root to the immediate parent. Only available for v2 version of the API. The v1 version of the API will return an empty list.
  • tags: Optional[list[str]] - The tags of the Runnable that generated the event.
  • metadata: Optional[dict[str, Any]] - The metadata of the Runnable that generated the event.
  • data: dict[str, Any]

Below is a table that illustrates some events that might be emitted by various chains. Metadata fields have been omitted from the table for brevity. Chain definitions have been included after the table.

Note

This reference table is for the v2 version of the schema.

+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | event | name | chunk | input | output | +==========================+==================+=====================================+===================================================+=====================================================+ | on_chat_model_start | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_stream | [model name] | AIMessageChunk(content="hello") | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_end | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | AIMessageChunk(content="hello world") | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_start | [model name] | | {'input': 'hello'} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_stream | [model name] | 'Hello' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_end | [model name] | | 'Hello human!' | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_start | format_docs | | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_stream | format_docs | 'hello world!, goodbye world!' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_end | format_docs | | [Document(...)] | 'hello world!, goodbye world!' | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_start | some_tool | | {"x": 1, "y": "2"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_end | some_tool | | | {"x": 1, "y": "2"} | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_start | [retriever name] | | {"query": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_end | [retriever name] | | {"query": "hello"} | [Document(...), ..] | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_start | [template_name] | | {"question": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_end | [template_name] | | {"question": "hello"} | ChatPromptValue(messages: [SystemMessage, ...]) | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+

In addition to the standard events, users can also dispatch custom events (see example below).

Custom events will be only be surfaced with in the v2 version of the API!

A custom event has following format:

+-----------+------+-----------------------------------------------------------------------------------------------------------+ | Attribute | Type | Description | +===========+======+===========================================================================================================+ | name | str | A user defined name for the event. | +-----------+------+-----------------------------------------------------------------------------------------------------------+ | data | Any | The data associated with the event. This can be anything, though we suggest making it JSON serializable. | +-----------+------+-----------------------------------------------------------------------------------------------------------+

Here are declarations associated with the standard events shown above:

format_docs:

def format_docs(docs: list[Document]) -> str:
    '''Format the docs.'''
    return ", ".join([doc.page_content for doc in docs])


format_docs = RunnableLambda(format_docs)

some_tool:

@tool
def some_tool(x: int, y: str) -> dict:
    '''Some_tool.'''
    return {"x": x, "y": y}

prompt:

template = ChatPromptTemplate.from_messages(
    [
        ("system", "You are Cat Agent 007"),
        ("human", "{question}"),
    ]
).with_config({"run_name": "my_template", "tags": ["my_template"]})
Example:

from langchain_core.runnables import RunnableLambda


async def reverse(s: str) -> str:
    return s[::-1]


chain = RunnableLambda(func=reverse)

events = [event async for event in chain.astream_events("hello", version="v2")]

# will produce the following events (run_id, and parent_ids
# has been omitted for brevity):
[
    {
        "data": {"input": "hello"},
        "event": "on_chain_start",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"chunk": "olleh"},
        "event": "on_chain_stream",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"output": "olleh"},
        "event": "on_chain_end",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
]

Example: Dispatch Custom Event

from langchain_core.callbacks.manager import (
    adispatch_custom_event,
)
from langchain_core.runnables import RunnableLambda, RunnableConfig
import asyncio


async def slow_thing(some_input: str, config: RunnableConfig) -> str:
    """Do something that takes a long time."""
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 1 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 2 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    return "Done"

slow_thing = RunnableLambda(slow_thing)

async for event in slow_thing.astream_events("some_input", version="v2"):
    print(event)

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
version Literal['v1', 'v2']

The version of the schema to use either 'v2' or 'v1'. Users should use 'v2'. 'v1' is for backwards compatibility and will be deprecated in 0.4.0. No default will be assigned until the API is stabilized. custom events will only be surfaced in 'v2'.

'v2'
include_names Sequence[str] | None

Only include events from Runnables with matching names.

None
include_types Sequence[str] | None

Only include events from Runnables with matching types.

None
include_tags Sequence[str] | None

Only include events from Runnables with matching tags.

None
exclude_names Sequence[str] | None

Exclude events from Runnables with matching names.

None
exclude_types Sequence[str] | None

Exclude events from Runnables with matching types.

None
exclude_tags Sequence[str] | None

Exclude events from Runnables with matching tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable. These will be passed to astream_log as this implementation of astream_events is built on top of astream_log.

{}

Yields:

Type Description
AsyncIterator[StreamEvent]

An async stream of StreamEvents.

Raises:

Type Description
NotImplementedError

If the version is not 'v1' or 'v2'.

transform

transform(
    input: Iterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> Iterator[Output]

Transform inputs to outputs.

Default implementation of transform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input Iterator[Input]

An iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
Output

The output of the Runnable.

atransform async

atransform(
    input: AsyncIterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> AsyncIterator[Output]

Transform inputs to outputs.

Default implementation of atransform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input AsyncIterator[Input]

An async iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[Output]

The output of the Runnable.

bind

bind(**kwargs: Any) -> Runnable[Input, Output]

Bind arguments to a Runnable, returning a new Runnable.

Useful when a Runnable in a chain requires an argument that is not in the output of the previous Runnable or included in the user input.

Parameters:

Name Type Description Default
kwargs Any

The arguments to bind to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the arguments bound.

Example
from langchain_ollama import ChatOllama
from langchain_core.output_parsers import StrOutputParser

llm = ChatOllama(model="llama3.1")

# Without bind.
chain = llm | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two three four five.'

# With bind.
chain = llm.bind(stop=["three"]) | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two'

with_config

with_config(
    config: RunnableConfig | None = None, **kwargs: Any
) -> Runnable[Input, Output]

Bind config to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

The config to bind to the Runnable.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the config bound.

with_listeners

with_listeners(
    *,
    on_start: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_end: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_error: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None
) -> Runnable[Input, Output]

Bind lifecycle listeners to a Runnable, returning a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called before the Runnable starts running, with the Run object. Defaults to None.

None
on_end Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda
from langchain_core.tracers.schemas import Run

import time


def test_runnable(time_to_sleep: int):
    time.sleep(time_to_sleep)


def fn_start(run_obj: Run):
    print("start_time:", run_obj.start_time)


def fn_end(run_obj: Run):
    print("end_time:", run_obj.end_time)


chain = RunnableLambda(test_runnable).with_listeners(
    on_start=fn_start, on_end=fn_end
)
chain.invoke(2)

with_alisteners

with_alisteners(
    *,
    on_start: AsyncListener | None = None,
    on_end: AsyncListener | None = None,
    on_error: AsyncListener | None = None
) -> Runnable[Input, Output]

Bind async lifecycle listeners to a Runnable.

Returns a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start AsyncListener | None

Called asynchronously before the Runnable starts running, with the Run object. Defaults to None.

None
on_end AsyncListener | None

Called asynchronously after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error AsyncListener | None

Called asynchronously if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda, Runnable
from datetime import datetime, timezone
import time
import asyncio

def format_t(timestamp: float) -> str:
    return datetime.fromtimestamp(timestamp, tz=timezone.utc).isoformat()

async def test_runnable(time_to_sleep: int):
    print(f"Runnable[{time_to_sleep}s]: starts at {format_t(time.time())}")
    await asyncio.sleep(time_to_sleep)
    print(f"Runnable[{time_to_sleep}s]: ends at {format_t(time.time())}")

async def fn_start(run_obj: Runnable):
    print(f"on start callback starts at {format_t(time.time())}")
    await asyncio.sleep(3)
    print(f"on start callback ends at {format_t(time.time())}")

async def fn_end(run_obj: Runnable):
    print(f"on end callback starts at {format_t(time.time())}")
    await asyncio.sleep(2)
    print(f"on end callback ends at {format_t(time.time())}")

runnable = RunnableLambda(test_runnable).with_alisteners(
    on_start=fn_start,
    on_end=fn_end
)
async def concurrent_runs():
    await asyncio.gather(runnable.ainvoke(2), runnable.ainvoke(3))

asyncio.run(concurrent_runs())
Result:
on start callback starts at 2025-03-01T07:05:22.875378+00:00
on start callback starts at 2025-03-01T07:05:22.875495+00:00
on start callback ends at 2025-03-01T07:05:25.878862+00:00
on start callback ends at 2025-03-01T07:05:25.878947+00:00
Runnable[2s]: starts at 2025-03-01T07:05:25.879392+00:00
Runnable[3s]: starts at 2025-03-01T07:05:25.879804+00:00
Runnable[2s]: ends at 2025-03-01T07:05:27.881998+00:00
on end callback starts at 2025-03-01T07:05:27.882360+00:00
Runnable[3s]: ends at 2025-03-01T07:05:28.881737+00:00
on end callback starts at 2025-03-01T07:05:28.882428+00:00
on end callback ends at 2025-03-01T07:05:29.883893+00:00
on end callback ends at 2025-03-01T07:05:30.884831+00:00

with_types

with_types(
    *,
    input_type: type[Input] | None = None,
    output_type: type[Output] | None = None
) -> Runnable[Input, Output]

Bind input and output types to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
input_type type[Input] | None

The input type to bind to the Runnable. Defaults to None.

None
output_type type[Output] | None

The output type to bind to the Runnable. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the types bound.

with_retry

with_retry(
    *,
    retry_if_exception_type: tuple[
        type[BaseException], ...
    ] = (Exception,),
    wait_exponential_jitter: bool = True,
    exponential_jitter_params: (
        ExponentialJitterParams | None
    ) = None,
    stop_after_attempt: int = 3
) -> Runnable[Input, Output]

Create a new Runnable that retries the original Runnable on exceptions.

Parameters:

Name Type Description Default
retry_if_exception_type tuple[type[BaseException], ...]

A tuple of exception types to retry on. Defaults to (Exception,).

(Exception,)
wait_exponential_jitter bool

Whether to add jitter to the wait time between retries. Defaults to True.

True
stop_after_attempt int

The maximum number of attempts to make before giving up. Defaults to 3.

3
exponential_jitter_params ExponentialJitterParams | None

Parameters for tenacity.wait_exponential_jitter. Namely: initial, max, exp_base, and jitter (all float values).

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable that retries the original Runnable on exceptions.

Example
from langchain_core.runnables import RunnableLambda

count = 0


def _lambda(x: int) -> None:
    global count
    count = count + 1
    if x == 1:
        raise ValueError("x is 1")
    else:
        pass


runnable = RunnableLambda(_lambda)
try:
    runnable.with_retry(
        stop_after_attempt=2,
        retry_if_exception_type=(ValueError,),
    ).invoke(1)
except ValueError:
    pass

assert count == 2

map

map() -> Runnable[list[Input], list[Output]]

Return a new Runnable that maps a list of inputs to a list of outputs.

Calls invoke with each input.

Returns:

Type Description
Runnable[list[Input], list[Output]]

A new Runnable that maps a list of inputs to a list of outputs.

Example
from langchain_core.runnables import RunnableLambda


def _lambda(x: int) -> int:
    return x + 1


runnable = RunnableLambda(_lambda)
print(runnable.map().invoke([1, 2, 3]))  # [2, 3, 4]

with_fallbacks

with_fallbacks(
    fallbacks: Sequence[Runnable[Input, Output]],
    *,
    exceptions_to_handle: tuple[
        type[BaseException], ...
    ] = (Exception,),
    exception_key: str | None = None
) -> RunnableWithFallbacks[Input, Output]

Add fallbacks to a Runnable, returning a new Runnable.

The new Runnable will try the original Runnable, and then each fallback in order, upon failures.

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle. Defaults to (Exception,).

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input. Defaults to None.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

Example
from typing import Iterator

from langchain_core.runnables import RunnableGenerator


def _generate_immediate_error(input: Iterator) -> Iterator[str]:
    raise ValueError()
    yield ""


def _generate(input: Iterator) -> Iterator[str]:
    yield from "foo bar"


runnable = RunnableGenerator(_generate_immediate_error).with_fallbacks(
    [RunnableGenerator(_generate)]
)
print("".join(runnable.stream({})))  # foo bar

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle.

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

as_tool

as_tool(
    args_schema: type[BaseModel] | None = None,
    *,
    name: str | None = None,
    description: str | None = None,
    arg_types: dict[str, type] | None = None
) -> BaseTool

Create a BaseTool from a Runnable.

as_tool will instantiate a BaseTool with a name, description, and args_schema from a Runnable. Where possible, schemas are inferred from runnable.get_input_schema. Alternatively (e.g., if the Runnable takes a dict as input and the specific dict keys are not typed), the schema can be specified directly with args_schema. You can also pass arg_types to just specify the required arguments and their types.

Parameters:

Name Type Description Default
args_schema type[BaseModel] | None

The schema for the tool. Defaults to None.

None
name str | None

The name of the tool. Defaults to None.

None
description str | None

The description of the tool. Defaults to None.

None
arg_types dict[str, type] | None

A dictionary of argument names to types. Defaults to None.

None

Returns:

Type Description
BaseTool

A BaseTool instance.

Typed dict input:

from typing_extensions import TypedDict
from langchain_core.runnables import RunnableLambda


class Args(TypedDict):
    a: int
    b: list[int]


def f(x: Args) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool()
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via args_schema:

from typing import Any
from pydantic import BaseModel, Field
from langchain_core.runnables import RunnableLambda

def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))

class FSchema(BaseModel):
    """Apply a function to an integer and list of integers."""

    a: int = Field(..., description="Integer")
    b: list[int] = Field(..., description="List of ints")

runnable = RunnableLambda(f)
as_tool = runnable.as_tool(FSchema)
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via arg_types:

from typing import Any
from langchain_core.runnables import RunnableLambda


def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool(arg_types={"a": int, "b": list[int]})
as_tool.invoke({"a": 3, "b": [1, 2]})

String input:

from langchain_core.runnables import RunnableLambda


def f(x: str) -> str:
    return x + "a"


def g(x: str) -> str:
    return x + "z"


runnable = RunnableLambda(f) | g
as_tool = runnable.as_tool()
as_tool.invoke("b")

Added in version 0.2.14

lc_id classmethod

lc_id() -> list[str]

Return a unique identifier for this class for serialization purposes.

The unique identifier is a list of strings that describes the path to the object. For example, for the class langchain.llms.openai.OpenAI, the id is ["langchain", "llms", "openai", "OpenAI"].

to_json

to_json() -> (
    SerializedConstructor | SerializedNotImplemented
)

Serialize the Runnable to JSON.

Returns:

Type Description
SerializedConstructor | SerializedNotImplemented

A JSON-serializable representation of the Runnable.

to_json_not_implemented

to_json_not_implemented() -> SerializedNotImplemented

Serialize a "not implemented" object.

Returns:

Type Description
SerializedNotImplemented

SerializedNotImplemented.

configurable_fields

configurable_fields(
    **kwargs: AnyConfigurableField,
) -> RunnableSerializable[Input, Output]

Configure particular Runnable fields at runtime.

Parameters:

Name Type Description Default
**kwargs AnyConfigurableField

A dictionary of ConfigurableField instances to configure.

{}

Raises:

Type Description
ValueError

If a configuration key is not found in the Runnable.

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the fields configured.

from langchain_core.runnables import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatOpenAI(max_tokens=20).configurable_fields(
    max_tokens=ConfigurableField(
        id="output_token_number",
        name="Max tokens in the output",
        description="The maximum number of tokens in the output",
    )
)

# max_tokens = 20
print("max_tokens_20: ", model.invoke("tell me something about chess").content)

# max_tokens = 200
print(
    "max_tokens_200: ",
    model.with_config(configurable={"output_token_number": 200})
    .invoke("tell me something about chess")
    .content,
)

configurable_alternatives

configurable_alternatives(
    which: ConfigurableField,
    *,
    default_key: str = "default",
    prefix_keys: bool = False,
    **kwargs: (
        Runnable[Input, Output]
        | Callable[[], Runnable[Input, Output]]
    )
) -> RunnableSerializable[Input, Output]

Configure alternatives for Runnables that can be set at runtime.

Parameters:

Name Type Description Default
which ConfigurableField

The ConfigurableField instance that will be used to select the alternative.

required
default_key str

The default key to use if no alternative is selected. Defaults to 'default'.

'default'
prefix_keys bool

Whether to prefix the keys with the ConfigurableField id. Defaults to False.

False
**kwargs Runnable[Input, Output] | Callable[[], Runnable[Input, Output]]

A dictionary of keys to Runnable instances or callables that return Runnable instances.

{}

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the alternatives configured.

from langchain_anthropic import ChatAnthropic
from langchain_core.runnables.utils import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatAnthropic(
    model_name="claude-3-7-sonnet-20250219"
).configurable_alternatives(
    ConfigurableField(id="llm"),
    default_key="anthropic",
    openai=ChatOpenAI(),
)

# uses the default model ChatAnthropic
print(model.invoke("which organization created you?").content)

# uses ChatOpenAI
print(
    model.with_config(configurable={"llm": "openai"})
    .invoke("which organization created you?")
    .content
)

set_verbose

set_verbose(verbose: bool | None) -> bool

If verbose is None, set it.

This allows users to pass in None as verbose to access the global setting.

Parameters:

Name Type Description Default
verbose bool | None

The verbosity setting to use.

required

Returns:

Type Description
bool

The verbosity setting to use.

with_structured_output

with_structured_output(
    schema: dict | type, **kwargs: Any
) -> Runnable[LanguageModelInput, dict | BaseModel]

Not implemented on this class.

get_token_ids

get_token_ids(text: str) -> list[int]

Return the ordered ids of the tokens in a text.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
list[int]

A list of ids corresponding to the tokens in the text, in order they occur

list[int]

in the text.

get_num_tokens_from_messages

get_num_tokens_from_messages(
    messages: list[BaseMessage],
    tools: Sequence | None = None,
) -> int

Get the number of tokens in the messages.

Useful for checking if an input fits in a model's context window.

Note

The base implementation of get_num_tokens_from_messages ignores tool schemas.

Parameters:

Name Type Description Default
messages list[BaseMessage]

The message inputs to tokenize.

required
tools Sequence | None

If provided, sequence of dict, BaseModel, function, or BaseTools to be converted to tool schemas.

None

Returns:

Type Description
int

The sum of the number of tokens across the messages.

generate

generate(
    prompts: list[str],
    stop: list[str] | None = None,
    callbacks: Callbacks | list[Callbacks] | None = None,
    *,
    tags: list[str] | list[list[str]] | None = None,
    metadata: (
        dict[str, Any] | list[dict[str, Any]] | None
    ) = None,
    run_name: str | list[str] | None = None,
    run_id: UUID | list[UUID | None] | None = None,
    **kwargs: Any
) -> LLMResult

Pass a sequence of prompts to a model and return generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
prompts list[str]

List of string prompts.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks | list[Callbacks] | None

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | list[list[str]] | None

List of tags to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
metadata dict[str, Any] | list[dict[str, Any]] | None

List of metadata dictionaries to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
run_name str | list[str] | None

List of run names to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
run_id UUID | list[UUID | None] | None

List of run IDs to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Raises:

Type Description
ValueError

If prompts is not a list.

ValueError

If the length of callbacks, tags, metadata, or run_name (if provided) does not match the length of prompts.

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input prompt and additional model provider-specific output.

agenerate async

agenerate(
    prompts: list[str],
    stop: list[str] | None = None,
    callbacks: Callbacks | list[Callbacks] | None = None,
    *,
    tags: list[str] | list[list[str]] | None = None,
    metadata: (
        dict[str, Any] | list[dict[str, Any]] | None
    ) = None,
    run_name: str | list[str] | None = None,
    run_id: UUID | list[UUID | None] | None = None,
    **kwargs: Any
) -> LLMResult

Asynchronously pass a sequence of prompts to a model and return generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
prompts list[str]

List of string prompts.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks | list[Callbacks] | None

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | list[list[str]] | None

List of tags to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
metadata dict[str, Any] | list[dict[str, Any]] | None

List of metadata dictionaries to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
run_name str | list[str] | None

List of run names to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
run_id UUID | list[UUID | None] | None

List of run IDs to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Raises:

Type Description
ValueError

If the length of callbacks, tags, metadata, or run_name (if provided) does not match the length of prompts.

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input prompt and additional model provider-specific output.

__str__

__str__() -> str

Return a string representation of the object for printing.

dict

dict(**kwargs: Any) -> dict

Return a dictionary of the LLM.

save

save(file_path: Path | str) -> None

Save the LLM.

Parameters:

Name Type Description Default
file_path Path | str

Path to file to save the LLM to.

required

Raises:

Type Description
ValueError

If the file path is not a string or Path object.

Example:

.. code-block:: python

    llm.save(file_path="path/llm.yaml")

__init__

__init__(
    *, model_name: Optional[str] = None, **kwargs: Any
) -> None

Needed for mypy typing to recognize model_name as a valid arg and for arg validation.

get_lc_namespace classmethod

get_lc_namespace() -> List[str]

Get the namespace of the langchain object.

validate_environment

validate_environment() -> Self

Validate that the python package exists in environment.

get_num_tokens

get_num_tokens(text: str) -> int

Get the number of tokens present in the text.

Useful for checking if an input will fit in a model's context window.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
int

The integer number of tokens in the text.

VertexAIModelGarden

Bases: _BaseVertexAIModelGarden, BaseLLM

Large language models served from Vertex AI Model Garden.

Methods:

Name Description
get_name

Get the name of the Runnable.

get_input_schema

Get a pydantic model that can be used to validate input to the Runnable.

get_input_jsonschema

Get a JSON schema that represents the input to the Runnable.

get_output_schema

Get a pydantic model that can be used to validate output to the Runnable.

get_output_jsonschema

Get a JSON schema that represents the output of the Runnable.

config_schema

The type of config this Runnable accepts specified as a pydantic model.

get_config_jsonschema

Get a JSON schema that represents the config of the Runnable.

get_graph

Return a graph representation of this Runnable.

get_prompts

Return a list of prompts used by this Runnable.

__or__

Runnable "or" operator.

__ror__

Runnable "reverse-or" operator.

pipe

Pipe runnables.

pick

Pick keys from the output dict of this Runnable.

assign

Assigns new fields to the dict output of this Runnable.

batch_as_completed

Run invoke in parallel on a list of inputs.

abatch_as_completed

Run ainvoke in parallel on a list of inputs.

astream_log

Stream all output from a Runnable, as reported to the callback system.

astream_events

Generate a stream of events.

transform

Transform inputs to outputs.

atransform

Transform inputs to outputs.

bind

Bind arguments to a Runnable, returning a new Runnable.

with_config

Bind config to a Runnable, returning a new Runnable.

with_listeners

Bind lifecycle listeners to a Runnable, returning a new Runnable.

with_alisteners

Bind async lifecycle listeners to a Runnable.

with_types

Bind input and output types to a Runnable, returning a new Runnable.

with_retry

Create a new Runnable that retries the original Runnable on exceptions.

map

Return a new Runnable that maps a list of inputs to a list of outputs.

with_fallbacks

Add fallbacks to a Runnable, returning a new Runnable.

as_tool

Create a BaseTool from a Runnable.

is_lc_serializable

Is this class serializable?

get_lc_namespace

Get the namespace of the langchain object.

lc_id

Return a unique identifier for this class for serialization purposes.

to_json

Serialize the Runnable to JSON.

to_json_not_implemented

Serialize a "not implemented" object.

configurable_fields

Configure particular Runnable fields at runtime.

configurable_alternatives

Configure alternatives for Runnables that can be set at runtime.

set_verbose

If verbose is None, set it.

with_structured_output

Not implemented on this class.

get_token_ids

Return the ordered ids of the tokens in a text.

get_num_tokens

Get the number of tokens present in the text.

get_num_tokens_from_messages

Get the number of tokens in the messages.

generate

Pass a sequence of prompts to a model and return generations.

agenerate

Asynchronously pass a sequence of prompts to a model and return generations.

__str__

Return a string representation of the object for printing.

dict

Return a dictionary of the LLM.

save

Save the LLM.

validate_environment

Validate that the python package exists in environment.

Attributes:

Name Type Description
InputType TypeAlias

Get the input type for this runnable.

OutputType type[str]

Get the input type for this runnable.

input_schema type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema type[BaseModel]

Output schema.

config_specs list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets dict[str, str]

A map of constructor argument names to secret ids.

lc_attributes dict

List of attribute names that should be included in the serialized kwargs.

cache BaseCache | bool | None

Whether to cache the response.

verbose bool

Whether to print out response text.

callbacks Callbacks

Callbacks to add to the run trace.

tags list[str] | None

Tags to add to the run trace.

metadata dict[str, Any] | None

Metadata to add to the run trace.

custom_get_token_ids Callable[[str], list[int]] | None

Optional encoder to use for counting tokens.

project Optional[str]

The default GCP project to use when making Vertex API calls.

location str

The default location to use when making API calls.

request_parallelism int

The amount of parallelism allowed for requests issued to VertexAI models.

max_retries int

The maximum number of retries to make when generating.

stop Optional[List[str]]

Optional list of stop words to use when generating.

model_name Optional[str]

Underlying model name.

full_model_name Optional[str]

The full name of the model's endpoint.

api_endpoint Optional[str]

Desired API endpoint, e.g., us-central1-aiplatform.googleapis.com

api_transport Optional[str]

The desired API transport method, can be either 'grpc' or 'rest'.

additional_headers Optional[Dict[str, str]]

A key-value dictionary representing additional headers for the model call

client_cert_source Optional[Callable[[], Tuple[bytes, bytes]]]

A callback which returns client certificate bytes and private key bytes both

credentials Any

The default custom credentials (google.auth.credentials.Credentials) to use

endpoint_version Literal['v1', 'v1beta1']

Whether to use v1 or v1beta1 endpoint.

prediction_client Union[PredictionServiceClient, PredictionServiceClient]

Returns PredictionServiceClient.

async_prediction_client Union[PredictionServiceAsyncClient, PredictionServiceAsyncClient]

Returns PredictionServiceClient.

endpoint_id str

A name of an endpoint where the model has been deployed.

allowed_model_args Optional[List[str]]

Allowed optional args to be passed to the model.

result_arg Optional[str]

Set result_arg to None if output of the model is expected to be a string.

single_example_per_request bool

LLM endpoint currently serves only the first example in the request

InputType property

InputType: TypeAlias

Get the input type for this runnable.

OutputType property

OutputType: type[str]

Get the input type for this runnable.

input_schema property

input_schema: type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema property

output_schema: type[BaseModel]

Output schema.

The type of output this Runnable produces specified as a pydantic model.

config_specs property

config_specs: list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets property

lc_secrets: dict[str, str]

A map of constructor argument names to secret ids.

For example,

lc_attributes property

lc_attributes: dict

List of attribute names that should be included in the serialized kwargs.

These attributes must be accepted by the constructor. Default is an empty dictionary.

cache class-attribute instance-attribute

cache: BaseCache | bool | None = Field(
    default=None, exclude=True
)

Whether to cache the response.

  • If true, will use the global cache.
  • If false, will not use a cache
  • If None, will use the global cache if it's set, otherwise no cache.
  • If instance of BaseCache, will use the provided cache.

Caching is not currently supported for streaming methods of models.

verbose class-attribute instance-attribute

verbose: bool = Field(
    default_factory=_get_verbosity, exclude=True, repr=False
)

Whether to print out response text.

callbacks class-attribute instance-attribute

callbacks: Callbacks = Field(default=None, exclude=True)

Callbacks to add to the run trace.

tags class-attribute instance-attribute

tags: list[str] | None = Field(default=None, exclude=True)

Tags to add to the run trace.

metadata class-attribute instance-attribute

metadata: dict[str, Any] | None = Field(
    default=None, exclude=True
)

Metadata to add to the run trace.

custom_get_token_ids class-attribute instance-attribute

custom_get_token_ids: Callable[[str], list[int]] | None = (
    Field(default=None, exclude=True)
)

Optional encoder to use for counting tokens.

project class-attribute instance-attribute

project: Optional[str] = None

The default GCP project to use when making Vertex API calls.

location class-attribute instance-attribute

location: str = Field(default=_DEFAULT_LOCATION)

The default location to use when making API calls.

request_parallelism class-attribute instance-attribute

request_parallelism: int = 5

The amount of parallelism allowed for requests issued to VertexAI models.

max_retries class-attribute instance-attribute

max_retries: int = 6

The maximum number of retries to make when generating.

stop class-attribute instance-attribute

stop: Optional[List[str]] = Field(
    default=None, alias="stop_sequences"
)

Optional list of stop words to use when generating.

model_name class-attribute instance-attribute

model_name: Optional[str] = Field(
    default=None, alias="model"
)

Underlying model name.

full_model_name class-attribute instance-attribute

full_model_name: Optional[str] = Field(
    default=None, exclude=True
)

The full name of the model's endpoint.

api_endpoint class-attribute instance-attribute

api_endpoint: Optional[str] = Field(
    default=None, alias="base_url"
)

Desired API endpoint, e.g., us-central1-aiplatform.googleapis.com

api_transport class-attribute instance-attribute

api_transport: Optional[str] = None

The desired API transport method, can be either 'grpc' or 'rest'. Uses the default parameter in vertexai.init if defined.

additional_headers class-attribute instance-attribute

additional_headers: Optional[Dict[str, str]] = Field(
    default=None
)

A key-value dictionary representing additional headers for the model call

client_cert_source class-attribute instance-attribute

client_cert_source: Optional[
    Callable[[], Tuple[bytes, bytes]]
] = None

A callback which returns client certificate bytes and private key bytes both

credentials class-attribute instance-attribute

credentials: Any = Field(default=None, exclude=True)

The default custom credentials (google.auth.credentials.Credentials) to use

endpoint_version class-attribute instance-attribute

endpoint_version: Literal['v1', 'v1beta1'] = 'v1beta1'

Whether to use v1 or v1beta1 endpoint.

v1 is more performant, but v1beta1 might have some new features.

prediction_client property

prediction_client: Union[
    PredictionServiceClient, PredictionServiceClient
]

Returns PredictionServiceClient.

async_prediction_client property

async_prediction_client: Union[
    PredictionServiceAsyncClient,
    PredictionServiceAsyncClient,
]

Returns PredictionServiceClient.

endpoint_id instance-attribute

endpoint_id: str

A name of an endpoint where the model has been deployed.

allowed_model_args class-attribute instance-attribute

allowed_model_args: Optional[List[str]] = None

Allowed optional args to be passed to the model.

result_arg class-attribute instance-attribute

result_arg: Optional[str] = 'generated_text'

Set result_arg to None if output of the model is expected to be a string.

single_example_per_request class-attribute instance-attribute

single_example_per_request: bool = True

LLM endpoint currently serves only the first example in the request

get_name

get_name(
    suffix: str | None = None, *, name: str | None = None
) -> str

Get the name of the Runnable.

Parameters:

Name Type Description Default
suffix str | None

An optional suffix to append to the name.

None
name str | None

An optional name to use instead of the Runnable's name.

None

Returns:

Type Description
str

The name of the Runnable.

get_input_schema

get_input_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate input to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic input schema that depends on which configuration the Runnable is invoked with.

This method allows to get an input schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate input.

get_input_jsonschema

get_input_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the input to the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the input to the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_input_jsonschema())

Added in version 0.3.0

get_output_schema

get_output_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate output to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic output schema that depends on which configuration the Runnable is invoked with.

This method allows to get an output schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate output.

get_output_jsonschema

get_output_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the output of the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the output of the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_output_jsonschema())

Added in version 0.3.0

config_schema

config_schema(
    *, include: Sequence[str] | None = None
) -> type[BaseModel]

The type of config this Runnable accepts specified as a pydantic model.

To mark a field as configurable, see the configurable_fields and configurable_alternatives methods.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate config.

get_config_jsonschema

get_config_jsonschema(
    *, include: Sequence[str] | None = None
) -> dict[str, Any]

Get a JSON schema that represents the config of the Runnable.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the config of the Runnable.

Added in version 0.3.0

get_graph

get_graph(config: RunnableConfig | None = None) -> Graph

Return a graph representation of this Runnable.

get_prompts

get_prompts(
    config: RunnableConfig | None = None,
) -> list[BasePromptTemplate]

Return a list of prompts used by this Runnable.

__or__

__or__(
    other: (
        Runnable[Any, Other]
        | Callable[[Iterator[Any]], Iterator[Other]]
        | Callable[
            [AsyncIterator[Any]], AsyncIterator[Other]
        ]
        | Callable[[Any], Other]
        | Mapping[
            str,
            Runnable[Any, Other]
            | Callable[[Any], Other]
            | Any,
        ]
    ),
) -> RunnableSerializable[Input, Other]

Runnable "or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Any, Other] | Callable[[Iterator[Any]], Iterator[Other]] | Callable[[AsyncIterator[Any]], AsyncIterator[Other]] | Callable[[Any], Other] | Mapping[str, Runnable[Any, Other] | Callable[[Any], Other] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

__ror__

__ror__(
    other: (
        Runnable[Other, Any]
        | Callable[[Iterator[Other]], Iterator[Any]]
        | Callable[
            [AsyncIterator[Other]], AsyncIterator[Any]
        ]
        | Callable[[Other], Any]
        | Mapping[
            str,
            Runnable[Other, Any]
            | Callable[[Other], Any]
            | Any,
        ]
    ),
) -> RunnableSerializable[Other, Output]

Runnable "reverse-or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Other, Any] | Callable[[Iterator[Other]], Iterator[Any]] | Callable[[AsyncIterator[Other]], AsyncIterator[Any]] | Callable[[Other], Any] | Mapping[str, Runnable[Other, Any] | Callable[[Other], Any] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Other, Output]

A new Runnable.

pipe

pipe(
    *others: Runnable[Any, Other] | Callable[[Any], Other],
    name: str | None = None
) -> RunnableSerializable[Input, Other]

Pipe runnables.

Compose this Runnable with Runnable-like objects to make a RunnableSequence.

Equivalent to RunnableSequence(self, *others) or self | others[0] | ...

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


def mul_two(x: int) -> int:
    return x * 2


runnable_1 = RunnableLambda(add_one)
runnable_2 = RunnableLambda(mul_two)
sequence = runnable_1.pipe(runnable_2)
# Or equivalently:
# sequence = runnable_1 | runnable_2
# sequence = RunnableSequence(first=runnable_1, last=runnable_2)
sequence.invoke(1)
await sequence.ainvoke(1)
# -> 4

sequence.batch([1, 2, 3])
await sequence.abatch([1, 2, 3])
# -> [4, 6, 8]

Parameters:

Name Type Description Default
*others Runnable[Any, Other] | Callable[[Any], Other]

Other Runnable or Runnable-like objects to compose

()
name str | None

An optional name for the resulting RunnableSequence.

None

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

pick

pick(
    keys: str | list[str],
) -> RunnableSerializable[Any, Any]

Pick keys from the output dict of this Runnable.

Pick single key:

```python
import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)
chain = RunnableMap(str=as_str, json=as_json)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3]}

json_only_chain = chain.pick("json")
json_only_chain.invoke("[1, 2, 3]")
# -> [1, 2, 3]
```

Pick list of keys:

```python
from typing import Any

import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)


def as_bytes(x: Any) -> bytes:
    return bytes(x, "utf-8")


chain = RunnableMap(
    str=as_str, json=as_json, bytes=RunnableLambda(as_bytes)
)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3], "bytes": b"[1, 2, 3]"}

json_and_bytes_chain = chain.pick(["json", "bytes"])
json_and_bytes_chain.invoke("[1, 2, 3]")
# -> {"json": [1, 2, 3], "bytes": b"[1, 2, 3]"}
```

Parameters:

Name Type Description Default
keys str | list[str]

A key or list of keys to pick from the output dict.

required

Returns:

Type Description
RunnableSerializable[Any, Any]

a new Runnable.

assign

assign(
    **kwargs: (
        Runnable[dict[str, Any], Any]
        | Callable[[dict[str, Any]], Any]
        | Mapping[
            str,
            Runnable[dict[str, Any], Any]
            | Callable[[dict[str, Any]], Any],
        ]
    ),
) -> RunnableSerializable[Any, Any]

Assigns new fields to the dict output of this Runnable.

from langchain_community.llms.fake import FakeStreamingListLLM
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import SystemMessagePromptTemplate
from langchain_core.runnables import Runnable
from operator import itemgetter

prompt = (
    SystemMessagePromptTemplate.from_template("You are a nice assistant.")
    + "{question}"
)
llm = FakeStreamingListLLM(responses=["foo-lish"])

chain: Runnable = prompt | llm | {"str": StrOutputParser()}

chain_with_assign = chain.assign(hello=itemgetter("str") | llm)

print(chain_with_assign.input_schema.model_json_schema())
# {'title': 'PromptInput', 'type': 'object', 'properties':
{'question': {'title': 'Question', 'type': 'string'}}}
print(chain_with_assign.output_schema.model_json_schema())
# {'title': 'RunnableSequenceOutput', 'type': 'object', 'properties':
{'str': {'title': 'Str',
'type': 'string'}, 'hello': {'title': 'Hello', 'type': 'string'}}}

Parameters:

Name Type Description Default
**kwargs Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any] | Mapping[str, Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any]]

A mapping of keys to Runnable or Runnable-like objects that will be invoked with the entire output dict of this Runnable.

{}

Returns:

Type Description
RunnableSerializable[Any, Any]

A new Runnable.

batch_as_completed

batch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> Iterator[tuple[int, Output | Exception]]

Run invoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
tuple[int, Output | Exception]

Tuples of the index of the input and the output from the Runnable.

abatch_as_completed async

abatch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> AsyncIterator[tuple[int, Output | Exception]]

Run ainvoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[tuple[int, Output | Exception]]

A tuple of the index of the input and the output from the Runnable.

astream_log async

astream_log(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    diff: bool = True,
    with_streamed_output_list: bool = True,
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

Stream all output from a Runnable, as reported to the callback system.

This includes all inner runs of LLMs, Retrievers, Tools, etc.

Output is streamed as Log objects, which include a list of Jsonpatch ops that describe how the state of the run has changed in each step, and the final state of the run.

The Jsonpatch ops can be applied in order to construct state.

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
diff bool

Whether to yield diffs between each step or the current state.

True
with_streamed_output_list bool

Whether to yield the streamed_output list.

True
include_names Sequence[str] | None

Only include logs with these names.

None
include_types Sequence[str] | None

Only include logs with these types.

None
include_tags Sequence[str] | None

Only include logs with these tags.

None
exclude_names Sequence[str] | None

Exclude logs with these names.

None
exclude_types Sequence[str] | None

Exclude logs with these types.

None
exclude_tags Sequence[str] | None

Exclude logs with these tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

A RunLogPatch or RunLog object.

astream_events async

astream_events(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    version: Literal["v1", "v2"] = "v2",
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[StreamEvent]

Generate a stream of events.

Use to create an iterator over StreamEvents that provide real-time information about the progress of the Runnable, including StreamEvents from intermediate results.

A StreamEvent is a dictionary with the following schema:

  • event: str - Event names are of the format: on_[runnable_type]_(start|stream|end).
  • name: str - The name of the Runnable that generated the event.
  • run_id: str - randomly generated ID associated with the given execution of the Runnable that emitted the event. A child Runnable that gets invoked as part of the execution of a parent Runnable is assigned its own unique ID.
  • parent_ids: list[str] - The IDs of the parent runnables that generated the event. The root Runnable will have an empty list. The order of the parent IDs is from the root to the immediate parent. Only available for v2 version of the API. The v1 version of the API will return an empty list.
  • tags: Optional[list[str]] - The tags of the Runnable that generated the event.
  • metadata: Optional[dict[str, Any]] - The metadata of the Runnable that generated the event.
  • data: dict[str, Any]

Below is a table that illustrates some events that might be emitted by various chains. Metadata fields have been omitted from the table for brevity. Chain definitions have been included after the table.

Note

This reference table is for the v2 version of the schema.

+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | event | name | chunk | input | output | +==========================+==================+=====================================+===================================================+=====================================================+ | on_chat_model_start | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_stream | [model name] | AIMessageChunk(content="hello") | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_end | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | AIMessageChunk(content="hello world") | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_start | [model name] | | {'input': 'hello'} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_stream | [model name] | 'Hello' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_end | [model name] | | 'Hello human!' | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_start | format_docs | | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_stream | format_docs | 'hello world!, goodbye world!' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_end | format_docs | | [Document(...)] | 'hello world!, goodbye world!' | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_start | some_tool | | {"x": 1, "y": "2"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_end | some_tool | | | {"x": 1, "y": "2"} | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_start | [retriever name] | | {"query": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_end | [retriever name] | | {"query": "hello"} | [Document(...), ..] | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_start | [template_name] | | {"question": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_end | [template_name] | | {"question": "hello"} | ChatPromptValue(messages: [SystemMessage, ...]) | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+

In addition to the standard events, users can also dispatch custom events (see example below).

Custom events will be only be surfaced with in the v2 version of the API!

A custom event has following format:

+-----------+------+-----------------------------------------------------------------------------------------------------------+ | Attribute | Type | Description | +===========+======+===========================================================================================================+ | name | str | A user defined name for the event. | +-----------+------+-----------------------------------------------------------------------------------------------------------+ | data | Any | The data associated with the event. This can be anything, though we suggest making it JSON serializable. | +-----------+------+-----------------------------------------------------------------------------------------------------------+

Here are declarations associated with the standard events shown above:

format_docs:

def format_docs(docs: list[Document]) -> str:
    '''Format the docs.'''
    return ", ".join([doc.page_content for doc in docs])


format_docs = RunnableLambda(format_docs)

some_tool:

@tool
def some_tool(x: int, y: str) -> dict:
    '''Some_tool.'''
    return {"x": x, "y": y}

prompt:

template = ChatPromptTemplate.from_messages(
    [
        ("system", "You are Cat Agent 007"),
        ("human", "{question}"),
    ]
).with_config({"run_name": "my_template", "tags": ["my_template"]})
Example:

from langchain_core.runnables import RunnableLambda


async def reverse(s: str) -> str:
    return s[::-1]


chain = RunnableLambda(func=reverse)

events = [event async for event in chain.astream_events("hello", version="v2")]

# will produce the following events (run_id, and parent_ids
# has been omitted for brevity):
[
    {
        "data": {"input": "hello"},
        "event": "on_chain_start",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"chunk": "olleh"},
        "event": "on_chain_stream",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"output": "olleh"},
        "event": "on_chain_end",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
]

Example: Dispatch Custom Event

from langchain_core.callbacks.manager import (
    adispatch_custom_event,
)
from langchain_core.runnables import RunnableLambda, RunnableConfig
import asyncio


async def slow_thing(some_input: str, config: RunnableConfig) -> str:
    """Do something that takes a long time."""
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 1 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 2 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    return "Done"

slow_thing = RunnableLambda(slow_thing)

async for event in slow_thing.astream_events("some_input", version="v2"):
    print(event)

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
version Literal['v1', 'v2']

The version of the schema to use either 'v2' or 'v1'. Users should use 'v2'. 'v1' is for backwards compatibility and will be deprecated in 0.4.0. No default will be assigned until the API is stabilized. custom events will only be surfaced in 'v2'.

'v2'
include_names Sequence[str] | None

Only include events from Runnables with matching names.

None
include_types Sequence[str] | None

Only include events from Runnables with matching types.

None
include_tags Sequence[str] | None

Only include events from Runnables with matching tags.

None
exclude_names Sequence[str] | None

Exclude events from Runnables with matching names.

None
exclude_types Sequence[str] | None

Exclude events from Runnables with matching types.

None
exclude_tags Sequence[str] | None

Exclude events from Runnables with matching tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable. These will be passed to astream_log as this implementation of astream_events is built on top of astream_log.

{}

Yields:

Type Description
AsyncIterator[StreamEvent]

An async stream of StreamEvents.

Raises:

Type Description
NotImplementedError

If the version is not 'v1' or 'v2'.

transform

transform(
    input: Iterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> Iterator[Output]

Transform inputs to outputs.

Default implementation of transform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input Iterator[Input]

An iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
Output

The output of the Runnable.

atransform async

atransform(
    input: AsyncIterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> AsyncIterator[Output]

Transform inputs to outputs.

Default implementation of atransform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input AsyncIterator[Input]

An async iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[Output]

The output of the Runnable.

bind

bind(**kwargs: Any) -> Runnable[Input, Output]

Bind arguments to a Runnable, returning a new Runnable.

Useful when a Runnable in a chain requires an argument that is not in the output of the previous Runnable or included in the user input.

Parameters:

Name Type Description Default
kwargs Any

The arguments to bind to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the arguments bound.

Example
from langchain_ollama import ChatOllama
from langchain_core.output_parsers import StrOutputParser

llm = ChatOllama(model="llama3.1")

# Without bind.
chain = llm | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two three four five.'

# With bind.
chain = llm.bind(stop=["three"]) | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two'

with_config

with_config(
    config: RunnableConfig | None = None, **kwargs: Any
) -> Runnable[Input, Output]

Bind config to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

The config to bind to the Runnable.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the config bound.

with_listeners

with_listeners(
    *,
    on_start: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_end: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_error: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None
) -> Runnable[Input, Output]

Bind lifecycle listeners to a Runnable, returning a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called before the Runnable starts running, with the Run object. Defaults to None.

None
on_end Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda
from langchain_core.tracers.schemas import Run

import time


def test_runnable(time_to_sleep: int):
    time.sleep(time_to_sleep)


def fn_start(run_obj: Run):
    print("start_time:", run_obj.start_time)


def fn_end(run_obj: Run):
    print("end_time:", run_obj.end_time)


chain = RunnableLambda(test_runnable).with_listeners(
    on_start=fn_start, on_end=fn_end
)
chain.invoke(2)

with_alisteners

with_alisteners(
    *,
    on_start: AsyncListener | None = None,
    on_end: AsyncListener | None = None,
    on_error: AsyncListener | None = None
) -> Runnable[Input, Output]

Bind async lifecycle listeners to a Runnable.

Returns a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start AsyncListener | None

Called asynchronously before the Runnable starts running, with the Run object. Defaults to None.

None
on_end AsyncListener | None

Called asynchronously after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error AsyncListener | None

Called asynchronously if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda, Runnable
from datetime import datetime, timezone
import time
import asyncio

def format_t(timestamp: float) -> str:
    return datetime.fromtimestamp(timestamp, tz=timezone.utc).isoformat()

async def test_runnable(time_to_sleep: int):
    print(f"Runnable[{time_to_sleep}s]: starts at {format_t(time.time())}")
    await asyncio.sleep(time_to_sleep)
    print(f"Runnable[{time_to_sleep}s]: ends at {format_t(time.time())}")

async def fn_start(run_obj: Runnable):
    print(f"on start callback starts at {format_t(time.time())}")
    await asyncio.sleep(3)
    print(f"on start callback ends at {format_t(time.time())}")

async def fn_end(run_obj: Runnable):
    print(f"on end callback starts at {format_t(time.time())}")
    await asyncio.sleep(2)
    print(f"on end callback ends at {format_t(time.time())}")

runnable = RunnableLambda(test_runnable).with_alisteners(
    on_start=fn_start,
    on_end=fn_end
)
async def concurrent_runs():
    await asyncio.gather(runnable.ainvoke(2), runnable.ainvoke(3))

asyncio.run(concurrent_runs())
Result:
on start callback starts at 2025-03-01T07:05:22.875378+00:00
on start callback starts at 2025-03-01T07:05:22.875495+00:00
on start callback ends at 2025-03-01T07:05:25.878862+00:00
on start callback ends at 2025-03-01T07:05:25.878947+00:00
Runnable[2s]: starts at 2025-03-01T07:05:25.879392+00:00
Runnable[3s]: starts at 2025-03-01T07:05:25.879804+00:00
Runnable[2s]: ends at 2025-03-01T07:05:27.881998+00:00
on end callback starts at 2025-03-01T07:05:27.882360+00:00
Runnable[3s]: ends at 2025-03-01T07:05:28.881737+00:00
on end callback starts at 2025-03-01T07:05:28.882428+00:00
on end callback ends at 2025-03-01T07:05:29.883893+00:00
on end callback ends at 2025-03-01T07:05:30.884831+00:00

with_types

with_types(
    *,
    input_type: type[Input] | None = None,
    output_type: type[Output] | None = None
) -> Runnable[Input, Output]

Bind input and output types to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
input_type type[Input] | None

The input type to bind to the Runnable. Defaults to None.

None
output_type type[Output] | None

The output type to bind to the Runnable. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the types bound.

with_retry

with_retry(
    *,
    retry_if_exception_type: tuple[
        type[BaseException], ...
    ] = (Exception,),
    wait_exponential_jitter: bool = True,
    exponential_jitter_params: (
        ExponentialJitterParams | None
    ) = None,
    stop_after_attempt: int = 3
) -> Runnable[Input, Output]

Create a new Runnable that retries the original Runnable on exceptions.

Parameters:

Name Type Description Default
retry_if_exception_type tuple[type[BaseException], ...]

A tuple of exception types to retry on. Defaults to (Exception,).

(Exception,)
wait_exponential_jitter bool

Whether to add jitter to the wait time between retries. Defaults to True.

True
stop_after_attempt int

The maximum number of attempts to make before giving up. Defaults to 3.

3
exponential_jitter_params ExponentialJitterParams | None

Parameters for tenacity.wait_exponential_jitter. Namely: initial, max, exp_base, and jitter (all float values).

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable that retries the original Runnable on exceptions.

Example
from langchain_core.runnables import RunnableLambda

count = 0


def _lambda(x: int) -> None:
    global count
    count = count + 1
    if x == 1:
        raise ValueError("x is 1")
    else:
        pass


runnable = RunnableLambda(_lambda)
try:
    runnable.with_retry(
        stop_after_attempt=2,
        retry_if_exception_type=(ValueError,),
    ).invoke(1)
except ValueError:
    pass

assert count == 2

map

map() -> Runnable[list[Input], list[Output]]

Return a new Runnable that maps a list of inputs to a list of outputs.

Calls invoke with each input.

Returns:

Type Description
Runnable[list[Input], list[Output]]

A new Runnable that maps a list of inputs to a list of outputs.

Example
from langchain_core.runnables import RunnableLambda


def _lambda(x: int) -> int:
    return x + 1


runnable = RunnableLambda(_lambda)
print(runnable.map().invoke([1, 2, 3]))  # [2, 3, 4]

with_fallbacks

with_fallbacks(
    fallbacks: Sequence[Runnable[Input, Output]],
    *,
    exceptions_to_handle: tuple[
        type[BaseException], ...
    ] = (Exception,),
    exception_key: str | None = None
) -> RunnableWithFallbacks[Input, Output]

Add fallbacks to a Runnable, returning a new Runnable.

The new Runnable will try the original Runnable, and then each fallback in order, upon failures.

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle. Defaults to (Exception,).

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input. Defaults to None.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

Example
from typing import Iterator

from langchain_core.runnables import RunnableGenerator


def _generate_immediate_error(input: Iterator) -> Iterator[str]:
    raise ValueError()
    yield ""


def _generate(input: Iterator) -> Iterator[str]:
    yield from "foo bar"


runnable = RunnableGenerator(_generate_immediate_error).with_fallbacks(
    [RunnableGenerator(_generate)]
)
print("".join(runnable.stream({})))  # foo bar

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle.

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

as_tool

as_tool(
    args_schema: type[BaseModel] | None = None,
    *,
    name: str | None = None,
    description: str | None = None,
    arg_types: dict[str, type] | None = None
) -> BaseTool

Create a BaseTool from a Runnable.

as_tool will instantiate a BaseTool with a name, description, and args_schema from a Runnable. Where possible, schemas are inferred from runnable.get_input_schema. Alternatively (e.g., if the Runnable takes a dict as input and the specific dict keys are not typed), the schema can be specified directly with args_schema. You can also pass arg_types to just specify the required arguments and their types.

Parameters:

Name Type Description Default
args_schema type[BaseModel] | None

The schema for the tool. Defaults to None.

None
name str | None

The name of the tool. Defaults to None.

None
description str | None

The description of the tool. Defaults to None.

None
arg_types dict[str, type] | None

A dictionary of argument names to types. Defaults to None.

None

Returns:

Type Description
BaseTool

A BaseTool instance.

Typed dict input:

from typing_extensions import TypedDict
from langchain_core.runnables import RunnableLambda


class Args(TypedDict):
    a: int
    b: list[int]


def f(x: Args) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool()
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via args_schema:

from typing import Any
from pydantic import BaseModel, Field
from langchain_core.runnables import RunnableLambda

def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))

class FSchema(BaseModel):
    """Apply a function to an integer and list of integers."""

    a: int = Field(..., description="Integer")
    b: list[int] = Field(..., description="List of ints")

runnable = RunnableLambda(f)
as_tool = runnable.as_tool(FSchema)
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via arg_types:

from typing import Any
from langchain_core.runnables import RunnableLambda


def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool(arg_types={"a": int, "b": list[int]})
as_tool.invoke({"a": 3, "b": [1, 2]})

String input:

from langchain_core.runnables import RunnableLambda


def f(x: str) -> str:
    return x + "a"


def g(x: str) -> str:
    return x + "z"


runnable = RunnableLambda(f) | g
as_tool = runnable.as_tool()
as_tool.invoke("b")

Added in version 0.2.14

is_lc_serializable classmethod

is_lc_serializable() -> bool

Is this class serializable?

By design, even if a class inherits from Serializable, it is not serializable by default. This is to prevent accidental serialization of objects that should not be serialized.

Returns:

Type Description
bool

Whether the class is serializable. Default is False.

get_lc_namespace classmethod

get_lc_namespace() -> list[str]

Get the namespace of the langchain object.

For example, if the class is langchain.llms.openai.OpenAI, then the namespace is ["langchain", "llms", "openai"]

Returns:

Type Description
list[str]

The namespace as a list of strings.

lc_id classmethod

lc_id() -> list[str]

Return a unique identifier for this class for serialization purposes.

The unique identifier is a list of strings that describes the path to the object. For example, for the class langchain.llms.openai.OpenAI, the id is ["langchain", "llms", "openai", "OpenAI"].

to_json

to_json() -> (
    SerializedConstructor | SerializedNotImplemented
)

Serialize the Runnable to JSON.

Returns:

Type Description
SerializedConstructor | SerializedNotImplemented

A JSON-serializable representation of the Runnable.

to_json_not_implemented

to_json_not_implemented() -> SerializedNotImplemented

Serialize a "not implemented" object.

Returns:

Type Description
SerializedNotImplemented

SerializedNotImplemented.

configurable_fields

configurable_fields(
    **kwargs: AnyConfigurableField,
) -> RunnableSerializable[Input, Output]

Configure particular Runnable fields at runtime.

Parameters:

Name Type Description Default
**kwargs AnyConfigurableField

A dictionary of ConfigurableField instances to configure.

{}

Raises:

Type Description
ValueError

If a configuration key is not found in the Runnable.

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the fields configured.

from langchain_core.runnables import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatOpenAI(max_tokens=20).configurable_fields(
    max_tokens=ConfigurableField(
        id="output_token_number",
        name="Max tokens in the output",
        description="The maximum number of tokens in the output",
    )
)

# max_tokens = 20
print("max_tokens_20: ", model.invoke("tell me something about chess").content)

# max_tokens = 200
print(
    "max_tokens_200: ",
    model.with_config(configurable={"output_token_number": 200})
    .invoke("tell me something about chess")
    .content,
)

configurable_alternatives

configurable_alternatives(
    which: ConfigurableField,
    *,
    default_key: str = "default",
    prefix_keys: bool = False,
    **kwargs: (
        Runnable[Input, Output]
        | Callable[[], Runnable[Input, Output]]
    )
) -> RunnableSerializable[Input, Output]

Configure alternatives for Runnables that can be set at runtime.

Parameters:

Name Type Description Default
which ConfigurableField

The ConfigurableField instance that will be used to select the alternative.

required
default_key str

The default key to use if no alternative is selected. Defaults to 'default'.

'default'
prefix_keys bool

Whether to prefix the keys with the ConfigurableField id. Defaults to False.

False
**kwargs Runnable[Input, Output] | Callable[[], Runnable[Input, Output]]

A dictionary of keys to Runnable instances or callables that return Runnable instances.

{}

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the alternatives configured.

from langchain_anthropic import ChatAnthropic
from langchain_core.runnables.utils import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatAnthropic(
    model_name="claude-3-7-sonnet-20250219"
).configurable_alternatives(
    ConfigurableField(id="llm"),
    default_key="anthropic",
    openai=ChatOpenAI(),
)

# uses the default model ChatAnthropic
print(model.invoke("which organization created you?").content)

# uses ChatOpenAI
print(
    model.with_config(configurable={"llm": "openai"})
    .invoke("which organization created you?")
    .content
)

set_verbose

set_verbose(verbose: bool | None) -> bool

If verbose is None, set it.

This allows users to pass in None as verbose to access the global setting.

Parameters:

Name Type Description Default
verbose bool | None

The verbosity setting to use.

required

Returns:

Type Description
bool

The verbosity setting to use.

with_structured_output

with_structured_output(
    schema: dict | type, **kwargs: Any
) -> Runnable[LanguageModelInput, dict | BaseModel]

Not implemented on this class.

get_token_ids

get_token_ids(text: str) -> list[int]

Return the ordered ids of the tokens in a text.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
list[int]

A list of ids corresponding to the tokens in the text, in order they occur

list[int]

in the text.

get_num_tokens

get_num_tokens(text: str) -> int

Get the number of tokens present in the text.

Useful for checking if an input fits in a model's context window.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
int

The integer number of tokens in the text.

get_num_tokens_from_messages

get_num_tokens_from_messages(
    messages: list[BaseMessage],
    tools: Sequence | None = None,
) -> int

Get the number of tokens in the messages.

Useful for checking if an input fits in a model's context window.

Note

The base implementation of get_num_tokens_from_messages ignores tool schemas.

Parameters:

Name Type Description Default
messages list[BaseMessage]

The message inputs to tokenize.

required
tools Sequence | None

If provided, sequence of dict, BaseModel, function, or BaseTools to be converted to tool schemas.

None

Returns:

Type Description
int

The sum of the number of tokens across the messages.

generate

generate(
    prompts: list[str],
    stop: list[str] | None = None,
    callbacks: Callbacks | list[Callbacks] | None = None,
    *,
    tags: list[str] | list[list[str]] | None = None,
    metadata: (
        dict[str, Any] | list[dict[str, Any]] | None
    ) = None,
    run_name: str | list[str] | None = None,
    run_id: UUID | list[UUID | None] | None = None,
    **kwargs: Any
) -> LLMResult

Pass a sequence of prompts to a model and return generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
prompts list[str]

List of string prompts.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks | list[Callbacks] | None

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | list[list[str]] | None

List of tags to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
metadata dict[str, Any] | list[dict[str, Any]] | None

List of metadata dictionaries to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
run_name str | list[str] | None

List of run names to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
run_id UUID | list[UUID | None] | None

List of run IDs to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Raises:

Type Description
ValueError

If prompts is not a list.

ValueError

If the length of callbacks, tags, metadata, or run_name (if provided) does not match the length of prompts.

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input prompt and additional model provider-specific output.

agenerate async

agenerate(
    prompts: list[str],
    stop: list[str] | None = None,
    callbacks: Callbacks | list[Callbacks] | None = None,
    *,
    tags: list[str] | list[list[str]] | None = None,
    metadata: (
        dict[str, Any] | list[dict[str, Any]] | None
    ) = None,
    run_name: str | list[str] | None = None,
    run_id: UUID | list[UUID | None] | None = None,
    **kwargs: Any
) -> LLMResult

Asynchronously pass a sequence of prompts to a model and return generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
prompts list[str]

List of string prompts.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks | list[Callbacks] | None

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | list[list[str]] | None

List of tags to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
metadata dict[str, Any] | list[dict[str, Any]] | None

List of metadata dictionaries to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
run_name str | list[str] | None

List of run names to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
run_id UUID | list[UUID | None] | None

List of run IDs to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Raises:

Type Description
ValueError

If the length of callbacks, tags, metadata, or run_name (if provided) does not match the length of prompts.

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input prompt and additional model provider-specific output.

__str__

__str__() -> str

Return a string representation of the object for printing.

dict

dict(**kwargs: Any) -> dict

Return a dictionary of the LLM.

save

save(file_path: Path | str) -> None

Save the LLM.

Parameters:

Name Type Description Default
file_path Path | str

Path to file to save the LLM to.

required

Raises:

Type Description
ValueError

If the file path is not a string or Path object.

Example:

.. code-block:: python

    llm.save(file_path="path/llm.yaml")

validate_environment

validate_environment() -> Self

Validate that the python package exists in environment.

DataStoreDocumentStorage

Bases: DocumentStorage

Stores documents in Google Cloud DataStore.

Methods:

Name Description
amget

Async get the values associated with the given keys.

amset

Async set the values for the given keys.

amdelete

Async delete the given keys and their associated values.

ayield_keys

Async get an iterator over keys that match the given prefix.

__init__

Constructor.

mget

Gets a batch of documents by id.

mset

Stores a series of documents using each keys.

mdelete

Deletes a sequence of documents by key.

yield_keys

Yields the keys of all documents in the storage.

amget async

amget(keys: Sequence[K]) -> list[V | None]

Async get the values associated with the given keys.

Parameters:

Name Type Description Default
keys Sequence[K]

A sequence of keys.

required

Returns:

Type Description
list[V | None]

A sequence of optional values associated with the keys.

list[V | None]

If a key is not found, the corresponding value will be None.

amset async

amset(key_value_pairs: Sequence[tuple[K, V]]) -> None

Async set the values for the given keys.

Parameters:

Name Type Description Default
key_value_pairs Sequence[tuple[K, V]]

A sequence of key-value pairs.

required

amdelete async

amdelete(keys: Sequence[K]) -> None

Async delete the given keys and their associated values.

Parameters:

Name Type Description Default
keys Sequence[K]

A sequence of keys to delete.

required

ayield_keys async

ayield_keys(
    *, prefix: str | None = None
) -> AsyncIterator[K] | AsyncIterator[str]

Async get an iterator over keys that match the given prefix.

Parameters:

Name Type Description Default
prefix str

The prefix to match.

None

Yields:

Type Description
AsyncIterator[K] | AsyncIterator[str]

Iterator[K | str]: An iterator over keys that match the given prefix.

AsyncIterator[K] | AsyncIterator[str]

This method is allowed to return an iterator over either K or str

AsyncIterator[K] | AsyncIterator[str]

depending on what makes more sense for the given store.

__init__

__init__(
    datastore_client: Client,
    kind: str = "document_id",
    text_property_name: str = "text",
    metadata_property_name: str = "metadata",
    exclude_from_indexes: Optional[List[str]] = None,
) -> None

Constructor.

Parameters:

Name Type Description Default
bucket

Bucket where the documents will be stored.

required
prefix

Prefix that is prepended to all document names.

required

mget

mget(keys: Sequence[str]) -> List[Optional[Document]]

Gets a batch of documents by id.

Parameters:

Name Type Description Default
ids

List of ids for the text.

required

Returns:

Type Description
List[Optional[Document]]

List of texts. If the key id is not found for any id record returns a None instead.

mset

mset(
    key_value_pairs: Sequence[Tuple[str, Document]],
) -> None

Stores a series of documents using each keys.

Parameters:

Name Type Description Default
key_value_pairs Sequence[Tuple[K, V]]

A sequence of key-value pairs.

required

mdelete

mdelete(keys: Sequence[str]) -> None

Deletes a sequence of documents by key.

Parameters:

Name Type Description Default
keys Sequence[str]

A sequence of keys to delete.

required

yield_keys

yield_keys(*, prefix: str | None = None) -> Iterator[str]

Yields the keys of all documents in the storage.

Parameters:

Name Type Description Default
prefix str | None

Ignored

None

GCSDocumentStorage

Bases: DocumentStorage

Stores documents in Google Cloud Storage. For each pair id, document_text the name of the blob will be {prefix}/{id} stored in plain text format.

Methods:

Name Description
amget

Async get the values associated with the given keys.

amset

Async set the values for the given keys.

amdelete

Async delete the given keys and their associated values.

ayield_keys

Async get an iterator over keys that match the given prefix.

__init__

Constructor.

mset

Stores a series of documents using each keys.

mget

Gets a batch of documents by id.

mdelete

Deletes a batch of documents by id.

yield_keys

Yields the keys present in the storage.

amget async

amget(keys: Sequence[K]) -> list[V | None]

Async get the values associated with the given keys.

Parameters:

Name Type Description Default
keys Sequence[K]

A sequence of keys.

required

Returns:

Type Description
list[V | None]

A sequence of optional values associated with the keys.

list[V | None]

If a key is not found, the corresponding value will be None.

amset async

amset(key_value_pairs: Sequence[tuple[K, V]]) -> None

Async set the values for the given keys.

Parameters:

Name Type Description Default
key_value_pairs Sequence[tuple[K, V]]

A sequence of key-value pairs.

required

amdelete async

amdelete(keys: Sequence[K]) -> None

Async delete the given keys and their associated values.

Parameters:

Name Type Description Default
keys Sequence[K]

A sequence of keys to delete.

required

ayield_keys async

ayield_keys(
    *, prefix: str | None = None
) -> AsyncIterator[K] | AsyncIterator[str]

Async get an iterator over keys that match the given prefix.

Parameters:

Name Type Description Default
prefix str

The prefix to match.

None

Yields:

Type Description
AsyncIterator[K] | AsyncIterator[str]

Iterator[K | str]: An iterator over keys that match the given prefix.

AsyncIterator[K] | AsyncIterator[str]

This method is allowed to return an iterator over either K or str

AsyncIterator[K] | AsyncIterator[str]

depending on what makes more sense for the given store.

__init__

__init__(
    bucket: Bucket,
    prefix: Optional[str] = "documents",
    threaded=True,
    n_threads=8,
) -> None

Constructor.

Parameters:

Name Type Description Default
bucket Bucket

Bucket where the documents will be stored.

required
prefix Optional[str]

Prefix that is prepended to all document names.

'documents'

mset

mset(
    key_value_pairs: Sequence[Tuple[str, Document]],
) -> None

Stores a series of documents using each keys.

Parameters:

Name Type Description Default
key_value_pairs Sequence[Tuple[K, V]]

A sequence of key-value pairs.

required

mget

mget(keys: Sequence[str]) -> List[Optional[Document]]

Gets a batch of documents by id. The default implementation only loops get_by_id. Subclasses that have faster ways to retrieve data by batch should implement this method.

Parameters:

Name Type Description Default
ids

List of ids for the text.

required

Returns:

Type Description
List[Optional[Document]]

List of documents. If the key id is not found for any id record returns a None instead.

mdelete

mdelete(keys: Sequence[str]) -> None

Deletes a batch of documents by id.

Parameters:

Name Type Description Default
keys Sequence[str]

List of ids for the text.

required

yield_keys

yield_keys(*, prefix: str | None = None) -> Iterator[str]

Yields the keys present in the storage.

Parameters:

Name Type Description Default
prefix str | None

Ignored. Uses the prefix provided in the constructor.

None

VectorSearchVectorStore

Bases: _BaseVertexAIVectorStore

VertexAI VectorStore that handles the search and indexing using Vector Search and stores the documents in Google Cloud Storage.

Methods:

Name Description
add_texts

Run more texts through the embeddings and add to the vectorstore.

delete

Delete by vector ID.

get_by_ids

Get documents by their IDs.

aget_by_ids

Async get documents by their IDs.

adelete

Async delete by vector ID or other criteria.

aadd_texts

Async run more texts through the embeddings and add to the vectorstore.

add_documents

Add or update documents in the vectorstore.

aadd_documents

Async run more documents through the embeddings and add to the vectorstore.

search

Return docs most similar to query using a specified search type.

asearch

Async return docs most similar to query using a specified search type.

similarity_search

Return docs most similar to query.

similarity_search_with_score

Return docs most similar to query and their cosine distance from the query.

asimilarity_search_with_score

Async run similarity search with distance.

similarity_search_with_relevance_scores

Return docs and relevance scores in the range [0, 1].

asimilarity_search_with_relevance_scores

Async return docs and relevance scores in the range [0, 1].

asimilarity_search

Async return docs most similar to query.

similarity_search_by_vector

Return docs most similar to embedding vector.

asimilarity_search_by_vector

Async return docs most similar to embedding vector.

max_marginal_relevance_search

Return docs selected using the maximal marginal relevance.

amax_marginal_relevance_search

Async return docs selected using the maximal marginal relevance.

max_marginal_relevance_search_by_vector

Return docs selected using the maximal marginal relevance.

amax_marginal_relevance_search_by_vector

Async return docs selected using the maximal marginal relevance.

from_documents

Return VectorStore initialized from documents and embeddings.

afrom_documents

Async return VectorStore initialized from documents and embeddings.

from_texts

Use from components instead.

afrom_texts

Async return VectorStore initialized from texts and embeddings.

as_retriever

Return VectorStoreRetriever initialized from this VectorStore.

__init__

Constructor.

similarity_search_by_vector_with_score

Return docs most similar to the embedding and their cosine distance.

from_components

Takes the object creation out of the constructor.

Attributes:

Name Type Description
embbedings Embeddings

Returns the embeddings object.

embbedings property

embbedings: Embeddings

Returns the embeddings object.

add_texts

add_texts(
    texts: Iterable[str],
    metadatas: Union[List[dict], None] = None,
    *,
    ids: Optional[List[str]] = None,
    is_complete_overwrite: bool = False,
    **kwargs: Any
) -> List[str]

Run more texts through the embeddings and add to the vectorstore.

Parameters:

Name Type Description Default
texts Iterable[str]

Iterable of strings to add to the vectorstore.

required
metadatas Union[List[dict], None]

Optional list of metadatas associated with the texts.

None
ids Optional[List[str]]

Optional list of ids to be assigned to the texts in the index. If None, unique ids will be generated.

None
is_complete_overwrite bool

Optional, determines whether this is an append or overwrite operation. Only relevant for BATCH UPDATE indexes.

False
kwargs Any

vectorstore specific parameters.

{}

Returns:

Type Description
List[str]

List of ids from adding the texts into the vectorstore.

delete

delete(
    ids: Optional[List[str]] = None, **kwargs: Any
) -> Optional[bool]

Delete by vector ID.

Parameters:

Name Type Description Default
ids Optional[List[str]]

List of ids to delete.

None
**kwargs Any

If added metadata={}, deletes the documents

{}

Returns:

Type Description
Optional[bool]

Optional[bool]: True if deletion is successful.

Raises:

Type Description
ValueError

If ids is None or an empty list.

RuntimeError

If an error occurs during the deletion process.

get_by_ids

get_by_ids(ids: Sequence[str]) -> list[Document]

Get documents by their IDs.

The returned documents are expected to have the ID field set to the ID of the document in the vector store.

Fewer documents may be returned than requested if some IDs are not found or if there are duplicated IDs.

Users should not assume that the order of the returned documents matches the order of the input IDs. Instead, users should rely on the ID field of the returned documents.

This method should NOT raise exceptions if no documents are found for some IDs.

Parameters:

Name Type Description Default
ids Sequence[str]

List of ids to retrieve.

required

Returns:

Type Description
list[Document]

List of Documents.

Added in version 0.2.11

aget_by_ids async

aget_by_ids(ids: Sequence[str]) -> list[Document]

Async get documents by their IDs.

The returned documents are expected to have the ID field set to the ID of the document in the vector store.

Fewer documents may be returned than requested if some IDs are not found or if there are duplicated IDs.

Users should not assume that the order of the returned documents matches the order of the input IDs. Instead, users should rely on the ID field of the returned documents.

This method should NOT raise exceptions if no documents are found for some IDs.

Parameters:

Name Type Description Default
ids Sequence[str]

List of ids to retrieve.

required

Returns:

Type Description
list[Document]

List of Documents.

Added in version 0.2.11

adelete async

adelete(
    ids: list[str] | None = None, **kwargs: Any
) -> bool | None

Async delete by vector ID or other criteria.

Parameters:

Name Type Description Default
ids list[str] | None

List of ids to delete. If None, delete all. Default is None.

None
**kwargs Any

Other keyword arguments that subclasses might use.

{}

Returns:

Type Description
bool | None

Optional[bool]: True if deletion is successful,

bool | None

False otherwise, None if not implemented.

aadd_texts async

aadd_texts(
    texts: Iterable[str],
    metadatas: list[dict] | None = None,
    *,
    ids: list[str] | None = None,
    **kwargs: Any
) -> list[str]

Async run more texts through the embeddings and add to the vectorstore.

Parameters:

Name Type Description Default
texts Iterable[str]

Iterable of strings to add to the vectorstore.

required
metadatas list[dict] | None

Optional list of metadatas associated with the texts. Default is None.

None
ids list[str] | None

Optional list

None
**kwargs Any

vectorstore specific parameters.

{}

Returns:

Type Description
list[str]

List of ids from adding the texts into the vectorstore.

Raises:

Type Description
ValueError

If the number of metadatas does not match the number of texts.

ValueError

If the number of ids does not match the number of texts.

add_documents

add_documents(
    documents: list[Document], **kwargs: Any
) -> list[str]

Add or update documents in the vectorstore.

Parameters:

Name Type Description Default
documents list[Document]

Documents to add to the vectorstore.

required
kwargs Any

Additional keyword arguments. if kwargs contains ids and documents contain ids, the ids in the kwargs will receive precedence.

{}

Returns:

Type Description
list[str]

List of IDs of the added texts.

aadd_documents async

aadd_documents(
    documents: list[Document], **kwargs: Any
) -> list[str]

Async run more documents through the embeddings and add to the vectorstore.

Parameters:

Name Type Description Default
documents list[Document]

Documents to add to the vectorstore.

required
kwargs Any

Additional keyword arguments.

{}

Returns:

Type Description
list[str]

List of IDs of the added texts.

search

search(
    query: str, search_type: str, **kwargs: Any
) -> list[Document]

Return docs most similar to query using a specified search type.

Parameters:

Name Type Description Default
query str

Input text

required
search_type str

Type of search to perform. Can be "similarity", "mmr", or "similarity_score_threshold".

required
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents most similar to the query.

Raises:

Type Description
ValueError

If search_type is not one of "similarity", "mmr", or "similarity_score_threshold".

asearch async

asearch(
    query: str, search_type: str, **kwargs: Any
) -> list[Document]

Async return docs most similar to query using a specified search type.

Parameters:

Name Type Description Default
query str

Input text.

required
search_type str

Type of search to perform. Can be "similarity", "mmr", or "similarity_score_threshold".

required
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents most similar to the query.

Raises:

Type Description
ValueError

If search_type is not one of "similarity", "mmr", or "similarity_score_threshold".

similarity_search(
    query: str,
    k: int = 4,
    filter: Optional[List[Namespace]] = None,
    numeric_filter: Optional[List[NumericNamespace]] = None,
    **kwargs: Any
) -> List[Document]

Return docs most similar to query.

Parameters:

Name Type Description Default
query str

The string that will be used to search for similar documents.

required
k int

The amount of neighbors that will be retrieved.

4
filter Optional[List[Namespace]]

Optional. A list of Namespaces for filtering the matching results. For example: [Namespace("color", ["red"], []), Namespace("shape", [], ["squared"])] will match datapoints that satisfy "red color" but not include datapoints with "squared shape". Please refer to https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#json for more detail.

None
numeric_filter Optional[List[NumericNamespace]]

Optional. A list of NumericNamespaces for filterning the matching results. Please refer to https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#json for more detail.

None

Returns:

Type Description
List[Document]

A list of k matching documents.

similarity_search_with_score

similarity_search_with_score(
    query: str,
    k: int = 4,
    filter: Optional[List[Namespace]] = None,
    numeric_filter: Optional[List[NumericNamespace]] = None,
    **kwargs: Any
) -> List[Tuple[Document, Union[float, Dict[str, float]]]]

Return docs most similar to query and their cosine distance from the query.

Parameters:

Name Type Description Default
query str

String query look up documents similar to.

required
k int

Number of Documents to return. Defaults to 4.

4
filter Optional[List[Namespace]]

Optional. A list of Namespaces for filtering the matching results. For example: [Namespace("color", ["red"], []), Namespace("shape", [], ["squared"])] will match datapoints that satisfy "red color" but not include datapoints with "squared shape". Please refer to https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#json for more detail.

None
numeric_filter Optional[List[NumericNamespace]]

Optional. A list of NumericNamespaces for filterning the matching results. Please refer to https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#json for more detail.

None

Returns:

Type Description
List[Tuple[Document, Union[float, Dict[str, float]]]]

List[Tuple[Document, float]]: List of documents most similar to

List[Tuple[Document, Union[float, Dict[str, float]]]]

the query text and cosine distance in float for each.

List[Tuple[Document, Union[float, Dict[str, float]]]]

Higher score represents more similarity.

asimilarity_search_with_score async

asimilarity_search_with_score(
    *args: Any, **kwargs: Any
) -> list[tuple[Document, float]]

Async run similarity search with distance.

Parameters:

Name Type Description Default
*args Any

Arguments to pass to the search method.

()
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[tuple[Document, float]]

List of Tuples of (doc, similarity_score).

similarity_search_with_relevance_scores

similarity_search_with_relevance_scores(
    query: str, k: int = 4, **kwargs: Any
) -> list[tuple[Document, float]]

Return docs and relevance scores in the range [0, 1].

0 is dissimilar, 1 is most similar.

Parameters:

Name Type Description Default
query str

Input text.

required
k int

Number of Documents to return. Defaults to 4.

4
**kwargs Any

kwargs to be passed to similarity search. Should include: score_threshold: Optional, a floating point value between 0 to 1 to filter the resulting set of retrieved docs.

{}

Returns:

Type Description
list[tuple[Document, float]]

List of Tuples of (doc, similarity_score).

asimilarity_search_with_relevance_scores async

asimilarity_search_with_relevance_scores(
    query: str, k: int = 4, **kwargs: Any
) -> list[tuple[Document, float]]

Async return docs and relevance scores in the range [0, 1].

0 is dissimilar, 1 is most similar.

Parameters:

Name Type Description Default
query str

Input text.

required
k int

Number of Documents to return. Defaults to 4.

4
**kwargs Any

kwargs to be passed to similarity search. Should include: score_threshold: Optional, a floating point value between 0 to 1 to filter the resulting set of retrieved docs

{}

Returns:

Type Description
list[tuple[Document, float]]

List of Tuples of (doc, similarity_score)

asimilarity_search(
    query: str, k: int = 4, **kwargs: Any
) -> list[Document]

Async return docs most similar to query.

Parameters:

Name Type Description Default
query str

Input text.

required
k int

Number of Documents to return. Defaults to 4.

4
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents most similar to the query.

similarity_search_by_vector

similarity_search_by_vector(
    embedding: list[float], k: int = 4, **kwargs: Any
) -> list[Document]

Return docs most similar to embedding vector.

Parameters:

Name Type Description Default
embedding list[float]

Embedding to look up documents similar to.

required
k int

Number of Documents to return. Defaults to 4.

4
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents most similar to the query vector.

asimilarity_search_by_vector async

asimilarity_search_by_vector(
    embedding: list[float], k: int = 4, **kwargs: Any
) -> list[Document]

Async return docs most similar to embedding vector.

Parameters:

Name Type Description Default
embedding list[float]

Embedding to look up documents similar to.

required
k int

Number of Documents to return. Defaults to 4.

4
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents most similar to the query vector.

max_marginal_relevance_search(
    query: str,
    k: int = 4,
    fetch_k: int = 20,
    lambda_mult: float = 0.5,
    **kwargs: Any
) -> list[Document]

Return docs selected using the maximal marginal relevance.

Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents.

Parameters:

Name Type Description Default
query str

Text to look up documents similar to.

required
k int

Number of Documents to return. Defaults to 4.

4
fetch_k int

Number of Documents to fetch to pass to MMR algorithm. Default is 20.

20
lambda_mult float

Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. Defaults to 0.5.

0.5
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents selected by maximal marginal relevance.

amax_marginal_relevance_search(
    query: str,
    k: int = 4,
    fetch_k: int = 20,
    lambda_mult: float = 0.5,
    **kwargs: Any
) -> list[Document]

Async return docs selected using the maximal marginal relevance.

Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents.

Parameters:

Name Type Description Default
query str

Text to look up documents similar to.

required
k int

Number of Documents to return. Defaults to 4.

4
fetch_k int

Number of Documents to fetch to pass to MMR algorithm. Default is 20.

20
lambda_mult float

Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. Defaults to 0.5.

0.5
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents selected by maximal marginal relevance.

max_marginal_relevance_search_by_vector

max_marginal_relevance_search_by_vector(
    embedding: list[float],
    k: int = 4,
    fetch_k: int = 20,
    lambda_mult: float = 0.5,
    **kwargs: Any
) -> list[Document]

Return docs selected using the maximal marginal relevance.

Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents.

Parameters:

Name Type Description Default
embedding list[float]

Embedding to look up documents similar to.

required
k int

Number of Documents to return. Defaults to 4.

4
fetch_k int

Number of Documents to fetch to pass to MMR algorithm. Default is 20.

20
lambda_mult float

Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. Defaults to 0.5.

0.5
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents selected by maximal marginal relevance.

amax_marginal_relevance_search_by_vector async

amax_marginal_relevance_search_by_vector(
    embedding: list[float],
    k: int = 4,
    fetch_k: int = 20,
    lambda_mult: float = 0.5,
    **kwargs: Any
) -> list[Document]

Async return docs selected using the maximal marginal relevance.

Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents.

Parameters:

Name Type Description Default
embedding list[float]

Embedding to look up documents similar to.

required
k int

Number of Documents to return. Defaults to 4.

4
fetch_k int

Number of Documents to fetch to pass to MMR algorithm. Default is 20.

20
lambda_mult float

Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. Defaults to 0.5.

0.5
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents selected by maximal marginal relevance.

from_documents classmethod

from_documents(
    documents: list[Document],
    embedding: Embeddings,
    **kwargs: Any
) -> Self

Return VectorStore initialized from documents and embeddings.

Parameters:

Name Type Description Default
documents list[Document]

List of Documents to add to the vectorstore.

required
embedding Embeddings

Embedding function to use.

required
kwargs Any

Additional keyword arguments.

{}

Returns:

Name Type Description
VectorStore Self

VectorStore initialized from documents and embeddings.

afrom_documents async classmethod

afrom_documents(
    documents: list[Document],
    embedding: Embeddings,
    **kwargs: Any
) -> Self

Async return VectorStore initialized from documents and embeddings.

Parameters:

Name Type Description Default
documents list[Document]

List of Documents to add to the vectorstore.

required
embedding Embeddings

Embedding function to use.

required
kwargs Any

Additional keyword arguments.

{}

Returns:

Name Type Description
VectorStore Self

VectorStore initialized from documents and embeddings.

from_texts classmethod

from_texts(
    texts: List[str],
    embedding: Embeddings,
    metadatas: Union[List[dict], None] = None,
    **kwargs: Any
) -> _BaseVertexAIVectorStore

Use from components instead.

afrom_texts async classmethod

afrom_texts(
    texts: list[str],
    embedding: Embeddings,
    metadatas: list[dict] | None = None,
    *,
    ids: list[str] | None = None,
    **kwargs: Any
) -> Self

Async return VectorStore initialized from texts and embeddings.

Parameters:

Name Type Description Default
texts list[str]

Texts to add to the vectorstore.

required
embedding Embeddings

Embedding function to use.

required
metadatas list[dict] | None

Optional list of metadatas associated with the texts. Default is None.

None
ids list[str] | None

Optional list of IDs associated with the texts.

None
kwargs Any

Additional keyword arguments.

{}

Returns:

Name Type Description
VectorStore Self

VectorStore initialized from texts and embeddings.

as_retriever

as_retriever(**kwargs: Any) -> VectorStoreRetriever

Return VectorStoreRetriever initialized from this VectorStore.

Parameters:

Name Type Description Default
**kwargs Any

Keyword arguments to pass to the search function. Can include: search_type (Optional[str]): Defines the type of search that the Retriever should perform. Can be "similarity" (default), "mmr", or "similarity_score_threshold". search_kwargs (Optional[Dict]): Keyword arguments to pass to the search function. Can include things like: k: Amount of documents to return (Default: 4) score_threshold: Minimum relevance threshold for similarity_score_threshold fetch_k: Amount of documents to pass to MMR algorithm (Default: 20) lambda_mult: Diversity of results returned by MMR; 1 for minimum diversity and 0 for maximum. (Default: 0.5) filter: Filter by document metadata

{}

Returns:

Name Type Description
VectorStoreRetriever VectorStoreRetriever

Retriever class for VectorStore.

Examples:

.. code-block:: python

# Retrieve more documents with higher diversity
# Useful if your dataset has many similar documents
docsearch.as_retriever(
    search_type="mmr", search_kwargs={"k": 6, "lambda_mult": 0.25}
)

# Fetch more documents for the MMR algorithm to consider
# But only return the top 5
docsearch.as_retriever(
    search_type="mmr", search_kwargs={"k": 5, "fetch_k": 50}
)

# Only retrieve documents that have a relevance score
# Above a certain threshold
docsearch.as_retriever(
    search_type="similarity_score_threshold",
    search_kwargs={"score_threshold": 0.8},
)

# Only get the single most similar document from the dataset
docsearch.as_retriever(search_kwargs={"k": 1})

# Use a filter to only retrieve documents from a specific paper
docsearch.as_retriever(
    search_kwargs={"filter": {"paper_title": "GPT-4 Technical Report"}}
)

__init__

__init__(
    searcher: Searcher,
    document_storage: DocumentStorage,
    embbedings: Optional[Embeddings] = None,
    embeddings: Optional[Embeddings] = None,
) -> None

Constructor.

Parameters:

Name Type Description Default
searcher Searcher

Object in charge of searching and storing the index.

required
document_storage DocumentStorage

Object in charge of storing and retrieving documents.

required
embbedings Optional[Embeddings]

Object in charge of transforming text to embbeddings. Deprecated: Use 'embeddings' instead.

None
embeddings Optional[Embeddings]

Object in charge of transforming text to embeddings.

None

similarity_search_by_vector_with_score

similarity_search_by_vector_with_score(
    embedding: List[float],
    sparse_embedding: Optional[
        Dict[str, Union[List[int], List[float]]]
    ] = None,
    k: int = 4,
    rrf_ranking_alpha: float = 1,
    filter: Optional[List[Namespace]] = None,
    numeric_filter: Optional[List[NumericNamespace]] = None,
    **kwargs: Any
) -> List[Tuple[Document, Union[float, Dict[str, float]]]]

Return docs most similar to the embedding and their cosine distance.

Parameters:

Name Type Description Default
embedding List[float]

Embedding to look up documents similar to.

required
sparse_embedding Optional[Dict[str, Union[List[int], List[float]]]]

Sparse embedding dictionary which represents an embedding as a list of dimensions and as a list of sparse values: ie. {"values": [0.7, 0.5], "dimensions": [10, 20]}

None
k int

Number of Documents to return. Defaults to 4.

4
rrf_ranking_alpha float

Reciprocal Ranking Fusion weight, float between 0 and 1.0 Weights Dense Search VS Sparse Search, as an example: - rrf_ranking_alpha=1: Only Dense - rrf_ranking_alpha=0: Only Sparse - rrf_ranking_alpha=0.7: 0.7 weighting for dense and 0.3 for sparse

1
filter Optional[List[Namespace]]

Optional. A list of Namespaces for filtering the matching results. For example: [Namespace("color", ["red"], []), Namespace("shape", [], ["squared"])] will match datapoints that satisfy "red color" but not include datapoints with "squared shape". Please refer to https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#json for more detail.

None
numeric_filter Optional[List[NumericNamespace]]

Optional. A list of NumericNamespaces for filterning the matching results. Please refer to https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#json for more detail.

None

Returns:

Type Description
List[Tuple[Document, Union[float, Dict[str, float]]]]

List[Tuple[Document, Union[float, Dict[str, float]]]]:

List[Tuple[Document, Union[float, Dict[str, float]]]]

List of documents most similar to the query text and either

List[Tuple[Document, Union[float, Dict[str, float]]]]

cosine distance in float for each or dictionary with both dense and sparse

List[Tuple[Document, Union[float, Dict[str, float]]]]

scores if running hybrid search.

List[Tuple[Document, Union[float, Dict[str, float]]]]

Higher score represents more similarity.

from_components classmethod

from_components(
    project_id: str,
    region: str,
    gcs_bucket_name: str,
    index_id: str,
    endpoint_id: str,
    private_service_connect_ip_address: Optional[
        str
    ] = None,
    credentials: Optional[Credentials] = None,
    credentials_path: Optional[str] = None,
    embedding: Optional[Embeddings] = None,
    stream_update: bool = False,
    **kwargs: Any
) -> VectorSearchVectorStore

Takes the object creation out of the constructor.

Parameters:

Name Type Description Default
project_id str

The GCP project id.

required
region str

The default location making the API calls. It must have

required
gcs_bucket_name str

The location where the vectors will be stored in

required
index_id str

The id of the created index.

required
endpoint_id str

The id of the created endpoint.

required
private_service_connect_ip_address Optional[str]

The IP address of the private

None
credentials Optional[Credentials]

Google cloud Credentials object.

None
credentials_path Optional[str]

(Optional) The path of the Google credentials on

None
embedding Optional[Embeddings]

The :class:Embeddings that will be used for

None
stream_update bool

Whether to update with streaming or batching. VectorSearch index must be compatible with stream/batch updates.

False
kwargs Any

Additional keyword arguments to pass to VertexAIVectorSearch.init().

{}

Returns:

Type Description
VectorSearchVectorStore

A configured VertexAIVectorSearch.

VectorSearchVectorStoreDatastore

Bases: _BaseVertexAIVectorStore

VectorSearch with DatasTore document storage.

Methods:

Name Description
add_texts

Run more texts through the embeddings and add to the vectorstore.

delete

Delete by vector ID.

get_by_ids

Get documents by their IDs.

aget_by_ids

Async get documents by their IDs.

adelete

Async delete by vector ID or other criteria.

aadd_texts

Async run more texts through the embeddings and add to the vectorstore.

add_documents

Add or update documents in the vectorstore.

aadd_documents

Async run more documents through the embeddings and add to the vectorstore.

search

Return docs most similar to query using a specified search type.

asearch

Async return docs most similar to query using a specified search type.

similarity_search

Return docs most similar to query.

similarity_search_with_score

Return docs most similar to query and their cosine distance from the query.

asimilarity_search_with_score

Async run similarity search with distance.

similarity_search_with_relevance_scores

Return docs and relevance scores in the range [0, 1].

asimilarity_search_with_relevance_scores

Async return docs and relevance scores in the range [0, 1].

asimilarity_search

Async return docs most similar to query.

similarity_search_by_vector

Return docs most similar to embedding vector.

asimilarity_search_by_vector

Async return docs most similar to embedding vector.

max_marginal_relevance_search

Return docs selected using the maximal marginal relevance.

amax_marginal_relevance_search

Async return docs selected using the maximal marginal relevance.

max_marginal_relevance_search_by_vector

Return docs selected using the maximal marginal relevance.

amax_marginal_relevance_search_by_vector

Async return docs selected using the maximal marginal relevance.

from_documents

Return VectorStore initialized from documents and embeddings.

afrom_documents

Async return VectorStore initialized from documents and embeddings.

from_texts

Use from components instead.

afrom_texts

Async return VectorStore initialized from texts and embeddings.

as_retriever

Return VectorStoreRetriever initialized from this VectorStore.

__init__

Constructor.

similarity_search_by_vector_with_score

Return docs most similar to the embedding and their cosine distance.

from_components

Takes the object creation out of the constructor.

Attributes:

Name Type Description
embbedings Embeddings

Returns the embeddings object.

embbedings property

embbedings: Embeddings

Returns the embeddings object.

add_texts

add_texts(
    texts: Iterable[str],
    metadatas: Union[List[dict], None] = None,
    *,
    ids: Optional[List[str]] = None,
    is_complete_overwrite: bool = False,
    **kwargs: Any
) -> List[str]

Run more texts through the embeddings and add to the vectorstore.

Parameters:

Name Type Description Default
texts Iterable[str]

Iterable of strings to add to the vectorstore.

required
metadatas Union[List[dict], None]

Optional list of metadatas associated with the texts.

None
ids Optional[List[str]]

Optional list of ids to be assigned to the texts in the index. If None, unique ids will be generated.

None
is_complete_overwrite bool

Optional, determines whether this is an append or overwrite operation. Only relevant for BATCH UPDATE indexes.

False
kwargs Any

vectorstore specific parameters.

{}

Returns:

Type Description
List[str]

List of ids from adding the texts into the vectorstore.

delete

delete(
    ids: Optional[List[str]] = None, **kwargs: Any
) -> Optional[bool]

Delete by vector ID.

Parameters:

Name Type Description Default
ids Optional[List[str]]

List of ids to delete.

None
**kwargs Any

If added metadata={}, deletes the documents

{}

Returns:

Type Description
Optional[bool]

Optional[bool]: True if deletion is successful.

Raises:

Type Description
ValueError

If ids is None or an empty list.

RuntimeError

If an error occurs during the deletion process.

get_by_ids

get_by_ids(ids: Sequence[str]) -> list[Document]

Get documents by their IDs.

The returned documents are expected to have the ID field set to the ID of the document in the vector store.

Fewer documents may be returned than requested if some IDs are not found or if there are duplicated IDs.

Users should not assume that the order of the returned documents matches the order of the input IDs. Instead, users should rely on the ID field of the returned documents.

This method should NOT raise exceptions if no documents are found for some IDs.

Parameters:

Name Type Description Default
ids Sequence[str]

List of ids to retrieve.

required

Returns:

Type Description
list[Document]

List of Documents.

Added in version 0.2.11

aget_by_ids async

aget_by_ids(ids: Sequence[str]) -> list[Document]

Async get documents by their IDs.

The returned documents are expected to have the ID field set to the ID of the document in the vector store.

Fewer documents may be returned than requested if some IDs are not found or if there are duplicated IDs.

Users should not assume that the order of the returned documents matches the order of the input IDs. Instead, users should rely on the ID field of the returned documents.

This method should NOT raise exceptions if no documents are found for some IDs.

Parameters:

Name Type Description Default
ids Sequence[str]

List of ids to retrieve.

required

Returns:

Type Description
list[Document]

List of Documents.

Added in version 0.2.11

adelete async

adelete(
    ids: list[str] | None = None, **kwargs: Any
) -> bool | None

Async delete by vector ID or other criteria.

Parameters:

Name Type Description Default
ids list[str] | None

List of ids to delete. If None, delete all. Default is None.

None
**kwargs Any

Other keyword arguments that subclasses might use.

{}

Returns:

Type Description
bool | None

Optional[bool]: True if deletion is successful,

bool | None

False otherwise, None if not implemented.

aadd_texts async

aadd_texts(
    texts: Iterable[str],
    metadatas: list[dict] | None = None,
    *,
    ids: list[str] | None = None,
    **kwargs: Any
) -> list[str]

Async run more texts through the embeddings and add to the vectorstore.

Parameters:

Name Type Description Default
texts Iterable[str]

Iterable of strings to add to the vectorstore.

required
metadatas list[dict] | None

Optional list of metadatas associated with the texts. Default is None.

None
ids list[str] | None

Optional list

None
**kwargs Any

vectorstore specific parameters.

{}

Returns:

Type Description
list[str]

List of ids from adding the texts into the vectorstore.

Raises:

Type Description
ValueError

If the number of metadatas does not match the number of texts.

ValueError

If the number of ids does not match the number of texts.

add_documents

add_documents(
    documents: list[Document], **kwargs: Any
) -> list[str]

Add or update documents in the vectorstore.

Parameters:

Name Type Description Default
documents list[Document]

Documents to add to the vectorstore.

required
kwargs Any

Additional keyword arguments. if kwargs contains ids and documents contain ids, the ids in the kwargs will receive precedence.

{}

Returns:

Type Description
list[str]

List of IDs of the added texts.

aadd_documents async

aadd_documents(
    documents: list[Document], **kwargs: Any
) -> list[str]

Async run more documents through the embeddings and add to the vectorstore.

Parameters:

Name Type Description Default
documents list[Document]

Documents to add to the vectorstore.

required
kwargs Any

Additional keyword arguments.

{}

Returns:

Type Description
list[str]

List of IDs of the added texts.

search

search(
    query: str, search_type: str, **kwargs: Any
) -> list[Document]

Return docs most similar to query using a specified search type.

Parameters:

Name Type Description Default
query str

Input text

required
search_type str

Type of search to perform. Can be "similarity", "mmr", or "similarity_score_threshold".

required
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents most similar to the query.

Raises:

Type Description
ValueError

If search_type is not one of "similarity", "mmr", or "similarity_score_threshold".

asearch async

asearch(
    query: str, search_type: str, **kwargs: Any
) -> list[Document]

Async return docs most similar to query using a specified search type.

Parameters:

Name Type Description Default
query str

Input text.

required
search_type str

Type of search to perform. Can be "similarity", "mmr", or "similarity_score_threshold".

required
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents most similar to the query.

Raises:

Type Description
ValueError

If search_type is not one of "similarity", "mmr", or "similarity_score_threshold".

similarity_search(
    query: str,
    k: int = 4,
    filter: Optional[List[Namespace]] = None,
    numeric_filter: Optional[List[NumericNamespace]] = None,
    **kwargs: Any
) -> List[Document]

Return docs most similar to query.

Parameters:

Name Type Description Default
query str

The string that will be used to search for similar documents.

required
k int

The amount of neighbors that will be retrieved.

4
filter Optional[List[Namespace]]

Optional. A list of Namespaces for filtering the matching results. For example: [Namespace("color", ["red"], []), Namespace("shape", [], ["squared"])] will match datapoints that satisfy "red color" but not include datapoints with "squared shape". Please refer to https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#json for more detail.

None
numeric_filter Optional[List[NumericNamespace]]

Optional. A list of NumericNamespaces for filterning the matching results. Please refer to https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#json for more detail.

None

Returns:

Type Description
List[Document]

A list of k matching documents.

similarity_search_with_score

similarity_search_with_score(
    query: str,
    k: int = 4,
    filter: Optional[List[Namespace]] = None,
    numeric_filter: Optional[List[NumericNamespace]] = None,
    **kwargs: Any
) -> List[Tuple[Document, Union[float, Dict[str, float]]]]

Return docs most similar to query and their cosine distance from the query.

Parameters:

Name Type Description Default
query str

String query look up documents similar to.

required
k int

Number of Documents to return. Defaults to 4.

4
filter Optional[List[Namespace]]

Optional. A list of Namespaces for filtering the matching results. For example: [Namespace("color", ["red"], []), Namespace("shape", [], ["squared"])] will match datapoints that satisfy "red color" but not include datapoints with "squared shape". Please refer to https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#json for more detail.

None
numeric_filter Optional[List[NumericNamespace]]

Optional. A list of NumericNamespaces for filterning the matching results. Please refer to https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#json for more detail.

None

Returns:

Type Description
List[Tuple[Document, Union[float, Dict[str, float]]]]

List[Tuple[Document, float]]: List of documents most similar to

List[Tuple[Document, Union[float, Dict[str, float]]]]

the query text and cosine distance in float for each.

List[Tuple[Document, Union[float, Dict[str, float]]]]

Higher score represents more similarity.

asimilarity_search_with_score async

asimilarity_search_with_score(
    *args: Any, **kwargs: Any
) -> list[tuple[Document, float]]

Async run similarity search with distance.

Parameters:

Name Type Description Default
*args Any

Arguments to pass to the search method.

()
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[tuple[Document, float]]

List of Tuples of (doc, similarity_score).

similarity_search_with_relevance_scores

similarity_search_with_relevance_scores(
    query: str, k: int = 4, **kwargs: Any
) -> list[tuple[Document, float]]

Return docs and relevance scores in the range [0, 1].

0 is dissimilar, 1 is most similar.

Parameters:

Name Type Description Default
query str

Input text.

required
k int

Number of Documents to return. Defaults to 4.

4
**kwargs Any

kwargs to be passed to similarity search. Should include: score_threshold: Optional, a floating point value between 0 to 1 to filter the resulting set of retrieved docs.

{}

Returns:

Type Description
list[tuple[Document, float]]

List of Tuples of (doc, similarity_score).

asimilarity_search_with_relevance_scores async

asimilarity_search_with_relevance_scores(
    query: str, k: int = 4, **kwargs: Any
) -> list[tuple[Document, float]]

Async return docs and relevance scores in the range [0, 1].

0 is dissimilar, 1 is most similar.

Parameters:

Name Type Description Default
query str

Input text.

required
k int

Number of Documents to return. Defaults to 4.

4
**kwargs Any

kwargs to be passed to similarity search. Should include: score_threshold: Optional, a floating point value between 0 to 1 to filter the resulting set of retrieved docs

{}

Returns:

Type Description
list[tuple[Document, float]]

List of Tuples of (doc, similarity_score)

asimilarity_search(
    query: str, k: int = 4, **kwargs: Any
) -> list[Document]

Async return docs most similar to query.

Parameters:

Name Type Description Default
query str

Input text.

required
k int

Number of Documents to return. Defaults to 4.

4
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents most similar to the query.

similarity_search_by_vector

similarity_search_by_vector(
    embedding: list[float], k: int = 4, **kwargs: Any
) -> list[Document]

Return docs most similar to embedding vector.

Parameters:

Name Type Description Default
embedding list[float]

Embedding to look up documents similar to.

required
k int

Number of Documents to return. Defaults to 4.

4
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents most similar to the query vector.

asimilarity_search_by_vector async

asimilarity_search_by_vector(
    embedding: list[float], k: int = 4, **kwargs: Any
) -> list[Document]

Async return docs most similar to embedding vector.

Parameters:

Name Type Description Default
embedding list[float]

Embedding to look up documents similar to.

required
k int

Number of Documents to return. Defaults to 4.

4
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents most similar to the query vector.

max_marginal_relevance_search(
    query: str,
    k: int = 4,
    fetch_k: int = 20,
    lambda_mult: float = 0.5,
    **kwargs: Any
) -> list[Document]

Return docs selected using the maximal marginal relevance.

Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents.

Parameters:

Name Type Description Default
query str

Text to look up documents similar to.

required
k int

Number of Documents to return. Defaults to 4.

4
fetch_k int

Number of Documents to fetch to pass to MMR algorithm. Default is 20.

20
lambda_mult float

Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. Defaults to 0.5.

0.5
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents selected by maximal marginal relevance.

amax_marginal_relevance_search(
    query: str,
    k: int = 4,
    fetch_k: int = 20,
    lambda_mult: float = 0.5,
    **kwargs: Any
) -> list[Document]

Async return docs selected using the maximal marginal relevance.

Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents.

Parameters:

Name Type Description Default
query str

Text to look up documents similar to.

required
k int

Number of Documents to return. Defaults to 4.

4
fetch_k int

Number of Documents to fetch to pass to MMR algorithm. Default is 20.

20
lambda_mult float

Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. Defaults to 0.5.

0.5
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents selected by maximal marginal relevance.

max_marginal_relevance_search_by_vector

max_marginal_relevance_search_by_vector(
    embedding: list[float],
    k: int = 4,
    fetch_k: int = 20,
    lambda_mult: float = 0.5,
    **kwargs: Any
) -> list[Document]

Return docs selected using the maximal marginal relevance.

Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents.

Parameters:

Name Type Description Default
embedding list[float]

Embedding to look up documents similar to.

required
k int

Number of Documents to return. Defaults to 4.

4
fetch_k int

Number of Documents to fetch to pass to MMR algorithm. Default is 20.

20
lambda_mult float

Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. Defaults to 0.5.

0.5
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents selected by maximal marginal relevance.

amax_marginal_relevance_search_by_vector async

amax_marginal_relevance_search_by_vector(
    embedding: list[float],
    k: int = 4,
    fetch_k: int = 20,
    lambda_mult: float = 0.5,
    **kwargs: Any
) -> list[Document]

Async return docs selected using the maximal marginal relevance.

Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents.

Parameters:

Name Type Description Default
embedding list[float]

Embedding to look up documents similar to.

required
k int

Number of Documents to return. Defaults to 4.

4
fetch_k int

Number of Documents to fetch to pass to MMR algorithm. Default is 20.

20
lambda_mult float

Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. Defaults to 0.5.

0.5
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents selected by maximal marginal relevance.

from_documents classmethod

from_documents(
    documents: list[Document],
    embedding: Embeddings,
    **kwargs: Any
) -> Self

Return VectorStore initialized from documents and embeddings.

Parameters:

Name Type Description Default
documents list[Document]

List of Documents to add to the vectorstore.

required
embedding Embeddings

Embedding function to use.

required
kwargs Any

Additional keyword arguments.

{}

Returns:

Name Type Description
VectorStore Self

VectorStore initialized from documents and embeddings.

afrom_documents async classmethod

afrom_documents(
    documents: list[Document],
    embedding: Embeddings,
    **kwargs: Any
) -> Self

Async return VectorStore initialized from documents and embeddings.

Parameters:

Name Type Description Default
documents list[Document]

List of Documents to add to the vectorstore.

required
embedding Embeddings

Embedding function to use.

required
kwargs Any

Additional keyword arguments.

{}

Returns:

Name Type Description
VectorStore Self

VectorStore initialized from documents and embeddings.

from_texts classmethod

from_texts(
    texts: List[str],
    embedding: Embeddings,
    metadatas: Union[List[dict], None] = None,
    **kwargs: Any
) -> _BaseVertexAIVectorStore

Use from components instead.

afrom_texts async classmethod

afrom_texts(
    texts: list[str],
    embedding: Embeddings,
    metadatas: list[dict] | None = None,
    *,
    ids: list[str] | None = None,
    **kwargs: Any
) -> Self

Async return VectorStore initialized from texts and embeddings.

Parameters:

Name Type Description Default
texts list[str]

Texts to add to the vectorstore.

required
embedding Embeddings

Embedding function to use.

required
metadatas list[dict] | None

Optional list of metadatas associated with the texts. Default is None.

None
ids list[str] | None

Optional list of IDs associated with the texts.

None
kwargs Any

Additional keyword arguments.

{}

Returns:

Name Type Description
VectorStore Self

VectorStore initialized from texts and embeddings.

as_retriever

as_retriever(**kwargs: Any) -> VectorStoreRetriever

Return VectorStoreRetriever initialized from this VectorStore.

Parameters:

Name Type Description Default
**kwargs Any

Keyword arguments to pass to the search function. Can include: search_type (Optional[str]): Defines the type of search that the Retriever should perform. Can be "similarity" (default), "mmr", or "similarity_score_threshold". search_kwargs (Optional[Dict]): Keyword arguments to pass to the search function. Can include things like: k: Amount of documents to return (Default: 4) score_threshold: Minimum relevance threshold for similarity_score_threshold fetch_k: Amount of documents to pass to MMR algorithm (Default: 20) lambda_mult: Diversity of results returned by MMR; 1 for minimum diversity and 0 for maximum. (Default: 0.5) filter: Filter by document metadata

{}

Returns:

Name Type Description
VectorStoreRetriever VectorStoreRetriever

Retriever class for VectorStore.

Examples:

.. code-block:: python

# Retrieve more documents with higher diversity
# Useful if your dataset has many similar documents
docsearch.as_retriever(
    search_type="mmr", search_kwargs={"k": 6, "lambda_mult": 0.25}
)

# Fetch more documents for the MMR algorithm to consider
# But only return the top 5
docsearch.as_retriever(
    search_type="mmr", search_kwargs={"k": 5, "fetch_k": 50}
)

# Only retrieve documents that have a relevance score
# Above a certain threshold
docsearch.as_retriever(
    search_type="similarity_score_threshold",
    search_kwargs={"score_threshold": 0.8},
)

# Only get the single most similar document from the dataset
docsearch.as_retriever(search_kwargs={"k": 1})

# Use a filter to only retrieve documents from a specific paper
docsearch.as_retriever(
    search_kwargs={"filter": {"paper_title": "GPT-4 Technical Report"}}
)

__init__

__init__(
    searcher: Searcher,
    document_storage: DocumentStorage,
    embbedings: Optional[Embeddings] = None,
    embeddings: Optional[Embeddings] = None,
) -> None

Constructor.

Parameters:

Name Type Description Default
searcher Searcher

Object in charge of searching and storing the index.

required
document_storage DocumentStorage

Object in charge of storing and retrieving documents.

required
embbedings Optional[Embeddings]

Object in charge of transforming text to embbeddings. Deprecated: Use 'embeddings' instead.

None
embeddings Optional[Embeddings]

Object in charge of transforming text to embeddings.

None

similarity_search_by_vector_with_score

similarity_search_by_vector_with_score(
    embedding: List[float],
    sparse_embedding: Optional[
        Dict[str, Union[List[int], List[float]]]
    ] = None,
    k: int = 4,
    rrf_ranking_alpha: float = 1,
    filter: Optional[List[Namespace]] = None,
    numeric_filter: Optional[List[NumericNamespace]] = None,
    **kwargs: Any
) -> List[Tuple[Document, Union[float, Dict[str, float]]]]

Return docs most similar to the embedding and their cosine distance.

Parameters:

Name Type Description Default
embedding List[float]

Embedding to look up documents similar to.

required
sparse_embedding Optional[Dict[str, Union[List[int], List[float]]]]

Sparse embedding dictionary which represents an embedding as a list of dimensions and as a list of sparse values: ie. {"values": [0.7, 0.5], "dimensions": [10, 20]}

None
k int

Number of Documents to return. Defaults to 4.

4
rrf_ranking_alpha float

Reciprocal Ranking Fusion weight, float between 0 and 1.0 Weights Dense Search VS Sparse Search, as an example: - rrf_ranking_alpha=1: Only Dense - rrf_ranking_alpha=0: Only Sparse - rrf_ranking_alpha=0.7: 0.7 weighting for dense and 0.3 for sparse

1
filter Optional[List[Namespace]]

Optional. A list of Namespaces for filtering the matching results. For example: [Namespace("color", ["red"], []), Namespace("shape", [], ["squared"])] will match datapoints that satisfy "red color" but not include datapoints with "squared shape". Please refer to https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#json for more detail.

None
numeric_filter Optional[List[NumericNamespace]]

Optional. A list of NumericNamespaces for filterning the matching results. Please refer to https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#json for more detail.

None

Returns:

Type Description
List[Tuple[Document, Union[float, Dict[str, float]]]]

List[Tuple[Document, Union[float, Dict[str, float]]]]:

List[Tuple[Document, Union[float, Dict[str, float]]]]

List of documents most similar to the query text and either

List[Tuple[Document, Union[float, Dict[str, float]]]]

cosine distance in float for each or dictionary with both dense and sparse

List[Tuple[Document, Union[float, Dict[str, float]]]]

scores if running hybrid search.

List[Tuple[Document, Union[float, Dict[str, float]]]]

Higher score represents more similarity.

from_components classmethod

from_components(
    project_id: str,
    region: str,
    index_id: str,
    endpoint_id: str,
    index_staging_bucket_name: Optional[str] = None,
    credentials: Optional[Credentials] = None,
    credentials_path: Optional[str] = None,
    embedding: Optional[Embeddings] = None,
    stream_update: bool = False,
    datastore_client_kwargs: Optional[
        Dict[str, Any]
    ] = None,
    exclude_from_indexes: Optional[List[str]] = None,
    datastore_kind: str = "document_id",
    datastore_text_property_name: str = "text",
    datastore_metadata_property_name: str = "metadata",
    **kwargs: Dict[str, Any]
) -> VectorSearchVectorStoreDatastore

Takes the object creation out of the constructor.

Parameters:

Name Type Description Default
project_id str

The GCP project id.

required
region str

The default location making the API calls. It must have the same location as the GCS bucket and must be regional.

required
index_id str

The id of the created index.

required
endpoint_id str

The id of the created endpoint.

required
index_staging_bucket_name Optional[str]

(Optional) If the index is updated by batch, bucket where the data will be staged before updating the index. Only required when updating the index.

None
credentials Optional[Credentials]

Google cloud Credentials object.

None
credentials_path Optional[str]

(Optional) The path of the Google credentials on

None
embedding Optional[Embeddings]

The :class:Embeddings that will be used for

None
stream_update bool

Whether to update with streaming or batching. VectorSearch index must be compatible with stream/batch updates.

False
kwargs Dict[str, Any]

Additional keyword arguments to pass to VertexAIVectorSearch.init().

{}
exclude_from_indexes Optional[List[str]]

Fields to exclude from datastore indexing

None

Returns:

Type Description
VectorSearchVectorStoreDatastore

A configured VectorSearchVectorStoreDatastore.

VectorSearchVectorStoreGCS

Bases: VectorSearchVectorStore

Alias of VectorSearchVectorStore for consistency with the rest of vector stores with different document storage backends.

Methods:

Name Description
add_texts

Run more texts through the embeddings and add to the vectorstore.

delete

Delete by vector ID.

get_by_ids

Get documents by their IDs.

aget_by_ids

Async get documents by their IDs.

adelete

Async delete by vector ID or other criteria.

aadd_texts

Async run more texts through the embeddings and add to the vectorstore.

add_documents

Add or update documents in the vectorstore.

aadd_documents

Async run more documents through the embeddings and add to the vectorstore.

search

Return docs most similar to query using a specified search type.

asearch

Async return docs most similar to query using a specified search type.

similarity_search

Return docs most similar to query.

similarity_search_with_score

Return docs most similar to query and their cosine distance from the query.

asimilarity_search_with_score

Async run similarity search with distance.

similarity_search_with_relevance_scores

Return docs and relevance scores in the range [0, 1].

asimilarity_search_with_relevance_scores

Async return docs and relevance scores in the range [0, 1].

asimilarity_search

Async return docs most similar to query.

similarity_search_by_vector

Return docs most similar to embedding vector.

asimilarity_search_by_vector

Async return docs most similar to embedding vector.

max_marginal_relevance_search

Return docs selected using the maximal marginal relevance.

amax_marginal_relevance_search

Async return docs selected using the maximal marginal relevance.

max_marginal_relevance_search_by_vector

Return docs selected using the maximal marginal relevance.

amax_marginal_relevance_search_by_vector

Async return docs selected using the maximal marginal relevance.

from_documents

Return VectorStore initialized from documents and embeddings.

afrom_documents

Async return VectorStore initialized from documents and embeddings.

from_texts

Use from components instead.

afrom_texts

Async return VectorStore initialized from texts and embeddings.

as_retriever

Return VectorStoreRetriever initialized from this VectorStore.

__init__

Constructor.

similarity_search_by_vector_with_score

Return docs most similar to the embedding and their cosine distance.

from_components

Takes the object creation out of the constructor.

Attributes:

Name Type Description
embbedings Embeddings

Returns the embeddings object.

embbedings property

embbedings: Embeddings

Returns the embeddings object.

add_texts

add_texts(
    texts: Iterable[str],
    metadatas: Union[List[dict], None] = None,
    *,
    ids: Optional[List[str]] = None,
    is_complete_overwrite: bool = False,
    **kwargs: Any
) -> List[str]

Run more texts through the embeddings and add to the vectorstore.

Parameters:

Name Type Description Default
texts Iterable[str]

Iterable of strings to add to the vectorstore.

required
metadatas Union[List[dict], None]

Optional list of metadatas associated with the texts.

None
ids Optional[List[str]]

Optional list of ids to be assigned to the texts in the index. If None, unique ids will be generated.

None
is_complete_overwrite bool

Optional, determines whether this is an append or overwrite operation. Only relevant for BATCH UPDATE indexes.

False
kwargs Any

vectorstore specific parameters.

{}

Returns:

Type Description
List[str]

List of ids from adding the texts into the vectorstore.

delete

delete(
    ids: Optional[List[str]] = None, **kwargs: Any
) -> Optional[bool]

Delete by vector ID.

Parameters:

Name Type Description Default
ids Optional[List[str]]

List of ids to delete.

None
**kwargs Any

If added metadata={}, deletes the documents

{}

Returns:

Type Description
Optional[bool]

Optional[bool]: True if deletion is successful.

Raises:

Type Description
ValueError

If ids is None or an empty list.

RuntimeError

If an error occurs during the deletion process.

get_by_ids

get_by_ids(ids: Sequence[str]) -> list[Document]

Get documents by their IDs.

The returned documents are expected to have the ID field set to the ID of the document in the vector store.

Fewer documents may be returned than requested if some IDs are not found or if there are duplicated IDs.

Users should not assume that the order of the returned documents matches the order of the input IDs. Instead, users should rely on the ID field of the returned documents.

This method should NOT raise exceptions if no documents are found for some IDs.

Parameters:

Name Type Description Default
ids Sequence[str]

List of ids to retrieve.

required

Returns:

Type Description
list[Document]

List of Documents.

Added in version 0.2.11

aget_by_ids async

aget_by_ids(ids: Sequence[str]) -> list[Document]

Async get documents by their IDs.

The returned documents are expected to have the ID field set to the ID of the document in the vector store.

Fewer documents may be returned than requested if some IDs are not found or if there are duplicated IDs.

Users should not assume that the order of the returned documents matches the order of the input IDs. Instead, users should rely on the ID field of the returned documents.

This method should NOT raise exceptions if no documents are found for some IDs.

Parameters:

Name Type Description Default
ids Sequence[str]

List of ids to retrieve.

required

Returns:

Type Description
list[Document]

List of Documents.

Added in version 0.2.11

adelete async

adelete(
    ids: list[str] | None = None, **kwargs: Any
) -> bool | None

Async delete by vector ID or other criteria.

Parameters:

Name Type Description Default
ids list[str] | None

List of ids to delete. If None, delete all. Default is None.

None
**kwargs Any

Other keyword arguments that subclasses might use.

{}

Returns:

Type Description
bool | None

Optional[bool]: True if deletion is successful,

bool | None

False otherwise, None if not implemented.

aadd_texts async

aadd_texts(
    texts: Iterable[str],
    metadatas: list[dict] | None = None,
    *,
    ids: list[str] | None = None,
    **kwargs: Any
) -> list[str]

Async run more texts through the embeddings and add to the vectorstore.

Parameters:

Name Type Description Default
texts Iterable[str]

Iterable of strings to add to the vectorstore.

required
metadatas list[dict] | None

Optional list of metadatas associated with the texts. Default is None.

None
ids list[str] | None

Optional list

None
**kwargs Any

vectorstore specific parameters.

{}

Returns:

Type Description
list[str]

List of ids from adding the texts into the vectorstore.

Raises:

Type Description
ValueError

If the number of metadatas does not match the number of texts.

ValueError

If the number of ids does not match the number of texts.

add_documents

add_documents(
    documents: list[Document], **kwargs: Any
) -> list[str]

Add or update documents in the vectorstore.

Parameters:

Name Type Description Default
documents list[Document]

Documents to add to the vectorstore.

required
kwargs Any

Additional keyword arguments. if kwargs contains ids and documents contain ids, the ids in the kwargs will receive precedence.

{}

Returns:

Type Description
list[str]

List of IDs of the added texts.

aadd_documents async

aadd_documents(
    documents: list[Document], **kwargs: Any
) -> list[str]

Async run more documents through the embeddings and add to the vectorstore.

Parameters:

Name Type Description Default
documents list[Document]

Documents to add to the vectorstore.

required
kwargs Any

Additional keyword arguments.

{}

Returns:

Type Description
list[str]

List of IDs of the added texts.

search

search(
    query: str, search_type: str, **kwargs: Any
) -> list[Document]

Return docs most similar to query using a specified search type.

Parameters:

Name Type Description Default
query str

Input text

required
search_type str

Type of search to perform. Can be "similarity", "mmr", or "similarity_score_threshold".

required
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents most similar to the query.

Raises:

Type Description
ValueError

If search_type is not one of "similarity", "mmr", or "similarity_score_threshold".

asearch async

asearch(
    query: str, search_type: str, **kwargs: Any
) -> list[Document]

Async return docs most similar to query using a specified search type.

Parameters:

Name Type Description Default
query str

Input text.

required
search_type str

Type of search to perform. Can be "similarity", "mmr", or "similarity_score_threshold".

required
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents most similar to the query.

Raises:

Type Description
ValueError

If search_type is not one of "similarity", "mmr", or "similarity_score_threshold".

similarity_search(
    query: str,
    k: int = 4,
    filter: Optional[List[Namespace]] = None,
    numeric_filter: Optional[List[NumericNamespace]] = None,
    **kwargs: Any
) -> List[Document]

Return docs most similar to query.

Parameters:

Name Type Description Default
query str

The string that will be used to search for similar documents.

required
k int

The amount of neighbors that will be retrieved.

4
filter Optional[List[Namespace]]

Optional. A list of Namespaces for filtering the matching results. For example: [Namespace("color", ["red"], []), Namespace("shape", [], ["squared"])] will match datapoints that satisfy "red color" but not include datapoints with "squared shape". Please refer to https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#json for more detail.

None
numeric_filter Optional[List[NumericNamespace]]

Optional. A list of NumericNamespaces for filterning the matching results. Please refer to https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#json for more detail.

None

Returns:

Type Description
List[Document]

A list of k matching documents.

similarity_search_with_score

similarity_search_with_score(
    query: str,
    k: int = 4,
    filter: Optional[List[Namespace]] = None,
    numeric_filter: Optional[List[NumericNamespace]] = None,
    **kwargs: Any
) -> List[Tuple[Document, Union[float, Dict[str, float]]]]

Return docs most similar to query and their cosine distance from the query.

Parameters:

Name Type Description Default
query str

String query look up documents similar to.

required
k int

Number of Documents to return. Defaults to 4.

4
filter Optional[List[Namespace]]

Optional. A list of Namespaces for filtering the matching results. For example: [Namespace("color", ["red"], []), Namespace("shape", [], ["squared"])] will match datapoints that satisfy "red color" but not include datapoints with "squared shape". Please refer to https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#json for more detail.

None
numeric_filter Optional[List[NumericNamespace]]

Optional. A list of NumericNamespaces for filterning the matching results. Please refer to https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#json for more detail.

None

Returns:

Type Description
List[Tuple[Document, Union[float, Dict[str, float]]]]

List[Tuple[Document, float]]: List of documents most similar to

List[Tuple[Document, Union[float, Dict[str, float]]]]

the query text and cosine distance in float for each.

List[Tuple[Document, Union[float, Dict[str, float]]]]

Higher score represents more similarity.

asimilarity_search_with_score async

asimilarity_search_with_score(
    *args: Any, **kwargs: Any
) -> list[tuple[Document, float]]

Async run similarity search with distance.

Parameters:

Name Type Description Default
*args Any

Arguments to pass to the search method.

()
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[tuple[Document, float]]

List of Tuples of (doc, similarity_score).

similarity_search_with_relevance_scores

similarity_search_with_relevance_scores(
    query: str, k: int = 4, **kwargs: Any
) -> list[tuple[Document, float]]

Return docs and relevance scores in the range [0, 1].

0 is dissimilar, 1 is most similar.

Parameters:

Name Type Description Default
query str

Input text.

required
k int

Number of Documents to return. Defaults to 4.

4
**kwargs Any

kwargs to be passed to similarity search. Should include: score_threshold: Optional, a floating point value between 0 to 1 to filter the resulting set of retrieved docs.

{}

Returns:

Type Description
list[tuple[Document, float]]

List of Tuples of (doc, similarity_score).

asimilarity_search_with_relevance_scores async

asimilarity_search_with_relevance_scores(
    query: str, k: int = 4, **kwargs: Any
) -> list[tuple[Document, float]]

Async return docs and relevance scores in the range [0, 1].

0 is dissimilar, 1 is most similar.

Parameters:

Name Type Description Default
query str

Input text.

required
k int

Number of Documents to return. Defaults to 4.

4
**kwargs Any

kwargs to be passed to similarity search. Should include: score_threshold: Optional, a floating point value between 0 to 1 to filter the resulting set of retrieved docs

{}

Returns:

Type Description
list[tuple[Document, float]]

List of Tuples of (doc, similarity_score)

asimilarity_search(
    query: str, k: int = 4, **kwargs: Any
) -> list[Document]

Async return docs most similar to query.

Parameters:

Name Type Description Default
query str

Input text.

required
k int

Number of Documents to return. Defaults to 4.

4
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents most similar to the query.

similarity_search_by_vector

similarity_search_by_vector(
    embedding: list[float], k: int = 4, **kwargs: Any
) -> list[Document]

Return docs most similar to embedding vector.

Parameters:

Name Type Description Default
embedding list[float]

Embedding to look up documents similar to.

required
k int

Number of Documents to return. Defaults to 4.

4
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents most similar to the query vector.

asimilarity_search_by_vector async

asimilarity_search_by_vector(
    embedding: list[float], k: int = 4, **kwargs: Any
) -> list[Document]

Async return docs most similar to embedding vector.

Parameters:

Name Type Description Default
embedding list[float]

Embedding to look up documents similar to.

required
k int

Number of Documents to return. Defaults to 4.

4
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents most similar to the query vector.

max_marginal_relevance_search(
    query: str,
    k: int = 4,
    fetch_k: int = 20,
    lambda_mult: float = 0.5,
    **kwargs: Any
) -> list[Document]

Return docs selected using the maximal marginal relevance.

Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents.

Parameters:

Name Type Description Default
query str

Text to look up documents similar to.

required
k int

Number of Documents to return. Defaults to 4.

4
fetch_k int

Number of Documents to fetch to pass to MMR algorithm. Default is 20.

20
lambda_mult float

Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. Defaults to 0.5.

0.5
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents selected by maximal marginal relevance.

amax_marginal_relevance_search(
    query: str,
    k: int = 4,
    fetch_k: int = 20,
    lambda_mult: float = 0.5,
    **kwargs: Any
) -> list[Document]

Async return docs selected using the maximal marginal relevance.

Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents.

Parameters:

Name Type Description Default
query str

Text to look up documents similar to.

required
k int

Number of Documents to return. Defaults to 4.

4
fetch_k int

Number of Documents to fetch to pass to MMR algorithm. Default is 20.

20
lambda_mult float

Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. Defaults to 0.5.

0.5
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents selected by maximal marginal relevance.

max_marginal_relevance_search_by_vector

max_marginal_relevance_search_by_vector(
    embedding: list[float],
    k: int = 4,
    fetch_k: int = 20,
    lambda_mult: float = 0.5,
    **kwargs: Any
) -> list[Document]

Return docs selected using the maximal marginal relevance.

Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents.

Parameters:

Name Type Description Default
embedding list[float]

Embedding to look up documents similar to.

required
k int

Number of Documents to return. Defaults to 4.

4
fetch_k int

Number of Documents to fetch to pass to MMR algorithm. Default is 20.

20
lambda_mult float

Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. Defaults to 0.5.

0.5
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents selected by maximal marginal relevance.

amax_marginal_relevance_search_by_vector async

amax_marginal_relevance_search_by_vector(
    embedding: list[float],
    k: int = 4,
    fetch_k: int = 20,
    lambda_mult: float = 0.5,
    **kwargs: Any
) -> list[Document]

Async return docs selected using the maximal marginal relevance.

Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents.

Parameters:

Name Type Description Default
embedding list[float]

Embedding to look up documents similar to.

required
k int

Number of Documents to return. Defaults to 4.

4
fetch_k int

Number of Documents to fetch to pass to MMR algorithm. Default is 20.

20
lambda_mult float

Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. Defaults to 0.5.

0.5
**kwargs Any

Arguments to pass to the search method.

{}

Returns:

Type Description
list[Document]

List of Documents selected by maximal marginal relevance.

from_documents classmethod

from_documents(
    documents: list[Document],
    embedding: Embeddings,
    **kwargs: Any
) -> Self

Return VectorStore initialized from documents and embeddings.

Parameters:

Name Type Description Default
documents list[Document]

List of Documents to add to the vectorstore.

required
embedding Embeddings

Embedding function to use.

required
kwargs Any

Additional keyword arguments.

{}

Returns:

Name Type Description
VectorStore Self

VectorStore initialized from documents and embeddings.

afrom_documents async classmethod

afrom_documents(
    documents: list[Document],
    embedding: Embeddings,
    **kwargs: Any
) -> Self

Async return VectorStore initialized from documents and embeddings.

Parameters:

Name Type Description Default
documents list[Document]

List of Documents to add to the vectorstore.

required
embedding Embeddings

Embedding function to use.

required
kwargs Any

Additional keyword arguments.

{}

Returns:

Name Type Description
VectorStore Self

VectorStore initialized from documents and embeddings.

from_texts classmethod

from_texts(
    texts: List[str],
    embedding: Embeddings,
    metadatas: Union[List[dict], None] = None,
    **kwargs: Any
) -> _BaseVertexAIVectorStore

Use from components instead.

afrom_texts async classmethod

afrom_texts(
    texts: list[str],
    embedding: Embeddings,
    metadatas: list[dict] | None = None,
    *,
    ids: list[str] | None = None,
    **kwargs: Any
) -> Self

Async return VectorStore initialized from texts and embeddings.

Parameters:

Name Type Description Default
texts list[str]

Texts to add to the vectorstore.

required
embedding Embeddings

Embedding function to use.

required
metadatas list[dict] | None

Optional list of metadatas associated with the texts. Default is None.

None
ids list[str] | None

Optional list of IDs associated with the texts.

None
kwargs Any

Additional keyword arguments.

{}

Returns:

Name Type Description
VectorStore Self

VectorStore initialized from texts and embeddings.

as_retriever

as_retriever(**kwargs: Any) -> VectorStoreRetriever

Return VectorStoreRetriever initialized from this VectorStore.

Parameters:

Name Type Description Default
**kwargs Any

Keyword arguments to pass to the search function. Can include: search_type (Optional[str]): Defines the type of search that the Retriever should perform. Can be "similarity" (default), "mmr", or "similarity_score_threshold". search_kwargs (Optional[Dict]): Keyword arguments to pass to the search function. Can include things like: k: Amount of documents to return (Default: 4) score_threshold: Minimum relevance threshold for similarity_score_threshold fetch_k: Amount of documents to pass to MMR algorithm (Default: 20) lambda_mult: Diversity of results returned by MMR; 1 for minimum diversity and 0 for maximum. (Default: 0.5) filter: Filter by document metadata

{}

Returns:

Name Type Description
VectorStoreRetriever VectorStoreRetriever

Retriever class for VectorStore.

Examples:

.. code-block:: python

# Retrieve more documents with higher diversity
# Useful if your dataset has many similar documents
docsearch.as_retriever(
    search_type="mmr", search_kwargs={"k": 6, "lambda_mult": 0.25}
)

# Fetch more documents for the MMR algorithm to consider
# But only return the top 5
docsearch.as_retriever(
    search_type="mmr", search_kwargs={"k": 5, "fetch_k": 50}
)

# Only retrieve documents that have a relevance score
# Above a certain threshold
docsearch.as_retriever(
    search_type="similarity_score_threshold",
    search_kwargs={"score_threshold": 0.8},
)

# Only get the single most similar document from the dataset
docsearch.as_retriever(search_kwargs={"k": 1})

# Use a filter to only retrieve documents from a specific paper
docsearch.as_retriever(
    search_kwargs={"filter": {"paper_title": "GPT-4 Technical Report"}}
)

__init__

__init__(
    searcher: Searcher,
    document_storage: DocumentStorage,
    embbedings: Optional[Embeddings] = None,
    embeddings: Optional[Embeddings] = None,
) -> None

Constructor.

Parameters:

Name Type Description Default
searcher Searcher

Object in charge of searching and storing the index.

required
document_storage DocumentStorage

Object in charge of storing and retrieving documents.

required
embbedings Optional[Embeddings]

Object in charge of transforming text to embbeddings. Deprecated: Use 'embeddings' instead.

None
embeddings Optional[Embeddings]

Object in charge of transforming text to embeddings.

None

similarity_search_by_vector_with_score

similarity_search_by_vector_with_score(
    embedding: List[float],
    sparse_embedding: Optional[
        Dict[str, Union[List[int], List[float]]]
    ] = None,
    k: int = 4,
    rrf_ranking_alpha: float = 1,
    filter: Optional[List[Namespace]] = None,
    numeric_filter: Optional[List[NumericNamespace]] = None,
    **kwargs: Any
) -> List[Tuple[Document, Union[float, Dict[str, float]]]]

Return docs most similar to the embedding and their cosine distance.

Parameters:

Name Type Description Default
embedding List[float]

Embedding to look up documents similar to.

required
sparse_embedding Optional[Dict[str, Union[List[int], List[float]]]]

Sparse embedding dictionary which represents an embedding as a list of dimensions and as a list of sparse values: ie. {"values": [0.7, 0.5], "dimensions": [10, 20]}

None
k int

Number of Documents to return. Defaults to 4.

4
rrf_ranking_alpha float

Reciprocal Ranking Fusion weight, float between 0 and 1.0 Weights Dense Search VS Sparse Search, as an example: - rrf_ranking_alpha=1: Only Dense - rrf_ranking_alpha=0: Only Sparse - rrf_ranking_alpha=0.7: 0.7 weighting for dense and 0.3 for sparse

1
filter Optional[List[Namespace]]

Optional. A list of Namespaces for filtering the matching results. For example: [Namespace("color", ["red"], []), Namespace("shape", [], ["squared"])] will match datapoints that satisfy "red color" but not include datapoints with "squared shape". Please refer to https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#json for more detail.

None
numeric_filter Optional[List[NumericNamespace]]

Optional. A list of NumericNamespaces for filterning the matching results. Please refer to https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#json for more detail.

None

Returns:

Type Description
List[Tuple[Document, Union[float, Dict[str, float]]]]

List[Tuple[Document, Union[float, Dict[str, float]]]]:

List[Tuple[Document, Union[float, Dict[str, float]]]]

List of documents most similar to the query text and either

List[Tuple[Document, Union[float, Dict[str, float]]]]

cosine distance in float for each or dictionary with both dense and sparse

List[Tuple[Document, Union[float, Dict[str, float]]]]

scores if running hybrid search.

List[Tuple[Document, Union[float, Dict[str, float]]]]

Higher score represents more similarity.

from_components classmethod

from_components(
    project_id: str,
    region: str,
    gcs_bucket_name: str,
    index_id: str,
    endpoint_id: str,
    private_service_connect_ip_address: Optional[
        str
    ] = None,
    credentials: Optional[Credentials] = None,
    credentials_path: Optional[str] = None,
    embedding: Optional[Embeddings] = None,
    stream_update: bool = False,
    **kwargs: Any
) -> VectorSearchVectorStore

Takes the object creation out of the constructor.

Parameters:

Name Type Description Default
project_id str

The GCP project id.

required
region str

The default location making the API calls. It must have

required
gcs_bucket_name str

The location where the vectors will be stored in

required
index_id str

The id of the created index.

required
endpoint_id str

The id of the created endpoint.

required
private_service_connect_ip_address Optional[str]

The IP address of the private

None
credentials Optional[Credentials]

Google cloud Credentials object.

None
credentials_path Optional[str]

(Optional) The path of the Google credentials on

None
embedding Optional[Embeddings]

The :class:Embeddings that will be used for

None
stream_update bool

Whether to update with streaming or batching. VectorSearch index must be compatible with stream/batch updates.

False
kwargs Any

Additional keyword arguments to pass to VertexAIVectorSearch.init().

{}

Returns:

Type Description
VectorSearchVectorStore

A configured VertexAIVectorSearch.

VertexAIImageCaptioning

Bases: _BaseVertexAIImageCaptioning, BaseLLM

Implementation of the Image Captioning model as an LLM.

Methods:

Name Description
get_name

Get the name of the Runnable.

get_input_schema

Get a pydantic model that can be used to validate input to the Runnable.

get_input_jsonschema

Get a JSON schema that represents the input to the Runnable.

get_output_schema

Get a pydantic model that can be used to validate output to the Runnable.

get_output_jsonschema

Get a JSON schema that represents the output of the Runnable.

config_schema

The type of config this Runnable accepts specified as a pydantic model.

get_config_jsonschema

Get a JSON schema that represents the config of the Runnable.

get_graph

Return a graph representation of this Runnable.

get_prompts

Return a list of prompts used by this Runnable.

__or__

Runnable "or" operator.

__ror__

Runnable "reverse-or" operator.

pipe

Pipe runnables.

pick

Pick keys from the output dict of this Runnable.

assign

Assigns new fields to the dict output of this Runnable.

batch_as_completed

Run invoke in parallel on a list of inputs.

abatch_as_completed

Run ainvoke in parallel on a list of inputs.

astream_log

Stream all output from a Runnable, as reported to the callback system.

astream_events

Generate a stream of events.

transform

Transform inputs to outputs.

atransform

Transform inputs to outputs.

bind

Bind arguments to a Runnable, returning a new Runnable.

with_config

Bind config to a Runnable, returning a new Runnable.

with_listeners

Bind lifecycle listeners to a Runnable, returning a new Runnable.

with_alisteners

Bind async lifecycle listeners to a Runnable.

with_types

Bind input and output types to a Runnable, returning a new Runnable.

with_retry

Create a new Runnable that retries the original Runnable on exceptions.

map

Return a new Runnable that maps a list of inputs to a list of outputs.

with_fallbacks

Add fallbacks to a Runnable, returning a new Runnable.

as_tool

Create a BaseTool from a Runnable.

__init__
is_lc_serializable

Is this class serializable?

get_lc_namespace

Get the namespace of the langchain object.

lc_id

Return a unique identifier for this class for serialization purposes.

to_json

Serialize the Runnable to JSON.

to_json_not_implemented

Serialize a "not implemented" object.

configurable_fields

Configure particular Runnable fields at runtime.

configurable_alternatives

Configure alternatives for Runnables that can be set at runtime.

set_verbose

If verbose is None, set it.

with_structured_output

Not implemented on this class.

get_token_ids

Return the ordered ids of the tokens in a text.

get_num_tokens

Get the number of tokens present in the text.

get_num_tokens_from_messages

Get the number of tokens in the messages.

generate

Pass a sequence of prompts to a model and return generations.

agenerate

Asynchronously pass a sequence of prompts to a model and return generations.

__str__

Return a string representation of the object for printing.

dict

Return a dictionary of the LLM.

save

Save the LLM.

Attributes:

Name Type Description
InputType TypeAlias

Get the input type for this runnable.

OutputType type[str]

Get the input type for this runnable.

input_schema type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema type[BaseModel]

Output schema.

config_specs list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets dict[str, str]

A map of constructor argument names to secret ids.

lc_attributes dict

List of attribute names that should be included in the serialized kwargs.

cache BaseCache | bool | None

Whether to cache the response.

verbose bool

Whether to print out response text.

callbacks Callbacks

Callbacks to add to the run trace.

tags list[str] | None

Tags to add to the run trace.

metadata dict[str, Any] | None

Metadata to add to the run trace.

custom_get_token_ids Callable[[str], list[int]] | None

Optional encoder to use for counting tokens.

model_name str

Name of the model to use

number_of_results int

Number of results to return from one query

language str

Language of the query

project Union[str, None]

Google cloud project

InputType property

InputType: TypeAlias

Get the input type for this runnable.

OutputType property

OutputType: type[str]

Get the input type for this runnable.

input_schema property

input_schema: type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema property

output_schema: type[BaseModel]

Output schema.

The type of output this Runnable produces specified as a pydantic model.

config_specs property

config_specs: list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets property

lc_secrets: dict[str, str]

A map of constructor argument names to secret ids.

For example,

lc_attributes property

lc_attributes: dict

List of attribute names that should be included in the serialized kwargs.

These attributes must be accepted by the constructor. Default is an empty dictionary.

cache class-attribute instance-attribute

cache: BaseCache | bool | None = Field(
    default=None, exclude=True
)

Whether to cache the response.

  • If true, will use the global cache.
  • If false, will not use a cache
  • If None, will use the global cache if it's set, otherwise no cache.
  • If instance of BaseCache, will use the provided cache.

Caching is not currently supported for streaming methods of models.

verbose class-attribute instance-attribute

verbose: bool = Field(
    default_factory=_get_verbosity, exclude=True, repr=False
)

Whether to print out response text.

callbacks class-attribute instance-attribute

callbacks: Callbacks = Field(default=None, exclude=True)

Callbacks to add to the run trace.

tags class-attribute instance-attribute

tags: list[str] | None = Field(default=None, exclude=True)

Tags to add to the run trace.

metadata class-attribute instance-attribute

metadata: dict[str, Any] | None = Field(
    default=None, exclude=True
)

Metadata to add to the run trace.

custom_get_token_ids class-attribute instance-attribute

custom_get_token_ids: Callable[[str], list[int]] | None = (
    Field(default=None, exclude=True)
)

Optional encoder to use for counting tokens.

model_name class-attribute instance-attribute

model_name: str = Field(default='imagetext@001')

Name of the model to use

number_of_results class-attribute instance-attribute

number_of_results: int = Field(default=1)

Number of results to return from one query

language class-attribute instance-attribute

language: str = Field(default='en')

Language of the query

project class-attribute instance-attribute

project: Union[str, None] = Field(default=None)

Google cloud project

get_name

get_name(
    suffix: str | None = None, *, name: str | None = None
) -> str

Get the name of the Runnable.

Parameters:

Name Type Description Default
suffix str | None

An optional suffix to append to the name.

None
name str | None

An optional name to use instead of the Runnable's name.

None

Returns:

Type Description
str

The name of the Runnable.

get_input_schema

get_input_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate input to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic input schema that depends on which configuration the Runnable is invoked with.

This method allows to get an input schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate input.

get_input_jsonschema

get_input_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the input to the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the input to the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_input_jsonschema())

Added in version 0.3.0

get_output_schema

get_output_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate output to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic output schema that depends on which configuration the Runnable is invoked with.

This method allows to get an output schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate output.

get_output_jsonschema

get_output_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the output of the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the output of the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_output_jsonschema())

Added in version 0.3.0

config_schema

config_schema(
    *, include: Sequence[str] | None = None
) -> type[BaseModel]

The type of config this Runnable accepts specified as a pydantic model.

To mark a field as configurable, see the configurable_fields and configurable_alternatives methods.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate config.

get_config_jsonschema

get_config_jsonschema(
    *, include: Sequence[str] | None = None
) -> dict[str, Any]

Get a JSON schema that represents the config of the Runnable.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the config of the Runnable.

Added in version 0.3.0

get_graph

get_graph(config: RunnableConfig | None = None) -> Graph

Return a graph representation of this Runnable.

get_prompts

get_prompts(
    config: RunnableConfig | None = None,
) -> list[BasePromptTemplate]

Return a list of prompts used by this Runnable.

__or__

__or__(
    other: (
        Runnable[Any, Other]
        | Callable[[Iterator[Any]], Iterator[Other]]
        | Callable[
            [AsyncIterator[Any]], AsyncIterator[Other]
        ]
        | Callable[[Any], Other]
        | Mapping[
            str,
            Runnable[Any, Other]
            | Callable[[Any], Other]
            | Any,
        ]
    ),
) -> RunnableSerializable[Input, Other]

Runnable "or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Any, Other] | Callable[[Iterator[Any]], Iterator[Other]] | Callable[[AsyncIterator[Any]], AsyncIterator[Other]] | Callable[[Any], Other] | Mapping[str, Runnable[Any, Other] | Callable[[Any], Other] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

__ror__

__ror__(
    other: (
        Runnable[Other, Any]
        | Callable[[Iterator[Other]], Iterator[Any]]
        | Callable[
            [AsyncIterator[Other]], AsyncIterator[Any]
        ]
        | Callable[[Other], Any]
        | Mapping[
            str,
            Runnable[Other, Any]
            | Callable[[Other], Any]
            | Any,
        ]
    ),
) -> RunnableSerializable[Other, Output]

Runnable "reverse-or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Other, Any] | Callable[[Iterator[Other]], Iterator[Any]] | Callable[[AsyncIterator[Other]], AsyncIterator[Any]] | Callable[[Other], Any] | Mapping[str, Runnable[Other, Any] | Callable[[Other], Any] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Other, Output]

A new Runnable.

pipe

pipe(
    *others: Runnable[Any, Other] | Callable[[Any], Other],
    name: str | None = None
) -> RunnableSerializable[Input, Other]

Pipe runnables.

Compose this Runnable with Runnable-like objects to make a RunnableSequence.

Equivalent to RunnableSequence(self, *others) or self | others[0] | ...

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


def mul_two(x: int) -> int:
    return x * 2


runnable_1 = RunnableLambda(add_one)
runnable_2 = RunnableLambda(mul_two)
sequence = runnable_1.pipe(runnable_2)
# Or equivalently:
# sequence = runnable_1 | runnable_2
# sequence = RunnableSequence(first=runnable_1, last=runnable_2)
sequence.invoke(1)
await sequence.ainvoke(1)
# -> 4

sequence.batch([1, 2, 3])
await sequence.abatch([1, 2, 3])
# -> [4, 6, 8]

Parameters:

Name Type Description Default
*others Runnable[Any, Other] | Callable[[Any], Other]

Other Runnable or Runnable-like objects to compose

()
name str | None

An optional name for the resulting RunnableSequence.

None

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

pick

pick(
    keys: str | list[str],
) -> RunnableSerializable[Any, Any]

Pick keys from the output dict of this Runnable.

Pick single key:

```python
import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)
chain = RunnableMap(str=as_str, json=as_json)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3]}

json_only_chain = chain.pick("json")
json_only_chain.invoke("[1, 2, 3]")
# -> [1, 2, 3]
```

Pick list of keys:

```python
from typing import Any

import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)


def as_bytes(x: Any) -> bytes:
    return bytes(x, "utf-8")


chain = RunnableMap(
    str=as_str, json=as_json, bytes=RunnableLambda(as_bytes)
)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3], "bytes": b"[1, 2, 3]"}

json_and_bytes_chain = chain.pick(["json", "bytes"])
json_and_bytes_chain.invoke("[1, 2, 3]")
# -> {"json": [1, 2, 3], "bytes": b"[1, 2, 3]"}
```

Parameters:

Name Type Description Default
keys str | list[str]

A key or list of keys to pick from the output dict.

required

Returns:

Type Description
RunnableSerializable[Any, Any]

a new Runnable.

assign

assign(
    **kwargs: (
        Runnable[dict[str, Any], Any]
        | Callable[[dict[str, Any]], Any]
        | Mapping[
            str,
            Runnable[dict[str, Any], Any]
            | Callable[[dict[str, Any]], Any],
        ]
    ),
) -> RunnableSerializable[Any, Any]

Assigns new fields to the dict output of this Runnable.

from langchain_community.llms.fake import FakeStreamingListLLM
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import SystemMessagePromptTemplate
from langchain_core.runnables import Runnable
from operator import itemgetter

prompt = (
    SystemMessagePromptTemplate.from_template("You are a nice assistant.")
    + "{question}"
)
llm = FakeStreamingListLLM(responses=["foo-lish"])

chain: Runnable = prompt | llm | {"str": StrOutputParser()}

chain_with_assign = chain.assign(hello=itemgetter("str") | llm)

print(chain_with_assign.input_schema.model_json_schema())
# {'title': 'PromptInput', 'type': 'object', 'properties':
{'question': {'title': 'Question', 'type': 'string'}}}
print(chain_with_assign.output_schema.model_json_schema())
# {'title': 'RunnableSequenceOutput', 'type': 'object', 'properties':
{'str': {'title': 'Str',
'type': 'string'}, 'hello': {'title': 'Hello', 'type': 'string'}}}

Parameters:

Name Type Description Default
**kwargs Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any] | Mapping[str, Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any]]

A mapping of keys to Runnable or Runnable-like objects that will be invoked with the entire output dict of this Runnable.

{}

Returns:

Type Description
RunnableSerializable[Any, Any]

A new Runnable.

batch_as_completed

batch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> Iterator[tuple[int, Output | Exception]]

Run invoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
tuple[int, Output | Exception]

Tuples of the index of the input and the output from the Runnable.

abatch_as_completed async

abatch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> AsyncIterator[tuple[int, Output | Exception]]

Run ainvoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[tuple[int, Output | Exception]]

A tuple of the index of the input and the output from the Runnable.

astream_log async

astream_log(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    diff: bool = True,
    with_streamed_output_list: bool = True,
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

Stream all output from a Runnable, as reported to the callback system.

This includes all inner runs of LLMs, Retrievers, Tools, etc.

Output is streamed as Log objects, which include a list of Jsonpatch ops that describe how the state of the run has changed in each step, and the final state of the run.

The Jsonpatch ops can be applied in order to construct state.

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
diff bool

Whether to yield diffs between each step or the current state.

True
with_streamed_output_list bool

Whether to yield the streamed_output list.

True
include_names Sequence[str] | None

Only include logs with these names.

None
include_types Sequence[str] | None

Only include logs with these types.

None
include_tags Sequence[str] | None

Only include logs with these tags.

None
exclude_names Sequence[str] | None

Exclude logs with these names.

None
exclude_types Sequence[str] | None

Exclude logs with these types.

None
exclude_tags Sequence[str] | None

Exclude logs with these tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

A RunLogPatch or RunLog object.

astream_events async

astream_events(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    version: Literal["v1", "v2"] = "v2",
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[StreamEvent]

Generate a stream of events.

Use to create an iterator over StreamEvents that provide real-time information about the progress of the Runnable, including StreamEvents from intermediate results.

A StreamEvent is a dictionary with the following schema:

  • event: str - Event names are of the format: on_[runnable_type]_(start|stream|end).
  • name: str - The name of the Runnable that generated the event.
  • run_id: str - randomly generated ID associated with the given execution of the Runnable that emitted the event. A child Runnable that gets invoked as part of the execution of a parent Runnable is assigned its own unique ID.
  • parent_ids: list[str] - The IDs of the parent runnables that generated the event. The root Runnable will have an empty list. The order of the parent IDs is from the root to the immediate parent. Only available for v2 version of the API. The v1 version of the API will return an empty list.
  • tags: Optional[list[str]] - The tags of the Runnable that generated the event.
  • metadata: Optional[dict[str, Any]] - The metadata of the Runnable that generated the event.
  • data: dict[str, Any]

Below is a table that illustrates some events that might be emitted by various chains. Metadata fields have been omitted from the table for brevity. Chain definitions have been included after the table.

Note

This reference table is for the v2 version of the schema.

+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | event | name | chunk | input | output | +==========================+==================+=====================================+===================================================+=====================================================+ | on_chat_model_start | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_stream | [model name] | AIMessageChunk(content="hello") | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_end | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | AIMessageChunk(content="hello world") | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_start | [model name] | | {'input': 'hello'} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_stream | [model name] | 'Hello' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_end | [model name] | | 'Hello human!' | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_start | format_docs | | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_stream | format_docs | 'hello world!, goodbye world!' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_end | format_docs | | [Document(...)] | 'hello world!, goodbye world!' | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_start | some_tool | | {"x": 1, "y": "2"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_end | some_tool | | | {"x": 1, "y": "2"} | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_start | [retriever name] | | {"query": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_end | [retriever name] | | {"query": "hello"} | [Document(...), ..] | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_start | [template_name] | | {"question": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_end | [template_name] | | {"question": "hello"} | ChatPromptValue(messages: [SystemMessage, ...]) | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+

In addition to the standard events, users can also dispatch custom events (see example below).

Custom events will be only be surfaced with in the v2 version of the API!

A custom event has following format:

+-----------+------+-----------------------------------------------------------------------------------------------------------+ | Attribute | Type | Description | +===========+======+===========================================================================================================+ | name | str | A user defined name for the event. | +-----------+------+-----------------------------------------------------------------------------------------------------------+ | data | Any | The data associated with the event. This can be anything, though we suggest making it JSON serializable. | +-----------+------+-----------------------------------------------------------------------------------------------------------+

Here are declarations associated with the standard events shown above:

format_docs:

def format_docs(docs: list[Document]) -> str:
    '''Format the docs.'''
    return ", ".join([doc.page_content for doc in docs])


format_docs = RunnableLambda(format_docs)

some_tool:

@tool
def some_tool(x: int, y: str) -> dict:
    '''Some_tool.'''
    return {"x": x, "y": y}

prompt:

template = ChatPromptTemplate.from_messages(
    [
        ("system", "You are Cat Agent 007"),
        ("human", "{question}"),
    ]
).with_config({"run_name": "my_template", "tags": ["my_template"]})
Example:

from langchain_core.runnables import RunnableLambda


async def reverse(s: str) -> str:
    return s[::-1]


chain = RunnableLambda(func=reverse)

events = [event async for event in chain.astream_events("hello", version="v2")]

# will produce the following events (run_id, and parent_ids
# has been omitted for brevity):
[
    {
        "data": {"input": "hello"},
        "event": "on_chain_start",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"chunk": "olleh"},
        "event": "on_chain_stream",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"output": "olleh"},
        "event": "on_chain_end",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
]

Example: Dispatch Custom Event

from langchain_core.callbacks.manager import (
    adispatch_custom_event,
)
from langchain_core.runnables import RunnableLambda, RunnableConfig
import asyncio


async def slow_thing(some_input: str, config: RunnableConfig) -> str:
    """Do something that takes a long time."""
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 1 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 2 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    return "Done"

slow_thing = RunnableLambda(slow_thing)

async for event in slow_thing.astream_events("some_input", version="v2"):
    print(event)

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
version Literal['v1', 'v2']

The version of the schema to use either 'v2' or 'v1'. Users should use 'v2'. 'v1' is for backwards compatibility and will be deprecated in 0.4.0. No default will be assigned until the API is stabilized. custom events will only be surfaced in 'v2'.

'v2'
include_names Sequence[str] | None

Only include events from Runnables with matching names.

None
include_types Sequence[str] | None

Only include events from Runnables with matching types.

None
include_tags Sequence[str] | None

Only include events from Runnables with matching tags.

None
exclude_names Sequence[str] | None

Exclude events from Runnables with matching names.

None
exclude_types Sequence[str] | None

Exclude events from Runnables with matching types.

None
exclude_tags Sequence[str] | None

Exclude events from Runnables with matching tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable. These will be passed to astream_log as this implementation of astream_events is built on top of astream_log.

{}

Yields:

Type Description
AsyncIterator[StreamEvent]

An async stream of StreamEvents.

Raises:

Type Description
NotImplementedError

If the version is not 'v1' or 'v2'.

transform

transform(
    input: Iterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> Iterator[Output]

Transform inputs to outputs.

Default implementation of transform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input Iterator[Input]

An iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
Output

The output of the Runnable.

atransform async

atransform(
    input: AsyncIterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> AsyncIterator[Output]

Transform inputs to outputs.

Default implementation of atransform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input AsyncIterator[Input]

An async iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[Output]

The output of the Runnable.

bind

bind(**kwargs: Any) -> Runnable[Input, Output]

Bind arguments to a Runnable, returning a new Runnable.

Useful when a Runnable in a chain requires an argument that is not in the output of the previous Runnable or included in the user input.

Parameters:

Name Type Description Default
kwargs Any

The arguments to bind to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the arguments bound.

Example
from langchain_ollama import ChatOllama
from langchain_core.output_parsers import StrOutputParser

llm = ChatOllama(model="llama3.1")

# Without bind.
chain = llm | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two three four five.'

# With bind.
chain = llm.bind(stop=["three"]) | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two'

with_config

with_config(
    config: RunnableConfig | None = None, **kwargs: Any
) -> Runnable[Input, Output]

Bind config to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

The config to bind to the Runnable.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the config bound.

with_listeners

with_listeners(
    *,
    on_start: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_end: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_error: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None
) -> Runnable[Input, Output]

Bind lifecycle listeners to a Runnable, returning a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called before the Runnable starts running, with the Run object. Defaults to None.

None
on_end Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda
from langchain_core.tracers.schemas import Run

import time


def test_runnable(time_to_sleep: int):
    time.sleep(time_to_sleep)


def fn_start(run_obj: Run):
    print("start_time:", run_obj.start_time)


def fn_end(run_obj: Run):
    print("end_time:", run_obj.end_time)


chain = RunnableLambda(test_runnable).with_listeners(
    on_start=fn_start, on_end=fn_end
)
chain.invoke(2)

with_alisteners

with_alisteners(
    *,
    on_start: AsyncListener | None = None,
    on_end: AsyncListener | None = None,
    on_error: AsyncListener | None = None
) -> Runnable[Input, Output]

Bind async lifecycle listeners to a Runnable.

Returns a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start AsyncListener | None

Called asynchronously before the Runnable starts running, with the Run object. Defaults to None.

None
on_end AsyncListener | None

Called asynchronously after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error AsyncListener | None

Called asynchronously if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda, Runnable
from datetime import datetime, timezone
import time
import asyncio

def format_t(timestamp: float) -> str:
    return datetime.fromtimestamp(timestamp, tz=timezone.utc).isoformat()

async def test_runnable(time_to_sleep: int):
    print(f"Runnable[{time_to_sleep}s]: starts at {format_t(time.time())}")
    await asyncio.sleep(time_to_sleep)
    print(f"Runnable[{time_to_sleep}s]: ends at {format_t(time.time())}")

async def fn_start(run_obj: Runnable):
    print(f"on start callback starts at {format_t(time.time())}")
    await asyncio.sleep(3)
    print(f"on start callback ends at {format_t(time.time())}")

async def fn_end(run_obj: Runnable):
    print(f"on end callback starts at {format_t(time.time())}")
    await asyncio.sleep(2)
    print(f"on end callback ends at {format_t(time.time())}")

runnable = RunnableLambda(test_runnable).with_alisteners(
    on_start=fn_start,
    on_end=fn_end
)
async def concurrent_runs():
    await asyncio.gather(runnable.ainvoke(2), runnable.ainvoke(3))

asyncio.run(concurrent_runs())
Result:
on start callback starts at 2025-03-01T07:05:22.875378+00:00
on start callback starts at 2025-03-01T07:05:22.875495+00:00
on start callback ends at 2025-03-01T07:05:25.878862+00:00
on start callback ends at 2025-03-01T07:05:25.878947+00:00
Runnable[2s]: starts at 2025-03-01T07:05:25.879392+00:00
Runnable[3s]: starts at 2025-03-01T07:05:25.879804+00:00
Runnable[2s]: ends at 2025-03-01T07:05:27.881998+00:00
on end callback starts at 2025-03-01T07:05:27.882360+00:00
Runnable[3s]: ends at 2025-03-01T07:05:28.881737+00:00
on end callback starts at 2025-03-01T07:05:28.882428+00:00
on end callback ends at 2025-03-01T07:05:29.883893+00:00
on end callback ends at 2025-03-01T07:05:30.884831+00:00

with_types

with_types(
    *,
    input_type: type[Input] | None = None,
    output_type: type[Output] | None = None
) -> Runnable[Input, Output]

Bind input and output types to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
input_type type[Input] | None

The input type to bind to the Runnable. Defaults to None.

None
output_type type[Output] | None

The output type to bind to the Runnable. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the types bound.

with_retry

with_retry(
    *,
    retry_if_exception_type: tuple[
        type[BaseException], ...
    ] = (Exception,),
    wait_exponential_jitter: bool = True,
    exponential_jitter_params: (
        ExponentialJitterParams | None
    ) = None,
    stop_after_attempt: int = 3
) -> Runnable[Input, Output]

Create a new Runnable that retries the original Runnable on exceptions.

Parameters:

Name Type Description Default
retry_if_exception_type tuple[type[BaseException], ...]

A tuple of exception types to retry on. Defaults to (Exception,).

(Exception,)
wait_exponential_jitter bool

Whether to add jitter to the wait time between retries. Defaults to True.

True
stop_after_attempt int

The maximum number of attempts to make before giving up. Defaults to 3.

3
exponential_jitter_params ExponentialJitterParams | None

Parameters for tenacity.wait_exponential_jitter. Namely: initial, max, exp_base, and jitter (all float values).

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable that retries the original Runnable on exceptions.

Example
from langchain_core.runnables import RunnableLambda

count = 0


def _lambda(x: int) -> None:
    global count
    count = count + 1
    if x == 1:
        raise ValueError("x is 1")
    else:
        pass


runnable = RunnableLambda(_lambda)
try:
    runnable.with_retry(
        stop_after_attempt=2,
        retry_if_exception_type=(ValueError,),
    ).invoke(1)
except ValueError:
    pass

assert count == 2

map

map() -> Runnable[list[Input], list[Output]]

Return a new Runnable that maps a list of inputs to a list of outputs.

Calls invoke with each input.

Returns:

Type Description
Runnable[list[Input], list[Output]]

A new Runnable that maps a list of inputs to a list of outputs.

Example
from langchain_core.runnables import RunnableLambda


def _lambda(x: int) -> int:
    return x + 1


runnable = RunnableLambda(_lambda)
print(runnable.map().invoke([1, 2, 3]))  # [2, 3, 4]

with_fallbacks

with_fallbacks(
    fallbacks: Sequence[Runnable[Input, Output]],
    *,
    exceptions_to_handle: tuple[
        type[BaseException], ...
    ] = (Exception,),
    exception_key: str | None = None
) -> RunnableWithFallbacks[Input, Output]

Add fallbacks to a Runnable, returning a new Runnable.

The new Runnable will try the original Runnable, and then each fallback in order, upon failures.

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle. Defaults to (Exception,).

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input. Defaults to None.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

Example
from typing import Iterator

from langchain_core.runnables import RunnableGenerator


def _generate_immediate_error(input: Iterator) -> Iterator[str]:
    raise ValueError()
    yield ""


def _generate(input: Iterator) -> Iterator[str]:
    yield from "foo bar"


runnable = RunnableGenerator(_generate_immediate_error).with_fallbacks(
    [RunnableGenerator(_generate)]
)
print("".join(runnable.stream({})))  # foo bar

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle.

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

as_tool

as_tool(
    args_schema: type[BaseModel] | None = None,
    *,
    name: str | None = None,
    description: str | None = None,
    arg_types: dict[str, type] | None = None
) -> BaseTool

Create a BaseTool from a Runnable.

as_tool will instantiate a BaseTool with a name, description, and args_schema from a Runnable. Where possible, schemas are inferred from runnable.get_input_schema. Alternatively (e.g., if the Runnable takes a dict as input and the specific dict keys are not typed), the schema can be specified directly with args_schema. You can also pass arg_types to just specify the required arguments and their types.

Parameters:

Name Type Description Default
args_schema type[BaseModel] | None

The schema for the tool. Defaults to None.

None
name str | None

The name of the tool. Defaults to None.

None
description str | None

The description of the tool. Defaults to None.

None
arg_types dict[str, type] | None

A dictionary of argument names to types. Defaults to None.

None

Returns:

Type Description
BaseTool

A BaseTool instance.

Typed dict input:

from typing_extensions import TypedDict
from langchain_core.runnables import RunnableLambda


class Args(TypedDict):
    a: int
    b: list[int]


def f(x: Args) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool()
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via args_schema:

from typing import Any
from pydantic import BaseModel, Field
from langchain_core.runnables import RunnableLambda

def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))

class FSchema(BaseModel):
    """Apply a function to an integer and list of integers."""

    a: int = Field(..., description="Integer")
    b: list[int] = Field(..., description="List of ints")

runnable = RunnableLambda(f)
as_tool = runnable.as_tool(FSchema)
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via arg_types:

from typing import Any
from langchain_core.runnables import RunnableLambda


def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool(arg_types={"a": int, "b": list[int]})
as_tool.invoke({"a": 3, "b": [1, 2]})

String input:

from langchain_core.runnables import RunnableLambda


def f(x: str) -> str:
    return x + "a"


def g(x: str) -> str:
    return x + "z"


runnable = RunnableLambda(f) | g
as_tool = runnable.as_tool()
as_tool.invoke("b")

Added in version 0.2.14

__init__

__init__(*args: Any, **kwargs: Any) -> None

is_lc_serializable classmethod

is_lc_serializable() -> bool

Is this class serializable?

By design, even if a class inherits from Serializable, it is not serializable by default. This is to prevent accidental serialization of objects that should not be serialized.

Returns:

Type Description
bool

Whether the class is serializable. Default is False.

get_lc_namespace classmethod

get_lc_namespace() -> list[str]

Get the namespace of the langchain object.

For example, if the class is langchain.llms.openai.OpenAI, then the namespace is ["langchain", "llms", "openai"]

Returns:

Type Description
list[str]

The namespace as a list of strings.

lc_id classmethod

lc_id() -> list[str]

Return a unique identifier for this class for serialization purposes.

The unique identifier is a list of strings that describes the path to the object. For example, for the class langchain.llms.openai.OpenAI, the id is ["langchain", "llms", "openai", "OpenAI"].

to_json

to_json() -> (
    SerializedConstructor | SerializedNotImplemented
)

Serialize the Runnable to JSON.

Returns:

Type Description
SerializedConstructor | SerializedNotImplemented

A JSON-serializable representation of the Runnable.

to_json_not_implemented

to_json_not_implemented() -> SerializedNotImplemented

Serialize a "not implemented" object.

Returns:

Type Description
SerializedNotImplemented

SerializedNotImplemented.

configurable_fields

configurable_fields(
    **kwargs: AnyConfigurableField,
) -> RunnableSerializable[Input, Output]

Configure particular Runnable fields at runtime.

Parameters:

Name Type Description Default
**kwargs AnyConfigurableField

A dictionary of ConfigurableField instances to configure.

{}

Raises:

Type Description
ValueError

If a configuration key is not found in the Runnable.

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the fields configured.

from langchain_core.runnables import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatOpenAI(max_tokens=20).configurable_fields(
    max_tokens=ConfigurableField(
        id="output_token_number",
        name="Max tokens in the output",
        description="The maximum number of tokens in the output",
    )
)

# max_tokens = 20
print("max_tokens_20: ", model.invoke("tell me something about chess").content)

# max_tokens = 200
print(
    "max_tokens_200: ",
    model.with_config(configurable={"output_token_number": 200})
    .invoke("tell me something about chess")
    .content,
)

configurable_alternatives

configurable_alternatives(
    which: ConfigurableField,
    *,
    default_key: str = "default",
    prefix_keys: bool = False,
    **kwargs: (
        Runnable[Input, Output]
        | Callable[[], Runnable[Input, Output]]
    )
) -> RunnableSerializable[Input, Output]

Configure alternatives for Runnables that can be set at runtime.

Parameters:

Name Type Description Default
which ConfigurableField

The ConfigurableField instance that will be used to select the alternative.

required
default_key str

The default key to use if no alternative is selected. Defaults to 'default'.

'default'
prefix_keys bool

Whether to prefix the keys with the ConfigurableField id. Defaults to False.

False
**kwargs Runnable[Input, Output] | Callable[[], Runnable[Input, Output]]

A dictionary of keys to Runnable instances or callables that return Runnable instances.

{}

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the alternatives configured.

from langchain_anthropic import ChatAnthropic
from langchain_core.runnables.utils import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatAnthropic(
    model_name="claude-3-7-sonnet-20250219"
).configurable_alternatives(
    ConfigurableField(id="llm"),
    default_key="anthropic",
    openai=ChatOpenAI(),
)

# uses the default model ChatAnthropic
print(model.invoke("which organization created you?").content)

# uses ChatOpenAI
print(
    model.with_config(configurable={"llm": "openai"})
    .invoke("which organization created you?")
    .content
)

set_verbose

set_verbose(verbose: bool | None) -> bool

If verbose is None, set it.

This allows users to pass in None as verbose to access the global setting.

Parameters:

Name Type Description Default
verbose bool | None

The verbosity setting to use.

required

Returns:

Type Description
bool

The verbosity setting to use.

with_structured_output

with_structured_output(
    schema: dict | type, **kwargs: Any
) -> Runnable[LanguageModelInput, dict | BaseModel]

Not implemented on this class.

get_token_ids

get_token_ids(text: str) -> list[int]

Return the ordered ids of the tokens in a text.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
list[int]

A list of ids corresponding to the tokens in the text, in order they occur

list[int]

in the text.

get_num_tokens

get_num_tokens(text: str) -> int

Get the number of tokens present in the text.

Useful for checking if an input fits in a model's context window.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
int

The integer number of tokens in the text.

get_num_tokens_from_messages

get_num_tokens_from_messages(
    messages: list[BaseMessage],
    tools: Sequence | None = None,
) -> int

Get the number of tokens in the messages.

Useful for checking if an input fits in a model's context window.

Note

The base implementation of get_num_tokens_from_messages ignores tool schemas.

Parameters:

Name Type Description Default
messages list[BaseMessage]

The message inputs to tokenize.

required
tools Sequence | None

If provided, sequence of dict, BaseModel, function, or BaseTools to be converted to tool schemas.

None

Returns:

Type Description
int

The sum of the number of tokens across the messages.

generate

generate(
    prompts: list[str],
    stop: list[str] | None = None,
    callbacks: Callbacks | list[Callbacks] | None = None,
    *,
    tags: list[str] | list[list[str]] | None = None,
    metadata: (
        dict[str, Any] | list[dict[str, Any]] | None
    ) = None,
    run_name: str | list[str] | None = None,
    run_id: UUID | list[UUID | None] | None = None,
    **kwargs: Any
) -> LLMResult

Pass a sequence of prompts to a model and return generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
prompts list[str]

List of string prompts.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks | list[Callbacks] | None

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | list[list[str]] | None

List of tags to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
metadata dict[str, Any] | list[dict[str, Any]] | None

List of metadata dictionaries to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
run_name str | list[str] | None

List of run names to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
run_id UUID | list[UUID | None] | None

List of run IDs to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Raises:

Type Description
ValueError

If prompts is not a list.

ValueError

If the length of callbacks, tags, metadata, or run_name (if provided) does not match the length of prompts.

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input prompt and additional model provider-specific output.

agenerate async

agenerate(
    prompts: list[str],
    stop: list[str] | None = None,
    callbacks: Callbacks | list[Callbacks] | None = None,
    *,
    tags: list[str] | list[list[str]] | None = None,
    metadata: (
        dict[str, Any] | list[dict[str, Any]] | None
    ) = None,
    run_name: str | list[str] | None = None,
    run_id: UUID | list[UUID | None] | None = None,
    **kwargs: Any
) -> LLMResult

Asynchronously pass a sequence of prompts to a model and return generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
prompts list[str]

List of string prompts.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks | list[Callbacks] | None

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | list[list[str]] | None

List of tags to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
metadata dict[str, Any] | list[dict[str, Any]] | None

List of metadata dictionaries to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
run_name str | list[str] | None

List of run names to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
run_id UUID | list[UUID | None] | None

List of run IDs to associate with each prompt. If provided, the length of the list must match the length of the prompts list.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Raises:

Type Description
ValueError

If the length of callbacks, tags, metadata, or run_name (if provided) does not match the length of prompts.

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input prompt and additional model provider-specific output.

__str__

__str__() -> str

Return a string representation of the object for printing.

dict

dict(**kwargs: Any) -> dict

Return a dictionary of the LLM.

save

save(file_path: Path | str) -> None

Save the LLM.

Parameters:

Name Type Description Default
file_path Path | str

Path to file to save the LLM to.

required

Raises:

Type Description
ValueError

If the file path is not a string or Path object.

Example:

.. code-block:: python

    llm.save(file_path="path/llm.yaml")

VertexAIImageCaptioningChat

Bases: _BaseVertexAIImageCaptioning, BaseChatModel

Implementation of the Image Captioning model as a chat.

Methods:

Name Description
get_name

Get the name of the Runnable.

get_input_schema

Get a pydantic model that can be used to validate input to the Runnable.

get_input_jsonschema

Get a JSON schema that represents the input to the Runnable.

get_output_schema

Get a pydantic model that can be used to validate output to the Runnable.

get_output_jsonschema

Get a JSON schema that represents the output of the Runnable.

config_schema

The type of config this Runnable accepts specified as a pydantic model.

get_config_jsonschema

Get a JSON schema that represents the config of the Runnable.

get_graph

Return a graph representation of this Runnable.

get_prompts

Return a list of prompts used by this Runnable.

__or__

Runnable "or" operator.

__ror__

Runnable "reverse-or" operator.

pipe

Pipe runnables.

pick

Pick keys from the output dict of this Runnable.

assign

Assigns new fields to the dict output of this Runnable.

batch

Default implementation runs invoke in parallel using a thread pool executor.

batch_as_completed

Run invoke in parallel on a list of inputs.

abatch

Default implementation runs ainvoke in parallel using asyncio.gather.

abatch_as_completed

Run ainvoke in parallel on a list of inputs.

astream_log

Stream all output from a Runnable, as reported to the callback system.

astream_events

Generate a stream of events.

transform

Transform inputs to outputs.

atransform

Transform inputs to outputs.

bind

Bind arguments to a Runnable, returning a new Runnable.

with_config

Bind config to a Runnable, returning a new Runnable.

with_listeners

Bind lifecycle listeners to a Runnable, returning a new Runnable.

with_alisteners

Bind async lifecycle listeners to a Runnable.

with_types

Bind input and output types to a Runnable, returning a new Runnable.

with_retry

Create a new Runnable that retries the original Runnable on exceptions.

map

Return a new Runnable that maps a list of inputs to a list of outputs.

with_fallbacks

Add fallbacks to a Runnable, returning a new Runnable.

as_tool

Create a BaseTool from a Runnable.

__init__
is_lc_serializable

Is this class serializable?

get_lc_namespace

Get the namespace of the langchain object.

lc_id

Return a unique identifier for this class for serialization purposes.

to_json

Serialize the Runnable to JSON.

to_json_not_implemented

Serialize a "not implemented" object.

configurable_fields

Configure particular Runnable fields at runtime.

configurable_alternatives

Configure alternatives for Runnables that can be set at runtime.

set_verbose

If verbose is None, set it.

with_structured_output

Model wrapper that returns outputs formatted to match the given schema.

get_token_ids

Return the ordered ids of the tokens in a text.

get_num_tokens

Get the number of tokens present in the text.

get_num_tokens_from_messages

Get the number of tokens in the messages.

generate

Pass a sequence of prompts to the model and return model generations.

agenerate

Asynchronously pass a sequence of prompts to a model and return generations.

dict

Return a dictionary of the LLM.

bind_tools

Bind tools to the model.

Attributes:

Name Type Description
InputType TypeAlias

Get the input type for this runnable.

OutputType Any

Get the output type for this runnable.

input_schema type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema type[BaseModel]

Output schema.

config_specs list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets dict[str, str]

A map of constructor argument names to secret ids.

lc_attributes dict

List of attribute names that should be included in the serialized kwargs.

cache BaseCache | bool | None

Whether to cache the response.

verbose bool

Whether to print out response text.

callbacks Callbacks

Callbacks to add to the run trace.

tags list[str] | None

Tags to add to the run trace.

metadata dict[str, Any] | None

Metadata to add to the run trace.

custom_get_token_ids Callable[[str], list[int]] | None

Optional encoder to use for counting tokens.

rate_limiter BaseRateLimiter | None

An optional rate limiter to use for limiting the number of requests.

disable_streaming bool | Literal['tool_calling']

Whether to disable streaming for this model.

output_version str | None

Version of AIMessage output format to store in message content.

model_name str

Name of the model to use

number_of_results int

Number of results to return from one query

language str

Language of the query

project Union[str, None]

Google cloud project

InputType property

InputType: TypeAlias

Get the input type for this runnable.

OutputType property

OutputType: Any

Get the output type for this runnable.

input_schema property

input_schema: type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema property

output_schema: type[BaseModel]

Output schema.

The type of output this Runnable produces specified as a pydantic model.

config_specs property

config_specs: list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets property

lc_secrets: dict[str, str]

A map of constructor argument names to secret ids.

For example,

lc_attributes property

lc_attributes: dict

List of attribute names that should be included in the serialized kwargs.

These attributes must be accepted by the constructor. Default is an empty dictionary.

cache class-attribute instance-attribute

cache: BaseCache | bool | None = Field(
    default=None, exclude=True
)

Whether to cache the response.

  • If true, will use the global cache.
  • If false, will not use a cache
  • If None, will use the global cache if it's set, otherwise no cache.
  • If instance of BaseCache, will use the provided cache.

Caching is not currently supported for streaming methods of models.

verbose class-attribute instance-attribute

verbose: bool = Field(
    default_factory=_get_verbosity, exclude=True, repr=False
)

Whether to print out response text.

callbacks class-attribute instance-attribute

callbacks: Callbacks = Field(default=None, exclude=True)

Callbacks to add to the run trace.

tags class-attribute instance-attribute

tags: list[str] | None = Field(default=None, exclude=True)

Tags to add to the run trace.

metadata class-attribute instance-attribute

metadata: dict[str, Any] | None = Field(
    default=None, exclude=True
)

Metadata to add to the run trace.

custom_get_token_ids class-attribute instance-attribute

custom_get_token_ids: Callable[[str], list[int]] | None = (
    Field(default=None, exclude=True)
)

Optional encoder to use for counting tokens.

rate_limiter class-attribute instance-attribute

rate_limiter: BaseRateLimiter | None = Field(
    default=None, exclude=True
)

An optional rate limiter to use for limiting the number of requests.

disable_streaming class-attribute instance-attribute

disable_streaming: bool | Literal['tool_calling'] = False

Whether to disable streaming for this model.

If streaming is bypassed, then stream()/astream()/astream_events() will defer to invoke()/ainvoke().

  • If True, will always bypass streaming case.
  • If 'tool_calling', will bypass streaming case only when the model is called with a tools keyword argument. In other words, LangChain will automatically switch to non-streaming behavior (invoke()) only when the tools argument is provided. This offers the best of both worlds.
  • If False (default), will always use streaming case if available.

The main reason for this flag is that code might be written using stream() and a user may want to swap out a given model for another model whose the implementation does not properly support streaming.

output_version class-attribute instance-attribute

output_version: str | None = Field(
    default_factory=from_env(
        "LC_OUTPUT_VERSION", default=None
    )
)

Version of AIMessage output format to store in message content.

AIMessage.content_blocks will lazily parse the contents of content into a standard format. This flag can be used to additionally store the standard format in message content, e.g., for serialization purposes.

Supported values:

  • "v0": provider-specific format in content (can lazily-parse with .content_blocks)
  • "v1": standardized format in content (consistent with .content_blocks)

Partner packages (e.g., langchain-openai) can also use this field to roll out new content formats in a backward-compatible way.

Added in version 1.0

model_name class-attribute instance-attribute

model_name: str = Field(default='imagetext@001')

Name of the model to use

number_of_results class-attribute instance-attribute

number_of_results: int = Field(default=1)

Number of results to return from one query

language class-attribute instance-attribute

language: str = Field(default='en')

Language of the query

project class-attribute instance-attribute

project: Union[str, None] = Field(default=None)

Google cloud project

get_name

get_name(
    suffix: str | None = None, *, name: str | None = None
) -> str

Get the name of the Runnable.

Parameters:

Name Type Description Default
suffix str | None

An optional suffix to append to the name.

None
name str | None

An optional name to use instead of the Runnable's name.

None

Returns:

Type Description
str

The name of the Runnable.

get_input_schema

get_input_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate input to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic input schema that depends on which configuration the Runnable is invoked with.

This method allows to get an input schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate input.

get_input_jsonschema

get_input_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the input to the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the input to the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_input_jsonschema())

Added in version 0.3.0

get_output_schema

get_output_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate output to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic output schema that depends on which configuration the Runnable is invoked with.

This method allows to get an output schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate output.

get_output_jsonschema

get_output_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the output of the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the output of the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_output_jsonschema())

Added in version 0.3.0

config_schema

config_schema(
    *, include: Sequence[str] | None = None
) -> type[BaseModel]

The type of config this Runnable accepts specified as a pydantic model.

To mark a field as configurable, see the configurable_fields and configurable_alternatives methods.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate config.

get_config_jsonschema

get_config_jsonschema(
    *, include: Sequence[str] | None = None
) -> dict[str, Any]

Get a JSON schema that represents the config of the Runnable.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the config of the Runnable.

Added in version 0.3.0

get_graph

get_graph(config: RunnableConfig | None = None) -> Graph

Return a graph representation of this Runnable.

get_prompts

get_prompts(
    config: RunnableConfig | None = None,
) -> list[BasePromptTemplate]

Return a list of prompts used by this Runnable.

__or__

__or__(
    other: (
        Runnable[Any, Other]
        | Callable[[Iterator[Any]], Iterator[Other]]
        | Callable[
            [AsyncIterator[Any]], AsyncIterator[Other]
        ]
        | Callable[[Any], Other]
        | Mapping[
            str,
            Runnable[Any, Other]
            | Callable[[Any], Other]
            | Any,
        ]
    ),
) -> RunnableSerializable[Input, Other]

Runnable "or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Any, Other] | Callable[[Iterator[Any]], Iterator[Other]] | Callable[[AsyncIterator[Any]], AsyncIterator[Other]] | Callable[[Any], Other] | Mapping[str, Runnable[Any, Other] | Callable[[Any], Other] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

__ror__

__ror__(
    other: (
        Runnable[Other, Any]
        | Callable[[Iterator[Other]], Iterator[Any]]
        | Callable[
            [AsyncIterator[Other]], AsyncIterator[Any]
        ]
        | Callable[[Other], Any]
        | Mapping[
            str,
            Runnable[Other, Any]
            | Callable[[Other], Any]
            | Any,
        ]
    ),
) -> RunnableSerializable[Other, Output]

Runnable "reverse-or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Other, Any] | Callable[[Iterator[Other]], Iterator[Any]] | Callable[[AsyncIterator[Other]], AsyncIterator[Any]] | Callable[[Other], Any] | Mapping[str, Runnable[Other, Any] | Callable[[Other], Any] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Other, Output]

A new Runnable.

pipe

pipe(
    *others: Runnable[Any, Other] | Callable[[Any], Other],
    name: str | None = None
) -> RunnableSerializable[Input, Other]

Pipe runnables.

Compose this Runnable with Runnable-like objects to make a RunnableSequence.

Equivalent to RunnableSequence(self, *others) or self | others[0] | ...

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


def mul_two(x: int) -> int:
    return x * 2


runnable_1 = RunnableLambda(add_one)
runnable_2 = RunnableLambda(mul_two)
sequence = runnable_1.pipe(runnable_2)
# Or equivalently:
# sequence = runnable_1 | runnable_2
# sequence = RunnableSequence(first=runnable_1, last=runnable_2)
sequence.invoke(1)
await sequence.ainvoke(1)
# -> 4

sequence.batch([1, 2, 3])
await sequence.abatch([1, 2, 3])
# -> [4, 6, 8]

Parameters:

Name Type Description Default
*others Runnable[Any, Other] | Callable[[Any], Other]

Other Runnable or Runnable-like objects to compose

()
name str | None

An optional name for the resulting RunnableSequence.

None

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

pick

pick(
    keys: str | list[str],
) -> RunnableSerializable[Any, Any]

Pick keys from the output dict of this Runnable.

Pick single key:

```python
import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)
chain = RunnableMap(str=as_str, json=as_json)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3]}

json_only_chain = chain.pick("json")
json_only_chain.invoke("[1, 2, 3]")
# -> [1, 2, 3]
```

Pick list of keys:

```python
from typing import Any

import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)


def as_bytes(x: Any) -> bytes:
    return bytes(x, "utf-8")


chain = RunnableMap(
    str=as_str, json=as_json, bytes=RunnableLambda(as_bytes)
)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3], "bytes": b"[1, 2, 3]"}

json_and_bytes_chain = chain.pick(["json", "bytes"])
json_and_bytes_chain.invoke("[1, 2, 3]")
# -> {"json": [1, 2, 3], "bytes": b"[1, 2, 3]"}
```

Parameters:

Name Type Description Default
keys str | list[str]

A key or list of keys to pick from the output dict.

required

Returns:

Type Description
RunnableSerializable[Any, Any]

a new Runnable.

assign

assign(
    **kwargs: (
        Runnable[dict[str, Any], Any]
        | Callable[[dict[str, Any]], Any]
        | Mapping[
            str,
            Runnable[dict[str, Any], Any]
            | Callable[[dict[str, Any]], Any],
        ]
    ),
) -> RunnableSerializable[Any, Any]

Assigns new fields to the dict output of this Runnable.

from langchain_community.llms.fake import FakeStreamingListLLM
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import SystemMessagePromptTemplate
from langchain_core.runnables import Runnable
from operator import itemgetter

prompt = (
    SystemMessagePromptTemplate.from_template("You are a nice assistant.")
    + "{question}"
)
llm = FakeStreamingListLLM(responses=["foo-lish"])

chain: Runnable = prompt | llm | {"str": StrOutputParser()}

chain_with_assign = chain.assign(hello=itemgetter("str") | llm)

print(chain_with_assign.input_schema.model_json_schema())
# {'title': 'PromptInput', 'type': 'object', 'properties':
{'question': {'title': 'Question', 'type': 'string'}}}
print(chain_with_assign.output_schema.model_json_schema())
# {'title': 'RunnableSequenceOutput', 'type': 'object', 'properties':
{'str': {'title': 'Str',
'type': 'string'}, 'hello': {'title': 'Hello', 'type': 'string'}}}

Parameters:

Name Type Description Default
**kwargs Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any] | Mapping[str, Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any]]

A mapping of keys to Runnable or Runnable-like objects that will be invoked with the entire output dict of this Runnable.

{}

Returns:

Type Description
RunnableSerializable[Any, Any]

A new Runnable.

batch

batch(
    inputs: list[Input],
    config: (
        RunnableConfig | list[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> list[Output]

Default implementation runs invoke in parallel using a thread pool executor.

The default implementation of batch works well for IO bound runnables.

Subclasses should override this method if they can batch more efficiently; e.g., if the underlying Runnable uses an API which supports a batch mode.

Parameters:

Name Type Description Default
inputs list[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | list[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
list[Output]

A list of outputs from the Runnable.

batch_as_completed

batch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> Iterator[tuple[int, Output | Exception]]

Run invoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
tuple[int, Output | Exception]

Tuples of the index of the input and the output from the Runnable.

abatch async

abatch(
    inputs: list[Input],
    config: (
        RunnableConfig | list[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> list[Output]

Default implementation runs ainvoke in parallel using asyncio.gather.

The default implementation of batch works well for IO bound runnables.

Subclasses should override this method if they can batch more efficiently; e.g., if the underlying Runnable uses an API which supports a batch mode.

Parameters:

Name Type Description Default
inputs list[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | list[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
list[Output]

A list of outputs from the Runnable.

abatch_as_completed async

abatch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> AsyncIterator[tuple[int, Output | Exception]]

Run ainvoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[tuple[int, Output | Exception]]

A tuple of the index of the input and the output from the Runnable.

astream_log async

astream_log(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    diff: bool = True,
    with_streamed_output_list: bool = True,
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

Stream all output from a Runnable, as reported to the callback system.

This includes all inner runs of LLMs, Retrievers, Tools, etc.

Output is streamed as Log objects, which include a list of Jsonpatch ops that describe how the state of the run has changed in each step, and the final state of the run.

The Jsonpatch ops can be applied in order to construct state.

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
diff bool

Whether to yield diffs between each step or the current state.

True
with_streamed_output_list bool

Whether to yield the streamed_output list.

True
include_names Sequence[str] | None

Only include logs with these names.

None
include_types Sequence[str] | None

Only include logs with these types.

None
include_tags Sequence[str] | None

Only include logs with these tags.

None
exclude_names Sequence[str] | None

Exclude logs with these names.

None
exclude_types Sequence[str] | None

Exclude logs with these types.

None
exclude_tags Sequence[str] | None

Exclude logs with these tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

A RunLogPatch or RunLog object.

astream_events async

astream_events(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    version: Literal["v1", "v2"] = "v2",
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[StreamEvent]

Generate a stream of events.

Use to create an iterator over StreamEvents that provide real-time information about the progress of the Runnable, including StreamEvents from intermediate results.

A StreamEvent is a dictionary with the following schema:

  • event: str - Event names are of the format: on_[runnable_type]_(start|stream|end).
  • name: str - The name of the Runnable that generated the event.
  • run_id: str - randomly generated ID associated with the given execution of the Runnable that emitted the event. A child Runnable that gets invoked as part of the execution of a parent Runnable is assigned its own unique ID.
  • parent_ids: list[str] - The IDs of the parent runnables that generated the event. The root Runnable will have an empty list. The order of the parent IDs is from the root to the immediate parent. Only available for v2 version of the API. The v1 version of the API will return an empty list.
  • tags: Optional[list[str]] - The tags of the Runnable that generated the event.
  • metadata: Optional[dict[str, Any]] - The metadata of the Runnable that generated the event.
  • data: dict[str, Any]

Below is a table that illustrates some events that might be emitted by various chains. Metadata fields have been omitted from the table for brevity. Chain definitions have been included after the table.

Note

This reference table is for the v2 version of the schema.

+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | event | name | chunk | input | output | +==========================+==================+=====================================+===================================================+=====================================================+ | on_chat_model_start | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_stream | [model name] | AIMessageChunk(content="hello") | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_end | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | AIMessageChunk(content="hello world") | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_start | [model name] | | {'input': 'hello'} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_stream | [model name] | 'Hello' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_end | [model name] | | 'Hello human!' | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_start | format_docs | | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_stream | format_docs | 'hello world!, goodbye world!' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_end | format_docs | | [Document(...)] | 'hello world!, goodbye world!' | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_start | some_tool | | {"x": 1, "y": "2"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_end | some_tool | | | {"x": 1, "y": "2"} | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_start | [retriever name] | | {"query": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_end | [retriever name] | | {"query": "hello"} | [Document(...), ..] | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_start | [template_name] | | {"question": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_end | [template_name] | | {"question": "hello"} | ChatPromptValue(messages: [SystemMessage, ...]) | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+

In addition to the standard events, users can also dispatch custom events (see example below).

Custom events will be only be surfaced with in the v2 version of the API!

A custom event has following format:

+-----------+------+-----------------------------------------------------------------------------------------------------------+ | Attribute | Type | Description | +===========+======+===========================================================================================================+ | name | str | A user defined name for the event. | +-----------+------+-----------------------------------------------------------------------------------------------------------+ | data | Any | The data associated with the event. This can be anything, though we suggest making it JSON serializable. | +-----------+------+-----------------------------------------------------------------------------------------------------------+

Here are declarations associated with the standard events shown above:

format_docs:

def format_docs(docs: list[Document]) -> str:
    '''Format the docs.'''
    return ", ".join([doc.page_content for doc in docs])


format_docs = RunnableLambda(format_docs)

some_tool:

@tool
def some_tool(x: int, y: str) -> dict:
    '''Some_tool.'''
    return {"x": x, "y": y}

prompt:

template = ChatPromptTemplate.from_messages(
    [
        ("system", "You are Cat Agent 007"),
        ("human", "{question}"),
    ]
).with_config({"run_name": "my_template", "tags": ["my_template"]})
Example:

from langchain_core.runnables import RunnableLambda


async def reverse(s: str) -> str:
    return s[::-1]


chain = RunnableLambda(func=reverse)

events = [event async for event in chain.astream_events("hello", version="v2")]

# will produce the following events (run_id, and parent_ids
# has been omitted for brevity):
[
    {
        "data": {"input": "hello"},
        "event": "on_chain_start",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"chunk": "olleh"},
        "event": "on_chain_stream",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"output": "olleh"},
        "event": "on_chain_end",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
]

Example: Dispatch Custom Event

from langchain_core.callbacks.manager import (
    adispatch_custom_event,
)
from langchain_core.runnables import RunnableLambda, RunnableConfig
import asyncio


async def slow_thing(some_input: str, config: RunnableConfig) -> str:
    """Do something that takes a long time."""
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 1 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 2 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    return "Done"

slow_thing = RunnableLambda(slow_thing)

async for event in slow_thing.astream_events("some_input", version="v2"):
    print(event)

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
version Literal['v1', 'v2']

The version of the schema to use either 'v2' or 'v1'. Users should use 'v2'. 'v1' is for backwards compatibility and will be deprecated in 0.4.0. No default will be assigned until the API is stabilized. custom events will only be surfaced in 'v2'.

'v2'
include_names Sequence[str] | None

Only include events from Runnables with matching names.

None
include_types Sequence[str] | None

Only include events from Runnables with matching types.

None
include_tags Sequence[str] | None

Only include events from Runnables with matching tags.

None
exclude_names Sequence[str] | None

Exclude events from Runnables with matching names.

None
exclude_types Sequence[str] | None

Exclude events from Runnables with matching types.

None
exclude_tags Sequence[str] | None

Exclude events from Runnables with matching tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable. These will be passed to astream_log as this implementation of astream_events is built on top of astream_log.

{}

Yields:

Type Description
AsyncIterator[StreamEvent]

An async stream of StreamEvents.

Raises:

Type Description
NotImplementedError

If the version is not 'v1' or 'v2'.

transform

transform(
    input: Iterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> Iterator[Output]

Transform inputs to outputs.

Default implementation of transform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input Iterator[Input]

An iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
Output

The output of the Runnable.

atransform async

atransform(
    input: AsyncIterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> AsyncIterator[Output]

Transform inputs to outputs.

Default implementation of atransform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input AsyncIterator[Input]

An async iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[Output]

The output of the Runnable.

bind

bind(**kwargs: Any) -> Runnable[Input, Output]

Bind arguments to a Runnable, returning a new Runnable.

Useful when a Runnable in a chain requires an argument that is not in the output of the previous Runnable or included in the user input.

Parameters:

Name Type Description Default
kwargs Any

The arguments to bind to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the arguments bound.

Example
from langchain_ollama import ChatOllama
from langchain_core.output_parsers import StrOutputParser

llm = ChatOllama(model="llama3.1")

# Without bind.
chain = llm | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two three four five.'

# With bind.
chain = llm.bind(stop=["three"]) | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two'

with_config

with_config(
    config: RunnableConfig | None = None, **kwargs: Any
) -> Runnable[Input, Output]

Bind config to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

The config to bind to the Runnable.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the config bound.

with_listeners

with_listeners(
    *,
    on_start: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_end: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_error: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None
) -> Runnable[Input, Output]

Bind lifecycle listeners to a Runnable, returning a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called before the Runnable starts running, with the Run object. Defaults to None.

None
on_end Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda
from langchain_core.tracers.schemas import Run

import time


def test_runnable(time_to_sleep: int):
    time.sleep(time_to_sleep)


def fn_start(run_obj: Run):
    print("start_time:", run_obj.start_time)


def fn_end(run_obj: Run):
    print("end_time:", run_obj.end_time)


chain = RunnableLambda(test_runnable).with_listeners(
    on_start=fn_start, on_end=fn_end
)
chain.invoke(2)

with_alisteners

with_alisteners(
    *,
    on_start: AsyncListener | None = None,
    on_end: AsyncListener | None = None,
    on_error: AsyncListener | None = None
) -> Runnable[Input, Output]

Bind async lifecycle listeners to a Runnable.

Returns a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start AsyncListener | None

Called asynchronously before the Runnable starts running, with the Run object. Defaults to None.

None
on_end AsyncListener | None

Called asynchronously after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error AsyncListener | None

Called asynchronously if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda, Runnable
from datetime import datetime, timezone
import time
import asyncio

def format_t(timestamp: float) -> str:
    return datetime.fromtimestamp(timestamp, tz=timezone.utc).isoformat()

async def test_runnable(time_to_sleep: int):
    print(f"Runnable[{time_to_sleep}s]: starts at {format_t(time.time())}")
    await asyncio.sleep(time_to_sleep)
    print(f"Runnable[{time_to_sleep}s]: ends at {format_t(time.time())}")

async def fn_start(run_obj: Runnable):
    print(f"on start callback starts at {format_t(time.time())}")
    await asyncio.sleep(3)
    print(f"on start callback ends at {format_t(time.time())}")

async def fn_end(run_obj: Runnable):
    print(f"on end callback starts at {format_t(time.time())}")
    await asyncio.sleep(2)
    print(f"on end callback ends at {format_t(time.time())}")

runnable = RunnableLambda(test_runnable).with_alisteners(
    on_start=fn_start,
    on_end=fn_end
)
async def concurrent_runs():
    await asyncio.gather(runnable.ainvoke(2), runnable.ainvoke(3))

asyncio.run(concurrent_runs())
Result:
on start callback starts at 2025-03-01T07:05:22.875378+00:00
on start callback starts at 2025-03-01T07:05:22.875495+00:00
on start callback ends at 2025-03-01T07:05:25.878862+00:00
on start callback ends at 2025-03-01T07:05:25.878947+00:00
Runnable[2s]: starts at 2025-03-01T07:05:25.879392+00:00
Runnable[3s]: starts at 2025-03-01T07:05:25.879804+00:00
Runnable[2s]: ends at 2025-03-01T07:05:27.881998+00:00
on end callback starts at 2025-03-01T07:05:27.882360+00:00
Runnable[3s]: ends at 2025-03-01T07:05:28.881737+00:00
on end callback starts at 2025-03-01T07:05:28.882428+00:00
on end callback ends at 2025-03-01T07:05:29.883893+00:00
on end callback ends at 2025-03-01T07:05:30.884831+00:00

with_types

with_types(
    *,
    input_type: type[Input] | None = None,
    output_type: type[Output] | None = None
) -> Runnable[Input, Output]

Bind input and output types to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
input_type type[Input] | None

The input type to bind to the Runnable. Defaults to None.

None
output_type type[Output] | None

The output type to bind to the Runnable. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the types bound.

with_retry

with_retry(
    *,
    retry_if_exception_type: tuple[
        type[BaseException], ...
    ] = (Exception,),
    wait_exponential_jitter: bool = True,
    exponential_jitter_params: (
        ExponentialJitterParams | None
    ) = None,
    stop_after_attempt: int = 3
) -> Runnable[Input, Output]

Create a new Runnable that retries the original Runnable on exceptions.

Parameters:

Name Type Description Default
retry_if_exception_type tuple[type[BaseException], ...]

A tuple of exception types to retry on. Defaults to (Exception,).

(Exception,)
wait_exponential_jitter bool

Whether to add jitter to the wait time between retries. Defaults to True.

True
stop_after_attempt int

The maximum number of attempts to make before giving up. Defaults to 3.

3
exponential_jitter_params ExponentialJitterParams | None

Parameters for tenacity.wait_exponential_jitter. Namely: initial, max, exp_base, and jitter (all float values).

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable that retries the original Runnable on exceptions.

Example
from langchain_core.runnables import RunnableLambda

count = 0


def _lambda(x: int) -> None:
    global count
    count = count + 1
    if x == 1:
        raise ValueError("x is 1")
    else:
        pass


runnable = RunnableLambda(_lambda)
try:
    runnable.with_retry(
        stop_after_attempt=2,
        retry_if_exception_type=(ValueError,),
    ).invoke(1)
except ValueError:
    pass

assert count == 2

map

map() -> Runnable[list[Input], list[Output]]

Return a new Runnable that maps a list of inputs to a list of outputs.

Calls invoke with each input.

Returns:

Type Description
Runnable[list[Input], list[Output]]

A new Runnable that maps a list of inputs to a list of outputs.

Example
from langchain_core.runnables import RunnableLambda


def _lambda(x: int) -> int:
    return x + 1


runnable = RunnableLambda(_lambda)
print(runnable.map().invoke([1, 2, 3]))  # [2, 3, 4]

with_fallbacks

with_fallbacks(
    fallbacks: Sequence[Runnable[Input, Output]],
    *,
    exceptions_to_handle: tuple[
        type[BaseException], ...
    ] = (Exception,),
    exception_key: str | None = None
) -> RunnableWithFallbacks[Input, Output]

Add fallbacks to a Runnable, returning a new Runnable.

The new Runnable will try the original Runnable, and then each fallback in order, upon failures.

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle. Defaults to (Exception,).

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input. Defaults to None.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

Example
from typing import Iterator

from langchain_core.runnables import RunnableGenerator


def _generate_immediate_error(input: Iterator) -> Iterator[str]:
    raise ValueError()
    yield ""


def _generate(input: Iterator) -> Iterator[str]:
    yield from "foo bar"


runnable = RunnableGenerator(_generate_immediate_error).with_fallbacks(
    [RunnableGenerator(_generate)]
)
print("".join(runnable.stream({})))  # foo bar

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle.

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

as_tool

as_tool(
    args_schema: type[BaseModel] | None = None,
    *,
    name: str | None = None,
    description: str | None = None,
    arg_types: dict[str, type] | None = None
) -> BaseTool

Create a BaseTool from a Runnable.

as_tool will instantiate a BaseTool with a name, description, and args_schema from a Runnable. Where possible, schemas are inferred from runnable.get_input_schema. Alternatively (e.g., if the Runnable takes a dict as input and the specific dict keys are not typed), the schema can be specified directly with args_schema. You can also pass arg_types to just specify the required arguments and their types.

Parameters:

Name Type Description Default
args_schema type[BaseModel] | None

The schema for the tool. Defaults to None.

None
name str | None

The name of the tool. Defaults to None.

None
description str | None

The description of the tool. Defaults to None.

None
arg_types dict[str, type] | None

A dictionary of argument names to types. Defaults to None.

None

Returns:

Type Description
BaseTool

A BaseTool instance.

Typed dict input:

from typing_extensions import TypedDict
from langchain_core.runnables import RunnableLambda


class Args(TypedDict):
    a: int
    b: list[int]


def f(x: Args) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool()
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via args_schema:

from typing import Any
from pydantic import BaseModel, Field
from langchain_core.runnables import RunnableLambda

def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))

class FSchema(BaseModel):
    """Apply a function to an integer and list of integers."""

    a: int = Field(..., description="Integer")
    b: list[int] = Field(..., description="List of ints")

runnable = RunnableLambda(f)
as_tool = runnable.as_tool(FSchema)
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via arg_types:

from typing import Any
from langchain_core.runnables import RunnableLambda


def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool(arg_types={"a": int, "b": list[int]})
as_tool.invoke({"a": 3, "b": [1, 2]})

String input:

from langchain_core.runnables import RunnableLambda


def f(x: str) -> str:
    return x + "a"


def g(x: str) -> str:
    return x + "z"


runnable = RunnableLambda(f) | g
as_tool = runnable.as_tool()
as_tool.invoke("b")

Added in version 0.2.14

__init__

__init__(*args: Any, **kwargs: Any) -> None

is_lc_serializable classmethod

is_lc_serializable() -> bool

Is this class serializable?

By design, even if a class inherits from Serializable, it is not serializable by default. This is to prevent accidental serialization of objects that should not be serialized.

Returns:

Type Description
bool

Whether the class is serializable. Default is False.

get_lc_namespace classmethod

get_lc_namespace() -> list[str]

Get the namespace of the langchain object.

For example, if the class is langchain.llms.openai.OpenAI, then the namespace is ["langchain", "llms", "openai"]

Returns:

Type Description
list[str]

The namespace as a list of strings.

lc_id classmethod

lc_id() -> list[str]

Return a unique identifier for this class for serialization purposes.

The unique identifier is a list of strings that describes the path to the object. For example, for the class langchain.llms.openai.OpenAI, the id is ["langchain", "llms", "openai", "OpenAI"].

to_json

to_json() -> (
    SerializedConstructor | SerializedNotImplemented
)

Serialize the Runnable to JSON.

Returns:

Type Description
SerializedConstructor | SerializedNotImplemented

A JSON-serializable representation of the Runnable.

to_json_not_implemented

to_json_not_implemented() -> SerializedNotImplemented

Serialize a "not implemented" object.

Returns:

Type Description
SerializedNotImplemented

SerializedNotImplemented.

configurable_fields

configurable_fields(
    **kwargs: AnyConfigurableField,
) -> RunnableSerializable[Input, Output]

Configure particular Runnable fields at runtime.

Parameters:

Name Type Description Default
**kwargs AnyConfigurableField

A dictionary of ConfigurableField instances to configure.

{}

Raises:

Type Description
ValueError

If a configuration key is not found in the Runnable.

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the fields configured.

from langchain_core.runnables import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatOpenAI(max_tokens=20).configurable_fields(
    max_tokens=ConfigurableField(
        id="output_token_number",
        name="Max tokens in the output",
        description="The maximum number of tokens in the output",
    )
)

# max_tokens = 20
print("max_tokens_20: ", model.invoke("tell me something about chess").content)

# max_tokens = 200
print(
    "max_tokens_200: ",
    model.with_config(configurable={"output_token_number": 200})
    .invoke("tell me something about chess")
    .content,
)

configurable_alternatives

configurable_alternatives(
    which: ConfigurableField,
    *,
    default_key: str = "default",
    prefix_keys: bool = False,
    **kwargs: (
        Runnable[Input, Output]
        | Callable[[], Runnable[Input, Output]]
    )
) -> RunnableSerializable[Input, Output]

Configure alternatives for Runnables that can be set at runtime.

Parameters:

Name Type Description Default
which ConfigurableField

The ConfigurableField instance that will be used to select the alternative.

required
default_key str

The default key to use if no alternative is selected. Defaults to 'default'.

'default'
prefix_keys bool

Whether to prefix the keys with the ConfigurableField id. Defaults to False.

False
**kwargs Runnable[Input, Output] | Callable[[], Runnable[Input, Output]]

A dictionary of keys to Runnable instances or callables that return Runnable instances.

{}

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the alternatives configured.

from langchain_anthropic import ChatAnthropic
from langchain_core.runnables.utils import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatAnthropic(
    model_name="claude-3-7-sonnet-20250219"
).configurable_alternatives(
    ConfigurableField(id="llm"),
    default_key="anthropic",
    openai=ChatOpenAI(),
)

# uses the default model ChatAnthropic
print(model.invoke("which organization created you?").content)

# uses ChatOpenAI
print(
    model.with_config(configurable={"llm": "openai"})
    .invoke("which organization created you?")
    .content
)

set_verbose

set_verbose(verbose: bool | None) -> bool

If verbose is None, set it.

This allows users to pass in None as verbose to access the global setting.

Parameters:

Name Type Description Default
verbose bool | None

The verbosity setting to use.

required

Returns:

Type Description
bool

The verbosity setting to use.

with_structured_output

with_structured_output(
    schema: Dict | type,
    *,
    include_raw: bool = False,
    **kwargs: Any
) -> Runnable[LanguageModelInput, Dict | BaseModel]

Model wrapper that returns outputs formatted to match the given schema.

Parameters:

Name Type Description Default
schema Dict | type

The output schema. Can be passed in as:

  • an OpenAI function/tool schema,
  • a JSON Schema,
  • a TypedDict class,
  • or a Pydantic class.

If schema is a Pydantic class then the model output will be a Pydantic instance of that class, and the model-generated fields will be validated by the Pydantic class. Otherwise the model output will be a dict and will not be validated. See langchain_core.utils.function_calling.convert_to_openai_tool for more on how to properly specify types and descriptions of schema fields when specifying a Pydantic or TypedDict class.

required
include_raw bool

If False then only the parsed structured output is returned. If an error occurs during model output parsing it will be raised. If True then both the raw model response (a BaseMessage) and the parsed model response will be returned. If an error occurs during output parsing it will be caught and returned as well. The final output is always a dict with keys 'raw', 'parsed', and 'parsing_error'.

False

Raises:

Type Description
ValueError

If there are any unsupported kwargs.

NotImplementedError

If the model does not implement with_structured_output().

Returns:

Type Description
Runnable[LanguageModelInput, Dict | BaseModel]

A Runnable that takes same inputs as a langchain_core.language_models.chat.BaseChatModel.

Runnable[LanguageModelInput, Dict | BaseModel]

If include_raw is False and schema is a Pydantic class, Runnable outputs

Runnable[LanguageModelInput, Dict | BaseModel]

an instance of schema (i.e., a Pydantic object).

Runnable[LanguageModelInput, Dict | BaseModel]

Otherwise, if include_raw is False then Runnable outputs a dict.

Runnable[LanguageModelInput, Dict | BaseModel]

If include_raw is True, then Runnable outputs a dict with keys:

Runnable[LanguageModelInput, Dict | BaseModel]
  • 'raw': BaseMessage
Runnable[LanguageModelInput, Dict | BaseModel]
  • 'parsed': None if there was a parsing error, otherwise the type depends on the schema as described above.
Runnable[LanguageModelInput, Dict | BaseModel]
  • 'parsing_error': Optional[BaseException]
Pydantic schema (include_raw=False):

.. code-block:: python

from pydantic import BaseModel


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


llm = ChatModel(model="model-name", temperature=0)
structured_llm = llm.with_structured_output(AnswerWithJustification)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)

# -> AnswerWithJustification(
#     answer='They weigh the same',
#     justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'
# )
Pydantic schema (include_raw=True):

.. code-block:: python

from pydantic import BaseModel


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


llm = ChatModel(model="model-name", temperature=0)
structured_llm = llm.with_structured_output(
    AnswerWithJustification, include_raw=True
)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)
# -> {
#     'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),
#     'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),
#     'parsing_error': None
# }
Dict schema (include_raw=False):

.. code-block:: python

from pydantic import BaseModel
from langchain_core.utils.function_calling import convert_to_openai_tool


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


dict_schema = convert_to_openai_tool(AnswerWithJustification)
llm = ChatModel(model="model-name", temperature=0)
structured_llm = llm.with_structured_output(dict_schema)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)
# -> {
#     'answer': 'They weigh the same',
#     'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
# }

Behavior changed in 0.2.26

Added support for TypedDict class.

get_token_ids

get_token_ids(text: str) -> list[int]

Return the ordered ids of the tokens in a text.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
list[int]

A list of ids corresponding to the tokens in the text, in order they occur

list[int]

in the text.

get_num_tokens

get_num_tokens(text: str) -> int

Get the number of tokens present in the text.

Useful for checking if an input fits in a model's context window.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
int

The integer number of tokens in the text.

get_num_tokens_from_messages

get_num_tokens_from_messages(
    messages: list[BaseMessage],
    tools: Sequence | None = None,
) -> int

Get the number of tokens in the messages.

Useful for checking if an input fits in a model's context window.

Note

The base implementation of get_num_tokens_from_messages ignores tool schemas.

Parameters:

Name Type Description Default
messages list[BaseMessage]

The message inputs to tokenize.

required
tools Sequence | None

If provided, sequence of dict, BaseModel, function, or BaseTools to be converted to tool schemas.

None

Returns:

Type Description
int

The sum of the number of tokens across the messages.

generate

generate(
    messages: list[list[BaseMessage]],
    stop: list[str] | None = None,
    callbacks: Callbacks = None,
    *,
    tags: list[str] | None = None,
    metadata: dict[str, Any] | None = None,
    run_name: str | None = None,
    run_id: UUID | None = None,
    **kwargs: Any
) -> LLMResult

Pass a sequence of prompts to the model and return model generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
messages list[list[BaseMessage]]

List of list of messages.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | None

The tags to apply.

None
metadata dict[str, Any] | None

The metadata to apply.

None
run_name str | None

The name of the run.

None
run_id UUID | None

The ID of the run.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input

LLMResult

prompt and additional model provider-specific output.

agenerate async

agenerate(
    messages: list[list[BaseMessage]],
    stop: list[str] | None = None,
    callbacks: Callbacks = None,
    *,
    tags: list[str] | None = None,
    metadata: dict[str, Any] | None = None,
    run_name: str | None = None,
    run_id: UUID | None = None,
    **kwargs: Any
) -> LLMResult

Asynchronously pass a sequence of prompts to a model and return generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
messages list[list[BaseMessage]]

List of list of messages.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | None

The tags to apply.

None
metadata dict[str, Any] | None

The metadata to apply.

None
run_name str | None

The name of the run.

None
run_id UUID | None

The ID of the run.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input

LLMResult

prompt and additional model provider-specific output.

dict

dict(**kwargs: Any) -> dict

Return a dictionary of the LLM.

bind_tools

bind_tools(
    tools: Sequence[
        Dict[str, Any] | type | Callable | BaseTool
    ],
    *,
    tool_choice: str | None = None,
    **kwargs: Any
) -> Runnable[LanguageModelInput, AIMessage]

Bind tools to the model.

Parameters:

Name Type Description Default
tools Sequence[Dict[str, Any] | type | Callable | BaseTool]

Sequence of tools to bind to the model.

required
tool_choice str | None

The tool to use. If "any" then any tool can be used.

None

Returns:

Type Description
Runnable[LanguageModelInput, AIMessage]

A Runnable that returns a message.

VertexAIImageEditorChat

Bases: _BaseVertexAIImageGenerator, BaseChatModel

Given an image and a prompt, edits the image. Currently only supports mask free editing.

Methods:

Name Description
get_name

Get the name of the Runnable.

get_input_schema

Get a pydantic model that can be used to validate input to the Runnable.

get_input_jsonschema

Get a JSON schema that represents the input to the Runnable.

get_output_schema

Get a pydantic model that can be used to validate output to the Runnable.

get_output_jsonschema

Get a JSON schema that represents the output of the Runnable.

config_schema

The type of config this Runnable accepts specified as a pydantic model.

get_config_jsonschema

Get a JSON schema that represents the config of the Runnable.

get_graph

Return a graph representation of this Runnable.

get_prompts

Return a list of prompts used by this Runnable.

__or__

Runnable "or" operator.

__ror__

Runnable "reverse-or" operator.

pipe

Pipe runnables.

pick

Pick keys from the output dict of this Runnable.

assign

Assigns new fields to the dict output of this Runnable.

batch

Default implementation runs invoke in parallel using a thread pool executor.

batch_as_completed

Run invoke in parallel on a list of inputs.

abatch

Default implementation runs ainvoke in parallel using asyncio.gather.

abatch_as_completed

Run ainvoke in parallel on a list of inputs.

astream_log

Stream all output from a Runnable, as reported to the callback system.

astream_events

Generate a stream of events.

transform

Transform inputs to outputs.

atransform

Transform inputs to outputs.

bind

Bind arguments to a Runnable, returning a new Runnable.

with_config

Bind config to a Runnable, returning a new Runnable.

with_listeners

Bind lifecycle listeners to a Runnable, returning a new Runnable.

with_alisteners

Bind async lifecycle listeners to a Runnable.

with_types

Bind input and output types to a Runnable, returning a new Runnable.

with_retry

Create a new Runnable that retries the original Runnable on exceptions.

map

Return a new Runnable that maps a list of inputs to a list of outputs.

with_fallbacks

Add fallbacks to a Runnable, returning a new Runnable.

as_tool

Create a BaseTool from a Runnable.

__init__
is_lc_serializable

Is this class serializable?

get_lc_namespace

Get the namespace of the langchain object.

lc_id

Return a unique identifier for this class for serialization purposes.

to_json

Serialize the Runnable to JSON.

to_json_not_implemented

Serialize a "not implemented" object.

configurable_fields

Configure particular Runnable fields at runtime.

configurable_alternatives

Configure alternatives for Runnables that can be set at runtime.

set_verbose

If verbose is None, set it.

with_structured_output

Model wrapper that returns outputs formatted to match the given schema.

get_token_ids

Return the ordered ids of the tokens in a text.

get_num_tokens

Get the number of tokens present in the text.

get_num_tokens_from_messages

Get the number of tokens in the messages.

generate

Pass a sequence of prompts to the model and return model generations.

agenerate

Asynchronously pass a sequence of prompts to a model and return generations.

dict

Return a dictionary of the LLM.

bind_tools

Bind tools to the model.

Attributes:

Name Type Description
InputType TypeAlias

Get the input type for this runnable.

OutputType Any

Get the output type for this runnable.

input_schema type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema type[BaseModel]

Output schema.

config_specs list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets dict[str, str]

A map of constructor argument names to secret ids.

lc_attributes dict

List of attribute names that should be included in the serialized kwargs.

cache BaseCache | bool | None

Whether to cache the response.

verbose bool

Whether to print out response text.

callbacks Callbacks

Callbacks to add to the run trace.

tags list[str] | None

Tags to add to the run trace.

metadata dict[str, Any] | None

Metadata to add to the run trace.

custom_get_token_ids Callable[[str], list[int]] | None

Optional encoder to use for counting tokens.

rate_limiter BaseRateLimiter | None

An optional rate limiter to use for limiting the number of requests.

disable_streaming bool | Literal['tool_calling']

Whether to disable streaming for this model.

output_version str | None

Version of AIMessage output format to store in message content.

model_name str

Name of the base model

negative_prompt Union[str, None]

A description of what you want to omit in

number_of_results int

Number of images to generate

guidance_scale Union[float, None]

Controls the strength of the prompt

language Union[str, None]

Language of the text prompt for the image Supported values are "en" for English,

seed Union[int, None]

Random seed for the image generation

project Union[str, None]

Google cloud project id

InputType property

InputType: TypeAlias

Get the input type for this runnable.

OutputType property

OutputType: Any

Get the output type for this runnable.

input_schema property

input_schema: type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema property

output_schema: type[BaseModel]

Output schema.

The type of output this Runnable produces specified as a pydantic model.

config_specs property

config_specs: list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets property

lc_secrets: dict[str, str]

A map of constructor argument names to secret ids.

For example,

lc_attributes property

lc_attributes: dict

List of attribute names that should be included in the serialized kwargs.

These attributes must be accepted by the constructor. Default is an empty dictionary.

cache class-attribute instance-attribute

cache: BaseCache | bool | None = Field(
    default=None, exclude=True
)

Whether to cache the response.

  • If true, will use the global cache.
  • If false, will not use a cache
  • If None, will use the global cache if it's set, otherwise no cache.
  • If instance of BaseCache, will use the provided cache.

Caching is not currently supported for streaming methods of models.

verbose class-attribute instance-attribute

verbose: bool = Field(
    default_factory=_get_verbosity, exclude=True, repr=False
)

Whether to print out response text.

callbacks class-attribute instance-attribute

callbacks: Callbacks = Field(default=None, exclude=True)

Callbacks to add to the run trace.

tags class-attribute instance-attribute

tags: list[str] | None = Field(default=None, exclude=True)

Tags to add to the run trace.

metadata class-attribute instance-attribute

metadata: dict[str, Any] | None = Field(
    default=None, exclude=True
)

Metadata to add to the run trace.

custom_get_token_ids class-attribute instance-attribute

custom_get_token_ids: Callable[[str], list[int]] | None = (
    Field(default=None, exclude=True)
)

Optional encoder to use for counting tokens.

rate_limiter class-attribute instance-attribute

rate_limiter: BaseRateLimiter | None = Field(
    default=None, exclude=True
)

An optional rate limiter to use for limiting the number of requests.

disable_streaming class-attribute instance-attribute

disable_streaming: bool | Literal['tool_calling'] = False

Whether to disable streaming for this model.

If streaming is bypassed, then stream()/astream()/astream_events() will defer to invoke()/ainvoke().

  • If True, will always bypass streaming case.
  • If 'tool_calling', will bypass streaming case only when the model is called with a tools keyword argument. In other words, LangChain will automatically switch to non-streaming behavior (invoke()) only when the tools argument is provided. This offers the best of both worlds.
  • If False (default), will always use streaming case if available.

The main reason for this flag is that code might be written using stream() and a user may want to swap out a given model for another model whose the implementation does not properly support streaming.

output_version class-attribute instance-attribute

output_version: str | None = Field(
    default_factory=from_env(
        "LC_OUTPUT_VERSION", default=None
    )
)

Version of AIMessage output format to store in message content.

AIMessage.content_blocks will lazily parse the contents of content into a standard format. This flag can be used to additionally store the standard format in message content, e.g., for serialization purposes.

Supported values:

  • "v0": provider-specific format in content (can lazily-parse with .content_blocks)
  • "v1": standardized format in content (consistent with .content_blocks)

Partner packages (e.g., langchain-openai) can also use this field to roll out new content formats in a backward-compatible way.

Added in version 1.0

model_name class-attribute instance-attribute

model_name: str = Field(default='imagen-3.0-generate-002')

Name of the base model

negative_prompt class-attribute instance-attribute

negative_prompt: Union[str, None] = Field(default=None)

A description of what you want to omit in the generated images

number_of_results class-attribute instance-attribute

number_of_results: int = Field(default=1)

Number of images to generate

guidance_scale class-attribute instance-attribute

guidance_scale: Union[float, None] = Field(default=None)

Controls the strength of the prompt

language class-attribute instance-attribute

language: Union[str, None] = Field(default=None)

Language of the text prompt for the image Supported values are "en" for English, "hi" for Hindi, "ja" for Japanese, "ko" for Korean, and "auto" for automatic language detection

seed class-attribute instance-attribute

seed: Union[int, None] = Field(default=None)

Random seed for the image generation

project class-attribute instance-attribute

project: Union[str, None] = Field(default=None)

Google cloud project id

get_name

get_name(
    suffix: str | None = None, *, name: str | None = None
) -> str

Get the name of the Runnable.

Parameters:

Name Type Description Default
suffix str | None

An optional suffix to append to the name.

None
name str | None

An optional name to use instead of the Runnable's name.

None

Returns:

Type Description
str

The name of the Runnable.

get_input_schema

get_input_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate input to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic input schema that depends on which configuration the Runnable is invoked with.

This method allows to get an input schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate input.

get_input_jsonschema

get_input_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the input to the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the input to the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_input_jsonschema())

Added in version 0.3.0

get_output_schema

get_output_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate output to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic output schema that depends on which configuration the Runnable is invoked with.

This method allows to get an output schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate output.

get_output_jsonschema

get_output_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the output of the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the output of the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_output_jsonschema())

Added in version 0.3.0

config_schema

config_schema(
    *, include: Sequence[str] | None = None
) -> type[BaseModel]

The type of config this Runnable accepts specified as a pydantic model.

To mark a field as configurable, see the configurable_fields and configurable_alternatives methods.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate config.

get_config_jsonschema

get_config_jsonschema(
    *, include: Sequence[str] | None = None
) -> dict[str, Any]

Get a JSON schema that represents the config of the Runnable.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the config of the Runnable.

Added in version 0.3.0

get_graph

get_graph(config: RunnableConfig | None = None) -> Graph

Return a graph representation of this Runnable.

get_prompts

get_prompts(
    config: RunnableConfig | None = None,
) -> list[BasePromptTemplate]

Return a list of prompts used by this Runnable.

__or__

__or__(
    other: (
        Runnable[Any, Other]
        | Callable[[Iterator[Any]], Iterator[Other]]
        | Callable[
            [AsyncIterator[Any]], AsyncIterator[Other]
        ]
        | Callable[[Any], Other]
        | Mapping[
            str,
            Runnable[Any, Other]
            | Callable[[Any], Other]
            | Any,
        ]
    ),
) -> RunnableSerializable[Input, Other]

Runnable "or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Any, Other] | Callable[[Iterator[Any]], Iterator[Other]] | Callable[[AsyncIterator[Any]], AsyncIterator[Other]] | Callable[[Any], Other] | Mapping[str, Runnable[Any, Other] | Callable[[Any], Other] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

__ror__

__ror__(
    other: (
        Runnable[Other, Any]
        | Callable[[Iterator[Other]], Iterator[Any]]
        | Callable[
            [AsyncIterator[Other]], AsyncIterator[Any]
        ]
        | Callable[[Other], Any]
        | Mapping[
            str,
            Runnable[Other, Any]
            | Callable[[Other], Any]
            | Any,
        ]
    ),
) -> RunnableSerializable[Other, Output]

Runnable "reverse-or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Other, Any] | Callable[[Iterator[Other]], Iterator[Any]] | Callable[[AsyncIterator[Other]], AsyncIterator[Any]] | Callable[[Other], Any] | Mapping[str, Runnable[Other, Any] | Callable[[Other], Any] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Other, Output]

A new Runnable.

pipe

pipe(
    *others: Runnable[Any, Other] | Callable[[Any], Other],
    name: str | None = None
) -> RunnableSerializable[Input, Other]

Pipe runnables.

Compose this Runnable with Runnable-like objects to make a RunnableSequence.

Equivalent to RunnableSequence(self, *others) or self | others[0] | ...

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


def mul_two(x: int) -> int:
    return x * 2


runnable_1 = RunnableLambda(add_one)
runnable_2 = RunnableLambda(mul_two)
sequence = runnable_1.pipe(runnable_2)
# Or equivalently:
# sequence = runnable_1 | runnable_2
# sequence = RunnableSequence(first=runnable_1, last=runnable_2)
sequence.invoke(1)
await sequence.ainvoke(1)
# -> 4

sequence.batch([1, 2, 3])
await sequence.abatch([1, 2, 3])
# -> [4, 6, 8]

Parameters:

Name Type Description Default
*others Runnable[Any, Other] | Callable[[Any], Other]

Other Runnable or Runnable-like objects to compose

()
name str | None

An optional name for the resulting RunnableSequence.

None

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

pick

pick(
    keys: str | list[str],
) -> RunnableSerializable[Any, Any]

Pick keys from the output dict of this Runnable.

Pick single key:

```python
import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)
chain = RunnableMap(str=as_str, json=as_json)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3]}

json_only_chain = chain.pick("json")
json_only_chain.invoke("[1, 2, 3]")
# -> [1, 2, 3]
```

Pick list of keys:

```python
from typing import Any

import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)


def as_bytes(x: Any) -> bytes:
    return bytes(x, "utf-8")


chain = RunnableMap(
    str=as_str, json=as_json, bytes=RunnableLambda(as_bytes)
)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3], "bytes": b"[1, 2, 3]"}

json_and_bytes_chain = chain.pick(["json", "bytes"])
json_and_bytes_chain.invoke("[1, 2, 3]")
# -> {"json": [1, 2, 3], "bytes": b"[1, 2, 3]"}
```

Parameters:

Name Type Description Default
keys str | list[str]

A key or list of keys to pick from the output dict.

required

Returns:

Type Description
RunnableSerializable[Any, Any]

a new Runnable.

assign

assign(
    **kwargs: (
        Runnable[dict[str, Any], Any]
        | Callable[[dict[str, Any]], Any]
        | Mapping[
            str,
            Runnable[dict[str, Any], Any]
            | Callable[[dict[str, Any]], Any],
        ]
    ),
) -> RunnableSerializable[Any, Any]

Assigns new fields to the dict output of this Runnable.

from langchain_community.llms.fake import FakeStreamingListLLM
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import SystemMessagePromptTemplate
from langchain_core.runnables import Runnable
from operator import itemgetter

prompt = (
    SystemMessagePromptTemplate.from_template("You are a nice assistant.")
    + "{question}"
)
llm = FakeStreamingListLLM(responses=["foo-lish"])

chain: Runnable = prompt | llm | {"str": StrOutputParser()}

chain_with_assign = chain.assign(hello=itemgetter("str") | llm)

print(chain_with_assign.input_schema.model_json_schema())
# {'title': 'PromptInput', 'type': 'object', 'properties':
{'question': {'title': 'Question', 'type': 'string'}}}
print(chain_with_assign.output_schema.model_json_schema())
# {'title': 'RunnableSequenceOutput', 'type': 'object', 'properties':
{'str': {'title': 'Str',
'type': 'string'}, 'hello': {'title': 'Hello', 'type': 'string'}}}

Parameters:

Name Type Description Default
**kwargs Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any] | Mapping[str, Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any]]

A mapping of keys to Runnable or Runnable-like objects that will be invoked with the entire output dict of this Runnable.

{}

Returns:

Type Description
RunnableSerializable[Any, Any]

A new Runnable.

batch

batch(
    inputs: list[Input],
    config: (
        RunnableConfig | list[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> list[Output]

Default implementation runs invoke in parallel using a thread pool executor.

The default implementation of batch works well for IO bound runnables.

Subclasses should override this method if they can batch more efficiently; e.g., if the underlying Runnable uses an API which supports a batch mode.

Parameters:

Name Type Description Default
inputs list[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | list[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
list[Output]

A list of outputs from the Runnable.

batch_as_completed

batch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> Iterator[tuple[int, Output | Exception]]

Run invoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
tuple[int, Output | Exception]

Tuples of the index of the input and the output from the Runnable.

abatch async

abatch(
    inputs: list[Input],
    config: (
        RunnableConfig | list[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> list[Output]

Default implementation runs ainvoke in parallel using asyncio.gather.

The default implementation of batch works well for IO bound runnables.

Subclasses should override this method if they can batch more efficiently; e.g., if the underlying Runnable uses an API which supports a batch mode.

Parameters:

Name Type Description Default
inputs list[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | list[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
list[Output]

A list of outputs from the Runnable.

abatch_as_completed async

abatch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> AsyncIterator[tuple[int, Output | Exception]]

Run ainvoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[tuple[int, Output | Exception]]

A tuple of the index of the input and the output from the Runnable.

astream_log async

astream_log(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    diff: bool = True,
    with_streamed_output_list: bool = True,
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

Stream all output from a Runnable, as reported to the callback system.

This includes all inner runs of LLMs, Retrievers, Tools, etc.

Output is streamed as Log objects, which include a list of Jsonpatch ops that describe how the state of the run has changed in each step, and the final state of the run.

The Jsonpatch ops can be applied in order to construct state.

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
diff bool

Whether to yield diffs between each step or the current state.

True
with_streamed_output_list bool

Whether to yield the streamed_output list.

True
include_names Sequence[str] | None

Only include logs with these names.

None
include_types Sequence[str] | None

Only include logs with these types.

None
include_tags Sequence[str] | None

Only include logs with these tags.

None
exclude_names Sequence[str] | None

Exclude logs with these names.

None
exclude_types Sequence[str] | None

Exclude logs with these types.

None
exclude_tags Sequence[str] | None

Exclude logs with these tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

A RunLogPatch or RunLog object.

astream_events async

astream_events(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    version: Literal["v1", "v2"] = "v2",
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[StreamEvent]

Generate a stream of events.

Use to create an iterator over StreamEvents that provide real-time information about the progress of the Runnable, including StreamEvents from intermediate results.

A StreamEvent is a dictionary with the following schema:

  • event: str - Event names are of the format: on_[runnable_type]_(start|stream|end).
  • name: str - The name of the Runnable that generated the event.
  • run_id: str - randomly generated ID associated with the given execution of the Runnable that emitted the event. A child Runnable that gets invoked as part of the execution of a parent Runnable is assigned its own unique ID.
  • parent_ids: list[str] - The IDs of the parent runnables that generated the event. The root Runnable will have an empty list. The order of the parent IDs is from the root to the immediate parent. Only available for v2 version of the API. The v1 version of the API will return an empty list.
  • tags: Optional[list[str]] - The tags of the Runnable that generated the event.
  • metadata: Optional[dict[str, Any]] - The metadata of the Runnable that generated the event.
  • data: dict[str, Any]

Below is a table that illustrates some events that might be emitted by various chains. Metadata fields have been omitted from the table for brevity. Chain definitions have been included after the table.

Note

This reference table is for the v2 version of the schema.

+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | event | name | chunk | input | output | +==========================+==================+=====================================+===================================================+=====================================================+ | on_chat_model_start | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_stream | [model name] | AIMessageChunk(content="hello") | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_end | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | AIMessageChunk(content="hello world") | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_start | [model name] | | {'input': 'hello'} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_stream | [model name] | 'Hello' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_end | [model name] | | 'Hello human!' | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_start | format_docs | | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_stream | format_docs | 'hello world!, goodbye world!' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_end | format_docs | | [Document(...)] | 'hello world!, goodbye world!' | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_start | some_tool | | {"x": 1, "y": "2"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_end | some_tool | | | {"x": 1, "y": "2"} | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_start | [retriever name] | | {"query": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_end | [retriever name] | | {"query": "hello"} | [Document(...), ..] | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_start | [template_name] | | {"question": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_end | [template_name] | | {"question": "hello"} | ChatPromptValue(messages: [SystemMessage, ...]) | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+

In addition to the standard events, users can also dispatch custom events (see example below).

Custom events will be only be surfaced with in the v2 version of the API!

A custom event has following format:

+-----------+------+-----------------------------------------------------------------------------------------------------------+ | Attribute | Type | Description | +===========+======+===========================================================================================================+ | name | str | A user defined name for the event. | +-----------+------+-----------------------------------------------------------------------------------------------------------+ | data | Any | The data associated with the event. This can be anything, though we suggest making it JSON serializable. | +-----------+------+-----------------------------------------------------------------------------------------------------------+

Here are declarations associated with the standard events shown above:

format_docs:

def format_docs(docs: list[Document]) -> str:
    '''Format the docs.'''
    return ", ".join([doc.page_content for doc in docs])


format_docs = RunnableLambda(format_docs)

some_tool:

@tool
def some_tool(x: int, y: str) -> dict:
    '''Some_tool.'''
    return {"x": x, "y": y}

prompt:

template = ChatPromptTemplate.from_messages(
    [
        ("system", "You are Cat Agent 007"),
        ("human", "{question}"),
    ]
).with_config({"run_name": "my_template", "tags": ["my_template"]})
Example:

from langchain_core.runnables import RunnableLambda


async def reverse(s: str) -> str:
    return s[::-1]


chain = RunnableLambda(func=reverse)

events = [event async for event in chain.astream_events("hello", version="v2")]

# will produce the following events (run_id, and parent_ids
# has been omitted for brevity):
[
    {
        "data": {"input": "hello"},
        "event": "on_chain_start",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"chunk": "olleh"},
        "event": "on_chain_stream",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"output": "olleh"},
        "event": "on_chain_end",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
]

Example: Dispatch Custom Event

from langchain_core.callbacks.manager import (
    adispatch_custom_event,
)
from langchain_core.runnables import RunnableLambda, RunnableConfig
import asyncio


async def slow_thing(some_input: str, config: RunnableConfig) -> str:
    """Do something that takes a long time."""
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 1 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 2 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    return "Done"

slow_thing = RunnableLambda(slow_thing)

async for event in slow_thing.astream_events("some_input", version="v2"):
    print(event)

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
version Literal['v1', 'v2']

The version of the schema to use either 'v2' or 'v1'. Users should use 'v2'. 'v1' is for backwards compatibility and will be deprecated in 0.4.0. No default will be assigned until the API is stabilized. custom events will only be surfaced in 'v2'.

'v2'
include_names Sequence[str] | None

Only include events from Runnables with matching names.

None
include_types Sequence[str] | None

Only include events from Runnables with matching types.

None
include_tags Sequence[str] | None

Only include events from Runnables with matching tags.

None
exclude_names Sequence[str] | None

Exclude events from Runnables with matching names.

None
exclude_types Sequence[str] | None

Exclude events from Runnables with matching types.

None
exclude_tags Sequence[str] | None

Exclude events from Runnables with matching tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable. These will be passed to astream_log as this implementation of astream_events is built on top of astream_log.

{}

Yields:

Type Description
AsyncIterator[StreamEvent]

An async stream of StreamEvents.

Raises:

Type Description
NotImplementedError

If the version is not 'v1' or 'v2'.

transform

transform(
    input: Iterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> Iterator[Output]

Transform inputs to outputs.

Default implementation of transform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input Iterator[Input]

An iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
Output

The output of the Runnable.

atransform async

atransform(
    input: AsyncIterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> AsyncIterator[Output]

Transform inputs to outputs.

Default implementation of atransform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input AsyncIterator[Input]

An async iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[Output]

The output of the Runnable.

bind

bind(**kwargs: Any) -> Runnable[Input, Output]

Bind arguments to a Runnable, returning a new Runnable.

Useful when a Runnable in a chain requires an argument that is not in the output of the previous Runnable or included in the user input.

Parameters:

Name Type Description Default
kwargs Any

The arguments to bind to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the arguments bound.

Example
from langchain_ollama import ChatOllama
from langchain_core.output_parsers import StrOutputParser

llm = ChatOllama(model="llama3.1")

# Without bind.
chain = llm | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two three four five.'

# With bind.
chain = llm.bind(stop=["three"]) | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two'

with_config

with_config(
    config: RunnableConfig | None = None, **kwargs: Any
) -> Runnable[Input, Output]

Bind config to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

The config to bind to the Runnable.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the config bound.

with_listeners

with_listeners(
    *,
    on_start: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_end: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_error: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None
) -> Runnable[Input, Output]

Bind lifecycle listeners to a Runnable, returning a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called before the Runnable starts running, with the Run object. Defaults to None.

None
on_end Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda
from langchain_core.tracers.schemas import Run

import time


def test_runnable(time_to_sleep: int):
    time.sleep(time_to_sleep)


def fn_start(run_obj: Run):
    print("start_time:", run_obj.start_time)


def fn_end(run_obj: Run):
    print("end_time:", run_obj.end_time)


chain = RunnableLambda(test_runnable).with_listeners(
    on_start=fn_start, on_end=fn_end
)
chain.invoke(2)

with_alisteners

with_alisteners(
    *,
    on_start: AsyncListener | None = None,
    on_end: AsyncListener | None = None,
    on_error: AsyncListener | None = None
) -> Runnable[Input, Output]

Bind async lifecycle listeners to a Runnable.

Returns a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start AsyncListener | None

Called asynchronously before the Runnable starts running, with the Run object. Defaults to None.

None
on_end AsyncListener | None

Called asynchronously after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error AsyncListener | None

Called asynchronously if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda, Runnable
from datetime import datetime, timezone
import time
import asyncio

def format_t(timestamp: float) -> str:
    return datetime.fromtimestamp(timestamp, tz=timezone.utc).isoformat()

async def test_runnable(time_to_sleep: int):
    print(f"Runnable[{time_to_sleep}s]: starts at {format_t(time.time())}")
    await asyncio.sleep(time_to_sleep)
    print(f"Runnable[{time_to_sleep}s]: ends at {format_t(time.time())}")

async def fn_start(run_obj: Runnable):
    print(f"on start callback starts at {format_t(time.time())}")
    await asyncio.sleep(3)
    print(f"on start callback ends at {format_t(time.time())}")

async def fn_end(run_obj: Runnable):
    print(f"on end callback starts at {format_t(time.time())}")
    await asyncio.sleep(2)
    print(f"on end callback ends at {format_t(time.time())}")

runnable = RunnableLambda(test_runnable).with_alisteners(
    on_start=fn_start,
    on_end=fn_end
)
async def concurrent_runs():
    await asyncio.gather(runnable.ainvoke(2), runnable.ainvoke(3))

asyncio.run(concurrent_runs())
Result:
on start callback starts at 2025-03-01T07:05:22.875378+00:00
on start callback starts at 2025-03-01T07:05:22.875495+00:00
on start callback ends at 2025-03-01T07:05:25.878862+00:00
on start callback ends at 2025-03-01T07:05:25.878947+00:00
Runnable[2s]: starts at 2025-03-01T07:05:25.879392+00:00
Runnable[3s]: starts at 2025-03-01T07:05:25.879804+00:00
Runnable[2s]: ends at 2025-03-01T07:05:27.881998+00:00
on end callback starts at 2025-03-01T07:05:27.882360+00:00
Runnable[3s]: ends at 2025-03-01T07:05:28.881737+00:00
on end callback starts at 2025-03-01T07:05:28.882428+00:00
on end callback ends at 2025-03-01T07:05:29.883893+00:00
on end callback ends at 2025-03-01T07:05:30.884831+00:00

with_types

with_types(
    *,
    input_type: type[Input] | None = None,
    output_type: type[Output] | None = None
) -> Runnable[Input, Output]

Bind input and output types to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
input_type type[Input] | None

The input type to bind to the Runnable. Defaults to None.

None
output_type type[Output] | None

The output type to bind to the Runnable. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the types bound.

with_retry

with_retry(
    *,
    retry_if_exception_type: tuple[
        type[BaseException], ...
    ] = (Exception,),
    wait_exponential_jitter: bool = True,
    exponential_jitter_params: (
        ExponentialJitterParams | None
    ) = None,
    stop_after_attempt: int = 3
) -> Runnable[Input, Output]

Create a new Runnable that retries the original Runnable on exceptions.

Parameters:

Name Type Description Default
retry_if_exception_type tuple[type[BaseException], ...]

A tuple of exception types to retry on. Defaults to (Exception,).

(Exception,)
wait_exponential_jitter bool

Whether to add jitter to the wait time between retries. Defaults to True.

True
stop_after_attempt int

The maximum number of attempts to make before giving up. Defaults to 3.

3
exponential_jitter_params ExponentialJitterParams | None

Parameters for tenacity.wait_exponential_jitter. Namely: initial, max, exp_base, and jitter (all float values).

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable that retries the original Runnable on exceptions.

Example
from langchain_core.runnables import RunnableLambda

count = 0


def _lambda(x: int) -> None:
    global count
    count = count + 1
    if x == 1:
        raise ValueError("x is 1")
    else:
        pass


runnable = RunnableLambda(_lambda)
try:
    runnable.with_retry(
        stop_after_attempt=2,
        retry_if_exception_type=(ValueError,),
    ).invoke(1)
except ValueError:
    pass

assert count == 2

map

map() -> Runnable[list[Input], list[Output]]

Return a new Runnable that maps a list of inputs to a list of outputs.

Calls invoke with each input.

Returns:

Type Description
Runnable[list[Input], list[Output]]

A new Runnable that maps a list of inputs to a list of outputs.

Example
from langchain_core.runnables import RunnableLambda


def _lambda(x: int) -> int:
    return x + 1


runnable = RunnableLambda(_lambda)
print(runnable.map().invoke([1, 2, 3]))  # [2, 3, 4]

with_fallbacks

with_fallbacks(
    fallbacks: Sequence[Runnable[Input, Output]],
    *,
    exceptions_to_handle: tuple[
        type[BaseException], ...
    ] = (Exception,),
    exception_key: str | None = None
) -> RunnableWithFallbacks[Input, Output]

Add fallbacks to a Runnable, returning a new Runnable.

The new Runnable will try the original Runnable, and then each fallback in order, upon failures.

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle. Defaults to (Exception,).

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input. Defaults to None.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

Example
from typing import Iterator

from langchain_core.runnables import RunnableGenerator


def _generate_immediate_error(input: Iterator) -> Iterator[str]:
    raise ValueError()
    yield ""


def _generate(input: Iterator) -> Iterator[str]:
    yield from "foo bar"


runnable = RunnableGenerator(_generate_immediate_error).with_fallbacks(
    [RunnableGenerator(_generate)]
)
print("".join(runnable.stream({})))  # foo bar

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle.

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

as_tool

as_tool(
    args_schema: type[BaseModel] | None = None,
    *,
    name: str | None = None,
    description: str | None = None,
    arg_types: dict[str, type] | None = None
) -> BaseTool

Create a BaseTool from a Runnable.

as_tool will instantiate a BaseTool with a name, description, and args_schema from a Runnable. Where possible, schemas are inferred from runnable.get_input_schema. Alternatively (e.g., if the Runnable takes a dict as input and the specific dict keys are not typed), the schema can be specified directly with args_schema. You can also pass arg_types to just specify the required arguments and their types.

Parameters:

Name Type Description Default
args_schema type[BaseModel] | None

The schema for the tool. Defaults to None.

None
name str | None

The name of the tool. Defaults to None.

None
description str | None

The description of the tool. Defaults to None.

None
arg_types dict[str, type] | None

A dictionary of argument names to types. Defaults to None.

None

Returns:

Type Description
BaseTool

A BaseTool instance.

Typed dict input:

from typing_extensions import TypedDict
from langchain_core.runnables import RunnableLambda


class Args(TypedDict):
    a: int
    b: list[int]


def f(x: Args) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool()
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via args_schema:

from typing import Any
from pydantic import BaseModel, Field
from langchain_core.runnables import RunnableLambda

def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))

class FSchema(BaseModel):
    """Apply a function to an integer and list of integers."""

    a: int = Field(..., description="Integer")
    b: list[int] = Field(..., description="List of ints")

runnable = RunnableLambda(f)
as_tool = runnable.as_tool(FSchema)
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via arg_types:

from typing import Any
from langchain_core.runnables import RunnableLambda


def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool(arg_types={"a": int, "b": list[int]})
as_tool.invoke({"a": 3, "b": [1, 2]})

String input:

from langchain_core.runnables import RunnableLambda


def f(x: str) -> str:
    return x + "a"


def g(x: str) -> str:
    return x + "z"


runnable = RunnableLambda(f) | g
as_tool = runnable.as_tool()
as_tool.invoke("b")

Added in version 0.2.14

__init__

__init__(*args: Any, **kwargs: Any) -> None

is_lc_serializable classmethod

is_lc_serializable() -> bool

Is this class serializable?

By design, even if a class inherits from Serializable, it is not serializable by default. This is to prevent accidental serialization of objects that should not be serialized.

Returns:

Type Description
bool

Whether the class is serializable. Default is False.

get_lc_namespace classmethod

get_lc_namespace() -> list[str]

Get the namespace of the langchain object.

For example, if the class is langchain.llms.openai.OpenAI, then the namespace is ["langchain", "llms", "openai"]

Returns:

Type Description
list[str]

The namespace as a list of strings.

lc_id classmethod

lc_id() -> list[str]

Return a unique identifier for this class for serialization purposes.

The unique identifier is a list of strings that describes the path to the object. For example, for the class langchain.llms.openai.OpenAI, the id is ["langchain", "llms", "openai", "OpenAI"].

to_json

to_json() -> (
    SerializedConstructor | SerializedNotImplemented
)

Serialize the Runnable to JSON.

Returns:

Type Description
SerializedConstructor | SerializedNotImplemented

A JSON-serializable representation of the Runnable.

to_json_not_implemented

to_json_not_implemented() -> SerializedNotImplemented

Serialize a "not implemented" object.

Returns:

Type Description
SerializedNotImplemented

SerializedNotImplemented.

configurable_fields

configurable_fields(
    **kwargs: AnyConfigurableField,
) -> RunnableSerializable[Input, Output]

Configure particular Runnable fields at runtime.

Parameters:

Name Type Description Default
**kwargs AnyConfigurableField

A dictionary of ConfigurableField instances to configure.

{}

Raises:

Type Description
ValueError

If a configuration key is not found in the Runnable.

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the fields configured.

from langchain_core.runnables import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatOpenAI(max_tokens=20).configurable_fields(
    max_tokens=ConfigurableField(
        id="output_token_number",
        name="Max tokens in the output",
        description="The maximum number of tokens in the output",
    )
)

# max_tokens = 20
print("max_tokens_20: ", model.invoke("tell me something about chess").content)

# max_tokens = 200
print(
    "max_tokens_200: ",
    model.with_config(configurable={"output_token_number": 200})
    .invoke("tell me something about chess")
    .content,
)

configurable_alternatives

configurable_alternatives(
    which: ConfigurableField,
    *,
    default_key: str = "default",
    prefix_keys: bool = False,
    **kwargs: (
        Runnable[Input, Output]
        | Callable[[], Runnable[Input, Output]]
    )
) -> RunnableSerializable[Input, Output]

Configure alternatives for Runnables that can be set at runtime.

Parameters:

Name Type Description Default
which ConfigurableField

The ConfigurableField instance that will be used to select the alternative.

required
default_key str

The default key to use if no alternative is selected. Defaults to 'default'.

'default'
prefix_keys bool

Whether to prefix the keys with the ConfigurableField id. Defaults to False.

False
**kwargs Runnable[Input, Output] | Callable[[], Runnable[Input, Output]]

A dictionary of keys to Runnable instances or callables that return Runnable instances.

{}

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the alternatives configured.

from langchain_anthropic import ChatAnthropic
from langchain_core.runnables.utils import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatAnthropic(
    model_name="claude-3-7-sonnet-20250219"
).configurable_alternatives(
    ConfigurableField(id="llm"),
    default_key="anthropic",
    openai=ChatOpenAI(),
)

# uses the default model ChatAnthropic
print(model.invoke("which organization created you?").content)

# uses ChatOpenAI
print(
    model.with_config(configurable={"llm": "openai"})
    .invoke("which organization created you?")
    .content
)

set_verbose

set_verbose(verbose: bool | None) -> bool

If verbose is None, set it.

This allows users to pass in None as verbose to access the global setting.

Parameters:

Name Type Description Default
verbose bool | None

The verbosity setting to use.

required

Returns:

Type Description
bool

The verbosity setting to use.

with_structured_output

with_structured_output(
    schema: Dict | type,
    *,
    include_raw: bool = False,
    **kwargs: Any
) -> Runnable[LanguageModelInput, Dict | BaseModel]

Model wrapper that returns outputs formatted to match the given schema.

Parameters:

Name Type Description Default
schema Dict | type

The output schema. Can be passed in as:

  • an OpenAI function/tool schema,
  • a JSON Schema,
  • a TypedDict class,
  • or a Pydantic class.

If schema is a Pydantic class then the model output will be a Pydantic instance of that class, and the model-generated fields will be validated by the Pydantic class. Otherwise the model output will be a dict and will not be validated. See langchain_core.utils.function_calling.convert_to_openai_tool for more on how to properly specify types and descriptions of schema fields when specifying a Pydantic or TypedDict class.

required
include_raw bool

If False then only the parsed structured output is returned. If an error occurs during model output parsing it will be raised. If True then both the raw model response (a BaseMessage) and the parsed model response will be returned. If an error occurs during output parsing it will be caught and returned as well. The final output is always a dict with keys 'raw', 'parsed', and 'parsing_error'.

False

Raises:

Type Description
ValueError

If there are any unsupported kwargs.

NotImplementedError

If the model does not implement with_structured_output().

Returns:

Type Description
Runnable[LanguageModelInput, Dict | BaseModel]

A Runnable that takes same inputs as a langchain_core.language_models.chat.BaseChatModel.

Runnable[LanguageModelInput, Dict | BaseModel]

If include_raw is False and schema is a Pydantic class, Runnable outputs

Runnable[LanguageModelInput, Dict | BaseModel]

an instance of schema (i.e., a Pydantic object).

Runnable[LanguageModelInput, Dict | BaseModel]

Otherwise, if include_raw is False then Runnable outputs a dict.

Runnable[LanguageModelInput, Dict | BaseModel]

If include_raw is True, then Runnable outputs a dict with keys:

Runnable[LanguageModelInput, Dict | BaseModel]
  • 'raw': BaseMessage
Runnable[LanguageModelInput, Dict | BaseModel]
  • 'parsed': None if there was a parsing error, otherwise the type depends on the schema as described above.
Runnable[LanguageModelInput, Dict | BaseModel]
  • 'parsing_error': Optional[BaseException]
Pydantic schema (include_raw=False):

.. code-block:: python

from pydantic import BaseModel


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


llm = ChatModel(model="model-name", temperature=0)
structured_llm = llm.with_structured_output(AnswerWithJustification)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)

# -> AnswerWithJustification(
#     answer='They weigh the same',
#     justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'
# )
Pydantic schema (include_raw=True):

.. code-block:: python

from pydantic import BaseModel


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


llm = ChatModel(model="model-name", temperature=0)
structured_llm = llm.with_structured_output(
    AnswerWithJustification, include_raw=True
)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)
# -> {
#     'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),
#     'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),
#     'parsing_error': None
# }
Dict schema (include_raw=False):

.. code-block:: python

from pydantic import BaseModel
from langchain_core.utils.function_calling import convert_to_openai_tool


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


dict_schema = convert_to_openai_tool(AnswerWithJustification)
llm = ChatModel(model="model-name", temperature=0)
structured_llm = llm.with_structured_output(dict_schema)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)
# -> {
#     'answer': 'They weigh the same',
#     'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
# }

Behavior changed in 0.2.26

Added support for TypedDict class.

get_token_ids

get_token_ids(text: str) -> list[int]

Return the ordered ids of the tokens in a text.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
list[int]

A list of ids corresponding to the tokens in the text, in order they occur

list[int]

in the text.

get_num_tokens

get_num_tokens(text: str) -> int

Get the number of tokens present in the text.

Useful for checking if an input fits in a model's context window.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
int

The integer number of tokens in the text.

get_num_tokens_from_messages

get_num_tokens_from_messages(
    messages: list[BaseMessage],
    tools: Sequence | None = None,
) -> int

Get the number of tokens in the messages.

Useful for checking if an input fits in a model's context window.

Note

The base implementation of get_num_tokens_from_messages ignores tool schemas.

Parameters:

Name Type Description Default
messages list[BaseMessage]

The message inputs to tokenize.

required
tools Sequence | None

If provided, sequence of dict, BaseModel, function, or BaseTools to be converted to tool schemas.

None

Returns:

Type Description
int

The sum of the number of tokens across the messages.

generate

generate(
    messages: list[list[BaseMessage]],
    stop: list[str] | None = None,
    callbacks: Callbacks = None,
    *,
    tags: list[str] | None = None,
    metadata: dict[str, Any] | None = None,
    run_name: str | None = None,
    run_id: UUID | None = None,
    **kwargs: Any
) -> LLMResult

Pass a sequence of prompts to the model and return model generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
messages list[list[BaseMessage]]

List of list of messages.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | None

The tags to apply.

None
metadata dict[str, Any] | None

The metadata to apply.

None
run_name str | None

The name of the run.

None
run_id UUID | None

The ID of the run.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input

LLMResult

prompt and additional model provider-specific output.

agenerate async

agenerate(
    messages: list[list[BaseMessage]],
    stop: list[str] | None = None,
    callbacks: Callbacks = None,
    *,
    tags: list[str] | None = None,
    metadata: dict[str, Any] | None = None,
    run_name: str | None = None,
    run_id: UUID | None = None,
    **kwargs: Any
) -> LLMResult

Asynchronously pass a sequence of prompts to a model and return generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
messages list[list[BaseMessage]]

List of list of messages.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | None

The tags to apply.

None
metadata dict[str, Any] | None

The metadata to apply.

None
run_name str | None

The name of the run.

None
run_id UUID | None

The ID of the run.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input

LLMResult

prompt and additional model provider-specific output.

dict

dict(**kwargs: Any) -> dict

Return a dictionary of the LLM.

bind_tools

bind_tools(
    tools: Sequence[
        Dict[str, Any] | type | Callable | BaseTool
    ],
    *,
    tool_choice: str | None = None,
    **kwargs: Any
) -> Runnable[LanguageModelInput, AIMessage]

Bind tools to the model.

Parameters:

Name Type Description Default
tools Sequence[Dict[str, Any] | type | Callable | BaseTool]

Sequence of tools to bind to the model.

required
tool_choice str | None

The tool to use. If "any" then any tool can be used.

None

Returns:

Type Description
Runnable[LanguageModelInput, AIMessage]

A Runnable that returns a message.

VertexAIImageGeneratorChat

Bases: _BaseVertexAIImageGenerator, BaseChatModel

Generates an image from a prompt.

Methods:

Name Description
get_name

Get the name of the Runnable.

get_input_schema

Get a pydantic model that can be used to validate input to the Runnable.

get_input_jsonschema

Get a JSON schema that represents the input to the Runnable.

get_output_schema

Get a pydantic model that can be used to validate output to the Runnable.

get_output_jsonschema

Get a JSON schema that represents the output of the Runnable.

config_schema

The type of config this Runnable accepts specified as a pydantic model.

get_config_jsonschema

Get a JSON schema that represents the config of the Runnable.

get_graph

Return a graph representation of this Runnable.

get_prompts

Return a list of prompts used by this Runnable.

__or__

Runnable "or" operator.

__ror__

Runnable "reverse-or" operator.

pipe

Pipe runnables.

pick

Pick keys from the output dict of this Runnable.

assign

Assigns new fields to the dict output of this Runnable.

batch

Default implementation runs invoke in parallel using a thread pool executor.

batch_as_completed

Run invoke in parallel on a list of inputs.

abatch

Default implementation runs ainvoke in parallel using asyncio.gather.

abatch_as_completed

Run ainvoke in parallel on a list of inputs.

astream_log

Stream all output from a Runnable, as reported to the callback system.

astream_events

Generate a stream of events.

transform

Transform inputs to outputs.

atransform

Transform inputs to outputs.

bind

Bind arguments to a Runnable, returning a new Runnable.

with_config

Bind config to a Runnable, returning a new Runnable.

with_listeners

Bind lifecycle listeners to a Runnable, returning a new Runnable.

with_alisteners

Bind async lifecycle listeners to a Runnable.

with_types

Bind input and output types to a Runnable, returning a new Runnable.

with_retry

Create a new Runnable that retries the original Runnable on exceptions.

map

Return a new Runnable that maps a list of inputs to a list of outputs.

with_fallbacks

Add fallbacks to a Runnable, returning a new Runnable.

as_tool

Create a BaseTool from a Runnable.

__init__
is_lc_serializable

Is this class serializable?

get_lc_namespace

Get the namespace of the langchain object.

lc_id

Return a unique identifier for this class for serialization purposes.

to_json

Serialize the Runnable to JSON.

to_json_not_implemented

Serialize a "not implemented" object.

configurable_fields

Configure particular Runnable fields at runtime.

configurable_alternatives

Configure alternatives for Runnables that can be set at runtime.

set_verbose

If verbose is None, set it.

with_structured_output

Model wrapper that returns outputs formatted to match the given schema.

get_token_ids

Return the ordered ids of the tokens in a text.

get_num_tokens

Get the number of tokens present in the text.

get_num_tokens_from_messages

Get the number of tokens in the messages.

generate

Pass a sequence of prompts to the model and return model generations.

agenerate

Asynchronously pass a sequence of prompts to a model and return generations.

dict

Return a dictionary of the LLM.

bind_tools

Bind tools to the model.

Attributes:

Name Type Description
InputType TypeAlias

Get the input type for this runnable.

OutputType Any

Get the output type for this runnable.

input_schema type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema type[BaseModel]

Output schema.

config_specs list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets dict[str, str]

A map of constructor argument names to secret ids.

lc_attributes dict

List of attribute names that should be included in the serialized kwargs.

cache BaseCache | bool | None

Whether to cache the response.

verbose bool

Whether to print out response text.

callbacks Callbacks

Callbacks to add to the run trace.

tags list[str] | None

Tags to add to the run trace.

metadata dict[str, Any] | None

Metadata to add to the run trace.

custom_get_token_ids Callable[[str], list[int]] | None

Optional encoder to use for counting tokens.

rate_limiter BaseRateLimiter | None

An optional rate limiter to use for limiting the number of requests.

disable_streaming bool | Literal['tool_calling']

Whether to disable streaming for this model.

output_version str | None

Version of AIMessage output format to store in message content.

model_name str

Name of the base model

negative_prompt Union[str, None]

A description of what you want to omit in

number_of_results int

Number of images to generate

guidance_scale Union[float, None]

Controls the strength of the prompt

language Union[str, None]

Language of the text prompt for the image Supported values are "en" for English,

seed Union[int, None]

Random seed for the image generation

project Union[str, None]

Google cloud project id

InputType property

InputType: TypeAlias

Get the input type for this runnable.

OutputType property

OutputType: Any

Get the output type for this runnable.

input_schema property

input_schema: type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema property

output_schema: type[BaseModel]

Output schema.

The type of output this Runnable produces specified as a pydantic model.

config_specs property

config_specs: list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets property

lc_secrets: dict[str, str]

A map of constructor argument names to secret ids.

For example,

lc_attributes property

lc_attributes: dict

List of attribute names that should be included in the serialized kwargs.

These attributes must be accepted by the constructor. Default is an empty dictionary.

cache class-attribute instance-attribute

cache: BaseCache | bool | None = Field(
    default=None, exclude=True
)

Whether to cache the response.

  • If true, will use the global cache.
  • If false, will not use a cache
  • If None, will use the global cache if it's set, otherwise no cache.
  • If instance of BaseCache, will use the provided cache.

Caching is not currently supported for streaming methods of models.

verbose class-attribute instance-attribute

verbose: bool = Field(
    default_factory=_get_verbosity, exclude=True, repr=False
)

Whether to print out response text.

callbacks class-attribute instance-attribute

callbacks: Callbacks = Field(default=None, exclude=True)

Callbacks to add to the run trace.

tags class-attribute instance-attribute

tags: list[str] | None = Field(default=None, exclude=True)

Tags to add to the run trace.

metadata class-attribute instance-attribute

metadata: dict[str, Any] | None = Field(
    default=None, exclude=True
)

Metadata to add to the run trace.

custom_get_token_ids class-attribute instance-attribute

custom_get_token_ids: Callable[[str], list[int]] | None = (
    Field(default=None, exclude=True)
)

Optional encoder to use for counting tokens.

rate_limiter class-attribute instance-attribute

rate_limiter: BaseRateLimiter | None = Field(
    default=None, exclude=True
)

An optional rate limiter to use for limiting the number of requests.

disable_streaming class-attribute instance-attribute

disable_streaming: bool | Literal['tool_calling'] = False

Whether to disable streaming for this model.

If streaming is bypassed, then stream()/astream()/astream_events() will defer to invoke()/ainvoke().

  • If True, will always bypass streaming case.
  • If 'tool_calling', will bypass streaming case only when the model is called with a tools keyword argument. In other words, LangChain will automatically switch to non-streaming behavior (invoke()) only when the tools argument is provided. This offers the best of both worlds.
  • If False (default), will always use streaming case if available.

The main reason for this flag is that code might be written using stream() and a user may want to swap out a given model for another model whose the implementation does not properly support streaming.

output_version class-attribute instance-attribute

output_version: str | None = Field(
    default_factory=from_env(
        "LC_OUTPUT_VERSION", default=None
    )
)

Version of AIMessage output format to store in message content.

AIMessage.content_blocks will lazily parse the contents of content into a standard format. This flag can be used to additionally store the standard format in message content, e.g., for serialization purposes.

Supported values:

  • "v0": provider-specific format in content (can lazily-parse with .content_blocks)
  • "v1": standardized format in content (consistent with .content_blocks)

Partner packages (e.g., langchain-openai) can also use this field to roll out new content formats in a backward-compatible way.

Added in version 1.0

model_name class-attribute instance-attribute

model_name: str = Field(default='imagen-3.0-generate-002')

Name of the base model

negative_prompt class-attribute instance-attribute

negative_prompt: Union[str, None] = Field(default=None)

A description of what you want to omit in the generated images

number_of_results class-attribute instance-attribute

number_of_results: int = Field(default=1)

Number of images to generate

guidance_scale class-attribute instance-attribute

guidance_scale: Union[float, None] = Field(default=None)

Controls the strength of the prompt

language class-attribute instance-attribute

language: Union[str, None] = Field(default=None)

Language of the text prompt for the image Supported values are "en" for English, "hi" for Hindi, "ja" for Japanese, "ko" for Korean, and "auto" for automatic language detection

seed class-attribute instance-attribute

seed: Union[int, None] = Field(default=None)

Random seed for the image generation

project class-attribute instance-attribute

project: Union[str, None] = Field(default=None)

Google cloud project id

get_name

get_name(
    suffix: str | None = None, *, name: str | None = None
) -> str

Get the name of the Runnable.

Parameters:

Name Type Description Default
suffix str | None

An optional suffix to append to the name.

None
name str | None

An optional name to use instead of the Runnable's name.

None

Returns:

Type Description
str

The name of the Runnable.

get_input_schema

get_input_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate input to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic input schema that depends on which configuration the Runnable is invoked with.

This method allows to get an input schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate input.

get_input_jsonschema

get_input_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the input to the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the input to the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_input_jsonschema())

Added in version 0.3.0

get_output_schema

get_output_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate output to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic output schema that depends on which configuration the Runnable is invoked with.

This method allows to get an output schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate output.

get_output_jsonschema

get_output_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the output of the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the output of the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_output_jsonschema())

Added in version 0.3.0

config_schema

config_schema(
    *, include: Sequence[str] | None = None
) -> type[BaseModel]

The type of config this Runnable accepts specified as a pydantic model.

To mark a field as configurable, see the configurable_fields and configurable_alternatives methods.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate config.

get_config_jsonschema

get_config_jsonschema(
    *, include: Sequence[str] | None = None
) -> dict[str, Any]

Get a JSON schema that represents the config of the Runnable.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the config of the Runnable.

Added in version 0.3.0

get_graph

get_graph(config: RunnableConfig | None = None) -> Graph

Return a graph representation of this Runnable.

get_prompts

get_prompts(
    config: RunnableConfig | None = None,
) -> list[BasePromptTemplate]

Return a list of prompts used by this Runnable.

__or__

__or__(
    other: (
        Runnable[Any, Other]
        | Callable[[Iterator[Any]], Iterator[Other]]
        | Callable[
            [AsyncIterator[Any]], AsyncIterator[Other]
        ]
        | Callable[[Any], Other]
        | Mapping[
            str,
            Runnable[Any, Other]
            | Callable[[Any], Other]
            | Any,
        ]
    ),
) -> RunnableSerializable[Input, Other]

Runnable "or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Any, Other] | Callable[[Iterator[Any]], Iterator[Other]] | Callable[[AsyncIterator[Any]], AsyncIterator[Other]] | Callable[[Any], Other] | Mapping[str, Runnable[Any, Other] | Callable[[Any], Other] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

__ror__

__ror__(
    other: (
        Runnable[Other, Any]
        | Callable[[Iterator[Other]], Iterator[Any]]
        | Callable[
            [AsyncIterator[Other]], AsyncIterator[Any]
        ]
        | Callable[[Other], Any]
        | Mapping[
            str,
            Runnable[Other, Any]
            | Callable[[Other], Any]
            | Any,
        ]
    ),
) -> RunnableSerializable[Other, Output]

Runnable "reverse-or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Other, Any] | Callable[[Iterator[Other]], Iterator[Any]] | Callable[[AsyncIterator[Other]], AsyncIterator[Any]] | Callable[[Other], Any] | Mapping[str, Runnable[Other, Any] | Callable[[Other], Any] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Other, Output]

A new Runnable.

pipe

pipe(
    *others: Runnable[Any, Other] | Callable[[Any], Other],
    name: str | None = None
) -> RunnableSerializable[Input, Other]

Pipe runnables.

Compose this Runnable with Runnable-like objects to make a RunnableSequence.

Equivalent to RunnableSequence(self, *others) or self | others[0] | ...

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


def mul_two(x: int) -> int:
    return x * 2


runnable_1 = RunnableLambda(add_one)
runnable_2 = RunnableLambda(mul_two)
sequence = runnable_1.pipe(runnable_2)
# Or equivalently:
# sequence = runnable_1 | runnable_2
# sequence = RunnableSequence(first=runnable_1, last=runnable_2)
sequence.invoke(1)
await sequence.ainvoke(1)
# -> 4

sequence.batch([1, 2, 3])
await sequence.abatch([1, 2, 3])
# -> [4, 6, 8]

Parameters:

Name Type Description Default
*others Runnable[Any, Other] | Callable[[Any], Other]

Other Runnable or Runnable-like objects to compose

()
name str | None

An optional name for the resulting RunnableSequence.

None

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

pick

pick(
    keys: str | list[str],
) -> RunnableSerializable[Any, Any]

Pick keys from the output dict of this Runnable.

Pick single key:

```python
import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)
chain = RunnableMap(str=as_str, json=as_json)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3]}

json_only_chain = chain.pick("json")
json_only_chain.invoke("[1, 2, 3]")
# -> [1, 2, 3]
```

Pick list of keys:

```python
from typing import Any

import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)


def as_bytes(x: Any) -> bytes:
    return bytes(x, "utf-8")


chain = RunnableMap(
    str=as_str, json=as_json, bytes=RunnableLambda(as_bytes)
)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3], "bytes": b"[1, 2, 3]"}

json_and_bytes_chain = chain.pick(["json", "bytes"])
json_and_bytes_chain.invoke("[1, 2, 3]")
# -> {"json": [1, 2, 3], "bytes": b"[1, 2, 3]"}
```

Parameters:

Name Type Description Default
keys str | list[str]

A key or list of keys to pick from the output dict.

required

Returns:

Type Description
RunnableSerializable[Any, Any]

a new Runnable.

assign

assign(
    **kwargs: (
        Runnable[dict[str, Any], Any]
        | Callable[[dict[str, Any]], Any]
        | Mapping[
            str,
            Runnable[dict[str, Any], Any]
            | Callable[[dict[str, Any]], Any],
        ]
    ),
) -> RunnableSerializable[Any, Any]

Assigns new fields to the dict output of this Runnable.

from langchain_community.llms.fake import FakeStreamingListLLM
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import SystemMessagePromptTemplate
from langchain_core.runnables import Runnable
from operator import itemgetter

prompt = (
    SystemMessagePromptTemplate.from_template("You are a nice assistant.")
    + "{question}"
)
llm = FakeStreamingListLLM(responses=["foo-lish"])

chain: Runnable = prompt | llm | {"str": StrOutputParser()}

chain_with_assign = chain.assign(hello=itemgetter("str") | llm)

print(chain_with_assign.input_schema.model_json_schema())
# {'title': 'PromptInput', 'type': 'object', 'properties':
{'question': {'title': 'Question', 'type': 'string'}}}
print(chain_with_assign.output_schema.model_json_schema())
# {'title': 'RunnableSequenceOutput', 'type': 'object', 'properties':
{'str': {'title': 'Str',
'type': 'string'}, 'hello': {'title': 'Hello', 'type': 'string'}}}

Parameters:

Name Type Description Default
**kwargs Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any] | Mapping[str, Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any]]

A mapping of keys to Runnable or Runnable-like objects that will be invoked with the entire output dict of this Runnable.

{}

Returns:

Type Description
RunnableSerializable[Any, Any]

A new Runnable.

batch

batch(
    inputs: list[Input],
    config: (
        RunnableConfig | list[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> list[Output]

Default implementation runs invoke in parallel using a thread pool executor.

The default implementation of batch works well for IO bound runnables.

Subclasses should override this method if they can batch more efficiently; e.g., if the underlying Runnable uses an API which supports a batch mode.

Parameters:

Name Type Description Default
inputs list[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | list[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
list[Output]

A list of outputs from the Runnable.

batch_as_completed

batch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> Iterator[tuple[int, Output | Exception]]

Run invoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
tuple[int, Output | Exception]

Tuples of the index of the input and the output from the Runnable.

abatch async

abatch(
    inputs: list[Input],
    config: (
        RunnableConfig | list[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> list[Output]

Default implementation runs ainvoke in parallel using asyncio.gather.

The default implementation of batch works well for IO bound runnables.

Subclasses should override this method if they can batch more efficiently; e.g., if the underlying Runnable uses an API which supports a batch mode.

Parameters:

Name Type Description Default
inputs list[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | list[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
list[Output]

A list of outputs from the Runnable.

abatch_as_completed async

abatch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> AsyncIterator[tuple[int, Output | Exception]]

Run ainvoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[tuple[int, Output | Exception]]

A tuple of the index of the input and the output from the Runnable.

astream_log async

astream_log(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    diff: bool = True,
    with_streamed_output_list: bool = True,
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

Stream all output from a Runnable, as reported to the callback system.

This includes all inner runs of LLMs, Retrievers, Tools, etc.

Output is streamed as Log objects, which include a list of Jsonpatch ops that describe how the state of the run has changed in each step, and the final state of the run.

The Jsonpatch ops can be applied in order to construct state.

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
diff bool

Whether to yield diffs between each step or the current state.

True
with_streamed_output_list bool

Whether to yield the streamed_output list.

True
include_names Sequence[str] | None

Only include logs with these names.

None
include_types Sequence[str] | None

Only include logs with these types.

None
include_tags Sequence[str] | None

Only include logs with these tags.

None
exclude_names Sequence[str] | None

Exclude logs with these names.

None
exclude_types Sequence[str] | None

Exclude logs with these types.

None
exclude_tags Sequence[str] | None

Exclude logs with these tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

A RunLogPatch or RunLog object.

astream_events async

astream_events(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    version: Literal["v1", "v2"] = "v2",
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[StreamEvent]

Generate a stream of events.

Use to create an iterator over StreamEvents that provide real-time information about the progress of the Runnable, including StreamEvents from intermediate results.

A StreamEvent is a dictionary with the following schema:

  • event: str - Event names are of the format: on_[runnable_type]_(start|stream|end).
  • name: str - The name of the Runnable that generated the event.
  • run_id: str - randomly generated ID associated with the given execution of the Runnable that emitted the event. A child Runnable that gets invoked as part of the execution of a parent Runnable is assigned its own unique ID.
  • parent_ids: list[str] - The IDs of the parent runnables that generated the event. The root Runnable will have an empty list. The order of the parent IDs is from the root to the immediate parent. Only available for v2 version of the API. The v1 version of the API will return an empty list.
  • tags: Optional[list[str]] - The tags of the Runnable that generated the event.
  • metadata: Optional[dict[str, Any]] - The metadata of the Runnable that generated the event.
  • data: dict[str, Any]

Below is a table that illustrates some events that might be emitted by various chains. Metadata fields have been omitted from the table for brevity. Chain definitions have been included after the table.

Note

This reference table is for the v2 version of the schema.

+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | event | name | chunk | input | output | +==========================+==================+=====================================+===================================================+=====================================================+ | on_chat_model_start | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_stream | [model name] | AIMessageChunk(content="hello") | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_end | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | AIMessageChunk(content="hello world") | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_start | [model name] | | {'input': 'hello'} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_stream | [model name] | 'Hello' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_end | [model name] | | 'Hello human!' | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_start | format_docs | | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_stream | format_docs | 'hello world!, goodbye world!' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_end | format_docs | | [Document(...)] | 'hello world!, goodbye world!' | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_start | some_tool | | {"x": 1, "y": "2"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_end | some_tool | | | {"x": 1, "y": "2"} | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_start | [retriever name] | | {"query": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_end | [retriever name] | | {"query": "hello"} | [Document(...), ..] | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_start | [template_name] | | {"question": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_end | [template_name] | | {"question": "hello"} | ChatPromptValue(messages: [SystemMessage, ...]) | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+

In addition to the standard events, users can also dispatch custom events (see example below).

Custom events will be only be surfaced with in the v2 version of the API!

A custom event has following format:

+-----------+------+-----------------------------------------------------------------------------------------------------------+ | Attribute | Type | Description | +===========+======+===========================================================================================================+ | name | str | A user defined name for the event. | +-----------+------+-----------------------------------------------------------------------------------------------------------+ | data | Any | The data associated with the event. This can be anything, though we suggest making it JSON serializable. | +-----------+------+-----------------------------------------------------------------------------------------------------------+

Here are declarations associated with the standard events shown above:

format_docs:

def format_docs(docs: list[Document]) -> str:
    '''Format the docs.'''
    return ", ".join([doc.page_content for doc in docs])


format_docs = RunnableLambda(format_docs)

some_tool:

@tool
def some_tool(x: int, y: str) -> dict:
    '''Some_tool.'''
    return {"x": x, "y": y}

prompt:

template = ChatPromptTemplate.from_messages(
    [
        ("system", "You are Cat Agent 007"),
        ("human", "{question}"),
    ]
).with_config({"run_name": "my_template", "tags": ["my_template"]})
Example:

from langchain_core.runnables import RunnableLambda


async def reverse(s: str) -> str:
    return s[::-1]


chain = RunnableLambda(func=reverse)

events = [event async for event in chain.astream_events("hello", version="v2")]

# will produce the following events (run_id, and parent_ids
# has been omitted for brevity):
[
    {
        "data": {"input": "hello"},
        "event": "on_chain_start",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"chunk": "olleh"},
        "event": "on_chain_stream",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"output": "olleh"},
        "event": "on_chain_end",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
]

Example: Dispatch Custom Event

from langchain_core.callbacks.manager import (
    adispatch_custom_event,
)
from langchain_core.runnables import RunnableLambda, RunnableConfig
import asyncio


async def slow_thing(some_input: str, config: RunnableConfig) -> str:
    """Do something that takes a long time."""
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 1 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 2 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    return "Done"

slow_thing = RunnableLambda(slow_thing)

async for event in slow_thing.astream_events("some_input", version="v2"):
    print(event)

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
version Literal['v1', 'v2']

The version of the schema to use either 'v2' or 'v1'. Users should use 'v2'. 'v1' is for backwards compatibility and will be deprecated in 0.4.0. No default will be assigned until the API is stabilized. custom events will only be surfaced in 'v2'.

'v2'
include_names Sequence[str] | None

Only include events from Runnables with matching names.

None
include_types Sequence[str] | None

Only include events from Runnables with matching types.

None
include_tags Sequence[str] | None

Only include events from Runnables with matching tags.

None
exclude_names Sequence[str] | None

Exclude events from Runnables with matching names.

None
exclude_types Sequence[str] | None

Exclude events from Runnables with matching types.

None
exclude_tags Sequence[str] | None

Exclude events from Runnables with matching tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable. These will be passed to astream_log as this implementation of astream_events is built on top of astream_log.

{}

Yields:

Type Description
AsyncIterator[StreamEvent]

An async stream of StreamEvents.

Raises:

Type Description
NotImplementedError

If the version is not 'v1' or 'v2'.

transform

transform(
    input: Iterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> Iterator[Output]

Transform inputs to outputs.

Default implementation of transform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input Iterator[Input]

An iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
Output

The output of the Runnable.

atransform async

atransform(
    input: AsyncIterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> AsyncIterator[Output]

Transform inputs to outputs.

Default implementation of atransform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input AsyncIterator[Input]

An async iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[Output]

The output of the Runnable.

bind

bind(**kwargs: Any) -> Runnable[Input, Output]

Bind arguments to a Runnable, returning a new Runnable.

Useful when a Runnable in a chain requires an argument that is not in the output of the previous Runnable or included in the user input.

Parameters:

Name Type Description Default
kwargs Any

The arguments to bind to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the arguments bound.

Example
from langchain_ollama import ChatOllama
from langchain_core.output_parsers import StrOutputParser

llm = ChatOllama(model="llama3.1")

# Without bind.
chain = llm | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two three four five.'

# With bind.
chain = llm.bind(stop=["three"]) | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two'

with_config

with_config(
    config: RunnableConfig | None = None, **kwargs: Any
) -> Runnable[Input, Output]

Bind config to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

The config to bind to the Runnable.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the config bound.

with_listeners

with_listeners(
    *,
    on_start: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_end: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_error: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None
) -> Runnable[Input, Output]

Bind lifecycle listeners to a Runnable, returning a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called before the Runnable starts running, with the Run object. Defaults to None.

None
on_end Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda
from langchain_core.tracers.schemas import Run

import time


def test_runnable(time_to_sleep: int):
    time.sleep(time_to_sleep)


def fn_start(run_obj: Run):
    print("start_time:", run_obj.start_time)


def fn_end(run_obj: Run):
    print("end_time:", run_obj.end_time)


chain = RunnableLambda(test_runnable).with_listeners(
    on_start=fn_start, on_end=fn_end
)
chain.invoke(2)

with_alisteners

with_alisteners(
    *,
    on_start: AsyncListener | None = None,
    on_end: AsyncListener | None = None,
    on_error: AsyncListener | None = None
) -> Runnable[Input, Output]

Bind async lifecycle listeners to a Runnable.

Returns a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start AsyncListener | None

Called asynchronously before the Runnable starts running, with the Run object. Defaults to None.

None
on_end AsyncListener | None

Called asynchronously after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error AsyncListener | None

Called asynchronously if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda, Runnable
from datetime import datetime, timezone
import time
import asyncio

def format_t(timestamp: float) -> str:
    return datetime.fromtimestamp(timestamp, tz=timezone.utc).isoformat()

async def test_runnable(time_to_sleep: int):
    print(f"Runnable[{time_to_sleep}s]: starts at {format_t(time.time())}")
    await asyncio.sleep(time_to_sleep)
    print(f"Runnable[{time_to_sleep}s]: ends at {format_t(time.time())}")

async def fn_start(run_obj: Runnable):
    print(f"on start callback starts at {format_t(time.time())}")
    await asyncio.sleep(3)
    print(f"on start callback ends at {format_t(time.time())}")

async def fn_end(run_obj: Runnable):
    print(f"on end callback starts at {format_t(time.time())}")
    await asyncio.sleep(2)
    print(f"on end callback ends at {format_t(time.time())}")

runnable = RunnableLambda(test_runnable).with_alisteners(
    on_start=fn_start,
    on_end=fn_end
)
async def concurrent_runs():
    await asyncio.gather(runnable.ainvoke(2), runnable.ainvoke(3))

asyncio.run(concurrent_runs())
Result:
on start callback starts at 2025-03-01T07:05:22.875378+00:00
on start callback starts at 2025-03-01T07:05:22.875495+00:00
on start callback ends at 2025-03-01T07:05:25.878862+00:00
on start callback ends at 2025-03-01T07:05:25.878947+00:00
Runnable[2s]: starts at 2025-03-01T07:05:25.879392+00:00
Runnable[3s]: starts at 2025-03-01T07:05:25.879804+00:00
Runnable[2s]: ends at 2025-03-01T07:05:27.881998+00:00
on end callback starts at 2025-03-01T07:05:27.882360+00:00
Runnable[3s]: ends at 2025-03-01T07:05:28.881737+00:00
on end callback starts at 2025-03-01T07:05:28.882428+00:00
on end callback ends at 2025-03-01T07:05:29.883893+00:00
on end callback ends at 2025-03-01T07:05:30.884831+00:00

with_types

with_types(
    *,
    input_type: type[Input] | None = None,
    output_type: type[Output] | None = None
) -> Runnable[Input, Output]

Bind input and output types to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
input_type type[Input] | None

The input type to bind to the Runnable. Defaults to None.

None
output_type type[Output] | None

The output type to bind to the Runnable. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the types bound.

with_retry

with_retry(
    *,
    retry_if_exception_type: tuple[
        type[BaseException], ...
    ] = (Exception,),
    wait_exponential_jitter: bool = True,
    exponential_jitter_params: (
        ExponentialJitterParams | None
    ) = None,
    stop_after_attempt: int = 3
) -> Runnable[Input, Output]

Create a new Runnable that retries the original Runnable on exceptions.

Parameters:

Name Type Description Default
retry_if_exception_type tuple[type[BaseException], ...]

A tuple of exception types to retry on. Defaults to (Exception,).

(Exception,)
wait_exponential_jitter bool

Whether to add jitter to the wait time between retries. Defaults to True.

True
stop_after_attempt int

The maximum number of attempts to make before giving up. Defaults to 3.

3
exponential_jitter_params ExponentialJitterParams | None

Parameters for tenacity.wait_exponential_jitter. Namely: initial, max, exp_base, and jitter (all float values).

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable that retries the original Runnable on exceptions.

Example
from langchain_core.runnables import RunnableLambda

count = 0


def _lambda(x: int) -> None:
    global count
    count = count + 1
    if x == 1:
        raise ValueError("x is 1")
    else:
        pass


runnable = RunnableLambda(_lambda)
try:
    runnable.with_retry(
        stop_after_attempt=2,
        retry_if_exception_type=(ValueError,),
    ).invoke(1)
except ValueError:
    pass

assert count == 2

map

map() -> Runnable[list[Input], list[Output]]

Return a new Runnable that maps a list of inputs to a list of outputs.

Calls invoke with each input.

Returns:

Type Description
Runnable[list[Input], list[Output]]

A new Runnable that maps a list of inputs to a list of outputs.

Example
from langchain_core.runnables import RunnableLambda


def _lambda(x: int) -> int:
    return x + 1


runnable = RunnableLambda(_lambda)
print(runnable.map().invoke([1, 2, 3]))  # [2, 3, 4]

with_fallbacks

with_fallbacks(
    fallbacks: Sequence[Runnable[Input, Output]],
    *,
    exceptions_to_handle: tuple[
        type[BaseException], ...
    ] = (Exception,),
    exception_key: str | None = None
) -> RunnableWithFallbacks[Input, Output]

Add fallbacks to a Runnable, returning a new Runnable.

The new Runnable will try the original Runnable, and then each fallback in order, upon failures.

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle. Defaults to (Exception,).

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input. Defaults to None.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

Example
from typing import Iterator

from langchain_core.runnables import RunnableGenerator


def _generate_immediate_error(input: Iterator) -> Iterator[str]:
    raise ValueError()
    yield ""


def _generate(input: Iterator) -> Iterator[str]:
    yield from "foo bar"


runnable = RunnableGenerator(_generate_immediate_error).with_fallbacks(
    [RunnableGenerator(_generate)]
)
print("".join(runnable.stream({})))  # foo bar

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle.

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

as_tool

as_tool(
    args_schema: type[BaseModel] | None = None,
    *,
    name: str | None = None,
    description: str | None = None,
    arg_types: dict[str, type] | None = None
) -> BaseTool

Create a BaseTool from a Runnable.

as_tool will instantiate a BaseTool with a name, description, and args_schema from a Runnable. Where possible, schemas are inferred from runnable.get_input_schema. Alternatively (e.g., if the Runnable takes a dict as input and the specific dict keys are not typed), the schema can be specified directly with args_schema. You can also pass arg_types to just specify the required arguments and their types.

Parameters:

Name Type Description Default
args_schema type[BaseModel] | None

The schema for the tool. Defaults to None.

None
name str | None

The name of the tool. Defaults to None.

None
description str | None

The description of the tool. Defaults to None.

None
arg_types dict[str, type] | None

A dictionary of argument names to types. Defaults to None.

None

Returns:

Type Description
BaseTool

A BaseTool instance.

Typed dict input:

from typing_extensions import TypedDict
from langchain_core.runnables import RunnableLambda


class Args(TypedDict):
    a: int
    b: list[int]


def f(x: Args) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool()
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via args_schema:

from typing import Any
from pydantic import BaseModel, Field
from langchain_core.runnables import RunnableLambda

def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))

class FSchema(BaseModel):
    """Apply a function to an integer and list of integers."""

    a: int = Field(..., description="Integer")
    b: list[int] = Field(..., description="List of ints")

runnable = RunnableLambda(f)
as_tool = runnable.as_tool(FSchema)
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via arg_types:

from typing import Any
from langchain_core.runnables import RunnableLambda


def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool(arg_types={"a": int, "b": list[int]})
as_tool.invoke({"a": 3, "b": [1, 2]})

String input:

from langchain_core.runnables import RunnableLambda


def f(x: str) -> str:
    return x + "a"


def g(x: str) -> str:
    return x + "z"


runnable = RunnableLambda(f) | g
as_tool = runnable.as_tool()
as_tool.invoke("b")

Added in version 0.2.14

__init__

__init__(*args: Any, **kwargs: Any) -> None

is_lc_serializable classmethod

is_lc_serializable() -> bool

Is this class serializable?

By design, even if a class inherits from Serializable, it is not serializable by default. This is to prevent accidental serialization of objects that should not be serialized.

Returns:

Type Description
bool

Whether the class is serializable. Default is False.

get_lc_namespace classmethod

get_lc_namespace() -> list[str]

Get the namespace of the langchain object.

For example, if the class is langchain.llms.openai.OpenAI, then the namespace is ["langchain", "llms", "openai"]

Returns:

Type Description
list[str]

The namespace as a list of strings.

lc_id classmethod

lc_id() -> list[str]

Return a unique identifier for this class for serialization purposes.

The unique identifier is a list of strings that describes the path to the object. For example, for the class langchain.llms.openai.OpenAI, the id is ["langchain", "llms", "openai", "OpenAI"].

to_json

to_json() -> (
    SerializedConstructor | SerializedNotImplemented
)

Serialize the Runnable to JSON.

Returns:

Type Description
SerializedConstructor | SerializedNotImplemented

A JSON-serializable representation of the Runnable.

to_json_not_implemented

to_json_not_implemented() -> SerializedNotImplemented

Serialize a "not implemented" object.

Returns:

Type Description
SerializedNotImplemented

SerializedNotImplemented.

configurable_fields

configurable_fields(
    **kwargs: AnyConfigurableField,
) -> RunnableSerializable[Input, Output]

Configure particular Runnable fields at runtime.

Parameters:

Name Type Description Default
**kwargs AnyConfigurableField

A dictionary of ConfigurableField instances to configure.

{}

Raises:

Type Description
ValueError

If a configuration key is not found in the Runnable.

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the fields configured.

from langchain_core.runnables import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatOpenAI(max_tokens=20).configurable_fields(
    max_tokens=ConfigurableField(
        id="output_token_number",
        name="Max tokens in the output",
        description="The maximum number of tokens in the output",
    )
)

# max_tokens = 20
print("max_tokens_20: ", model.invoke("tell me something about chess").content)

# max_tokens = 200
print(
    "max_tokens_200: ",
    model.with_config(configurable={"output_token_number": 200})
    .invoke("tell me something about chess")
    .content,
)

configurable_alternatives

configurable_alternatives(
    which: ConfigurableField,
    *,
    default_key: str = "default",
    prefix_keys: bool = False,
    **kwargs: (
        Runnable[Input, Output]
        | Callable[[], Runnable[Input, Output]]
    )
) -> RunnableSerializable[Input, Output]

Configure alternatives for Runnables that can be set at runtime.

Parameters:

Name Type Description Default
which ConfigurableField

The ConfigurableField instance that will be used to select the alternative.

required
default_key str

The default key to use if no alternative is selected. Defaults to 'default'.

'default'
prefix_keys bool

Whether to prefix the keys with the ConfigurableField id. Defaults to False.

False
**kwargs Runnable[Input, Output] | Callable[[], Runnable[Input, Output]]

A dictionary of keys to Runnable instances or callables that return Runnable instances.

{}

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the alternatives configured.

from langchain_anthropic import ChatAnthropic
from langchain_core.runnables.utils import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatAnthropic(
    model_name="claude-3-7-sonnet-20250219"
).configurable_alternatives(
    ConfigurableField(id="llm"),
    default_key="anthropic",
    openai=ChatOpenAI(),
)

# uses the default model ChatAnthropic
print(model.invoke("which organization created you?").content)

# uses ChatOpenAI
print(
    model.with_config(configurable={"llm": "openai"})
    .invoke("which organization created you?")
    .content
)

set_verbose

set_verbose(verbose: bool | None) -> bool

If verbose is None, set it.

This allows users to pass in None as verbose to access the global setting.

Parameters:

Name Type Description Default
verbose bool | None

The verbosity setting to use.

required

Returns:

Type Description
bool

The verbosity setting to use.

with_structured_output

with_structured_output(
    schema: Dict | type,
    *,
    include_raw: bool = False,
    **kwargs: Any
) -> Runnable[LanguageModelInput, Dict | BaseModel]

Model wrapper that returns outputs formatted to match the given schema.

Parameters:

Name Type Description Default
schema Dict | type

The output schema. Can be passed in as:

  • an OpenAI function/tool schema,
  • a JSON Schema,
  • a TypedDict class,
  • or a Pydantic class.

If schema is a Pydantic class then the model output will be a Pydantic instance of that class, and the model-generated fields will be validated by the Pydantic class. Otherwise the model output will be a dict and will not be validated. See langchain_core.utils.function_calling.convert_to_openai_tool for more on how to properly specify types and descriptions of schema fields when specifying a Pydantic or TypedDict class.

required
include_raw bool

If False then only the parsed structured output is returned. If an error occurs during model output parsing it will be raised. If True then both the raw model response (a BaseMessage) and the parsed model response will be returned. If an error occurs during output parsing it will be caught and returned as well. The final output is always a dict with keys 'raw', 'parsed', and 'parsing_error'.

False

Raises:

Type Description
ValueError

If there are any unsupported kwargs.

NotImplementedError

If the model does not implement with_structured_output().

Returns:

Type Description
Runnable[LanguageModelInput, Dict | BaseModel]

A Runnable that takes same inputs as a langchain_core.language_models.chat.BaseChatModel.

Runnable[LanguageModelInput, Dict | BaseModel]

If include_raw is False and schema is a Pydantic class, Runnable outputs

Runnable[LanguageModelInput, Dict | BaseModel]

an instance of schema (i.e., a Pydantic object).

Runnable[LanguageModelInput, Dict | BaseModel]

Otherwise, if include_raw is False then Runnable outputs a dict.

Runnable[LanguageModelInput, Dict | BaseModel]

If include_raw is True, then Runnable outputs a dict with keys:

Runnable[LanguageModelInput, Dict | BaseModel]
  • 'raw': BaseMessage
Runnable[LanguageModelInput, Dict | BaseModel]
  • 'parsed': None if there was a parsing error, otherwise the type depends on the schema as described above.
Runnable[LanguageModelInput, Dict | BaseModel]
  • 'parsing_error': Optional[BaseException]
Pydantic schema (include_raw=False):

.. code-block:: python

from pydantic import BaseModel


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


llm = ChatModel(model="model-name", temperature=0)
structured_llm = llm.with_structured_output(AnswerWithJustification)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)

# -> AnswerWithJustification(
#     answer='They weigh the same',
#     justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'
# )
Pydantic schema (include_raw=True):

.. code-block:: python

from pydantic import BaseModel


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


llm = ChatModel(model="model-name", temperature=0)
structured_llm = llm.with_structured_output(
    AnswerWithJustification, include_raw=True
)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)
# -> {
#     'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),
#     'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),
#     'parsing_error': None
# }
Dict schema (include_raw=False):

.. code-block:: python

from pydantic import BaseModel
from langchain_core.utils.function_calling import convert_to_openai_tool


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


dict_schema = convert_to_openai_tool(AnswerWithJustification)
llm = ChatModel(model="model-name", temperature=0)
structured_llm = llm.with_structured_output(dict_schema)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)
# -> {
#     'answer': 'They weigh the same',
#     'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
# }

Behavior changed in 0.2.26

Added support for TypedDict class.

get_token_ids

get_token_ids(text: str) -> list[int]

Return the ordered ids of the tokens in a text.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
list[int]

A list of ids corresponding to the tokens in the text, in order they occur

list[int]

in the text.

get_num_tokens

get_num_tokens(text: str) -> int

Get the number of tokens present in the text.

Useful for checking if an input fits in a model's context window.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
int

The integer number of tokens in the text.

get_num_tokens_from_messages

get_num_tokens_from_messages(
    messages: list[BaseMessage],
    tools: Sequence | None = None,
) -> int

Get the number of tokens in the messages.

Useful for checking if an input fits in a model's context window.

Note

The base implementation of get_num_tokens_from_messages ignores tool schemas.

Parameters:

Name Type Description Default
messages list[BaseMessage]

The message inputs to tokenize.

required
tools Sequence | None

If provided, sequence of dict, BaseModel, function, or BaseTools to be converted to tool schemas.

None

Returns:

Type Description
int

The sum of the number of tokens across the messages.

generate

generate(
    messages: list[list[BaseMessage]],
    stop: list[str] | None = None,
    callbacks: Callbacks = None,
    *,
    tags: list[str] | None = None,
    metadata: dict[str, Any] | None = None,
    run_name: str | None = None,
    run_id: UUID | None = None,
    **kwargs: Any
) -> LLMResult

Pass a sequence of prompts to the model and return model generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
messages list[list[BaseMessage]]

List of list of messages.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | None

The tags to apply.

None
metadata dict[str, Any] | None

The metadata to apply.

None
run_name str | None

The name of the run.

None
run_id UUID | None

The ID of the run.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input

LLMResult

prompt and additional model provider-specific output.

agenerate async

agenerate(
    messages: list[list[BaseMessage]],
    stop: list[str] | None = None,
    callbacks: Callbacks = None,
    *,
    tags: list[str] | None = None,
    metadata: dict[str, Any] | None = None,
    run_name: str | None = None,
    run_id: UUID | None = None,
    **kwargs: Any
) -> LLMResult

Asynchronously pass a sequence of prompts to a model and return generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
messages list[list[BaseMessage]]

List of list of messages.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | None

The tags to apply.

None
metadata dict[str, Any] | None

The metadata to apply.

None
run_name str | None

The name of the run.

None
run_id UUID | None

The ID of the run.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input

LLMResult

prompt and additional model provider-specific output.

dict

dict(**kwargs: Any) -> dict

Return a dictionary of the LLM.

bind_tools

bind_tools(
    tools: Sequence[
        Dict[str, Any] | type | Callable | BaseTool
    ],
    *,
    tool_choice: str | None = None,
    **kwargs: Any
) -> Runnable[LanguageModelInput, AIMessage]

Bind tools to the model.

Parameters:

Name Type Description Default
tools Sequence[Dict[str, Any] | type | Callable | BaseTool]

Sequence of tools to bind to the model.

required
tool_choice str | None

The tool to use. If "any" then any tool can be used.

None

Returns:

Type Description
Runnable[LanguageModelInput, AIMessage]

A Runnable that returns a message.

VertexAIVisualQnAChat

Bases: _BaseImageTextModel, BaseChatModel

Chat implementation of a visual QnA model.

Methods:

Name Description
get_name

Get the name of the Runnable.

get_input_schema

Get a pydantic model that can be used to validate input to the Runnable.

get_input_jsonschema

Get a JSON schema that represents the input to the Runnable.

get_output_schema

Get a pydantic model that can be used to validate output to the Runnable.

get_output_jsonschema

Get a JSON schema that represents the output of the Runnable.

config_schema

The type of config this Runnable accepts specified as a pydantic model.

get_config_jsonschema

Get a JSON schema that represents the config of the Runnable.

get_graph

Return a graph representation of this Runnable.

get_prompts

Return a list of prompts used by this Runnable.

__or__

Runnable "or" operator.

__ror__

Runnable "reverse-or" operator.

pipe

Pipe runnables.

pick

Pick keys from the output dict of this Runnable.

assign

Assigns new fields to the dict output of this Runnable.

batch

Default implementation runs invoke in parallel using a thread pool executor.

batch_as_completed

Run invoke in parallel on a list of inputs.

abatch

Default implementation runs ainvoke in parallel using asyncio.gather.

abatch_as_completed

Run ainvoke in parallel on a list of inputs.

astream_log

Stream all output from a Runnable, as reported to the callback system.

astream_events

Generate a stream of events.

transform

Transform inputs to outputs.

atransform

Transform inputs to outputs.

bind

Bind arguments to a Runnable, returning a new Runnable.

with_config

Bind config to a Runnable, returning a new Runnable.

with_listeners

Bind lifecycle listeners to a Runnable, returning a new Runnable.

with_alisteners

Bind async lifecycle listeners to a Runnable.

with_types

Bind input and output types to a Runnable, returning a new Runnable.

with_retry

Create a new Runnable that retries the original Runnable on exceptions.

map

Return a new Runnable that maps a list of inputs to a list of outputs.

with_fallbacks

Add fallbacks to a Runnable, returning a new Runnable.

as_tool

Create a BaseTool from a Runnable.

__init__
is_lc_serializable

Is this class serializable?

get_lc_namespace

Get the namespace of the langchain object.

lc_id

Return a unique identifier for this class for serialization purposes.

to_json

Serialize the Runnable to JSON.

to_json_not_implemented

Serialize a "not implemented" object.

configurable_fields

Configure particular Runnable fields at runtime.

configurable_alternatives

Configure alternatives for Runnables that can be set at runtime.

set_verbose

If verbose is None, set it.

with_structured_output

Model wrapper that returns outputs formatted to match the given schema.

get_token_ids

Return the ordered ids of the tokens in a text.

get_num_tokens

Get the number of tokens present in the text.

get_num_tokens_from_messages

Get the number of tokens in the messages.

generate

Pass a sequence of prompts to the model and return model generations.

agenerate

Asynchronously pass a sequence of prompts to a model and return generations.

dict

Return a dictionary of the LLM.

bind_tools

Bind tools to the model.

Attributes:

Name Type Description
InputType TypeAlias

Get the input type for this runnable.

OutputType Any

Get the output type for this runnable.

input_schema type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema type[BaseModel]

Output schema.

config_specs list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets dict[str, str]

A map of constructor argument names to secret ids.

lc_attributes dict

List of attribute names that should be included in the serialized kwargs.

cache BaseCache | bool | None

Whether to cache the response.

verbose bool

Whether to print out response text.

callbacks Callbacks

Callbacks to add to the run trace.

tags list[str] | None

Tags to add to the run trace.

metadata dict[str, Any] | None

Metadata to add to the run trace.

custom_get_token_ids Callable[[str], list[int]] | None

Optional encoder to use for counting tokens.

rate_limiter BaseRateLimiter | None

An optional rate limiter to use for limiting the number of requests.

disable_streaming bool | Literal['tool_calling']

Whether to disable streaming for this model.

output_version str | None

Version of AIMessage output format to store in message content.

model_name str

Name of the model to use

number_of_results int

Number of results to return from one query

language str

Language of the query

project Union[str, None]

Google cloud project

InputType property

InputType: TypeAlias

Get the input type for this runnable.

OutputType property

OutputType: Any

Get the output type for this runnable.

input_schema property

input_schema: type[BaseModel]

The type of input this Runnable accepts specified as a pydantic model.

output_schema property

output_schema: type[BaseModel]

Output schema.

The type of output this Runnable produces specified as a pydantic model.

config_specs property

config_specs: list[ConfigurableFieldSpec]

List configurable fields for this Runnable.

lc_secrets property

lc_secrets: dict[str, str]

A map of constructor argument names to secret ids.

For example,

lc_attributes property

lc_attributes: dict

List of attribute names that should be included in the serialized kwargs.

These attributes must be accepted by the constructor. Default is an empty dictionary.

cache class-attribute instance-attribute

cache: BaseCache | bool | None = Field(
    default=None, exclude=True
)

Whether to cache the response.

  • If true, will use the global cache.
  • If false, will not use a cache
  • If None, will use the global cache if it's set, otherwise no cache.
  • If instance of BaseCache, will use the provided cache.

Caching is not currently supported for streaming methods of models.

verbose class-attribute instance-attribute

verbose: bool = Field(
    default_factory=_get_verbosity, exclude=True, repr=False
)

Whether to print out response text.

callbacks class-attribute instance-attribute

callbacks: Callbacks = Field(default=None, exclude=True)

Callbacks to add to the run trace.

tags class-attribute instance-attribute

tags: list[str] | None = Field(default=None, exclude=True)

Tags to add to the run trace.

metadata class-attribute instance-attribute

metadata: dict[str, Any] | None = Field(
    default=None, exclude=True
)

Metadata to add to the run trace.

custom_get_token_ids class-attribute instance-attribute

custom_get_token_ids: Callable[[str], list[int]] | None = (
    Field(default=None, exclude=True)
)

Optional encoder to use for counting tokens.

rate_limiter class-attribute instance-attribute

rate_limiter: BaseRateLimiter | None = Field(
    default=None, exclude=True
)

An optional rate limiter to use for limiting the number of requests.

disable_streaming class-attribute instance-attribute

disable_streaming: bool | Literal['tool_calling'] = False

Whether to disable streaming for this model.

If streaming is bypassed, then stream()/astream()/astream_events() will defer to invoke()/ainvoke().

  • If True, will always bypass streaming case.
  • If 'tool_calling', will bypass streaming case only when the model is called with a tools keyword argument. In other words, LangChain will automatically switch to non-streaming behavior (invoke()) only when the tools argument is provided. This offers the best of both worlds.
  • If False (default), will always use streaming case if available.

The main reason for this flag is that code might be written using stream() and a user may want to swap out a given model for another model whose the implementation does not properly support streaming.

output_version class-attribute instance-attribute

output_version: str | None = Field(
    default_factory=from_env(
        "LC_OUTPUT_VERSION", default=None
    )
)

Version of AIMessage output format to store in message content.

AIMessage.content_blocks will lazily parse the contents of content into a standard format. This flag can be used to additionally store the standard format in message content, e.g., for serialization purposes.

Supported values:

  • "v0": provider-specific format in content (can lazily-parse with .content_blocks)
  • "v1": standardized format in content (consistent with .content_blocks)

Partner packages (e.g., langchain-openai) can also use this field to roll out new content formats in a backward-compatible way.

Added in version 1.0

model_name class-attribute instance-attribute

model_name: str = Field(default='imagetext@001')

Name of the model to use

number_of_results class-attribute instance-attribute

number_of_results: int = Field(default=1)

Number of results to return from one query

language class-attribute instance-attribute

language: str = Field(default='en')

Language of the query

project class-attribute instance-attribute

project: Union[str, None] = Field(default=None)

Google cloud project

get_name

get_name(
    suffix: str | None = None, *, name: str | None = None
) -> str

Get the name of the Runnable.

Parameters:

Name Type Description Default
suffix str | None

An optional suffix to append to the name.

None
name str | None

An optional name to use instead of the Runnable's name.

None

Returns:

Type Description
str

The name of the Runnable.

get_input_schema

get_input_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate input to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic input schema that depends on which configuration the Runnable is invoked with.

This method allows to get an input schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate input.

get_input_jsonschema

get_input_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the input to the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the input to the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_input_jsonschema())

Added in version 0.3.0

get_output_schema

get_output_schema(
    config: RunnableConfig | None = None,
) -> type[BaseModel]

Get a pydantic model that can be used to validate output to the Runnable.

Runnables that leverage the configurable_fields and configurable_alternatives methods will have a dynamic output schema that depends on which configuration the Runnable is invoked with.

This method allows to get an output schema for a specific configuration.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate output.

get_output_jsonschema

get_output_jsonschema(
    config: RunnableConfig | None = None,
) -> dict[str, Any]

Get a JSON schema that represents the output of the Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

A config to use when generating the schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the output of the Runnable.

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


runnable = RunnableLambda(add_one)

print(runnable.get_output_jsonschema())

Added in version 0.3.0

config_schema

config_schema(
    *, include: Sequence[str] | None = None
) -> type[BaseModel]

The type of config this Runnable accepts specified as a pydantic model.

To mark a field as configurable, see the configurable_fields and configurable_alternatives methods.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
type[BaseModel]

A pydantic model that can be used to validate config.

get_config_jsonschema

get_config_jsonschema(
    *, include: Sequence[str] | None = None
) -> dict[str, Any]

Get a JSON schema that represents the config of the Runnable.

Parameters:

Name Type Description Default
include Sequence[str] | None

A list of fields to include in the config schema.

None

Returns:

Type Description
dict[str, Any]

A JSON schema that represents the config of the Runnable.

Added in version 0.3.0

get_graph

get_graph(config: RunnableConfig | None = None) -> Graph

Return a graph representation of this Runnable.

get_prompts

get_prompts(
    config: RunnableConfig | None = None,
) -> list[BasePromptTemplate]

Return a list of prompts used by this Runnable.

__or__

__or__(
    other: (
        Runnable[Any, Other]
        | Callable[[Iterator[Any]], Iterator[Other]]
        | Callable[
            [AsyncIterator[Any]], AsyncIterator[Other]
        ]
        | Callable[[Any], Other]
        | Mapping[
            str,
            Runnable[Any, Other]
            | Callable[[Any], Other]
            | Any,
        ]
    ),
) -> RunnableSerializable[Input, Other]

Runnable "or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Any, Other] | Callable[[Iterator[Any]], Iterator[Other]] | Callable[[AsyncIterator[Any]], AsyncIterator[Other]] | Callable[[Any], Other] | Mapping[str, Runnable[Any, Other] | Callable[[Any], Other] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

__ror__

__ror__(
    other: (
        Runnable[Other, Any]
        | Callable[[Iterator[Other]], Iterator[Any]]
        | Callable[
            [AsyncIterator[Other]], AsyncIterator[Any]
        ]
        | Callable[[Other], Any]
        | Mapping[
            str,
            Runnable[Other, Any]
            | Callable[[Other], Any]
            | Any,
        ]
    ),
) -> RunnableSerializable[Other, Output]

Runnable "reverse-or" operator.

Compose this Runnable with another object to create a RunnableSequence.

Parameters:

Name Type Description Default
other Runnable[Other, Any] | Callable[[Iterator[Other]], Iterator[Any]] | Callable[[AsyncIterator[Other]], AsyncIterator[Any]] | Callable[[Other], Any] | Mapping[str, Runnable[Other, Any] | Callable[[Other], Any] | Any]

Another Runnable or a Runnable-like object.

required

Returns:

Type Description
RunnableSerializable[Other, Output]

A new Runnable.

pipe

pipe(
    *others: Runnable[Any, Other] | Callable[[Any], Other],
    name: str | None = None
) -> RunnableSerializable[Input, Other]

Pipe runnables.

Compose this Runnable with Runnable-like objects to make a RunnableSequence.

Equivalent to RunnableSequence(self, *others) or self | others[0] | ...

Example
from langchain_core.runnables import RunnableLambda


def add_one(x: int) -> int:
    return x + 1


def mul_two(x: int) -> int:
    return x * 2


runnable_1 = RunnableLambda(add_one)
runnable_2 = RunnableLambda(mul_two)
sequence = runnable_1.pipe(runnable_2)
# Or equivalently:
# sequence = runnable_1 | runnable_2
# sequence = RunnableSequence(first=runnable_1, last=runnable_2)
sequence.invoke(1)
await sequence.ainvoke(1)
# -> 4

sequence.batch([1, 2, 3])
await sequence.abatch([1, 2, 3])
# -> [4, 6, 8]

Parameters:

Name Type Description Default
*others Runnable[Any, Other] | Callable[[Any], Other]

Other Runnable or Runnable-like objects to compose

()
name str | None

An optional name for the resulting RunnableSequence.

None

Returns:

Type Description
RunnableSerializable[Input, Other]

A new Runnable.

pick

pick(
    keys: str | list[str],
) -> RunnableSerializable[Any, Any]

Pick keys from the output dict of this Runnable.

Pick single key:

```python
import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)
chain = RunnableMap(str=as_str, json=as_json)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3]}

json_only_chain = chain.pick("json")
json_only_chain.invoke("[1, 2, 3]")
# -> [1, 2, 3]
```

Pick list of keys:

```python
from typing import Any

import json

from langchain_core.runnables import RunnableLambda, RunnableMap

as_str = RunnableLambda(str)
as_json = RunnableLambda(json.loads)


def as_bytes(x: Any) -> bytes:
    return bytes(x, "utf-8")


chain = RunnableMap(
    str=as_str, json=as_json, bytes=RunnableLambda(as_bytes)
)

chain.invoke("[1, 2, 3]")
# -> {"str": "[1, 2, 3]", "json": [1, 2, 3], "bytes": b"[1, 2, 3]"}

json_and_bytes_chain = chain.pick(["json", "bytes"])
json_and_bytes_chain.invoke("[1, 2, 3]")
# -> {"json": [1, 2, 3], "bytes": b"[1, 2, 3]"}
```

Parameters:

Name Type Description Default
keys str | list[str]

A key or list of keys to pick from the output dict.

required

Returns:

Type Description
RunnableSerializable[Any, Any]

a new Runnable.

assign

assign(
    **kwargs: (
        Runnable[dict[str, Any], Any]
        | Callable[[dict[str, Any]], Any]
        | Mapping[
            str,
            Runnable[dict[str, Any], Any]
            | Callable[[dict[str, Any]], Any],
        ]
    ),
) -> RunnableSerializable[Any, Any]

Assigns new fields to the dict output of this Runnable.

from langchain_community.llms.fake import FakeStreamingListLLM
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import SystemMessagePromptTemplate
from langchain_core.runnables import Runnable
from operator import itemgetter

prompt = (
    SystemMessagePromptTemplate.from_template("You are a nice assistant.")
    + "{question}"
)
llm = FakeStreamingListLLM(responses=["foo-lish"])

chain: Runnable = prompt | llm | {"str": StrOutputParser()}

chain_with_assign = chain.assign(hello=itemgetter("str") | llm)

print(chain_with_assign.input_schema.model_json_schema())
# {'title': 'PromptInput', 'type': 'object', 'properties':
{'question': {'title': 'Question', 'type': 'string'}}}
print(chain_with_assign.output_schema.model_json_schema())
# {'title': 'RunnableSequenceOutput', 'type': 'object', 'properties':
{'str': {'title': 'Str',
'type': 'string'}, 'hello': {'title': 'Hello', 'type': 'string'}}}

Parameters:

Name Type Description Default
**kwargs Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any] | Mapping[str, Runnable[dict[str, Any], Any] | Callable[[dict[str, Any]], Any]]

A mapping of keys to Runnable or Runnable-like objects that will be invoked with the entire output dict of this Runnable.

{}

Returns:

Type Description
RunnableSerializable[Any, Any]

A new Runnable.

batch

batch(
    inputs: list[Input],
    config: (
        RunnableConfig | list[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> list[Output]

Default implementation runs invoke in parallel using a thread pool executor.

The default implementation of batch works well for IO bound runnables.

Subclasses should override this method if they can batch more efficiently; e.g., if the underlying Runnable uses an API which supports a batch mode.

Parameters:

Name Type Description Default
inputs list[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | list[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
list[Output]

A list of outputs from the Runnable.

batch_as_completed

batch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> Iterator[tuple[int, Output | Exception]]

Run invoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
tuple[int, Output | Exception]

Tuples of the index of the input and the output from the Runnable.

abatch async

abatch(
    inputs: list[Input],
    config: (
        RunnableConfig | list[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> list[Output]

Default implementation runs ainvoke in parallel using asyncio.gather.

The default implementation of batch works well for IO bound runnables.

Subclasses should override this method if they can batch more efficiently; e.g., if the underlying Runnable uses an API which supports a batch mode.

Parameters:

Name Type Description Default
inputs list[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | list[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
**kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
list[Output]

A list of outputs from the Runnable.

abatch_as_completed async

abatch_as_completed(
    inputs: Sequence[Input],
    config: (
        RunnableConfig | Sequence[RunnableConfig] | None
    ) = None,
    *,
    return_exceptions: bool = False,
    **kwargs: Any | None
) -> AsyncIterator[tuple[int, Output | Exception]]

Run ainvoke in parallel on a list of inputs.

Yields results as they complete.

Parameters:

Name Type Description Default
inputs Sequence[Input]

A list of inputs to the Runnable.

required
config RunnableConfig | Sequence[RunnableConfig] | None

A config to use when invoking the Runnable. The config supports standard keys like 'tags', 'metadata' for tracing purposes, 'max_concurrency' for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False.

False
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[tuple[int, Output | Exception]]

A tuple of the index of the input and the output from the Runnable.

astream_log async

astream_log(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    diff: bool = True,
    with_streamed_output_list: bool = True,
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

Stream all output from a Runnable, as reported to the callback system.

This includes all inner runs of LLMs, Retrievers, Tools, etc.

Output is streamed as Log objects, which include a list of Jsonpatch ops that describe how the state of the run has changed in each step, and the final state of the run.

The Jsonpatch ops can be applied in order to construct state.

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
diff bool

Whether to yield diffs between each step or the current state.

True
with_streamed_output_list bool

Whether to yield the streamed_output list.

True
include_names Sequence[str] | None

Only include logs with these names.

None
include_types Sequence[str] | None

Only include logs with these types.

None
include_tags Sequence[str] | None

Only include logs with these tags.

None
exclude_names Sequence[str] | None

Exclude logs with these names.

None
exclude_types Sequence[str] | None

Exclude logs with these types.

None
exclude_tags Sequence[str] | None

Exclude logs with these tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[RunLogPatch] | AsyncIterator[RunLog]

A RunLogPatch or RunLog object.

astream_events async

astream_events(
    input: Any,
    config: RunnableConfig | None = None,
    *,
    version: Literal["v1", "v2"] = "v2",
    include_names: Sequence[str] | None = None,
    include_types: Sequence[str] | None = None,
    include_tags: Sequence[str] | None = None,
    exclude_names: Sequence[str] | None = None,
    exclude_types: Sequence[str] | None = None,
    exclude_tags: Sequence[str] | None = None,
    **kwargs: Any
) -> AsyncIterator[StreamEvent]

Generate a stream of events.

Use to create an iterator over StreamEvents that provide real-time information about the progress of the Runnable, including StreamEvents from intermediate results.

A StreamEvent is a dictionary with the following schema:

  • event: str - Event names are of the format: on_[runnable_type]_(start|stream|end).
  • name: str - The name of the Runnable that generated the event.
  • run_id: str - randomly generated ID associated with the given execution of the Runnable that emitted the event. A child Runnable that gets invoked as part of the execution of a parent Runnable is assigned its own unique ID.
  • parent_ids: list[str] - The IDs of the parent runnables that generated the event. The root Runnable will have an empty list. The order of the parent IDs is from the root to the immediate parent. Only available for v2 version of the API. The v1 version of the API will return an empty list.
  • tags: Optional[list[str]] - The tags of the Runnable that generated the event.
  • metadata: Optional[dict[str, Any]] - The metadata of the Runnable that generated the event.
  • data: dict[str, Any]

Below is a table that illustrates some events that might be emitted by various chains. Metadata fields have been omitted from the table for brevity. Chain definitions have been included after the table.

Note

This reference table is for the v2 version of the schema.

+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | event | name | chunk | input | output | +==========================+==================+=====================================+===================================================+=====================================================+ | on_chat_model_start | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_stream | [model name] | AIMessageChunk(content="hello") | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chat_model_end | [model name] | | {"messages": [[SystemMessage, HumanMessage]]} | AIMessageChunk(content="hello world") | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_start | [model name] | | {'input': 'hello'} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_stream | [model name] | 'Hello' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_llm_end | [model name] | | 'Hello human!' | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_start | format_docs | | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_stream | format_docs | 'hello world!, goodbye world!' | | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_chain_end | format_docs | | [Document(...)] | 'hello world!, goodbye world!' | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_start | some_tool | | {"x": 1, "y": "2"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_tool_end | some_tool | | | {"x": 1, "y": "2"} | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_start | [retriever name] | | {"query": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_retriever_end | [retriever name] | | {"query": "hello"} | [Document(...), ..] | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_start | [template_name] | | {"question": "hello"} | | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+ | on_prompt_end | [template_name] | | {"question": "hello"} | ChatPromptValue(messages: [SystemMessage, ...]) | +--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+

In addition to the standard events, users can also dispatch custom events (see example below).

Custom events will be only be surfaced with in the v2 version of the API!

A custom event has following format:

+-----------+------+-----------------------------------------------------------------------------------------------------------+ | Attribute | Type | Description | +===========+======+===========================================================================================================+ | name | str | A user defined name for the event. | +-----------+------+-----------------------------------------------------------------------------------------------------------+ | data | Any | The data associated with the event. This can be anything, though we suggest making it JSON serializable. | +-----------+------+-----------------------------------------------------------------------------------------------------------+

Here are declarations associated with the standard events shown above:

format_docs:

def format_docs(docs: list[Document]) -> str:
    '''Format the docs.'''
    return ", ".join([doc.page_content for doc in docs])


format_docs = RunnableLambda(format_docs)

some_tool:

@tool
def some_tool(x: int, y: str) -> dict:
    '''Some_tool.'''
    return {"x": x, "y": y}

prompt:

template = ChatPromptTemplate.from_messages(
    [
        ("system", "You are Cat Agent 007"),
        ("human", "{question}"),
    ]
).with_config({"run_name": "my_template", "tags": ["my_template"]})
Example:

from langchain_core.runnables import RunnableLambda


async def reverse(s: str) -> str:
    return s[::-1]


chain = RunnableLambda(func=reverse)

events = [event async for event in chain.astream_events("hello", version="v2")]

# will produce the following events (run_id, and parent_ids
# has been omitted for brevity):
[
    {
        "data": {"input": "hello"},
        "event": "on_chain_start",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"chunk": "olleh"},
        "event": "on_chain_stream",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
    {
        "data": {"output": "olleh"},
        "event": "on_chain_end",
        "metadata": {},
        "name": "reverse",
        "tags": [],
    },
]

Example: Dispatch Custom Event

from langchain_core.callbacks.manager import (
    adispatch_custom_event,
)
from langchain_core.runnables import RunnableLambda, RunnableConfig
import asyncio


async def slow_thing(some_input: str, config: RunnableConfig) -> str:
    """Do something that takes a long time."""
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 1 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    await adispatch_custom_event(
        "progress_event",
        {"message": "Finished step 2 of 3"},
        config=config # Must be included for python < 3.10
    )
    await asyncio.sleep(1) # Placeholder for some slow operation
    return "Done"

slow_thing = RunnableLambda(slow_thing)

async for event in slow_thing.astream_events("some_input", version="v2"):
    print(event)

Parameters:

Name Type Description Default
input Any

The input to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable.

None
version Literal['v1', 'v2']

The version of the schema to use either 'v2' or 'v1'. Users should use 'v2'. 'v1' is for backwards compatibility and will be deprecated in 0.4.0. No default will be assigned until the API is stabilized. custom events will only be surfaced in 'v2'.

'v2'
include_names Sequence[str] | None

Only include events from Runnables with matching names.

None
include_types Sequence[str] | None

Only include events from Runnables with matching types.

None
include_tags Sequence[str] | None

Only include events from Runnables with matching tags.

None
exclude_names Sequence[str] | None

Exclude events from Runnables with matching names.

None
exclude_types Sequence[str] | None

Exclude events from Runnables with matching types.

None
exclude_tags Sequence[str] | None

Exclude events from Runnables with matching tags.

None
kwargs Any

Additional keyword arguments to pass to the Runnable. These will be passed to astream_log as this implementation of astream_events is built on top of astream_log.

{}

Yields:

Type Description
AsyncIterator[StreamEvent]

An async stream of StreamEvents.

Raises:

Type Description
NotImplementedError

If the version is not 'v1' or 'v2'.

transform

transform(
    input: Iterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> Iterator[Output]

Transform inputs to outputs.

Default implementation of transform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input Iterator[Input]

An iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
Output

The output of the Runnable.

atransform async

atransform(
    input: AsyncIterator[Input],
    config: RunnableConfig | None = None,
    **kwargs: Any | None
) -> AsyncIterator[Output]

Transform inputs to outputs.

Default implementation of atransform, which buffers input and calls astream.

Subclasses should override this method if they can start producing output while input is still being generated.

Parameters:

Name Type Description Default
input AsyncIterator[Input]

An async iterator of inputs to the Runnable.

required
config RunnableConfig | None

The config to use for the Runnable. Defaults to None.

None
kwargs Any | None

Additional keyword arguments to pass to the Runnable.

{}

Yields:

Type Description
AsyncIterator[Output]

The output of the Runnable.

bind

bind(**kwargs: Any) -> Runnable[Input, Output]

Bind arguments to a Runnable, returning a new Runnable.

Useful when a Runnable in a chain requires an argument that is not in the output of the previous Runnable or included in the user input.

Parameters:

Name Type Description Default
kwargs Any

The arguments to bind to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the arguments bound.

Example
from langchain_ollama import ChatOllama
from langchain_core.output_parsers import StrOutputParser

llm = ChatOllama(model="llama3.1")

# Without bind.
chain = llm | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two three four five.'

# With bind.
chain = llm.bind(stop=["three"]) | StrOutputParser()

chain.invoke("Repeat quoted words exactly: 'One two three four five.'")
# Output is 'One two'

with_config

with_config(
    config: RunnableConfig | None = None, **kwargs: Any
) -> Runnable[Input, Output]

Bind config to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
config RunnableConfig | None

The config to bind to the Runnable.

None
kwargs Any

Additional keyword arguments to pass to the Runnable.

{}

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the config bound.

with_listeners

with_listeners(
    *,
    on_start: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_end: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None,
    on_error: (
        Callable[[Run], None]
        | Callable[[Run, RunnableConfig], None]
        | None
    ) = None
) -> Runnable[Input, Output]

Bind lifecycle listeners to a Runnable, returning a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called before the Runnable starts running, with the Run object. Defaults to None.

None
on_end Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error Callable[[Run], None] | Callable[[Run, RunnableConfig], None] | None

Called if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda
from langchain_core.tracers.schemas import Run

import time


def test_runnable(time_to_sleep: int):
    time.sleep(time_to_sleep)


def fn_start(run_obj: Run):
    print("start_time:", run_obj.start_time)


def fn_end(run_obj: Run):
    print("end_time:", run_obj.end_time)


chain = RunnableLambda(test_runnable).with_listeners(
    on_start=fn_start, on_end=fn_end
)
chain.invoke(2)

with_alisteners

with_alisteners(
    *,
    on_start: AsyncListener | None = None,
    on_end: AsyncListener | None = None,
    on_error: AsyncListener | None = None
) -> Runnable[Input, Output]

Bind async lifecycle listeners to a Runnable.

Returns a new Runnable.

The Run object contains information about the run, including its id, type, input, output, error, start_time, end_time, and any tags or metadata added to the run.

Parameters:

Name Type Description Default
on_start AsyncListener | None

Called asynchronously before the Runnable starts running, with the Run object. Defaults to None.

None
on_end AsyncListener | None

Called asynchronously after the Runnable finishes running, with the Run object. Defaults to None.

None
on_error AsyncListener | None

Called asynchronously if the Runnable throws an error, with the Run object. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the listeners bound.

Example
from langchain_core.runnables import RunnableLambda, Runnable
from datetime import datetime, timezone
import time
import asyncio

def format_t(timestamp: float) -> str:
    return datetime.fromtimestamp(timestamp, tz=timezone.utc).isoformat()

async def test_runnable(time_to_sleep: int):
    print(f"Runnable[{time_to_sleep}s]: starts at {format_t(time.time())}")
    await asyncio.sleep(time_to_sleep)
    print(f"Runnable[{time_to_sleep}s]: ends at {format_t(time.time())}")

async def fn_start(run_obj: Runnable):
    print(f"on start callback starts at {format_t(time.time())}")
    await asyncio.sleep(3)
    print(f"on start callback ends at {format_t(time.time())}")

async def fn_end(run_obj: Runnable):
    print(f"on end callback starts at {format_t(time.time())}")
    await asyncio.sleep(2)
    print(f"on end callback ends at {format_t(time.time())}")

runnable = RunnableLambda(test_runnable).with_alisteners(
    on_start=fn_start,
    on_end=fn_end
)
async def concurrent_runs():
    await asyncio.gather(runnable.ainvoke(2), runnable.ainvoke(3))

asyncio.run(concurrent_runs())
Result:
on start callback starts at 2025-03-01T07:05:22.875378+00:00
on start callback starts at 2025-03-01T07:05:22.875495+00:00
on start callback ends at 2025-03-01T07:05:25.878862+00:00
on start callback ends at 2025-03-01T07:05:25.878947+00:00
Runnable[2s]: starts at 2025-03-01T07:05:25.879392+00:00
Runnable[3s]: starts at 2025-03-01T07:05:25.879804+00:00
Runnable[2s]: ends at 2025-03-01T07:05:27.881998+00:00
on end callback starts at 2025-03-01T07:05:27.882360+00:00
Runnable[3s]: ends at 2025-03-01T07:05:28.881737+00:00
on end callback starts at 2025-03-01T07:05:28.882428+00:00
on end callback ends at 2025-03-01T07:05:29.883893+00:00
on end callback ends at 2025-03-01T07:05:30.884831+00:00

with_types

with_types(
    *,
    input_type: type[Input] | None = None,
    output_type: type[Output] | None = None
) -> Runnable[Input, Output]

Bind input and output types to a Runnable, returning a new Runnable.

Parameters:

Name Type Description Default
input_type type[Input] | None

The input type to bind to the Runnable. Defaults to None.

None
output_type type[Output] | None

The output type to bind to the Runnable. Defaults to None.

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable with the types bound.

with_retry

with_retry(
    *,
    retry_if_exception_type: tuple[
        type[BaseException], ...
    ] = (Exception,),
    wait_exponential_jitter: bool = True,
    exponential_jitter_params: (
        ExponentialJitterParams | None
    ) = None,
    stop_after_attempt: int = 3
) -> Runnable[Input, Output]

Create a new Runnable that retries the original Runnable on exceptions.

Parameters:

Name Type Description Default
retry_if_exception_type tuple[type[BaseException], ...]

A tuple of exception types to retry on. Defaults to (Exception,).

(Exception,)
wait_exponential_jitter bool

Whether to add jitter to the wait time between retries. Defaults to True.

True
stop_after_attempt int

The maximum number of attempts to make before giving up. Defaults to 3.

3
exponential_jitter_params ExponentialJitterParams | None

Parameters for tenacity.wait_exponential_jitter. Namely: initial, max, exp_base, and jitter (all float values).

None

Returns:

Type Description
Runnable[Input, Output]

A new Runnable that retries the original Runnable on exceptions.

Example
from langchain_core.runnables import RunnableLambda

count = 0


def _lambda(x: int) -> None:
    global count
    count = count + 1
    if x == 1:
        raise ValueError("x is 1")
    else:
        pass


runnable = RunnableLambda(_lambda)
try:
    runnable.with_retry(
        stop_after_attempt=2,
        retry_if_exception_type=(ValueError,),
    ).invoke(1)
except ValueError:
    pass

assert count == 2

map

map() -> Runnable[list[Input], list[Output]]

Return a new Runnable that maps a list of inputs to a list of outputs.

Calls invoke with each input.

Returns:

Type Description
Runnable[list[Input], list[Output]]

A new Runnable that maps a list of inputs to a list of outputs.

Example
from langchain_core.runnables import RunnableLambda


def _lambda(x: int) -> int:
    return x + 1


runnable = RunnableLambda(_lambda)
print(runnable.map().invoke([1, 2, 3]))  # [2, 3, 4]

with_fallbacks

with_fallbacks(
    fallbacks: Sequence[Runnable[Input, Output]],
    *,
    exceptions_to_handle: tuple[
        type[BaseException], ...
    ] = (Exception,),
    exception_key: str | None = None
) -> RunnableWithFallbacks[Input, Output]

Add fallbacks to a Runnable, returning a new Runnable.

The new Runnable will try the original Runnable, and then each fallback in order, upon failures.

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle. Defaults to (Exception,).

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input. Defaults to None.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

Example
from typing import Iterator

from langchain_core.runnables import RunnableGenerator


def _generate_immediate_error(input: Iterator) -> Iterator[str]:
    raise ValueError()
    yield ""


def _generate(input: Iterator) -> Iterator[str]:
    yield from "foo bar"


runnable = RunnableGenerator(_generate_immediate_error).with_fallbacks(
    [RunnableGenerator(_generate)]
)
print("".join(runnable.stream({})))  # foo bar

Parameters:

Name Type Description Default
fallbacks Sequence[Runnable[Input, Output]]

A sequence of runnables to try if the original Runnable fails.

required
exceptions_to_handle tuple[type[BaseException], ...]

A tuple of exception types to handle.

(Exception,)
exception_key str | None

If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If None, exceptions will not be passed to fallbacks. If used, the base Runnable and its fallbacks must accept a dictionary as input.

None

Returns:

Type Description
RunnableWithFallbacks[Input, Output]

A new Runnable that will try the original Runnable, and then each

RunnableWithFallbacks[Input, Output]

fallback in order, upon failures.

as_tool

as_tool(
    args_schema: type[BaseModel] | None = None,
    *,
    name: str | None = None,
    description: str | None = None,
    arg_types: dict[str, type] | None = None
) -> BaseTool

Create a BaseTool from a Runnable.

as_tool will instantiate a BaseTool with a name, description, and args_schema from a Runnable. Where possible, schemas are inferred from runnable.get_input_schema. Alternatively (e.g., if the Runnable takes a dict as input and the specific dict keys are not typed), the schema can be specified directly with args_schema. You can also pass arg_types to just specify the required arguments and their types.

Parameters:

Name Type Description Default
args_schema type[BaseModel] | None

The schema for the tool. Defaults to None.

None
name str | None

The name of the tool. Defaults to None.

None
description str | None

The description of the tool. Defaults to None.

None
arg_types dict[str, type] | None

A dictionary of argument names to types. Defaults to None.

None

Returns:

Type Description
BaseTool

A BaseTool instance.

Typed dict input:

from typing_extensions import TypedDict
from langchain_core.runnables import RunnableLambda


class Args(TypedDict):
    a: int
    b: list[int]


def f(x: Args) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool()
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via args_schema:

from typing import Any
from pydantic import BaseModel, Field
from langchain_core.runnables import RunnableLambda

def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))

class FSchema(BaseModel):
    """Apply a function to an integer and list of integers."""

    a: int = Field(..., description="Integer")
    b: list[int] = Field(..., description="List of ints")

runnable = RunnableLambda(f)
as_tool = runnable.as_tool(FSchema)
as_tool.invoke({"a": 3, "b": [1, 2]})

dict input, specifying schema via arg_types:

from typing import Any
from langchain_core.runnables import RunnableLambda


def f(x: dict[str, Any]) -> str:
    return str(x["a"] * max(x["b"]))


runnable = RunnableLambda(f)
as_tool = runnable.as_tool(arg_types={"a": int, "b": list[int]})
as_tool.invoke({"a": 3, "b": [1, 2]})

String input:

from langchain_core.runnables import RunnableLambda


def f(x: str) -> str:
    return x + "a"


def g(x: str) -> str:
    return x + "z"


runnable = RunnableLambda(f) | g
as_tool = runnable.as_tool()
as_tool.invoke("b")

Added in version 0.2.14

__init__

__init__(*args: Any, **kwargs: Any) -> None

is_lc_serializable classmethod

is_lc_serializable() -> bool

Is this class serializable?

By design, even if a class inherits from Serializable, it is not serializable by default. This is to prevent accidental serialization of objects that should not be serialized.

Returns:

Type Description
bool

Whether the class is serializable. Default is False.

get_lc_namespace classmethod

get_lc_namespace() -> list[str]

Get the namespace of the langchain object.

For example, if the class is langchain.llms.openai.OpenAI, then the namespace is ["langchain", "llms", "openai"]

Returns:

Type Description
list[str]

The namespace as a list of strings.

lc_id classmethod

lc_id() -> list[str]

Return a unique identifier for this class for serialization purposes.

The unique identifier is a list of strings that describes the path to the object. For example, for the class langchain.llms.openai.OpenAI, the id is ["langchain", "llms", "openai", "OpenAI"].

to_json

to_json() -> (
    SerializedConstructor | SerializedNotImplemented
)

Serialize the Runnable to JSON.

Returns:

Type Description
SerializedConstructor | SerializedNotImplemented

A JSON-serializable representation of the Runnable.

to_json_not_implemented

to_json_not_implemented() -> SerializedNotImplemented

Serialize a "not implemented" object.

Returns:

Type Description
SerializedNotImplemented

SerializedNotImplemented.

configurable_fields

configurable_fields(
    **kwargs: AnyConfigurableField,
) -> RunnableSerializable[Input, Output]

Configure particular Runnable fields at runtime.

Parameters:

Name Type Description Default
**kwargs AnyConfigurableField

A dictionary of ConfigurableField instances to configure.

{}

Raises:

Type Description
ValueError

If a configuration key is not found in the Runnable.

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the fields configured.

from langchain_core.runnables import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatOpenAI(max_tokens=20).configurable_fields(
    max_tokens=ConfigurableField(
        id="output_token_number",
        name="Max tokens in the output",
        description="The maximum number of tokens in the output",
    )
)

# max_tokens = 20
print("max_tokens_20: ", model.invoke("tell me something about chess").content)

# max_tokens = 200
print(
    "max_tokens_200: ",
    model.with_config(configurable={"output_token_number": 200})
    .invoke("tell me something about chess")
    .content,
)

configurable_alternatives

configurable_alternatives(
    which: ConfigurableField,
    *,
    default_key: str = "default",
    prefix_keys: bool = False,
    **kwargs: (
        Runnable[Input, Output]
        | Callable[[], Runnable[Input, Output]]
    )
) -> RunnableSerializable[Input, Output]

Configure alternatives for Runnables that can be set at runtime.

Parameters:

Name Type Description Default
which ConfigurableField

The ConfigurableField instance that will be used to select the alternative.

required
default_key str

The default key to use if no alternative is selected. Defaults to 'default'.

'default'
prefix_keys bool

Whether to prefix the keys with the ConfigurableField id. Defaults to False.

False
**kwargs Runnable[Input, Output] | Callable[[], Runnable[Input, Output]]

A dictionary of keys to Runnable instances or callables that return Runnable instances.

{}

Returns:

Type Description
RunnableSerializable[Input, Output]

A new Runnable with the alternatives configured.

from langchain_anthropic import ChatAnthropic
from langchain_core.runnables.utils import ConfigurableField
from langchain_openai import ChatOpenAI

model = ChatAnthropic(
    model_name="claude-3-7-sonnet-20250219"
).configurable_alternatives(
    ConfigurableField(id="llm"),
    default_key="anthropic",
    openai=ChatOpenAI(),
)

# uses the default model ChatAnthropic
print(model.invoke("which organization created you?").content)

# uses ChatOpenAI
print(
    model.with_config(configurable={"llm": "openai"})
    .invoke("which organization created you?")
    .content
)

set_verbose

set_verbose(verbose: bool | None) -> bool

If verbose is None, set it.

This allows users to pass in None as verbose to access the global setting.

Parameters:

Name Type Description Default
verbose bool | None

The verbosity setting to use.

required

Returns:

Type Description
bool

The verbosity setting to use.

with_structured_output

with_structured_output(
    schema: Dict | type,
    *,
    include_raw: bool = False,
    **kwargs: Any
) -> Runnable[LanguageModelInput, Dict | BaseModel]

Model wrapper that returns outputs formatted to match the given schema.

Parameters:

Name Type Description Default
schema Dict | type

The output schema. Can be passed in as:

  • an OpenAI function/tool schema,
  • a JSON Schema,
  • a TypedDict class,
  • or a Pydantic class.

If schema is a Pydantic class then the model output will be a Pydantic instance of that class, and the model-generated fields will be validated by the Pydantic class. Otherwise the model output will be a dict and will not be validated. See langchain_core.utils.function_calling.convert_to_openai_tool for more on how to properly specify types and descriptions of schema fields when specifying a Pydantic or TypedDict class.

required
include_raw bool

If False then only the parsed structured output is returned. If an error occurs during model output parsing it will be raised. If True then both the raw model response (a BaseMessage) and the parsed model response will be returned. If an error occurs during output parsing it will be caught and returned as well. The final output is always a dict with keys 'raw', 'parsed', and 'parsing_error'.

False

Raises:

Type Description
ValueError

If there are any unsupported kwargs.

NotImplementedError

If the model does not implement with_structured_output().

Returns:

Type Description
Runnable[LanguageModelInput, Dict | BaseModel]

A Runnable that takes same inputs as a langchain_core.language_models.chat.BaseChatModel.

Runnable[LanguageModelInput, Dict | BaseModel]

If include_raw is False and schema is a Pydantic class, Runnable outputs

Runnable[LanguageModelInput, Dict | BaseModel]

an instance of schema (i.e., a Pydantic object).

Runnable[LanguageModelInput, Dict | BaseModel]

Otherwise, if include_raw is False then Runnable outputs a dict.

Runnable[LanguageModelInput, Dict | BaseModel]

If include_raw is True, then Runnable outputs a dict with keys:

Runnable[LanguageModelInput, Dict | BaseModel]
  • 'raw': BaseMessage
Runnable[LanguageModelInput, Dict | BaseModel]
  • 'parsed': None if there was a parsing error, otherwise the type depends on the schema as described above.
Runnable[LanguageModelInput, Dict | BaseModel]
  • 'parsing_error': Optional[BaseException]
Pydantic schema (include_raw=False):

.. code-block:: python

from pydantic import BaseModel


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


llm = ChatModel(model="model-name", temperature=0)
structured_llm = llm.with_structured_output(AnswerWithJustification)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)

# -> AnswerWithJustification(
#     answer='They weigh the same',
#     justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'
# )
Pydantic schema (include_raw=True):

.. code-block:: python

from pydantic import BaseModel


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


llm = ChatModel(model="model-name", temperature=0)
structured_llm = llm.with_structured_output(
    AnswerWithJustification, include_raw=True
)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)
# -> {
#     'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),
#     'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),
#     'parsing_error': None
# }
Dict schema (include_raw=False):

.. code-block:: python

from pydantic import BaseModel
from langchain_core.utils.function_calling import convert_to_openai_tool


class AnswerWithJustification(BaseModel):
    '''An answer to the user question along with justification for the answer.'''

    answer: str
    justification: str


dict_schema = convert_to_openai_tool(AnswerWithJustification)
llm = ChatModel(model="model-name", temperature=0)
structured_llm = llm.with_structured_output(dict_schema)

structured_llm.invoke(
    "What weighs more a pound of bricks or a pound of feathers"
)
# -> {
#     'answer': 'They weigh the same',
#     'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
# }

Behavior changed in 0.2.26

Added support for TypedDict class.

get_token_ids

get_token_ids(text: str) -> list[int]

Return the ordered ids of the tokens in a text.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
list[int]

A list of ids corresponding to the tokens in the text, in order they occur

list[int]

in the text.

get_num_tokens

get_num_tokens(text: str) -> int

Get the number of tokens present in the text.

Useful for checking if an input fits in a model's context window.

Parameters:

Name Type Description Default
text str

The string input to tokenize.

required

Returns:

Type Description
int

The integer number of tokens in the text.

get_num_tokens_from_messages

get_num_tokens_from_messages(
    messages: list[BaseMessage],
    tools: Sequence | None = None,
) -> int

Get the number of tokens in the messages.

Useful for checking if an input fits in a model's context window.

Note

The base implementation of get_num_tokens_from_messages ignores tool schemas.

Parameters:

Name Type Description Default
messages list[BaseMessage]

The message inputs to tokenize.

required
tools Sequence | None

If provided, sequence of dict, BaseModel, function, or BaseTools to be converted to tool schemas.

None

Returns:

Type Description
int

The sum of the number of tokens across the messages.

generate

generate(
    messages: list[list[BaseMessage]],
    stop: list[str] | None = None,
    callbacks: Callbacks = None,
    *,
    tags: list[str] | None = None,
    metadata: dict[str, Any] | None = None,
    run_name: str | None = None,
    run_id: UUID | None = None,
    **kwargs: Any
) -> LLMResult

Pass a sequence of prompts to the model and return model generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
messages list[list[BaseMessage]]

List of list of messages.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | None

The tags to apply.

None
metadata dict[str, Any] | None

The metadata to apply.

None
run_name str | None

The name of the run.

None
run_id UUID | None

The ID of the run.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input

LLMResult

prompt and additional model provider-specific output.

agenerate async

agenerate(
    messages: list[list[BaseMessage]],
    stop: list[str] | None = None,
    callbacks: Callbacks = None,
    *,
    tags: list[str] | None = None,
    metadata: dict[str, Any] | None = None,
    run_name: str | None = None,
    run_id: UUID | None = None,
    **kwargs: Any
) -> LLMResult

Asynchronously pass a sequence of prompts to a model and return generations.

This method should make use of batched calls for models that expose a batched API.

Use this method when you want to:

  1. Take advantage of batched calls,
  2. Need more output from the model than just the top generated value,
  3. Are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models).

Parameters:

Name Type Description Default
messages list[list[BaseMessage]]

List of list of messages.

required
stop list[str] | None

Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings.

None
callbacks Callbacks

Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation.

None
tags list[str] | None

The tags to apply.

None
metadata dict[str, Any] | None

The metadata to apply.

None
run_name str | None

The name of the run.

None
run_id UUID | None

The ID of the run.

None
**kwargs Any

Arbitrary additional keyword arguments. These are usually passed to the model provider API call.

{}

Returns:

Type Description
LLMResult

An LLMResult, which contains a list of candidate Generations for each input

LLMResult

prompt and additional model provider-specific output.

dict

dict(**kwargs: Any) -> dict

Return a dictionary of the LLM.

bind_tools

bind_tools(
    tools: Sequence[
        Dict[str, Any] | type | Callable | BaseTool
    ],
    *,
    tool_choice: str | None = None,
    **kwargs: Any
) -> Runnable[LanguageModelInput, AIMessage]

Bind tools to the model.

Parameters:

Name Type Description Default
tools Sequence[Dict[str, Any] | type | Callable | BaseTool]

Sequence of tools to bind to the model.

required
tool_choice str | None

The tool to use. If "any" then any tool can be used.

None

Returns:

Type Description
Runnable[LanguageModelInput, AIMessage]

A Runnable that returns a message.

create_structured_runnable

create_structured_runnable(
    function: Union[
        Type[BaseModel], Sequence[Type[BaseModel]]
    ],
    llm: Runnable,
    *,
    prompt: Optional[BasePromptTemplate] = None,
    use_extra_step: bool = False
) -> Runnable

Create a runnable sequence that uses OpenAI functions.

Parameters:

Name Type Description Default
function Union[Type[BaseModel], Sequence[Type[BaseModel]]]

Either a single pydantic.BaseModel class or a sequence of pydantic.BaseModels classes. For best results, pydantic.BaseModels should have descriptions of the parameters.

required
llm Runnable

Language model to use, assumed to support the Google Vertex function-calling API.

required
prompt Optional[BasePromptTemplate]

BasePromptTemplate to pass to the model.

None
use_extra_step bool

whether to make an extra step to parse output into a function

False

Returns:

Type Description
Runnable

A runnable sequence that will pass in the given functions to the model when run.

Example

.. code-block:: python

    from typing import Optional

    from langchain_google_vertexai import ChatVertexAI, create_structured_runnable
    from langchain_core.prompts import ChatPromptTemplate
    from pydantic import BaseModel, Field


    class RecordPerson(BaseModel):
        """Record some identifying information about a person."""

        name: str = Field(..., description="The person's name")
        age: int = Field(..., description="The person's age")
        fav_food: Optional[str] = Field(None, description="The person's favorite food")


    class RecordDog(BaseModel):
        """Record some identifying information about a dog."""

        name: str = Field(..., description="The dog's name")
        color: str = Field(..., description="The dog's color")
        fav_food: Optional[str] = Field(None, description="The dog's favorite food")


    llm = ChatVertexAI(model_name="gemini-pro")
    prompt = ChatPromptTemplate.from_template("""
    You are a world class algorithm for recording entities.
    Make calls to the relevant function to record the entities in the following input: {input}
    Tip: Make sure to answer in the correct format"""
                             )
    chain = create_structured_runnable([RecordPerson, RecordDog], llm, prompt=prompt)
    chain.invoke({"input": "Harry was a chubby brown beagle who loved chicken"})
    # -> RecordDog(name="Harry", color="brown", fav_food="chicken")

get_vertex_maas_model

get_vertex_maas_model(model_name, **kwargs)

Return a corresponding Vertex MaaS instance.

A factory method based on model's name.

create_context_cache

create_context_cache(
    model: ChatVertexAI,
    messages: List[BaseMessage],
    expire_time: Optional[datetime] = None,
    time_to_live: Optional[timedelta] = None,
    tools: Optional[_ToolsType] = None,
    tool_config: Optional[_ToolConfigDict] = None,
) -> str

Creates a cache for content in some model.

Parameters:

Name Type Description Default
model ChatVertexAI

ChatVertexAI model. Must be at least gemini-2.5-pro or gemini-2.0-flash.

required
messages List[BaseMessage]

List of messages to cache.

required
expire_time Optional[datetime]

Timestamp of when this resource is considered expired.

None
time_to_live Optional[timedelta]

The TTL for this resource. If provided, the expiration time is

None
computed

created_time + TTL.

required
tools Optional[_ToolsType]

A list of tool definitions to bind to this chat model. Can be a pydantic model, callable, or BaseTool. Pydantic models, callables, and BaseTools will be automatically converted to their schema dictionary representation.

None
tool_config Optional[_ToolConfigDict]

Optional. Immutable. Tool config. This config is shared for all tools.

None

Raises:

Type Description
ValueError

If model doesn't support context catching.

Returns:

Type Description
str

String with the identificator of the created cache.