@samchon/openapi
This repository has been archived and is no longer maintained.
All functionality has been merged into typia as built-in namespace modules.
flowchart TB
subgraph "OpenAPI Specification"
v20("Swagger v2.0") --upgrades--> emended[["OpenAPI v3.1 (emended)"]]
v30("OpenAPI v3.0") --upgrades--> emended
v31("OpenAPI v3.1") --emends--> emended
end
subgraph "LLM Function Calling"
emended --normalizes--> migration[["Migration Schema"]]
migration --"AI-Ready"--> schema{{"LLM Function Schema"}}
end
Transform OpenAPI documents into LLM function calling applications.
@samchon/openapi converts OpenAPI/Swagger documents into LLM function calling schemas. With full TypeScript type safety, automatic validation, and support for every OpenAPI version, it's the simplest way to make your HTTP backend AI-callable.
Key Features
- 🌐 Multi-Provider Support: Works with OpenAI, Claude, Qwen, Llama, and other LLM providers
- 📝 Complete OpenAPI Coverage: Swagger 2.0, OpenAPI 3.0, and OpenAPI 3.1 fully supported
- 🔒 Type-Safe Validation: Built-in validation with detailed error feedback for LLM responses
- 🔄 MCP Integration: Compose function calling schemas from Model Context Protocol servers
- 📊 Emended Specification: Standardized OpenAPI v3.1 format that removes ambiguities
- ✅ Production Ready: Battle-tested with 98%+ success rates in real-world LLM applications
Live Demo:
https://github.com/user-attachments/assets/e1faf30b-c703-4451-b68b-2e7a8170bce5
Watch how @samchon/openapi powers an AI shopping chatbot with @agentica
Quick Start
npm install @samchon/openapi
Transform your OpenAPI document into an LLM function calling application:
import { HttpLlm, OpenApi } from "@samchon/openapi";
// 1. Load and convert your OpenAPI document
const document: OpenApi.IDocument = OpenApi.convert(swagger);
// 2. Generate LLM function calling schemas
const application: IHttpLlmApplication = HttpLlm.application({
document,
});
// 3. Find a function to call
const func: IHttpLlmFunction | undefined = application.functions.find(
(f) => f.path === "/bbs/articles" && f.method === "post"
);
// 4. Use with any LLM provider (OpenAI, Claude, Qwen, etc.)
const completion = await llm.chat.completions.create({
model: "gpt-4o", // or claude-3-5-sonnet, qwen-plus, etc.
messages: [...],
tools: [{
type: "function",
function: {
name: func.name,
description: func.description,
parameters: func.parameters,
}
}],
});
// 5. Execute with validation
const result = await HttpLlm.execute({
connection: { host: "http://localhost:3000" },
application,
function: func,
input: llmGeneratedArgs,
});
That's it! Your HTTP backend is now AI-callable across all major LLM providers.
OpenAPI Definitions
@samchon/openapi provides complete TypeScript definitions for all OpenAPI versions and introduces an "emended" OpenAPI v3.1 specification that serves as an intermediate format.
flowchart TB
v20(Swagger v2.0) --upgrades--> emended[["<b><u>OpenAPI v3.1 (emended)</u></b>"]]
v30(OpenAPI v3.0) --upgrades--> emended
v31(OpenAPI v3.1) --emends--> emended
emended --downgrades--> v20d(Swagger v2.0)
emended --downgrades--> v30d(OpenAPI v3.0)
Supported Specifications:
What is "Emended" OpenAPI?
The emended specification removes ambiguities and duplications from OpenAPI v3.1, creating a cleaner, more consistent format. All conversions flow through this intermediate format.
Key Improvements:
- Operations: Merges parameters from path and operation levels, resolves all references
- JSON Schema: Eliminates mixed types, unifies nullable handling, standardizes array/tuple representations
- Schema Composition: Consolidates
anyOf, oneOf, allOf patterns into simpler structures
Converting Between Versions
import { OpenApi } from "@samchon/openapi";
// Convert any version to emended format
const emended: OpenApi.IDocument = OpenApi.convert(swagger); // Swagger 2.0/3.0/3.1
// Downgrade to older versions if needed
const v30: OpenApiV3.IDocument = OpenApi.downgrade(emended, "3.0");
const v20: SwaggerV2.IDocument = OpenApi.downgrade(emended, "2.0");
Validating OpenAPI Documents
Use typia for runtime validation with detailed type checking:
import { OpenApi, OpenApiV3, OpenApiV3_1, SwaggerV2 } from "@samchon/openapi";
import typia from "typia";
const document: any = await fetch("swagger.json").then(r => r.json());
// Validate with detailed error messages
const result = typia.validate<
SwaggerV2.IDocument | OpenApiV3.IDocument | OpenApiV3_1.IDocument
>(document);
if (result.success) {
const emended: OpenApi.IDocument = OpenApi.convert(result.data);
} else {
console.error(result.errors); // Detailed validation errors
}
Try it: Type assertion | Detailed validation
LLM Function Calling
Turn your HTTP backend into an AI-callable service. @samchon/openapi converts your OpenAPI document into function calling schemas that work with OpenAI GPT, Claude, Qwen, Llama, and other LLM providers.
Type Definitions:
Complete Example
Here's a full example showing LLM function calling with OpenAI (works identically with Claude, Qwen, etc.):
import { HttpLlm, OpenApi, IHttpLlmApplication, IHttpLlmFunction } from "@samchon/openapi";
import OpenAI from "openai";
// 1. Convert OpenAPI to LLM function calling application
const document: OpenApi.IDocument = OpenApi.convert(swagger);
const application: IHttpLlmApplication = HttpLlm.application({
document,
});
// 2. Find the function by path and method
const func: IHttpLlmFunction | undefined = application.functions.find(
(f) => f.path === "/shoppings/sellers/sale" && f.method === "post"
);
if (!func) throw new Error("Function not found");
// 3. Let OpenAI GPT call the function
const client: OpenAI = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const completion: OpenAI.ChatCompletion = await client.chat.completions.create({
model: "gpt-4o",
messages: [
{ role: "system", content: "You are a helpful shopping assistant." },
{ role: "user", content: "I want to sell Microsoft Surface Pro 9..." }
],
tools: [{
type: "function",
function: {
name: func.name,
description: func.description,
parameters: func.parameters,
}
}],
});
// 4. Execute the function call on your actual server
const toolCall = completion.choices[0].message.tool_calls![0];
const result = await HttpLlm.execute({
connection: { host: "http://localhost:37001" },
application,
function: func,
input: JSON.parse(toolCall.function.arguments),
});
Works with Any LLM Provider:
// OpenAI
const openai = new OpenAI({ apiKey: "..." });
// Anthropic Claude
const anthropic = new Anthropic({ apiKey: "..." });
// Alibaba Qwen via DashScope
const qwen = new OpenAI({
apiKey: "...",
baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1",
});
// All use the same func.parameters schema
Validation Feedback - Fixing LLM Mistakes
The Problem: LLMs make type errors. A lot.
Even when your schema says Array<string>, GPT might return just "string". In real-world testing with OpenAI GPT-4o-mini on a shopping service:
- 1st attempt: 70% success rate ❌
- 2nd attempt (with validation feedback): 98% success rate ✅
- 3rd attempt: Never failed ✅
The Solution: Validate LLM output and send errors back for correction.
import { HttpLlm, IHttpLlmFunction, IValidation } from "@samchon/openapi";
const func: IHttpLlmFunction = application.functions[0];
// Validate LLM-generated arguments
const result: IValidation<unknown> = func.validate(llmArguments);
if (result.success === false) {
// Send detailed error feedback to LLM
return await retryWithFeedback({
message: "Type errors detected. Please correct the arguments.",
errors: result.errors, // Detailed error information
});
} else {
// Execute the validated function
const output = await HttpLlm.execute({
connection: { host: "http://localhost:3000" },
application,
function: func,
input: result.data,
});
return output;
}
The validation uses typia.validate<T>(), which provides the most accurate validation and extremely detailed error messages compared to other validators:
| Components |
typia |
TypeBox |
ajv |
io-ts |
zod |
C.V. |
| Easy to use |
✅ |
❌ |
❌ |
❌ |
❌ |
❌ |