Aerostack
memory

ai-output-validate Edge Function — Ai

AI

Validates and optionally repairs LLM output against a schema type (JSON, list, boolean, number, email, URL) without calling any external API.

navin @navin verified
Updated Mar 12, 2026
GitHub

Edge function ai-output-validate Validates and optionally repairs LLM output against a schema type (JSON, list, boolean, number, email, URL) without calling any external API.. Deployed on Cloudflare Workers — zero cold starts, globally distributed. Mount it via your Aerostack workspace to call it from any AI agent.

terminal — aerostack cli
$ npx aerostack add navin/ai-output-validate
smart_toy

Use with AI Assistants

MCP

Connect Claude, Cursor, or any MCP-compatible client — then call this function by slug

① Add MCP Server

Add this once — access all Aerostack functions from your AI tool.

claude_desktop_config.json
{
  "mcpServers": {
    "aerostack": {
      "url": "https://mcp.aerostack.dev",
      "type": "http"
    }
  }
}

② Call this function

Ask your AI to use the call_function tool with this slug:

call_function
call_function({
  slug: "ai-output-validate",
  args: {
    "output": "example_output",
    "schema": null,
    "repair": false
  }
})

ai-output-validate — Validate and repair LLM output format

Checks that an LLM's raw text output conforms to an expected schema type (JSON, list, boolean, number, email, URL), with optional auto-repair for common formatting mistakes.


API

POST /api/ai-output-validate

Request body

Field Type Required Default Description
output string Raw text output from the LLM
schema object Validation schema (see below)
repair boolean false Attempt to auto-fix minor formatting issues

Schema object

Field Type Description
type "json" | "list" | "boolean" | "number" | "email" | "url" Expected output type
required string[] (JSON only) Required keys in the parsed object
minItems number (list only) Minimum number of items
maxItems number (list only) Maximum number of items

Success response (200)

{
  "success": true,
  "data": {
    "valid": true,
    "parsed": {"name": "Alice", "age": 30},
    "errors": [],
    "repaired": false
  }
}

Error responses

Code HTTP When
INVALID_INPUT 400 Missing required field
INTERNAL_ERROR 500 Unknown schema type

Usage

cURL
curl -X POST "$FUNCTION_URL" \
  -H "Content-Type: application/json" \
  -d '{
    "output": "```json\n{\"name\": \"Alice\"}\n```",
    "schema": {"type": "json", "required": ["name"]},
    "repair": true
  }'
TypeScript / JavaScript (HTTP)
const response = await fetch(FUNCTION_URL, {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    output: llmResponse,
    schema: { type: 'json', required: ['title', 'summary'] },
    repair: true,
  }),
});
const { data } = await response.json();
if (data.valid) console.log(data.parsed);
Direct import (Node / Bun / Deno)
import { aiOutputValidate } from '@aerostack/functions/ai-output-validate';

const { valid, parsed, errors } = aiOutputValidate({
  output: rawLlmText,
  schema: { type: 'boolean' },
  repair: true,
});

Use Cases

  • Validating that an LLM's structured output is parseable JSON before inserting it into a database.
  • Checking that a classification output is a valid boolean (yes/no) before branching workflow logic.
  • Extracting and verifying a URL or email from a generated text response.

Notes

  • repair: true for JSON tries to extract content from markdown code fences (```json ... ```).
  • repair: true for boolean maps common words (yes/no/yep/nope/correct/incorrect) to true/false.
  • List detection: newline-separated lists take priority over comma-separated.
  • repaired: true in the response means the output was modified before parsing.

Metadata

upgrade Version 1.0.0
gavel License MIT
language Language typescript
cloud Provider aerostack

Tags

ai
deployed_code

Build and publish your own functions

Write a TypeScript function, deploy it to the edge, and share it with thousands of developers — in minutes.

More AI Functions

Browse AI Functions →

Frequently asked questions

What does the ai-output-validate function do? +

ai-output-validate is a serverless edge function for ai automation written in aerostack. Deploy it to Cloudflare Workers via your Aerostack workspace.

How do I deploy the ai-output-validate function? +

Install the Aerostack CLI and run: ```bash aerostack deploy function @navin/ai-output-validate ``` It will be live on Cloudflare Workers in seconds.

What runtime does ai-output-validate use? +

ai-output-validate runs on aerostack on the Cloudflare Workers edge runtime — zero cold starts, globally distributed.

Can I customise the ai-output-validate function? +

Yes. Fork the function from your Aerostack dashboard, modify the source, and redeploy. All changes are version-controlled.