RunnableRetry

Open in ColabOpen in GitHub

Overview

This tutorial covers how to use RunnableRetry to handle retry logic effectively in LangChain workflows. We'll demonstrate how to configure and use RunnableRetry with examples that showcase custom retry policies to make your workflow resilient to failures.

Table of Contents

References


Environment Setup

Set up the environment. You may refer to Environment Setup for more details.

[Note]

  • langchain-opentutorial is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.

  • You can checkout the langchain-opentutorial for more details.

You can alternatively set API keys such as OPENAI_API_KEY in a .env file and load them.

[Note] This is not necessary if you've already set the required API keys in previous steps.

What is RunnableRetry

RunnableRetry is a utility provided by LangChain that allows you to add retry mechanisms for individual Runnable objects. Instead of wrapping your entire workflow in retry logic, you can apply retry policies at the level of specific tasks. This helps you handle transient issues, such as network errors or intermittent failures, without restarting the entire workflow.

Why use RunnableRetry

By using RunnableRetry, you can:

  • Avoid wrapping the entire workflow with retry logic: Instead of restarting the entire process during frequent network calls or API failures, you can retry individual Runnable units.

  • Implement retries per task: This enables more efficient task recovery and makes workflows more robust.

  • Flexible implementation: You can implement retries using .with_retry() or define a custom retry strategy by creating a RunnableRetry with specific events, such as exception types and exponential backoff.

Base RunnableRetry Example

Below is a simple example to demonstrate the effectiveness of RunnableRetry. In this example, we simulate a task with a chance of failure and use RunnableRetry to automatically retry it up to a maximum attempts.

or you can simply implemented with .with_retry() method.

RunnableRetry Bind with Chains

In this example, we’ll take it a step further and demonstrate how to construct a Chain using ChatOpenAI. The example will show not just the basic chain setup but also how to enhance it by incorporating RunnableRetry for robust error handling and PydanticOutputParser for structured output validation.

Components Used:

  • RunnableRetry: Automatically retries failed tasks to handle transient issues, such as API call failures or timeouts.

  • PydanticOutputParser: Ensures the output is parsed and validated against a defined schema, making the workflow more reliable and predictable.

with PydanticOutputParser, check our another tutorial here.

or you can use .with_retry() with Runnables.

Combining RunnableRetry with .with_structured_output()

Methods like .bind() or .with_retry() create a new Runnable object, making the original instance's chainable methods (e.g., .with_structured_output()) unavailable.

To retain structured output functionality:

  1. Apply .with_structured_output() first to include parsing logic in the LLM instance.

  2. Then wrap it with RunnableRetry or .with_retry() to add retry or chaining logic.

Why this order is important

  • Loss of Methods: The new Runnable created by RunnableRetry or .with_retry() doesn't have the original methods.

  • Ensure Structured Output: Adding .with_structured_output() before retry logic ensures structured parsing stays intact while allowing retries.

or you can using with_structured_output() method with with_retry() method like this.

Note: If the model doesn’t support .with_structured_output() or you want to use a custom parsing method, refer to the LangChain documentation on advanced structured output for more details.

Last updated