-
Notifications
You must be signed in to change notification settings - Fork 0
OpenAPIParser
gitpavleenbali edited this page Feb 17, 2026
·
2 revisions
The OpenAPIParser class parses OpenAPI/Swagger specifications and extracts operation details.
from pyai.openapi import OpenAPIParserOpenAPIParser(
spec: str | dict, # Spec URL, path, or dict
validate: bool = True # Validate spec structure
)# From URL
parser = OpenAPIParser("https://api.example.com/openapi.json")
# From file
parser = OpenAPIParser("./specs/api.yaml")
# From dict
parser = OpenAPIParser({
"openapi": "3.0.0",
"info": {"title": "My API", "version": "1.0"},
"paths": {...}
})operations = parser.get_operations()
for op in operations:
print(f"ID: {op.operation_id}")
print(f"Method: {op.method}")
print(f"Path: {op.path}")
print(f"Summary: {op.summary}")
print(f"Parameters: {op.parameters}")| Property | Type | Description |
|---|---|---|
title |
str | API title |
version |
str | API version |
description |
str | API description |
base_url |
str | Base URL |
servers |
list | Server URLs |
operations |
list | All operations |
Get all operations:
operations = parser.get_operations()Get specific operation:
op = parser.get_operation("getUserById")
print(op.parameters)
print(op.request_body)
print(op.responses)Get defined schemas:
schemas = parser.get_schemas()
for name, schema in schemas.items():
print(f"Schema: {name}")
print(f"Properties: {schema['properties']}")Get security definitions:
security = parser.get_security_schemes()
# {'api_key': {'type': 'apiKey', ...}, ...}Convert to agent tools:
tools = parser.to_tools()
# With filtering
tools = parser.to_tools(
include_operations=["getUser", "createUser"],
exclude_tags=["admin"]
)Each operation has:
op.operation_id # Unique identifier
op.method # HTTP method (get, post, etc.)
op.path # URL path
op.summary # Short description
op.description # Full description
op.parameters # List of parameters
op.request_body # Request body schema
op.responses # Response schemas
op.tags # Tags for categorization
op.security # Security requirementsfor param in op.parameters:
print(f"Name: {param['name']}")
print(f"In: {param['in']}") # path, query, header
print(f"Required: {param['required']}")
print(f"Type: {param['schema']['type']}")if op.request_body:
content_type = list(op.request_body['content'].keys())[0]
schema = op.request_body['content'][content_type]['schema']
print(f"Body schema: {schema}")from pyai.openapi import OpenAPIParser, SpecValidationError
try:
parser = OpenAPIParser("invalid_spec.yaml", validate=True)
except SpecValidationError as e:
print(f"Invalid spec: {e.errors}")| Version | Support |
|---|---|
| OpenAPI 3.0 | β Full |
| OpenAPI 3.1 | β Full |
| Swagger 2.0 | β Converted |
- OpenAPI-Module - Module overview
- OpenAPIClient - API client
Intelligence, Embedded.