3. OpenAPI Integration
Automatically generate tools from OpenAPI specifications
Overview
Liman can automatically generate tools from OpenAPI specifications. This means your existing REST APIs become agent tools without writing custom code.
This builds on the Adding Custom Tools guide.
Step 1: Create a Test API
First, install FastAPI:
uv add "fastapi[standard]"
Create server.py
with a simple user management API:
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
app = FastAPI(title="User Management API", version="1.0.0")
class User(BaseModel):
id: str
name: str
email: str
# Mock data
users = {
"1": {"id": "1", "name": "Alice", "email": "alice@example.com"},
"2": {"id": "2", "name": "Bob", "email": "bob@example.com"},
}
@app.get("/users/{user_name}", response_model=User, operation_id="get_user")
def get_user(user_name: str):
"""Get a user by their name"""
for user in users.values():
if user["name"].lower() == user_name.lower():
return User.model_validate(user)
raise HTTPException(status_code=404, detail=f"User {user_name} not found")
Step 2: Start Your API
Start the server:
fastapi run server.py
Visit http://localhost:8000/docs to see the auto-generated OpenAPI spec.
Step 3: Install liman-openapi
Package
uv add liman-openapi
Step 4: Create ToolNodes from OpenAPI
Update your main.py
:
from liman.agent import Agent
from liman_openapi import create_tool_nodes, load_openapi
...
async def main():
agent = Agent("./specs", start_node="LLMNode/chat", llm=get_llm())
openapi = load_openapi("http://localhost:8000/openapi.json")
create_tool_nodes(openapi, agent.registry, base_url="http://localhost:8000")
...
if __name__ == "__main__":
asyncio.run(main())
Step 5: Add Tool to LLMNode
Update specs/chat.yaml
:
kind: LLMNode
name: chat
prompts:
system:
en: |
You are a helpful assistant.
Always be polite and provide clear answers.
tools:
- get_user_by_name
- OpenAPI__get_user
Step 6: Test the Integration
- Start the API server:
fastapi run server.py
(we already did this above) - Run the agent:
bash python main.py # for detailed output, you can enable debug mode: # LIMAN_DEBUG=1 python main.py
Try these examples:
You: Who is Alice?
Agent: Alice is a user with the email address alice@example.com.
You: What emails do Bob and Pavel have?
Agent: Bob's email is bob@example.com. However, it seems there is no user named Pavel found in the system.
It's the same as in the previous example, but now we're calling your server!
How Auto-Generation Works
When you call:
openapi = load_openapi("http://localhost:8000/openapi.json")
create_tool_nodes(openapi, agent.registry, base_url="http://localhost:8000")
Liman:
- Fetches OpenAPI spec from your running server
- Generates ToolNode definitions for each endpoint
- Creates HTTP request functions automatically
- Registers tools with names like
OpenAPI__get_user
Your OpenAPI endpoint becomes a callable tool without any manual work!
Next Steps
Try the python/samples/simple_openapi sample to see this integration in action with more complex scenarios.
Last updated on