APIChain Class — langchain Architecture
Architecture documentation for the APIChain class in base.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 4c838e88_66f9_ad4e_abe5_a1e623b7b6c7["APIChain"] 097a4781_5519_0b5d_6244_98c64eadc0d6["Chain"] 4c838e88_66f9_ad4e_abe5_a1e623b7b6c7 -->|extends| 097a4781_5519_0b5d_6244_98c64eadc0d6 36d37670_e012_f502_4b19_b9823ce99006["base.py"] 4c838e88_66f9_ad4e_abe5_a1e623b7b6c7 -->|defined in| 36d37670_e012_f502_4b19_b9823ce99006 6a781639_503d_3e65_71b0_6368c121e3a6["input_keys()"] 4c838e88_66f9_ad4e_abe5_a1e623b7b6c7 -->|method| 6a781639_503d_3e65_71b0_6368c121e3a6 538dbcbf_eae4_a634_0caa_7467381430fa["output_keys()"] 4c838e88_66f9_ad4e_abe5_a1e623b7b6c7 -->|method| 538dbcbf_eae4_a634_0caa_7467381430fa 89683746_57f3_bd9f_bd34_28ab7062703b["validate_api_request_prompt()"] 4c838e88_66f9_ad4e_abe5_a1e623b7b6c7 -->|method| 89683746_57f3_bd9f_bd34_28ab7062703b d9d8b2a6_d3a3_1e7c_7873_172e347bf89a["validate_limit_to_domains()"] 4c838e88_66f9_ad4e_abe5_a1e623b7b6c7 -->|method| d9d8b2a6_d3a3_1e7c_7873_172e347bf89a 30f8581a_4237_56c0_3da8_ecfbaf619726["validate_api_answer_prompt()"] 4c838e88_66f9_ad4e_abe5_a1e623b7b6c7 -->|method| 30f8581a_4237_56c0_3da8_ecfbaf619726 096043c7_109b_33c8_6174_555916dfa31d["_call()"] 4c838e88_66f9_ad4e_abe5_a1e623b7b6c7 -->|method| 096043c7_109b_33c8_6174_555916dfa31d 5e2c283f_f554_cf10_de83_b02c230485df["_acall()"] 4c838e88_66f9_ad4e_abe5_a1e623b7b6c7 -->|method| 5e2c283f_f554_cf10_de83_b02c230485df f522adc6_2d27_7793_1e1a_f61f9d779a68["from_llm_and_api_docs()"] 4c838e88_66f9_ad4e_abe5_a1e623b7b6c7 -->|method| f522adc6_2d27_7793_1e1a_f61f9d779a68 41e5bb71_4821_21f9_69da_ff2725873641["_chain_type()"] 4c838e88_66f9_ad4e_abe5_a1e623b7b6c7 -->|method| 41e5bb71_4821_21f9_69da_ff2725873641 d49191ac_6f75_4948_5d94_f94fbe4f03de["__init__()"] 4c838e88_66f9_ad4e_abe5_a1e623b7b6c7 -->|method| d49191ac_6f75_4948_5d94_f94fbe4f03de
Relationship Graph
Source Code
libs/langchain/langchain_classic/chains/api/base.py lines 68–386
class APIChain(Chain):
"""Chain that makes API calls and summarizes the responses to answer a question.
**Security Note**: This API chain uses the requests toolkit
to make `GET`, `POST`, `PATCH`, `PUT`, and `DELETE` requests to an API.
Exercise care in who is allowed to use this chain. If exposing
to end users, consider that users will be able to make arbitrary
requests on behalf of the server hosting the code. For example,
users could ask the server to make a request to a private API
that is only accessible from the server.
Control access to who can submit issue requests using this toolkit and
what network access it has.
See https://docs.langchain.com/oss/python/security-policy for more
information.
!!! note
This class is deprecated. See below for a replacement implementation using
LangGraph. The benefits of this implementation are:
- Uses LLM tool calling features to encourage properly-formatted API requests;
- Support for both token-by-token and step-by-step streaming;
- Support for checkpointing and memory of chat history;
- Easier to modify or extend
(e.g., with additional tools, structured responses, etc.)
Install LangGraph with:
```bash
pip install -U langgraph
```
```python
from typing import Annotated, Sequence
from typing_extensions import TypedDict
from langchain_classic.chains.api.prompt import API_URL_PROMPT
from langchain_community.agent_toolkits.openapi.toolkit import RequestsToolkit
from langchain_community.utilities.requests import TextRequestsWrapper
from langchain_core.messages import BaseMessage
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.runnables import RunnableConfig
from langgraph.graph import END, StateGraph
from langgraph.graph.message import add_messages
from langgraph.prebuilt.tool_node import ToolNode
# NOTE: There are inherent risks in giving models discretion
# to execute real-world actions. We must "opt-in" to these
# risks by setting allow_dangerous_request=True to use these tools.
# This can be dangerous for calling unwanted requests. Please make
# sure your custom OpenAPI spec (yaml) is safe and that permissions
# associated with the tools are narrowly-scoped.
ALLOW_DANGEROUS_REQUESTS = True
# Subset of spec for https://jsonplaceholder.typicode.com
api_spec = \"\"\"
openapi: 3.0.0
info:
title: JSONPlaceholder API
version: 1.0.0
servers:
- url: https://jsonplaceholder.typicode.com
paths:
/posts:
get:
summary: Get posts
parameters: &id001
- name: _limit
in: query
required: false
schema:
type: integer
example: 2
description: Limit the number of results
\"\"\"
model = ChatOpenAI(model="gpt-4o-mini", temperature=0)
toolkit = RequestsToolkit(
Extends
Source
Frequently Asked Questions
What is the APIChain class?
APIChain is a class in the langchain codebase, defined in libs/langchain/langchain_classic/chains/api/base.py.
Where is APIChain defined?
APIChain is defined in libs/langchain/langchain_classic/chains/api/base.py at line 68.
What does APIChain extend?
APIChain extends Chain.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free