The Problem
Developers want LLMs to produce JSON‑formatted, type‑safe outputs without changing the base model or its weights. Existing approaches often leak out‑of‑schema values or require custom model heads, which increases engineering overhead and runtime cost.
What This Does
TypeLLM adds a lightweight runtime layer on top of any autoregressive model served via SGLang.
- Schema enforcement – The core logic in
typellm/schema.pyvalidates that the model’s string output matches a supplied JSON schema. - Dependency handling –
typellm/protocol.pybuilds a directed graph of fields (depends_on) and runs them in the required order, batching independent fields to reuse KV cache. - Thinking mode – Optional reasoning is inserted before the final constrained answer; the cost is a single token per field.
- Rich input support –
typellm/images.pyexposes an image‑to‑text pipeline that forwards images to the vision‑language model.
The package ships with a CLI (typellm/cli.py) that accepts a SGLang HTTP endpoint, a JSON schema, and optional flags for thinking, permutation averaging, or image input.
How It Is Wired
ENTRY POINTS
typellm/__main__.py→typellm/cli.py:maintypellm/cli.pyparses arguments and constructstypellm.runtime.TypeLLMClient.
INTERNAL CALL GRAPH
__main__ → cli.main → TypeLLMClient.__init__
→ _build_protocol (protocol.py) → _execute_graph (runtime.py)
→ _generate_field (runtime.py) → _decode_and_validate (schema.py)
→ (optional) _think_before_answer (runtime.py) → _run_model (sglang.py)
WHAT THIS CODE TOUCHES OUTSIDE ITSELF
- Network –
sglang.pysends an HTTP request to the SGLang server, receives a raw text response. - Filesystem – CLI writes the final JSON result to stdout or a file if
--outputis used. - Memory – In‑memory KV cache reuse is handled by SGLang; TypeLLM only orchestrates the prompt.
WHAT EACH FILE IS RESPONSIBLE FOR
cli.py– Argument parsing, client construction, CLI entry.runtime.py– Core execution engine, constructs prompts, handles batching, callssglang.py.protocol.py– Parses schema, builds dependency graph, orchestrates field execution order.schema.py– JSON schema validation, type mapping, numeric bounds enforcement.sglang.py– Thin wrapper around the SGLang HTTP client; handles tokenization and decoding.images.py– Converts image files to base64, injects into prompt.benchmark.py– Utilities for measuring throughput and latency.
The dependency graph is a tree or DAG; any cycle would be reported as an error in protocol.py. No global mutable state exists beyond the client instance.
How To Use It
# Install
pip install -U typellm
# Run a schema against a local SGLang server
typellm \
--server http://localhost:8000 \
--schema schema.json \
--input prompt.txt \
--output result.json \
--think
- Setup – Requires a running SGLang server (see README for deployment).
- Configuration – No extra config files; environment variables are optional (
SGLANG_HTTP_ENDPOINT). - Running – The CLI (
typellm) is the sole entry point; alternatively, importTypeLLMClientin Python and callclient.run(schema, prompt).
Real‑World Use
A customer in fintech uses TypeLLM to validate tax form responses. They load a JSON schema describing required fields, run the client against a nightly inference job, and pipe the validated JSON into downstream ETL. The single‑token thinking mode gives a 2‑second increase in latency but eliminates out‑of‑schema errors that caused manual triage.
Code Health & Issues
- Low/Risk –
pyproject.tomllists dependencies but no lockfile (poetry.lockorrequirements.txtpin).pyproject.toml - Missing – No Dockerfile or CI matrix for Windows; tests run only on Linux/Unix.
/.github/workflows/*.yml - Coverage – 20 test files cover core logic; no integration tests for SGLang server.
tests/*.py
The Bottom Line
TypeLLM offers a focused, schema‑driven wrapper for any SGLang‑servable LLM. It delivers type safety, minimal runtime cost, and flexible dependency management with a clear, minimal API. The primary trade‑off is the absence of a lockfile, which may affect reproducibility in CI pipelines. Organizations needing robust JSON output from existing models will benefit most.