Skip to main content

Per-grammar-role loss decomposition for fine-tuned structured JSON output

Project description

slotloss

Per-grammar-role loss analysis for structured JSON output from language models.

Fine-tuning your LLM for JSON? Your aggregate metrics might be hiding per-field regressions.

pip install slotloss

What it does

slotloss is the observability layer for structured JSON output. It meets you wherever you are:

You have Command Needs model?
Messy text with JSON slotloss --extract --data output.txt No
JSON + schema slotloss --validate --schema s.json --data output.jsonl No
Broken JSON + schema slotloss --fix --schema s.json --data output.jsonl No
JSON + schema slotloss --anatomy --schema s.json --data output.jsonl No
Two output sets slotloss --diff --schema s.json --data a.jsonl --data2 b.jsonl No
Model + checkpoint slotloss --checkpoint lora/ --schema s.json --data test.jsonl Yes

The problem

Standard fine-tuning + grammar-constrained decoding produces valid JSON. Aggregate loss improves. But:

STRUCTURAL         5.33 -> 0.00     -100%   OK
KEY                0.47 -> 0.00     -100%   OK
BOOLEAN            0.46 -> 1.05     +130%   !! REGRESSION
TOTAL              0.55 -> 0.17      -69%

Aggregate loss improved 69%. Boolean prediction got 130% worse. slotloss catches this.

Quick start

See QUICK_START.md for a hands-on walkthrough from messy output to full analysis.

Python API

from slotloss import analyze

report = analyze(
    model_name="Qwen/Qwen2.5-0.5B-Instruct",
    checkpoint="my_lora/",
    schema="schema.json",
    data="test.jsonl",
)
print(report)

if report.regressions:
    print(f"REGRESSIONS: {[r.role for r in report.regressions]}")

Exit code is 1 if regressions are detected. Use in CI/CD.

Grammar Roles

Role Description Examples
STRUCTURAL JSON syntax { } [ ] : ,
QUOTE String delimiters "
KEY Object key characters city, cuisine
ENUM_VALUE Categorical values Italian, Economy
BOOLEAN Boolean strings True, False
NUMBER Numeric characters 42, 3.14
FREE_TEXT Non-categorical content names, addresses
WHITESPACE Formatting spaces, newlines

Links

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

slotloss-0.2.0.tar.gz (27.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

slotloss-0.2.0-py3-none-any.whl (31.5 kB view details)

Uploaded Python 3

File details

Details for the file slotloss-0.2.0.tar.gz.

File metadata

  • Download URL: slotloss-0.2.0.tar.gz
  • Upload date:
  • Size: 27.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for slotloss-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ee6ca1ab70f6d098a5eb447fb75f3fbc7839032ff580d514fd583f9f1b41c4ca
MD5 a2deac4e8225549a1d86133296eac341
BLAKE2b-256 9cabd9f5e40f344c90115ef330fb93478327a5055afa1d6a1883a0ec4432f508

See more details on using hashes here.

File details

Details for the file slotloss-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: slotloss-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 31.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for slotloss-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 284a43b2ac50705b19a6a07f862eb5f4ff54cc65cf4b724a6903eb92d2f5bab2
MD5 7e7da99d0806bdcc0e2a31f5b716182a
BLAKE2b-256 bd5dc4a90cc3413870bdf1f8377b66efe07722a7529f538111234e32fa8eab8e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page