Switches
Use a switch when a generated value selects one of several sets of output rules. It expresses an ordered list of cases without nesting several if / then / else blocks.
Switches use x-runtime.switch, a Struct extension to JSON Schema. The first matching case applies; if none matches, the explicit default applies.
Example: route an incident
Struct classifies an incident's severity. The switch determines the accompanying action: page the on-call team for a critical incident, open a ticket for a warning, or log a routine update.
Save incident-switch.schema.json beside your client:
{
"type": "object",
"properties": {
"severity": {
"enum": [
"critical",
"warning",
"routine"
],
"description": "Classify a service outage as critical, degraded service as warning, and a healthy status update as routine."
},
"action": {
"enum": [
"page_on_call",
"open_ticket",
"log"
],
"description": "The action selected by the routing rules."
}
},
"required": [
"severity",
"action"
],
"additionalProperties": false,
"x-runtime": {
"switch": {
"on": {
"path": "/severity"
},
"cases": [
{
"when": {
"eq": "critical"
},
"then": {
"properties": {
"action": {
"const": "page_on_call"
}
}
}
},
{
"when": {
"in": [
"critical",
"warning"
]
},
"then": {
"properties": {
"action": {
"const": "open_ticket"
}
}
}
}
],
"default": {
"properties": {
"action": {
"const": "log"
}
}
}
}
}
}
Before you run this example
Call https://api.blatent.ai from your server. Set your API key in the same terminal where you run the example:
export BLATENT_API_KEY='YOUR_SERVICE_KEY'
Python requires Python 3.10+ and HTTPX:
python -m pip install "httpx>=0.28,<1"
TypeScript requires Node.js 24+ and uses its built-in fetch. The cURL examples require curl and jq.
Save the displayed files and the selected client in one directory. Run the client there with python example.py, node example.ts or bash example.sh. Downloads use the example's name in place of example.
- Python
- TypeScript
- cURL
import json
import os
from pathlib import Path
import httpx
request = {
"input": "The service is completely unavailable. Customers cannot sign in.",
"schema": json.loads(Path("incident-switch.schema.json").read_text(encoding="utf-8")),
}
with httpx.Client(
base_url="https://api.blatent.ai",
headers={"Authorization": f"Bearer {os.environ['BLATENT_API_KEY']}"},
timeout=195,
) as client:
response = client.post("/v1/resolve", json=request)
response.raise_for_status()
print(json.dumps(response.json()["output"]))
import { readFileSync } from "node:fs";
const request = {
"input": "The service is completely unavailable. Customers cannot sign in.",
"schema": JSON.parse(readFileSync("incident-switch.schema.json", "utf8")),
};
const response = await fetch(
"https://api.blatent.ai" + "/v1/resolve",
{
method: "POST",
headers: {
Authorization: `Bearer ${process.env.BLATENT_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify(request),
signal: AbortSignal.timeout(195_000),
},
);
if (!response.ok) throw new Error(`HTTP ${response.status}: ${await response.text()}`);
console.log(JSON.stringify((await response.json()).output));
set -eu
body=$(jq -n --argjson body '{"input":"The service is completely unavailable. Customers cannot sign in."}' \
--slurpfile f0 'incident-switch.schema.json' \
'$body | setpath(["schema"]; $f0[0])')
curl --fail-with-body --silent --show-error \
"https://api.blatent.ai/v1/resolve" \
-H "Authorization: Bearer $BLATENT_API_KEY" \
-H 'Content-Type: application/json' \
--data-binary "$body"
Illustrative output: {"severity":"critical","action":"page_on_call"}.
The answer names an action. It does not page anyone or create a ticket; your application performs those operations.
Read the switch
| Part | Meaning |
|---|---|
on.path | The output value to test, expressed as a JSON Pointer |
cases | Cases checked in the order written |
when | A condition on the selected value, such as eq or in |
then | Additional JSON Schema constraints for that case |
default | Additional constraints when no case matches |
/severity selects the severity property of this output object. A path starts from the output value described by the schema containing the switch. It does not read request state. For supplied facts, use state conditions.
The when expression uses Struct's condition operators, not a JSON Schema. In this example, eq takes one value and in takes a list. These test the value selected by on, unlike state comparisons that supply two operands. The then and default branches are JSON Schemas.
Case order matters
Both cases include critical. Because the specific case comes first, it selects page_on_call; the later case does not also apply. There is no fall-through.
| Generated severity | Required action |
|---|---|
critical | page_on_call |
warning | open_ticket |
routine | log |
The switch enforces the action for whichever severity Struct generates. The severity classification itself still depends on interpretation of the input.
Keep branch rules consistent
Declare the output fields and their allowed values in the surrounding schema. Each then and default adds constraints to those rules; it cannot introduce an undeclared property into a closed object or make an otherwise invalid value legal.
A switch requires at least one case and an explicit default. For a single condition, if / then / else is usually simpler. To apply several independent rules together, use allOf.