ai-output-validate Edge Function — Ai
AIValidates and optionally repairs LLM output against a schema type (JSON, list, boolean, number, email, URL) without calling any external API.
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.
npx aerostack add navin/ai-output-validate Use with AI Assistants
MCPConnect 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.
{
"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({
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: truefor JSON tries to extract content from markdown code fences (```json ... ```).repair: truefor boolean maps common words (yes/no/yep/nope/correct/incorrect) totrue/false.- List detection: newline-separated lists take priority over comma-separated.
repaired: truein the response means the output was modified before parsing.
Metadata
Tags
Publisher
@navin verified
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 →ai-chunk-overlap
by @navin
Splits text into overlapping chunks with configurable size, overlap, and boundary snapping (char, word, sentence) for RAG and embedding pipelines.
ai-context-window-fit
by @navin
Trims a conversation message array to fit within a model's context window using configurable strategies, without making any API calls.
ai-cost-estimate
by @navin
Calculates the API cost for an LLM request given a model name, prompt token count, and completion token count, supporting multiple currencies.
ai-extract-keywords
by @navin
Extracts the top N keywords from text using TF-IDF inspired scoring with built-in English stopword filtering, no external API calls required.
ai-guardrail-injection-detect
by @navin
Scores text for common prompt injection attack patterns including role overrides, instruction leaking, and jailbreak attempts.
ai-language-detect
by @navin
Detects the natural language of a text string using character trigram frequency analysis, supporting 13 languages with no external API calls.
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.