Implement Open API Specification using AI

Implement Open API Specification using AI

Open API is a standard for documenting APIs. Its most famous implementation for .Net is probably Swagger. Software developers have been using Open API for more than a decade, and it's really good at describing what an API does. There are tons of scaffolders built on top of it allowing us to automatically generate code, such as ReactJS frontends, C# middleware, etc. And at least for .Net, it is capable of generating such specifications automatically by using reflection.

Such Open API specifications also happens to be incredibly useful when dealing with generative AI, and specifically code generators. This is because it eliminates all ambiguity, resulting in a razor sharp "specification" being sent to the LLM. In the video below I'm demonstrating how to use our Hyperlambda Generator to generate code based upon Open API specs.

Most modern APIs have such Open API specs, including Magic. In magic, you can actually generate such specifications automatically by clicking a button. Below is an example from an HTTP POST API function that generates Hyperlambda code.

{
  "openapi": "3.1.0",
  "info": {
    "title": "AINIRO.IO Magic Cloud Cloudlet",
    "description": "Interact with your cloudlet",
    "version": "v20.0.3"
  },
  "servers": [
    {
      "url": "https://app.ainiro.io"
    }
  ],
  "paths": {
    "/magic/modules/vibe-coding/endpoints/hyperlambda/generate-hyperlambda": {
      "post": {
        "operationId": "post_hyperlambda_generate-hyperlambda",
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ],
        "description": null,
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "prompt": {
                    "type": "string",
                    "description": "Mandatory argument describing the Hyperlambda code you want to generate."
                  },
                  "data": {
                    "type": "string",
                    "description": "Optional argument being code you want the AI function to change."
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Above you can see how it documents the exact URL, input arguments, with natural language descriptions, etc. The above was automatically generated by selecting a single HTTP endpoint in Magic, and click the "Open API Spec" button. Below is a screenshot.

Open API specif

Open API + AI == Luv!

When generating code using AI, you want to eliminate ambiguity. With an Open API specification, you're basically "hand holding" the LLM, resulting in eliminating any confusion that might result in errors. Due to Open API's popularity, this implies that you reduce the risk of hallucinations significantly by using such "strongly typed" specifications. Basically, you reduce the risk for hallucinations and bugs, because the LLM knows the specification very well, in addition to that there's no more ambiguity left.

Generate your own Open API Spec

Interestingly, and I've tried this several times, you can ask the LLM to automatically generate Open API specifications by pointing it to some documentation website that explains an API. The specification below was automatically generated by scraping HubSpot's documentation, and asking the LLM to generate an Open API specification based upon it.

Open API specification for creating a contact in HubSpot

This allows you to "hand hold" the LLM during its generative process, by providing it with a well known structure it can leverage during its process. In addition to that such snippets can also be used as "context" for the LLM during prompting.

The end result becoming high quality middleware server-side code, allowing you to wrap APIs as you see fit, with significantly fewer bugs than whatever you could have prompted using exclusively natural language. Just because we've got AI today to generate our code, doesn't imply we should "forget" all other innovation that we've been implementing over the last 50+ years as software developers. Very often, the best AI is born in the intersection between old ideas and new LLMs.

Have a Custom AI Solution

At AINIRO we specialise in delivering custom AI solutions and AI chatbots with AI agent features. If you want to talk to us about how we can help you implement your next custom AI solution, you can reach out to us below.

Thomas Hansen

Thomas Hansen I am the CEO and Founder of AINIRO.IO, Ltd. I am a software developer with more than 25 years of experience. I write about Machine Learning, AI, and how to help organizations adopt said technologies. You can follow me on LinkedIn if you want to read more of what I write.

Published 20. May 2025

The Inevitable Collapse of AI Code Generators

AI code generators are becoming increasingly good at what they do. However, according to Facebook we've reached a 'wall'. The question is 'How do we pass the wall?'

Read More

AI Agents Creating AI Agents

I spent a couple of hours today creating an AI Agent that creates AI Agents, and you can see the results here.

Read More

Vibe Coding Saturday

I got bored, so I created a TODO AI Agent without coding, exclusively using our AI code generator to 'vibe code'.

Read More