Integrate APIs with OpenAPI Specs
LlamaIndex tool that loads an OpenAPI spec so an agent can explore its endpoints and parameters.
Why it matters
Seamlessly integrate your AI agent with any REST API by leveraging its OpenAPI specification. This tool allows agents to understand API endpoints, parameters, and make requests, enabling automated interactions with external services.
Outcomes
What it gets done
Load and parse OpenAPI specifications.
Enable agents to discover API endpoints and their details.
Allow agents to execute REST requests to specified endpoints.
Facilitate programmatic interaction with external APIs.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/li-tool-tools-openapi | bash Overview
OpenAPI Tool
The OpenAPI Tool loads an OpenAPI spec into a LlamaIndex agent so it can read endpoints, parameters, and the base URL, and can optionally call those endpoints when paired with the companion RequestsToolSpec. Use it when an agent needs to understand an API's shape from its OpenAPI spec. On its own it cannot call the API -- that needs RequestsToolSpec added too.
What it does
The OpenAPI Tool loads an OpenAPI spec and lets an agent retrieve its endpoints and endpoint details. It exposes load_openapi_spec, which returns the parsed OpenAPI spec the class was initialized with. Pairing it with the companion RequestsToolSpec lets the agent actually hit the endpoints it discovers with a REST request, rather than just reading about them.
When to use - and when NOT to
Use it when an agent needs to explore or reason about an API's shape -- endpoints, parameters, base URL -- from its OpenAPI spec, optionally following up with real requests via RequestsToolSpec. On its own it only reads the spec; without adding RequestsToolSpec to the agent's tools, it cannot actually call the API.
Inputs and outputs
Install with:
pip install llama-index-tools-openapi
Initialize OpenAPIToolSpec either with a URL to the spec, or with an already-parsed spec dict:
from llama_index.tools.openapi import OpenAPIToolSpec
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI
f = requests.get(
"https://raw.githubusercontent.com/APIs-guru/openapi-directory/main/APIs/openai.com/1.2.0/openapi.yaml"
).text
open_api_spec = yaml.safe_load(f)
### OR
open_spec = OpenAPIToolSpec(
url="https://raw.githubusercontent.com/APIs-guru/openapi-directory/main/APIs/openai.com/1.2.0/openapi.yaml"
)
tool_spec = OpenAPIToolSpec(open_api_spec)
agent = FunctionAgent(
tools=tool_spec.to_tool_list(),
llm=OpenAI(model="gpt-4.1"),
)
print(await agent.run("What is the base url for the API"))
print(await agent.run("What parameters does the x endpoint need?"))
Who it's for
Developers building agents that need to understand and navigate an API's OpenAPI spec -- and optionally call its endpoints -- without manually reading API documentation.
Source README
OpenAPI Tool
pip install llama-index-tools-openapi
This tool loads an OpenAPI spec and allow the Agent to retrieve endpoints and details about endpoints. The RequestsToolSpec can also be loaded into the agent to allow the agent to hit the necessary endpoints with a REST request.
Usage
This tool has more extensive example usage documented in a Jupyter notebook here
Here's an example usage of the OpenAPIToolSpec.
from llama_index.tools.openapi import OpenAPIToolSpec
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI
f = requests.get(
"https://raw.githubusercontent.com/APIs-guru/openapi-directory/main/APIs/openai.com/1.2.0/openapi.yaml"
).text
open_api_spec = yaml.safe_load(f)
### OR
open_spec = OpenAPIToolSpec(
url="https://raw.githubusercontent.com/APIs-guru/openapi-directory/main/APIs/openai.com/1.2.0/openapi.yaml"
)
tool_spec = OpenAPIToolSpec(open_api_spec)
agent = FunctionAgent(
tools=tool_spec.to_tool_list(),
llm=OpenAI(model="gpt-4.1"),
)
print(await agent.run("What is the base url for the API"))
print(await agent.run("What parameters does the x endpoint need?"))
load_openapi_spec: Returns the parsed OpenAPI spec that the class was initialized with
This loader is designed to be used as a way to load data as a Tool in a Agent.
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.