-
Notifications
You must be signed in to change notification settings - Fork 0
OpenAPI Module
gitpavleenbali edited this page Feb 17, 2026
·
2 revisions
The OpenAPI module enables automatic tool generation from OpenAPI/Swagger specifications, allowing agents to interact with any REST API.
from pyai.openapi import OpenAPIClient, OpenAPIParser
from pyai.openapi.tools import create_tools_from_spec| Component | Description |
|---|---|
| OpenAPIClient | HTTP client for API calls |
| OpenAPIParser | Parse OpenAPI specs |
| create_tools_from_spec | Generate agent tools |
from pyai.openapi import create_tools_from_spec
# Load from URL
tools = create_tools_from_spec("https://api.example.com/openapi.json")
# Load from file
tools = create_tools_from_spec("./api_spec.yaml")
# Use with agent
agent = Agent(
name="APIAgent",
tools=tools
)from pyai.openapi import OpenAPIClient
client = OpenAPIClient("https://api.example.com/openapi.json")
# Call an endpoint
response = client.call("getUsers", limit=10)
# With authentication
client = OpenAPIClient(
"https://api.example.com/openapi.json",
auth_token="Bearer your-token"
)tools = create_tools_from_spec(
"https://petstore.swagger.io/v2/swagger.json"
)
# Generated tools include:
# - getPetById
# - addPet
# - updatePet
# - deletePet
# etc.# Only include specific operations
tools = create_tools_from_spec(
"api_spec.yaml",
include_operations=["getUser", "createUser"]
)
# Exclude certain operations
tools = create_tools_from_spec(
"api_spec.yaml",
exclude_operations=["deleteUser"]
)
# Filter by tags
tools = create_tools_from_spec(
"api_spec.yaml",
include_tags=["users", "products"]
)tools = create_tools_from_spec(
"api_spec.yaml",
auth={"api_key": "your-key"}
)tools = create_tools_from_spec(
"api_spec.yaml",
auth={"bearer_token": "your-jwt-token"}
)tools = create_tools_from_spec(
"api_spec.yaml",
auth={
"oauth2": {
"client_id": "...",
"client_secret": "...",
"token_url": "https://..."
}
}
)from pyai import Agent
from pyai.openapi import create_tools_from_spec
# Create tools from Petstore API
tools = create_tools_from_spec(
"https://petstore.swagger.io/v2/swagger.json"
)
# Create agent with API tools
agent = Agent(
name="PetStoreAgent",
instructions="You help users manage pets in the store.",
tools=tools
)
# Agent can now call API endpoints
result = agent.run("Find all available dogs")tools = create_tools_from_spec(
"api_spec.yaml",
base_url="https://staging.api.example.com"
)tools = create_tools_from_spec(
"api_spec.yaml",
headers={
"X-Custom-Header": "value",
"Accept-Language": "en-US"
}
)- OpenAPIClient - Direct API client
- OpenAPIParser - Spec parsing
Intelligence, Embedded.