Skip to main content

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:

article-strings.schema.json
{
"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.

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"]))

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.