Constrain strings
Use string constraints when Struct should write or normalize text that must fit your application's requirements. A description explains the content; minLength, maxLength and pattern restrict the permitted values.
Limit the length
Set maxLength for a title, label or short summary. Add minLength: 1 when an empty string is not useful. These limits count Unicode characters, not words or bytes; a multi-character emoji can count as more than one character.
Match a pattern
Use pattern for a custom shape, such as a lowercase URL slug. The pattern ^[a-z]+(-[a-z]+)*$ allows words joined by single hyphens: support-inbox matches, while Support Inbox, -support and support--inbox do not.
JSON Schema patterns search within a value by default. Use ^ and $ when the whole value must match. Combine a pattern with maxLength to limit its size.
Example: a title and a URL slug
This example generates a title and slug from an article brief.
Save article-strings.schema.json beside your client:
{
"type": "object",
"properties": {
"title": {
"type": "string",
"minLength": 1,
"maxLength": 48,
"description": "Write a concise, informative title for the article described in the input."
},
"slug": {
"type": "string",
"minLength": 1,
"maxLength": 64,
"pattern": "^[a-z]+(-[a-z]+)*$",
"description": "Create a short URL slug for the article topic using lowercase words separated by hyphens."
}
},
"required": ["title", "slug"],
"additionalProperties": false
}
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": "An article about moving customer support from shared email inboxes to a ticketing system.",
"schema": json.loads(Path("article-strings.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": "An article about moving customer support from shared email inboxes to a ticketing system.",
"schema": JSON.parse(readFileSync("article-strings.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":"An article about moving customer support from shared email inboxes to a ticketing system."}' \
--slurpfile f0 'article-strings.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:
{"title":"Moving Support Beyond Shared Inboxes","slug":"moving-support-beyond-shared-inboxes"}
The schema enforces the length and slug syntax. The descriptions guide which title and slug suit the article.
Choose the right constraint
Use enum when the answer must come from a fixed list. Use pattern for a custom shape, and standard formats for values such as dates and timestamps.
For verbatim excerpts, use source quoting to enforce that the text comes from the input. A description asking for an exact copy does not provide that guarantee.
Regex backslashes must be escaped in JSON: "\\d" supplies \d. Backreferences and lookarounds are unsupported; see the pattern reference for the supported constructs.