Functional API

Open in ColabOpen in GitHub

Overview

This tutorial covers LangGraph's Functional API, focusing on workflow automation with @entrypoint and @task decorators.

Key features include state management, parallel processing, and human-in-the-loop capabilities.

Table of Contents

References


Environment Setup

Setting up your environment is the first step. See the Environment Setup guide for more details.

[Note]

The langchain-opentutorial is a package of easy-to-use environment setup guidance, useful functions and utilities for tutorials. Check out the langchain-opentutorial for more details.

You can set API keys in a .env file or set them manually.

[Note] If you’re not using the .env file, no worries! Just enter the keys directly in the cell below, and you’re good to go.

Functional API

The Functional API is a programming interface provided by LangGraph that extends existing Python functions with advanced features such as state management, parallel processing, and memory management, all while requiring minimal code modifications.

Core Components

The Functional API uses two primitives to define workflows:

  1. @entrypoint Decorator

  • Defines the entry point of a workflow

  • Automates state management and checkpointing

  • Manages streaming and interruption points

  1. @task Decorator

  • Defines units of work that can be executed asynchronously

  • Handles retry policies and error handling

  • Supports parallel processing

Use Cases

Asynchronous and Parallel Processing

Long-running tasks can significantly impact application performance.

The Functional API allows you to execute tasks asynchronously and in parallel, improving efficiency especially for I/O-bound operations like LLMs API calls.

The @task decorator makes it easy to convert regular functions into asynchronous tasks.

Interrupts and Human Intervention

Some workflows require human oversight or intervention at critical points.

The Functional API provides built-in support for human-in-the-loop processes through its interrupt mechanism.

This allows you to pause execution, get human input, and continue processing based on that input.

Automated State Management

The Functional API automatically handles state persistence and restoration between function calls.

This is particularly useful in conversational applications where maintaining context is crucial.

You can focus on your business logic while LangGraph handles the complexities of state management.

Last updated