smallwords#

Large language models can follow “write simply” instructions, but that soft guidance is often insufficient when a workflow also needs reproducible wording, portable output constraints, or offline validation. Prompt-only approaches are easy to start and hard to trust. They leave the active vocabulary implicit, make failures difficult to diagnose, and force each integration to recreate the same constraint logic in a different format.

smallwords addresses that gap by keeping one controlled vocabulary at the center of the workflow. From that single specification, the package can build prompt text, GBNF, JSON Schema, and full output validation that stay aligned.

Install#

pip install smallwords

Quick API#

Most integrations follow the same chain: choose a vocabulary, optionally add task words, build the matching prompt and portable resources, then validate the result.

For a first run without a model server, use python examples/offline_quickstart.py from a repository checkout. Pass only essential terms to allow_input_words: supplying an entire source passage allows all its uncommon words in the generated response.

smallwords.allow_input_words(wordlist, *texts, name=None, description=None)[source]#

Return a derived spec that also permits extracted words from task text.

Extraction groups Unicode letters and combining marks between delimiters. It does not segment unspaced languages or normalize equivalent Unicode spellings; added words retain the input’s code-point sequence.

Parameters:
  • wordlist (str | WordlistSpec) – Built-in wordlist name or inline wordlist specification.

  • texts (str) – One or more task strings whose normalized words should be allowed.

  • name (str | None) – Optional replacement name for the derived specification.

  • description (str | None) – Optional replacement description for the derived specification.

Returns:

A derived wordlist specification that includes the task words.

Return type:

WordlistSpec

smallwords.get_wordlist(name)[source]#

Return a named bundled wordlist or raise a helpful KeyError.

Parameters:

name (str) – Built-in wordlist name.

Returns:

The matching bundled wordlist specification.

Raises:

KeyError – If the requested name is not registered.

Return type:

WordlistSpec

smallwords.list_wordlists()[source]#

Return the bundled wordlist names in stable sorted order.

Returns:

The available bundled wordlist names.

Return type:

tuple[str, …]

class smallwords.OutputShape(thinking_mode='none', min_words_per_line=1, max_words_per_line=40, max_lines=8)[source]#

Describe the serialized response shape shared by every output resource.

Parameters:
  • thinking_mode (Literal['none', 'plan_final', 'thinking_answer'])

  • min_words_per_line (int)

  • max_words_per_line (int)

  • max_lines (int)

thinking_mode#

Wrapper mode for plain text or plan/final style responses.

Type:

Literal[‘none’, ‘plan_final’, ‘thinking_answer’]

min_words_per_line#

Minimum number of tokens required on one line.

Type:

int

max_words_per_line#

Maximum number of tokens allowed on one line.

Type:

int

max_lines#

Maximum number of lines allowed in the response body.

Type:

int

class smallwords.OutputResources(spec, shape=<factory>)[source]#

Portable GBNF and JSON Schema resources for a specific wordlist and shape.

Parameters:
spec#

Wordlist specification used for every generated resource.

Type:

smallwords.types.WordlistSpec

shape#

Serialized response shape shared by the grammar and schema.

Type:

smallwords.types.OutputShape

classmethod from_wordlist(wordlist, *, shape=None)[source]#

Resolve a named or inline wordlist into a resource bundle.

Parameters:
  • wordlist (str | WordlistSpec)

  • shape (OutputShape | None)

Return type:

OutputResources

property gbnf: str#

Compile the configured wordlist and shape into a GBNF string.

json_schema(*, key='text', title=None, description=None)[source]#

Build a strict single-key JSON Schema aligned with this resource bundle.

Parameters:
  • key (str)

  • title (str | None)

  • description (str | None)

Return type:

dict[str, Any]

validate(text)[source]#

Check text against this resource bundle’s vocabulary and shape.

Parameters:

text (str)

Return type:

OutputValidation

save_gbnf(path)[source]#

Write the generated GBNF resource to disk.

Parameters:

path (str | PathLike[str])

Return type:

None

save_json_schema(path, *, key='text', title=None, description=None)[source]#

Write the generated JSON Schema resource to disk.

Parameters:
  • path (str | PathLike[str])

  • key (str)

  • title (str | None)

  • description (str | None)

Return type:

None

class smallwords.OutputValidation(valid, out_of_vocab, issues)[source]#

Report whether text matches the configured output contract.

Parameters:
  • valid (bool)

  • out_of_vocab (tuple[str, ...])

  • issues (tuple[str, ...])

valid#

Whether the full vocabulary and output shape match.

Type:

bool

out_of_vocab#

Best-effort word diagnostics for invalid output, excluding valid wrapper labels such as PLAN: and FINAL:.

Type:

tuple[str, …]

issues#

Human-readable reasons for an invalid response.

Type:

tuple[str, …]

smallwords.validate_output(text, wordlist, *, shape=None)[source]#

Check text against the same vocabulary and shape as output resources.

Parameters:
  • text (str) – Generated response text, without the surrounding JSON object.

  • wordlist (str | WordlistSpec) – Built-in wordlist name or inline specification.

  • shape (OutputShape | None) – Output shape used to build the GBNF or JSON Schema. The default matches OutputResources.from_wordlist.

Returns:

A result with the full-contract verdict and diagnostic issues.

Return type:

OutputValidation

smallwords.prompts.build_prompt(kind, content, *, wordlist='basic_850', thinking=False)[source]#

Build a prompt for one supported task kind and active wordlist.

Parameters:
  • kind (Literal['answer', 'explain', 'rewrite', 'summarize'])

  • content (str)

  • wordlist (str | WordlistSpec)

  • thinking (bool)

Return type:

str

smallwords.is_compliant(text, wordlist)[source]#

Check vocabulary membership using the original public API.

This compatibility helper checks words only. Use validate_output when punctuation, capitalization, wrappers, and line limits must also match.

Parameters:
  • text (str)

  • wordlist (str | WordlistSpec)

Return type:

bool

smallwords.is_in_vocabulary(text, wordlist)[source]#

Return True when every alphabetic token is in the selected word list.

Parameters:
  • text (str) – Free-form text to validate.

  • wordlist (str | WordlistSpec) – Built-in wordlist name or inline wordlist specification.

Returns:

True when every normalized alphabetic token is permitted. This does not check numbers, punctuation, capitalization, or output shape.

Return type:

bool

smallwords.out_of_vocab(text, wordlist)[source]#

Return sorted unique tokens that are missing from the selected word list.

Parameters:
  • text (str) – Free-form text to validate.

  • wordlist (str | WordlistSpec) – Built-in wordlist name or inline wordlist specification.

Returns:

Sorted unique tokens that are not permitted by the selected wordlist.

Return type:

list[str]

OutputResources.validate and validate_output check the full vocabulary and output shape. is_compliant and out_of_vocab check alphabetic word membership only; they ignore numbers, punctuation, capitalization, and line limits.

allow_input_words extracts runs of Unicode letters and combining marks between delimiters. An unspaced Japanese run is treated as one literal; the helper does not perform language-specific word segmentation or inflection. Unicode spellings are exact after lowercasing, so precomposed and decomposed accents remain distinct. A valid full-contract response has no out-of-vocabulary issue; diagnostics for invalid output are best-effort token reports.

Built-ins#

The installed catalog currently includes:

  • moby_898

  • basic_850

  • special_english_1475

  • caveman_898

  • cowboy_898

  • pirate_898

Use smallwords.list_wordlists() to inspect the installed catalog. Use smallwords.get_wordlist() when a workflow needs the underlying specification object directly.

caveman_898 now contains 355 hand-curated, one-syllable words with chiefly Germanic roots. It retains eight plain exceptions for usefulness: fur, leach, oil, rock, tent, wall, war, and plant. Its name refers to its moby_898 starting point. The complete removed-word list is in docs/caveman_removed_words.txt.

The cowboy_898 and pirate_898 presets each replace 40 abstract base words with themed terms and everyday pronouns. build_prompt includes their voice guidance; GBNF and JSON Schema enforce vocabulary and output shape only. These are playful styles rather than historical dialect models. See the National Park Service cowboy history and Royal Museums Greenwich pirate word guide for source context.

Run python examples/themed_presets.py for offline samples of both presets.

Examples With Results#

The comparison below was produced locally on October 15, 2026 with llama-server and ibm-granite/granite-4.2-8b-GGUF.

Missile Guidance Contrast#

This comparison starts with the opening sentence and a compressed later line from the missile-guidance monologue. Six constrained rewrites express the same idea as a current place, a target place, and the span between them.

from smallwords import OutputResources, OutputShape, allow_input_words
from smallwords.prompts import build_prompt

source = (
    "The missile knows where it is at all times. "
    "By subtracting where it is from where it isn't, or where it isn't from "
    "where it is, it obtains a difference, or deviation."
)
spec = allow_input_words("moby_898", "missile")
shape = OutputShape(min_words_per_line=6, max_words_per_line=11, max_lines=3)
prompt = build_prompt("explain", source, wordlist=spec)
resources = OutputResources.from_wordlist(spec, shape=shape)

Original source:

The missile knows where it is at all times. By subtracting where it is from where it isn't, or where it isn't from where it is, it obtains a difference, or deviation.

Simple, constrained with moby_898 plus missile:

The missile knows where it is at all times.
It knows where it should be next.
The difference tells it how far to move.

Caveman, constrained with caveman_898:

Rock know where it be now.
Rock know where it should be.
Rock know how far it must go.

Basic English, constrained with basic_850:

The thing has knowledge of where the thing is now.
The thing is to be at the other place.
The distance from here to there is how far to move.

Special English, constrained with special_english_1475:

The missile knows where it is at all times.
It should be at the second place.
The distance tells how far to move from here to there.

Pirate, constrained with pirate_898:

The ship knows where it is now.
The crew knows where the ship should be.
The difference tells the crew which way to sail.

Cowboy, constrained with cowboy_898:

The cowboy knows where the cattle are now.
The ranch is where the cattle should be.
The difference tells the cowboy which way to ride.

The live run uses a compact task-focused subset for each grammar. It then checks the result against both that subset and the complete preset. Only the simple case adds a topic word, missile. See examples/readme_missile_contrast.py for all six prompts, grammars, schemas, model calls, and validation reports.

Pirate Greeting#

This example starts from the built-in pirate_898 list, selects a tiny greeting-focused vocabulary, and then applies a matching grammar.

from smallwords import OutputResources, OutputShape, WordlistSpec, get_wordlist
from smallwords.prompts import build_prompt

base = get_wordlist("pirate_898")
spec = WordlistSpec(
    name="pirate_898_greeting_focus",
    words=("ahoy", "good", "matey", "meet", "to", "you"),
    source_name="Selected surface forms from pirate_898 for the pirate greeting example",
    source_urls=base.source_urls,
    license_name=base.license_name,
    allowed_punctuation=(".",),
    variant_mode="surface_only",
    style_hint="Use a friendly pirate greeting.",
)
shape = OutputShape(min_words_per_line=6, max_words_per_line=6, max_lines=1)
prompt = build_prompt(
    "answer",
    "A pirate meets a new friend on a ship. What short friendly greeting should the pirate say?",
    wordlist=spec,
)
resources = OutputResources.from_wordlist(spec, shape=shape)

Constrained result:

Ahoy matey good to meet you.

Technical Rewrite#

This example selects a compact rewrite vocabulary from basic_850 that excludes the source terminology altogether. The output shape then forces one short ten-word sentence.

from smallwords import OutputResources, OutputShape, WordlistSpec, get_wordlist
from smallwords.prompts import build_prompt

base = get_wordlist("basic_850")
text = (
    "The thermal controller derates propulsion output after the sensor array "
    "reports an overtemperature fault."
)
spec = WordlistSpec(
    name="basic_850_rewrite_focus",
    words=("be", "cut", "engine", "heat", "high", "if", "power", "system", "this", "very", "when"),
    source_name="Selected from basic_850 for the rewrite example",
    source_urls=base.source_urls,
    license_name=base.license_name,
    allowed_punctuation=(".",),
)
shape = OutputShape(min_words_per_line=10, max_words_per_line=10, max_lines=1)
prompt = build_prompt("rewrite", text, wordlist=spec)
resources = OutputResources.from_wordlist(spec, shape=shape)

Constrained result:

When engine heat is very high this system cuts power.

The live scripts in examples/ print the full prompt, grammar, schema, and validation details. That fuller output matters because it lets a reader inspect not only the answer, but also the exact constraint setup that produced it.